原始碼安裝Nginx以及用systemctl管理
阿新 • • 發佈:2022-05-21
一、原始碼安裝Nginx:
先安裝gcc編譯器(安裝過的可以忽略)
[root@localhost ~]# yum -y install gcc gcc-c++ wget
下載 nginx軟體包
[root@localhost src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
解壓
[root@localhost src]# tar -zxvf nginx-1.20.1.tar.gz
進入nginx-1.20.1目錄
[root@localhost src]# cd nginx-1.20.1/
安裝依賴
[root@localhost nginx-1.14.0]# yum -y install openssl openssl-devel
/configure軟體檢查( ./configure--prefix=/usr/local/nginx --with-http_ssl_module 可以省略自定義安裝路徑)
[root@localhostnginx-1.14.0]#./configure
安裝
[root@localhost nginx-1.14.0]# make [root@localhost nginx-1.14.0]# make install
啟動
[root@localhost nginx-1.14.0]#cd /usr/local/nginx/sbin [root@localhost nginx-1.14.0]#./nginx
檢視是否啟動成功
[root@localhost nginx-1.14.0]# ps aux |grep nginx
二、systemctl管理:
建立配置檔案
原始碼安裝的nginx在/etc/systemd/system/multi-user.target.wants/目錄下是沒有nginx.service這個檔案的,所以要新建
[root@localhost nginx-1.14.0]#vim /usr/lib/systemd/system/nginx.service
寫入內容(全部複製進去,然後修改)
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID [Install] WantedBy=multi-user.target
修改 [Service]內容
將: ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf, 改為: ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
關閉之前啟動的nginx服務
[root@localhost nginx-1.14.0]# pkill -9 nginx
過載修改過的所有配置檔案
[root@localhost nginx-1.14.0]#systemctl daemon-reload
設定開機啟動
[root@localhost nginx-1.14.0]# systemctl enable nginx.service
重新啟動nginx服務
[root@localhost nginx-1.14.0]#systemctl start nginx