系統(tǒng)版本

1、安裝nginx服務器需要的相關依賴
yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre-devel
gcc gcc-c++編譯環(huán)境
gzip 模塊需要 zlib 庫
rewrite 模塊需要 pcre 庫
ssl 功能需要openssl庫
2、下載nginx源碼包。
wget http://nginx.org/download/nginx-1.19.2.tar.gz
3、解壓nginx源碼包
tar xvf nginx-1.19.2.tar.gz
4、進入安裝目錄
cd nginx-1.19.2
5、使用./configure,生成配置文件makefile,并配置nginx的安裝路徑
./configure --prefix=/usr/local/nginx

6、編譯安裝nginx
make && make install
7、因為編譯需要配置才能使用systemctl啟動,所以我們先通過安裝路徑啟動一下我們的nginx
/usr/local/nginx/sbin/nginx 啟動服務
/usr/local/nginx/sbin/nginx -s top 停止服務
/usr/local/nginx/sbin/nginx -s reload 重啟服務
8、通過netstat -tnlp 或者 ps aux|grep nginx查看服務是否啟動。

9、測試(注意如果你用的是VMWare虛擬機的話,需要關閉防火墻,才能在我們的物理機訪問nginx)

10、給nginx配置通過systemctl啟動,打開vim /usr/lib/systemd/system/nginx.service文件,輸入以下內容:
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
這個時候會出現(xiàn)如下圖所示的報錯

解決方法如下:
[root@localhost ~]# mkdir /etc/systemd/system/nginx.service.d
[root@localhost ~]# printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf
[root@localhost ~]# systemctl deamon-reload 加載新的unit 配置文件
11、如下圖所示就是配置成功了,可以設置一下開機自啟動systemctl enable nginx.service
