1、下載Nginx
http://nginx.org/en/download.html
nginx-1.20.0.tar.gz (已驗證到當前版本)
2、安裝依賴
依賴包安裝順序依次為:openssl、zlib、pcre
注:如果沒有安裝c++編譯環境,還得安裝,透過yum install gcc-c++完成安裝。
yum install gcc-c++
yum install -y gcc pcre-devel openssl openssl-devel zlib-devel
(1)openssl-fips-2.0.16.tar.gz
https://www.openssl.org/source/openssl-fips-2.0.16.tar.gz
[root@mycentos mypackages]# tar xzf openssl-fips-2.0.16.tar.gz [root@mycentos mypackages]# cd openssl-fips-2.0.16 [root@mycentos openssl-fips-2.0.16]# ./config [root@mycentos openssl-fips-2.0.16]# make [root@mycentos openssl-fips-2.0.16]# make install
(2)zlib-1.2.11.tar.gz
http://zlib.net/zlib-1.2.11.tar.gz
http://prdownloads.sourceforge.net/libpng/zlib-1.2.11.tar.gz?download
[root@mycentos mypackages]# tar xzf zlib-1.2.11.tar.gz [root@mycentos mypackages]# cd zlib-1.2.11 [root@mycentos zlib-1.2.11]# ./configure [root@mycentos zlib-1.2.11]# make [root@mycentos zlib-1.2.11]# make install
(3)pcre-8.42.tar.gz
https://sourceforge.net/projects/pcre/files/pcre/8.42/
[root@mycentos mypackages]# tar xzf pcre-8.42.tar.gz [root@mycentos mypackages]# cd pcre-8.42 [root@mycentos pcre-8.42]# ./configure [root@mycentos pcre-8.42]# make [root@mycentos pcre-8.42]# make install
3、安裝nginx
[root@ mycentos mypackages]# tar -zxvf nginx-1.20.0.tar.gz [root@ mycentos mypackages]# cd nginx-1.20.0 [root@ mycentos nginx-1.18.0]# ./configure --prefix=/usr/local/nginx [root@ mycentos nginx-1.18.0]# make [root@ mycentos nginx-1.18.0]# make install
如果要啟用SSL模組,那麼在編譯的時候增加:--with-http_ssl_module 引數。
安裝狀態監控模組,增加引數:--with-http_stub_status_module
[root@mycentos nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module [root@mycentos nginx-1.18.0]# make #注意:切記不要進行 make install,否則就會覆蓋安裝!!!
注意:正常安裝nginx和SSL模組安裝要分開安裝,合在一起安裝會報錯,找了好久沒找到原因!
執行完做命令後,我們可以檢視到在nginx解壓目錄下,objs資料夾中多了一個nginx的檔案,這個就是新版本的程式了,然後把新的程式複製過去覆蓋之前的即可(不放心你可以先備份舊的nginx)。
覆蓋以後到Nginx安裝目錄下,檢視SSL是否安裝成功,執行:./sbin/nginx -V
[root@mycentos nginx]# ./sbin/nginx -V
如果有:--with-http_ssl_module,說明安裝成功!
4、啟動nginx
[root@ mycentos ~]# /usr/local/nginx/sbin/nginx #重啟或關閉程式: [root@ mycentos ~]# /usr/local/nginx/sbin/nginx -s reload [root@ mycentos ~]# /usr/local/nginx/sbin/nginx -s stop
如果nginx啟動報錯:error while loading shared libraries: libpcre.so.1(低版本會報錯)
我們執行一下ldd命令,會發現:libpcre.so.1 => not fund
[root@ mycentos nginx]# ldd $(which /usr/local/nginx/sbin/nginx)
linux-vdso.so.1 => (0x00007ffd6bbe3000) libdl.so.2 => /lib64/libdl.so.2 (0x00007fef35b10000) librt.so.1 => /lib64/librt.so.1 (0x00007fef35908000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fef356ea000) libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fef354b3000) libpcre.so.1 => not found libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007fef35246000) libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007fef34e61000) libz.so.1 => /lib64/libz.so.1 (0x0000003036e00000) libc.so.6 => /lib64/libc.so.6 (0x00007fef34acc000) /lib64/ld-linux-x86-64.so.2 (0x00007fef35d1b000) libfreebl3.so => /lib64/libfreebl3.so (0x000000303ba00000) libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x0000003040600000) libkrb5.so.3 => /lib64/libkrb5.so.3 (0x0000003040200000) libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fef348c7000) libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x000000303fe00000) libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x000000303f600000) libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x0000003040a00000) libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fef346ac000) libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003036600000)
所以我們需要建立一下軟連線:在 /usr/local/lib/ 和 /lib/ 和 /lib64/ 三個目錄下建立軟連線:
注意:我的libpcre.so.1在lib64目錄下,所以只需要建立這一個軟連線即可。
[root@ mycentos nginx]# ln -s /usr/local/lib/libpcre.so.0.0.1 /usr/local/lib/libpcre.so.1 [root@ mycentos nginx]# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1
再次啟動nginx,啟動成功!
5、檢查nginx是否啟動成功
[root@mycentos ~]# cd /usr/local/nginx/sbin/ [root@mycentos sbin]# ./nginx -t
# 檢視程式列表 [root@mycentos ~]# ps -ef | grep nginx # 檢視程式ID [root@mycentos ~]# ps -C nginx -o pid # 檢視nginx執行的80埠 [root@mycentos ~]# netstat -anp | grep :80
6、配置防火牆
[root@mycentos ~]# vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
7、瀏覽器訪問http://127.0.0.1/