CentOS下編譯安裝lnmp之nginx篇詳解
一、安裝環境
宿主機=> win7,虛擬機器 centos => 系統版本:CentOS Linux release 7.5.1804 (Core),ip地址 192.168.1.168
二、nginx下載
開啟官網: http://nginx.org/ ,選擇 nginx-1.14.1 stable 版本,
開啟連結找到 nginx-1.14.2,右鍵複製連結地址,
將其下載到目錄 /usr/local/src 中,
三、nginx安裝
1、解壓nginx-1.14.2.tar.gz
2、進入nginx-1.14.2目錄檢視原始檔案
3、執行 ./configure 命令,指定安裝路徑為 /usr/local/nginx
執行完畢報如下錯誤:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
大意是 HTTP的重寫模組需要PCRE庫的支援,要麼通過--without-http_rewrite_module忽略此模組,要麼將PCRE安裝進系統,
要麼構築PCRE庫的原始碼並通過 --with-pcre=<path>選項指定其所在路徑,這裡選擇最後一項,下載PCRE原始碼到 /usr/local/src 。
3.1、開啟 http://pcre.org/ ,找到 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
點選進入找到 pcre2-10.21.tar.bz2 最新版,複製連結地址下載,這個是pcre2新版本,可能會不相容,此處測試一下
3.2、下載
3.3、解壓
3.4、 重新執行 ./configure
結果又報如下錯誤:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
意思是缺少 zlib 庫,如同pcre一樣,我們下載解壓zlib
3.5、開啟 http://www.zlib.net/ ,找到
複製連結地址並下載到 /usr/local/src 目錄中
解壓
3.6、再次執行 ../configure 命令
./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre2-10.21 --with-zlib=/usr/local/src/zlib-1.2.11
3.7 、繼續執行 make && make install
結果報錯:
src/core/ngx_regex.h:15:18: fatal error: pcre.h: No such file or directory
#include <pcre.h>
^
compilation terminated.
make[1]: *** [objs/src/core/nginx.o] Error 1
make[1]: Leaving directory `/usr/local/src/nginx-1.14.2'
make: *** [build] Error 2
查詢資料,應該還是 pcre 版本問題,可能 pcre 2版本高的問題吧,嘗試下載 pcre-8.42.tar.bz2 版本
下載
解壓
3.8、再次嘗試執行 ./configure 命令
./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11
3.9、未報錯,再次執行 make && make install
執行結果成功安裝 nginx
四、nginx 啟動
執行 sbin 下的 nginx 如下
說明執行成功,如果有諸如 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 之類的提示,說明是80埠被佔用了,使用fuser -n tcp 80檢視80埠被誰佔用,
使用 kill -9 pid號 關閉後重新啟動 nginx 即可
五、nginx 測試
在客戶端瀏覽器輸入centos主機ip地址,測試 nginx 伺服器是否可訪問
、
nginx無法訪問,看一下網路是否連通
網路沒問題,很大可能就是防火牆問題了,systemctl status firewalld 檢視防火牆狀態
可知防火牆處於開啟狀態,此時可以關閉防火牆,也可將80埠配置為允許,此處配置80埠為允許,
firewall-cmd --zone=public --add-port=80/tcp --permanent
重新載入 firewall-cmd --reload ,並檢視80埠開啟狀態 firewall-cmd --list-ports
到此,成功配置防火牆例外80埠,再次測試 nginx ,如下
說明 nginx 伺服器已經搭建成功。
六、nginx 安裝總結
1、nginx編譯安裝需要 pcre 和 zlib 庫的支援,且 pcre2-10 最新版容易出錯,最好使用 pcre-8版本。
2、注意nginx 啟動時80埠是否被佔用。
3、如果nginx啟動成功,但不能訪問時,首先檢視網路是否處於連通狀態,當網路連通時檢視防火牆狀態及80埠是否被拒絕