歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> CentOS 7編譯安裝Nginx1.9.0

CentOS 7編譯安裝Nginx1.9.0

日期:2017/3/3 12:28:32   编辑:Linux技術
原文:https://typecodes.com/web/centos7compilenginx.html
我遇到的問題:在make的時候,遇到如下問題:
make[2]: 進入目錄“/home/eastlhu/soft/nginx-1.9.15/pcre-8.38”
CDPATH="${ZSH_VERSION+.}:" && cd . && /bin/sh /home/eastlhu/soft/nginx-1.9.15/pcre-8.38/missing aclocal-1.15 -I m4
/home/eastlhu/soft/nginx-1.9.15/pcre-8.38/missing:行81: aclocal-1.15: 未找到命令
WARNING: 'aclocal-1.15' is missing on your system.
You should only need it if you modified 'acinclude.m4' or
'configure.ac' or m4 files included by 'configure.ac'.
The 'aclocal' program is part of the GNU Automake package:
<http://www.gnu.org/software/automake>
It also requires GNU Autoconf, GNU m4 and Perl in order to run:
<http://www.gnu.org/software/autoconf>
<http://www.gnu.org/software/m4/>
<http://www.perl.org/>
make[2]: *** [aclocal.m4] 錯誤 127
make[2]: 離開目錄“/home/eastlhu/soft/nginx-1.9.15/pcre-8.38”
make[1]: *** [pcre-8.38/.libs/libpcre.a] 錯誤 2
make[1]: 離開目錄“/home/eastlhu/soft/nginx-1.9.15”
make: *** [build] 錯誤 2
這時候進入pcre目錄,執行 autoreconf -ivf
然後繼續編譯,報了一個:
groff -mandoc -f H -T ps zlib.3 | ps2pdf - zlib.3.pdf
/bin/sh: ps2pdf: 未找到命令
安裝
[code]安裝ps2pdf
yum -y install ghostscript

下面開始正文:
該文主要記錄如何在CentOS 7.1中編譯安裝Nginx官方最新的1.9.0版本。由於像Nginx、Mysql和PHP7的的源碼都是用C/C++寫的,所以自己的CentOS 7.1服務器上必須要安裝gcc和g++軟件(CentOS 7系列會自帶這兩個編譯軟件)。


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

先創建一個名為nginx且
沒有登錄權限
的用戶和一個名為nginx的用戶組,然後安裝nginx所需的依賴庫和依賴包,最後通過
.configure
進行安裝的詳細配置。另外,補錄一個pcre的tar包備份地址:https://o3cex9zsl.qnssl.com/libs/nginx/pcre-8.36.tar.gz,以及一個zlib的tar包備份地址:https://o3cex9zsl.qnssl.com/libs/nginx/zlib-1.2.8.tar.gz。
#######新建nginx用戶和nginx組
[root@typecodes ~]# groupadd -r nginx && useradd -r -g nginx -s /bin/false -M nginx
#######yum安裝nginx必須的依賴庫
[root@typecodes ~]# yum -y install openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed

#######官網下載Nginx1.9.0的tar包,然後解壓到服務器上
[root@typecodes ~]# wget -chttp://nginx.org/download/nginx-1.9.0.tar.gz [root@typecodes ~]# tar -zxf nginx-1.9.0.tar.gz && cd nginx-1.9.0

#######下載pcre的tar包並解壓,以便支持Nginx的Rewrite功能
[root@typecodes nginx-1.9.0]# wget -chttp://git.typecodes.com/libs/php/pcre-8.36.tar.gz && tar -zxf pcre-8.36.tar.gz
#######下載zlib的tar包並解壓,以便支持Nginx的Gzip壓縮功能
[root@typecodes nginx-1.9.0]# wget -chttp://git.typecodes.com/libs/nginx/zlib-1.2.8.tar.gz [root@typecodes nginx-1.9.0]# tar -zxf zlib-1.2.8.tar.gz

#######新建Nginx1.9.0安裝時所需要的目錄
[root@typecodes nginx-1.9.0]# cd /var/tmp/ && mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
[root@typecodes tmp]# mkdir -p /var/run/nginx && cd ~/nginx-1.9.0

准備工作做好後,就開始正式配置Nginx-1.9.0的安裝明細了。注意,在使用下面這條
configure
參數配置時,一定要先把反斜槓“\”後面添加的注釋文字去掉!!!
[root@typecodes nginx-1.9.0]# ./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模塊][1.9.5以上改為:--with-http_v2_module]
--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.36 \                 [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.0特有的stream模塊]
--with-ld-opt="-Wl,-E"                  [gcc的編譯優化]

配置過程大概需要5分鐘左右,部分截圖如下:


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

最後,直接使用執行這條命令
[root@typecodes nginx-1.9.0]# make && make install
進行安裝即可。其中,make命令和make install命令的執行結果附圖如下:



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

成功安裝Nginx1.9.0後,我們需要進行一些配置,包括開機啟動、SSL/HTTPS服務等。其中,Nginx服務控制腳本
nginx
見文章《Nginx服務啟動、停止和重啟等操作的SHELL腳本》。
#######上傳Nginx服務控制腳本nginx,並賦予執行權限,刪除安裝包,添加Nginx服務到開機啟動
[root@typecodes ~]# mv ~/nginx /etc/init.d/nginx && chmod +x /etc/init.d/nginx
[root@typecodes ~]# rm -rf nginx-1.9.0*
[root@typecodes ~]# chkconfig --add nginx
[root@typecodes ~]# chkconfig nginx on

由於博客准備全站啟用https服務,所以直接將前文《阿裡雲CentOS 6.5系統LNMP環境安裝SSL證書》中產生的私鑰typecodes.key和證書文件typecodes_last.crt打包的ssl.tar.gz上傳到服務器使用。而Nginx配置文件nginx.conf見文章《2015博客升級記(六):Nginx配置HTTPS和SPDY實戰》。
#######上傳ssl文件和Nginx配置文件nginx.conf
[root@typecodes ~]# mkdir -p /etc/nginx/ssl && tar -zxf ~/ssl.tar.gz -C /etc/nginx/ssl
[root@typecodes ~]# cd /etc/nginx/ && tar -zcf etc.nginx.tar.gz ./
[root@typecodes ~]# rm -rf ~/ssl.tar.gz 
[root@typecodes ~]# mv ~/nginx.conf /etc/nginx
mv: overwrite ‘/etc/nginx/nginx.conf’? y

#######測試配置是否正常
root@typecodes ~]# 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@typecodes ~]# mkdir -p /var/run/nginx/
[root@typecodes ~]# service nginx start
Restarting nginx (via systemctl):  [  OK  ]

最後使用命令
[root@typecodes nginx]# nginx -V
查看Nginx1.9.0的詳細信息。


4 錯誤分析

這裡特意分析了一些Nginx安裝過程中可能出現的錯誤情況,詳見文章《Nginx編譯安裝時常見錯誤分析》。
Copyright © Linux教程網 All Rights Reserved