1.安裝工具和庫
# PCRE是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式
# zlib庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.目錄結構
源碼目錄:/home/werben/pkgsrc/nginx
安裝目錄:/home/werben/application/nginx
3.下載解壓源碼
# 官網地址: https://nginx.org/en/download.html
wget -c https://nginx.org/download/nginx-1.17.5.tar.gz
4.創建用戶組和用戶
groupadd www
useradd -g www www
5.編譯源碼
./configure --user=www --group=www --prefix=/home/werben/application/nginx --with-http_v2_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-pcre
make && make install
6.映射全局命令
ln -s /home/werben/application/nginx/sbin/nginx /usr/local/bin/nginx
7.啟動,停止,重啟
nginx -s stop
nginx -s quit
ngins -s reload
8.檢測配置文件nginx.conf正確性
nginx -t
9.開機自啟動
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=nginx
ExecReload=nginx reload
ExecStop=nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#重新加載守護進程
systemctl daemon-reload
#啟動nginx服務
systemctl start nginx.service
#停止nginx服務
systemctl stop nginx.service
#設置開機自啟動
systemctl enable nginx.service
#停止開機自啟動
systemctl disable nginx.service
#查看服務當前狀態
systemctl status nginx.service
#重新啟動服務
systemctl restart nginx.service
#查看所有已啟動的服務
systemctl list-units --type=service
10.出現問題和解決方法
#如果`systemctl start nginx.service`提示如下報錯
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
#執行
systemctl status nginx.service
#如果出現如下錯誤
Process: 35783 ExecStart=...nginx/sbin/nginx(code=exitedstatus=203/EXEC)
nginx.service: Control process exited, code=exited status=203
systemd[1]: nginx.service: Failed with result 'exit-code'.
localhost.localdomain systemd[1]: Failed to start nginx.
journalctl -xe
#如果看到如下信息
If you believe that systemd should be allowed execute access on the>
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do allow this access for now by executing:
# ausearch -c '(nginx)' --raw | audit2allow -M my-nginx
# semodule -X 300 -i my-nginx.pp
#解決方法
setenforce 0
vim /etc/selinux/config
SELINUX=disabled