CentOS7编译安装(升级)nginx

记录一下自己手动编译安装nginx最新版的过程。

下载:

wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar -xvf nginx-1.18.0.tar.gz && cd nginx-1.18.0

编译配置说明:https://www.nginx.com/resources/wiki/start/topics/tutorials/installoptions/

配置编译:

./configure \
  --prefix=/usr/local/opt/nginx@1.18 \
  --conf-path=/usr/local/etc/nginx/1.18.0 \
  --user=apache \
  --group=apache \
  --with-http_ssl_module

编译:

make && make install

ssl on;指令已经被弃用了,修改配置:

sed -i 's/    ssl on;//g' /path/to/virtual-hosts/*.conf
sed -i 's/    listen         443;/    listen         443 ssl;/g' /path/to/virtual-hosts/*.conf

测试配置文件是否OK:

/usr/local/opt/nginx@1.18/sbin/nginx -t -c /etc/nginx/nginx.conf

启用新版nginx:

/usr/sbin/nginx -s stop
/usr/local/opt/nginx@1.18/sbin/nginx -c /etc/nginx/nginx.conf

通过systemd控制nginx

新建service文件:

// /usr/lib/systemd/system/nginx@1.18.service

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

# 文件复制于/usr/lib/systemd/system/nginx.service

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /var/run/nginx.pid
ExecStartPre=/usr/local/opt/nginx@1.18/sbin/nginx -c /etc/nginx/nginx.conf -t
ExecStart=/usr/local/opt/nginx@1.18/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

关闭手动启动的nginx并启动service控制:

/usr/local/opt/nginx@1.18/sbin/nginx -c /etc/nginx/nginx.conf -s stop

# 如果你多次修改了service文件,需要执行这个命令
systemctl daemon-reload 

systemctl start nginx@1.18

# 开机启动
systemctl enable nginx@1.18.service

发表评论

电子邮件地址不会被公开。 必填项已用*标注