天天看點

CentOS 7 編譯安裝 Nginx 1.9.7

環境說明

VMware 12 中搭建的 CentOS 7 x64 4cpu 2G記憶體

環境中已經安裝了《

CentOS 7 編譯安裝 MySQL-5.7.9

》《

CentOS 7 編譯安裝PHP7

配置ip

參考《

》中的 “配置防火牆和開放端口”

依賴庫配置,編譯和安裝Nginx1.9.0

下載下傳

pcre-8.38.tar.gz zlib-1.2.8.tar.gz nginx-1.9.7.tar.gz

并上傳到/root/目錄

先建立一個名為nginx且沒有登入權限的使用者和一個名為nginx的使用者組,然後安裝nginx所需的依賴庫和依賴包,最後通過.configure進行安裝的詳細配置。

#######建立nginx使用者和nginx組     [root@localhost ~]# groupadd -r nginx && useradd -r -g nginx -s /bin/false -M nginx     #######yum安裝nginx必須的依賴庫     [root@localhost ~]# yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed     #######官網下載下傳Nginx1.9.7的tar包,然後解壓到伺服器上     [root@localhost ~]# tar -zxf nginx-1.9.7.tar.gz && cd nginx-1.9.7     #######下載下傳pcre的tar包并解壓,以便支援Nginx的Rewrite功能     [root@localhost nginx-1.9.7]# tar -zxf ../pcre-8.38.tar.gz     #######下載下傳zlib的tar包并解壓,以便支援Nginx的Gzip壓縮功能     [root@localhost nginx-1.9.7]# tar -zxf ../zlib-1.2.8.tar.gz     #######建立Nginx1.9.7安裝時所需要的目錄     [root@localhost nginx-1.9.7]# cd /var/tmp/ && mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}     [root@localhost tmp]# mkdir -p /var/run/nginx && cd ~/nginx-1.9.7           

準備工作做好後,就開始正式配置Nginx-1.9.7的安裝明細了。注意,在使用下面這條configure參數配置時,一定要先把反斜杠“\”後面添加的注釋文字去掉!!!

[root@localhost nginx-1.9.7]# ./configure \     --prefix=/usr/share/nginx \                     [Nginx安裝目錄]     --sbin-path=/usr/sbin/nginx \                   [Nginx的sbin目錄]     --conf-path=/etc/nginx/nginx.conf \             [Nginx的配置檔案]     --error-log-path=/var/log/nginx/error.log \     [Nginx的錯誤日志]     --http-log-path=/var/log/nginx/access.log \     [Nginx的通路日志]     --pid-path=/var/run/nginx/nginx.pid  \          [Nginx的程序ID]     --lock-path=/var/lock/nginx.lock \     --user=nginx \                          [Nginx所屬使用者]     --group=nginx \                         [Nginx所屬使用者組]     --with-http_ssl_module \                    [Nginx的ssl子產品]     --with-http_spdy_module \               [Nginx的Google spdy子產品]     --with-http_dav_module \     --with-http_flv_module \     --with-http_realip_module \     --with-http_addition_module \     --with-http_xslt_module \     --with-http_stub_status_module \     --with-http_sub_module \     --with-http_random_index_module \     --with-http_degradation_module \     --with-http_secure_link_module \     --with-http_gzip_static_module \            [Nginx的gzip壓縮子產品]     --with-http_perl_module \     --with-pcre=pcre-8.38 \                 [pcre的安裝目錄]     --with-zlib=zlib-1.2.8 \                    [pcre的安裝目錄]     --with-debug \                          [允許DEBUG]     --with-file-aio \     --with-mail \     --with-mail_ssl_module \     --http-client-body-temp-path=/var/tmp/nginx/client_body \     --http-proxy-temp-path=/var/tmp/nginx/proxy \     --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \     --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \     --http-scgi-temp-path=/var/tmp/nginx/scgi \     --with-stream \                         [Nginx1.9.7特有的stream子產品]     --with-ld-opt="-Wl,-E"                  [gcc的編譯優化]           

配置簡要,如下圖:

配置完後,就可以直接編譯和安裝了

最後,直接使用執行這條指令

[root@localhost nginx-1.9.7]# make && make install

進行安裝即可。其中,make指令和make install指令的執行結果附圖如下:

配置Nginx1.9.0,使之正常工作

成功安裝Nginx1.9.0後,我們需要進行一些配置,包括開機啟動、SSL/HTTPS服務等。其中,Nginx服務控制腳本nginx參考文章《

Nginx服務啟動、停止和重新開機等操作的SHELL腳本

》,或者從

github

下載下傳上傳到/root/目錄

#######上傳Nginx服務控制腳本nginx,并賦予執行權限,删除安裝包,添加Nginx服務到開機啟動     [root@localhost ~]# mv ~/nginx2 /etc/init.d/nginx && chmod +x /etc/init.d/nginx     [root@localhost ~]# rm -rf nginx-1.9.7*     [root@localhost ~]# chkconfig --add nginx     [root@localhost ~]# chkconfig nginx on     #######測試配置是否正常     [root@localhost nginx]# nginx -t     nginx: the configuration file /etc/nginx/nginx.conf syntax is ok     nginx: configuration file /etc/nginx/nginx.conf test is successful     #######建立Nginx程序日志nginx.pid目錄,并啟動服務     [root@localhost nginx]# mkdir -p /var/run/nginx     [root@localhost init.d]# service nginx start     Starting nginx (via systemctl):                            [  确定  ]           

這裡啟動nginx服務時,會報“env: /etc/init.d/nginx: 沒有那個檔案或目錄”錯誤,用vi指令重新建立/etc/init.d/nginx腳本就可以成功運作

最後使用指令[root@typecodes nginx]# nginx -V檢視Nginx1.9.7的詳細資訊。

主機浏覽器輸入ip位址:

常見錯誤參考:

Nginx編譯安裝時常見錯誤分析