CentOS 7.4 安裝 Nginx(配置單域名多HTTPS主機)
1. 下載 openssl 並解壓,並將解壓後的資料夾改名為 openssl-111。
wget https://www.openssl.org/source/openssl-1.1.1-pre8.tar.gz
tar -zxvf openssl-1.1.1-pre8.tar.gz
mv openssl-1.1.1-pre8 openssl-111
注意:這個 openssl 解壓即可,不要安裝。
2. 下載 nginx 並解壓,並將解壓後的資料夾改名為 nginx-114。
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz
mv nginx-1.14.0 nginx-114
3. 安裝 pcre 和 zlib 庫,否則安裝是會出錯。
yum -y install pcre-devel zlib-devel
4. 進入 nginx-114 目錄後輸入如下命令,進行設定、編譯、安裝。
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=./openssl-1.0.1e --with-openssl-opt="enable-tlsext"
make
make install
5. 如果僅僅出現 make[1]: Leaving directory `/root/runenv/nginx-114' 這一行錯誤或者說到這行後看不到後續日誌,可忽略。
6. 通過如下過程判斷 nginx 是否正常安裝。
首先,由於 prefix=/usr/local/nginx,看 /usr/local/ 目錄下是否有 nginx 目錄?
接著,如果目錄存在,看 nginx 能否正常啟動?
最後,如果 nginx 啟動成功,通過 http://IP 看是否能正常展示 Welcome to nginx! 網頁?
如果都可以,則Nginx 安裝成功。
7. 編輯 /lib/systemd/system/nginx.service 檔案,錄入如下內容,以配置系統服務讓 nginx 開機啟動。
[Unit] #服務說明
Description=nginx - high performance web server #服務描述
Documentation=http://nginx.org/en/docs/ #服務文件
#啟動順序,本服務在這些服務啟動前啟動
#Before=
#啟動順序,本服務在這些服務啟動後啟動
After=network.target network-online.target remote-fs.target nss-lookup.target
#推薦使用。這個單元啟動了,其需要的單元也會被啟動;其需要的單元停止了,對本單元無影響。
Wants=network-online.target
[Service] #服務執行引數設定
Type=forking #後臺執行
PIDFile=/run/nginx.pid #用於 systemd 跟蹤服務主程序
#指定啟動單元的命令或指令碼,ExecStartPre和ExecStartPost指定在ExecStart之前或之後使用者自定義執行的指令碼
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx #啟動
ExecReload=/bin/kill -s HUP $MAINPID #重啟
ExecStop=/bin/kill -s QUIT $MAINPID #停止
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true #給服務分配獨立的臨時空間
[Install] #執行級別下服務安裝的相關配置
WantedBy=multi-user.target #多使用者
8. 通過如下命令設定開機啟動或停止開機啟動。
systemctl enable nginx.service
systemctl disable nginx.service
9. 通過如下命令啟動、重啟、停止 nginx服務。
systemctl start nginx.service
systemctl restart nginx.service
systemctl stop nginx.service
10. 通過 /usr/local/nginx/sbin/nginx -V 命令,如果出現 TLS SNI support enabled,則表示可以同一個IP配置多個HTTPS主機了。