編譯安裝Nginx

如果要安裝Nginx,可以使用yum或者rpm來安裝,但需要配置epel源才能安裝。本文主要演示Nginx的編譯安裝過程。


系統環境:

主機:192.168.10.7 (CentOS 7)


一、安裝Nginx所依賴到的程序包所對應的開發包


這裏的開發包指的是以"devel"結尾的程序包,爲了讓Nginx支持URI重寫、SSL、zlib壓縮功能,則需要分別需要安裝pcre-devel、openssl-devel、zlib-devel。

[root@web ~]# yum -y install pcre-devel openssl-devel zlib-devel


注意:pcre的全稱爲perl compatible regular expressions,意爲“perl兼容正則表達式”。這裏選擇安裝pcre-devel是爲了使Nginx能夠支持URI重寫功能的rewrite模塊,而URI重寫是非常常用的功能。pcre的官網爲http://www.pcre.org/。


二、編譯安裝Nginx


Nginx的官網爲http://nginx.org/,本文以編譯安裝nginx-1.10.3爲例。

[root@web ~]# tar xf nginx-1.10.3.tar.gz
[root@web nginx-1.10.3]# cd nginx-1.10.3
[root@web nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf 
--user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var
/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock 
--with-http_ssl_module --with-http_gzip_static_module --with-debug --with-http_stub_status_module
[root@web nginx-1.10.3]# make -j 4 && make install
[root@web nginx-1.10.3]# ll /usr/local/nginx/
total 0
drwxr-xr-x. 2 root root 40 Jul 17 10:52 html
drwxr-xr-x. 2 root root 19 Jul 17 10:52 sbin


導出nginx程序文件路徑。

[root@web ~]# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH    # 修改PATH環境變量
[root@web ~]# . /etc/profile.d/nginx.sh
[root@web ~]# echo $PATH            # 查看修改後的PATH變量
/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin


下面是本次編譯使用的參數說明(可用./configure --help查看)。

--prefix=PATH                    set installation prefix        # 設置安裝目錄路徑
--conf-path=PATH                 set nginx.conf pathname        # 設置配置文件路徑名
--user=USER                      set non-privileged user for    # nginx進程的用戶權限
--group=GROUP                    set non-privileged group for   # nginx進程的用戶組權限
--error-log-path=PATH            set error log pathname         # 設置錯誤日誌路徑名
--http-log-path=PATH             set http access log pathname   # 設置訪問日誌路徑名
--pid-path=PATH                  set nginx.pid pathname         # 設置nginx進程的pid文件路徑名
--lock-path=PATH                 set nginx.lock pathname        # 設置nginx進程的鎖文件路徑名
--with-http_ssl_module           enable ngx_http_ssl_module     # 啓用ssl功能
--with-http_gzip_static_module   enable ngx_http_gzip_static_module
                                                                # 啓用gzip壓縮功能
--with-http_stub_status_module   enable ngx_http_stub_status_module
                                                                # 啓用狀態信息輸出功能
--with-debug                     enable debug logging           # 啓用日誌排錯功能

如果要查看當前使用的Nginx程序在編譯時使用的參數,可使用-V選項。

[root@web ~]# nginx -V
nginx version: nginx/1.10.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf 
--user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var
/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock 
--with-http_ssl_module --with-http_gzip_static_module --with-debug --with-http_stub_status_module


三、啓動服務並測試


安裝完Nginx之後還無法提供web服務,需要啓動Nginx才行,具體操作如下。


(1)啓動前需要檢查nginx配置文件的語法。

[root@web ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


注意:啓動前檢查配置文件的語法錯誤很重要,可以防止因爲配置錯誤而導致網站重啓或重載配置文件等對用戶的影響。


(2)啓動服務。

[root@web ~]# nginx


(3)查看nginx服務對應的端口是否啓動。

[root@web ~]# ss -tnl | egrep "^State|80"
State      Recv-Q Send-Q          Local Address:Port            Peer Address:Port
LISTEN     0      128                         *:80                         *:*
[root@web ~]# lsof -i :80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   16078  root    6u  IPv4 268024      0t0  TCP *:http (LISTEN)
nginx   16079 nginx    6u  IPv4 268024      0t0  TCP *:http (LISTEN)


(4)瀏覽Nginx提供的web頁面。

在Windows下打開瀏覽器,輸入http://192.168.10.7(192.168.10.7爲剛纔安裝Nginx服務的IP地址)。回車後可看到如下內容。

wKioL1lsMD3ABz74AAAnxldzT6Y524.png

說明Nginx服務搭建成功了。


注意:在本次測試中selinux和iptables是關閉的。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章