Ubuntu下怎么用最新源码编译Nginx并打入插件,以及相关nginx.conf配置说明。
安装依赖库
1 2 3 4 5 6 7 8 9 apt update apt install build-essential libtool apt install libpcre3 libpcre3-dev apt install zlib1g-dev apt install openssl libssl-dev
上面是一些必要的依赖,忽略上面,一把梭命令如下:
apt install build-essential libtool libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev
下载Nginx源码编译 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #Nginx官方下载并解压(https://nginx.org/en/download.html) wget https://nginx.org/download/nginx-1.19.2.tar.gz -O - | tar -xz cd nginx-1.19.2 #--prefix是各个文件路径,--with是打入的插件 ./configure --prefix=/etc/nginx \ --sbin-path=/usr/sbin /nginx \ --modules-path=/usr /lib/nginx /modules \ --conf-path=/etc /nginx/nginx .conf \ --error-log-path=/var/log /nginx/error .log \ --http-log-path=/var/log /nginx/access .log \ --pid-path=/var/run /nginx.pid \ --lock-path=/var /run/nginx .lock \ --http-client-body-temp-path=/var/cache /nginx/client _temp \ --http-proxy-temp-path=/var/cache /nginx/proxy _temp \ --http-fastcgi-temp-path=/var/cache /nginx/fastcgi _temp \ --http-uwsgi-temp-path=/var/cache /nginx/uwsgi _temp \ --http-scgi-temp-path=/var/cache /nginx/scgi _temp \ --user=nginx \ --group=nginx \ --with-compat \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_realip_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_sub_module \ --with-mail --with-mail_ssl_module \ --with-stream_realip_module \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --with-pcre \ --with-stream \ --with-http_mp4_module \ --with-http_auth_request_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_v2_module make make install
编译完成后就可以用nginx -t
检查一下nginx有没有什么错误提示,如下显示即为ok
1 2 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
如果出现报错的话可以按下面方法修复。
常见报错 当然启动Nginx前先用下面命令检查一下80/443端口有没有被占用:
netstat -lntp
nginx -t
常见报错:
1 2 3 4 5 nginx: [emerg] getpwnam("nginx" ) failed in /etc/ nginx/nginx.conf:2 nginx: configuration file /etc/ nginx/nginx.conf test failed useradd nginx
1 2 3 4 5 6 #如果出现下错误,说明client_temp文件夹不存在,创建一个即可 nginx: the configuration file /etc/ nginx/nginx.conf syntax is ok nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2 : No such file or directory) nginx: configuration file /etc/ nginx/nginx.conf test failed mkdir -p /var/ cache/nginx/ client_temp
创建systemd服务 vi /lib/systemd/system/nginx.service
添加内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [Unit] Description =The NGINX HTTP and reverse proxy serverAfter =syslog.target network-on line.target remote-fs.target nss-lookup.targetWants =network-on line.target[Service] Type =forkingPIDFile =/var/run/nginx.pidExecStartPre =/usr/sbin/nginx -tExecStart =/usr/sbin/nginxExecReload =/usr/sbin/nginx -s reloadExecStop =/bin/kill -s QUIT $MAINPID PrivateTmp =true [Install] WantedBy =multi-user.target
重载服务:systemctl daemon-reload
然后就可以启动Nginx了:
systemctl start nginx
Nginx的常用命令
systemctl enable nginx
#设置开机自动启动
systemctl status nginx
#查看运行状态,显示running表示成功运行
systemctl reload nginx
#重新载入
systemctl restart nginx
#重新启动
systemctl stop nginx
#立刻停止运行nginx
nginx -t
#测试配置文件
配置nginx.conf Nginx 配置文件主要分成四部分:main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和 location(URL匹配特定位置后的设置)。main 部分设置的指令影响其它所有部分的设置;server 部分的指令主要用于制定虚拟主机域名、IP 和端口号;upstream 的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;location 部分用于匹配网页位置(比如,根目录“/”,“/images”,等等)。他们之间的关系:server 继承 main,location 继承 server;upstream 既不会继承指令也不会被继承。
文件大体结构如下,最外层的main在nginx.conf中是默认实现的,并不会实际写出来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 main { user nginx; events { worker_connections 1024 ; } http { server_tokens off ; gzip on ; server { listen 80 ; location / { return 301 https://$server_name$request_uri; } } } }
nginx的默认配置项:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 worker_processes 1 ; events { worker_connections 1024 ; } http { include mime.types; default_type application/octet-stream; error_log /var/log/nginx/error.log crit ; sendfile on ; keepalive_timeout 65 ; server { listen 80 ; server_name myapp.info ; location / { root /usr/local/nginx; } } }
参考: