環境
Centos7.6
nginx-1.17.0
下載
官網:http://nginx.org/download/nginx-1.17.0.tar.gz
環境確認
在安裝nginx
前首先要確認系統中是否安裝gcc
、pcre-devel
、zlib-devel
、openssl-devel
- 檢查是否安裝過軟件包
yum list installed | grep xxx
- 安裝軟件包
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
上圖為已安裝
安裝
- 將
nginx-1.17.0.tar.gz
上傳至服務器並解壓
tar -xzvf nginx-1.17.0.tar.gz
解壓后如下所示:
nginx
目錄下編譯安裝nginx
./configure --prefix=/usr/local/nginx1.17.0 --conf-path=/usr/local/nginx1.17.0/nginx.conf --with-http_stub_status_module --with-http_ssl_module
--with-http_ssl_module
配置nginx
支持https
協議訪問,不使用https
可以不用添加該命令
該命令編譯nginx
時將配置文件nginx.conf
生成在nginx
目錄下,因編譯后出現錯誤,采用這種方式,詳見后面錯誤記錄,因此,nginx
的配置文件不再是conf
中的nginx.conf
- 順序執行
make
,make install
編譯
make
make install
- 測試是否安裝成功
./sbin/nginx -t
- 啟動
nginx
./sbin/nginx
- 停止
nginx
./sbin/nginx -s stop
- 重啟
nginx
./sbin/nginx -s reload
- 查看
nginx
進程
ps -ef | grep nginx
- 訪問:瀏覽器訪問服務器
IP
(nginx
默認端口為80
),出現如下界面則證明成功
配置HTTPS
- 服務器上安裝
openssl
,openssl-devel
yum install openssl openssl-devel
- 創建證書存放目錄
mkdir /usr/local/nginx/conf/ssl
- 創建服務器私鑰
openssl genrsa -des3 -out server.key 2048 #根據提示輸入證書口令
- 創建簽名請求的證書(
CSR
)
openssl req -new -key server.key -out server.csr #輸入上面設置的口令,根據提示輸入相應的信息
- 對
key
進行解密
openssl rsa -in server.key -out server_nopasswd.key
- 標記證書使用上述私鑰和
CSR
openssl x509 -req -days 365 -in server.csr -signkey server_nopasswd.key -out server.crt
vim
修改nginx
配置文件,加載ssl
證書
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /usr/local/nginx-1.17.0/conf/ssl/server.crt;
ssl_certificate_key /usr/local/nginx-1.17.0/conf/ssl/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
-
輸入證書密碼啟動
nginx
-
瀏覽器訪問測試:
https://服務器IP + 端口443
,出現如下界面則成功
錯誤記錄
nginx
報錯:cp: `conf/koi-win' and `/usr/local/nginx/conf/koi-win' are the same file
該錯誤為編譯安裝nginx
時沒有指定conf-path
出現的,出現問題的命令:
./configure --prefix=/usr/local/nginx1.17.0 --with-http_stub_status_module --with-http_ssl_module
將命令改為如下指定conf-path
后正常:
./configure --prefix=/usr/local/nginx1.17.0 --conf-path=/usr/local/nginx1.17.0/nginx.conf --with-http_stub_status_module --with-http_ssl_module
暫不知具體原因,感謝大佬
centos安裝nginx 報錯:cp: 'conf/koi-win' and '/usr/local/nginx/conf/koi-win' are the same file
.end