【1.21.1】Nginx 在CentOS上的最新版本源代码编译
如何安装Nginx
在中文中重新表述如下:“虽然没有特别困难的事情,但我有备忘录。”
中国的环境
-
- OS : CentOS Stream 8
-
- Cloud : GCP GCE
- Nginx : 1.21.2 (2021.09.02 最新)
操作步骤
【参考】公式步骤
1. 下载 tar 文件
# cd /usr/local/src
# wget https://nginx.org/download/nginx-1.21.2.tar.gz
2. 解壓tar
# tar xzvf nginx-1.21.2.tar.gz
# rm nginx-1.21.2.tar.gz
# cd nginx-1.21.2
3. 配置
# ./configure
...
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
这里出现了错误
似乎需要 pcre 库
- pcre-develをインストール
在GG中,我发现有人通过这个方法成功解决了问题。
# dnf install pcre-devel
4. 制造
# make
5. 安装
# make install
6. 允许通过路径。
# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
# nginx -v
nginx version: nginx/1.21.2
将程序转化为守护进程的步骤
由於使用源碼安裝,無法使用systemctl進行操作,因此需要將其轉為守護進程。
方法很簡單,只需在/lib/systemd/system/中創建一個文件即可。
步骤
- serviceファイル作成
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
在这种情况下,无法使用/usr/local/nginx/logs/nginx.pid作为PID文件启动,因此需要在nginx.conf中进行配置更改。
pid /var/run/nginx.pid;
取消注释,并将其设置为 /var/run/nginx.pid(或 /run/nginx.pid)。
- 起動確認
# systemctl start nginx
# systemctl status nginx