1. 程式人生 > >CentOS6.5編譯安裝Nginx的方法

CentOS6.5編譯安裝Nginx的方法

Nginx的官網:http://nginx.org/ ,Nginx有三個版本:穩定版、開發版和歷史穩定版。開發版更新快,包含最新的功能和bug修復,但同時也可能會出現新的bug。開發版一旦更新穩定下來,就會被加入穩定版分支,穩定版更新較慢,但bug較少,所以生產環境優先選擇穩定版

一、下載Nginx安裝檔案

目前最新穩定版:http://nginx.org/download/nginx-1.10.1.tar.gz ,可以先下載好安裝檔案再通過ftp上傳的CentOS上,也可以在CentOS上直接通過wget命令下載,這裡我將檔案下載到了/home/software資料夾下,如下:

[[email protected]
software]# pwd /home/software [[email protected] software]# wget http://nginx.org/download/nginx-1.10.1.tar.gz

二、解壓安裝檔案

[[email protected] software]# tar -xzvf nginx-1.10.1.tar.gz 

三、執行configure命令

通過cd命令進入Nginx解壓檔案目錄,執行該目錄下的configure命令,--prefix是打算將Nginx安裝在哪個目錄。在執行configure命令之前,確保安裝了gcc、openssl-devel、pcre-devel和zlib-devel軟體庫

(gzip模組需要 zlib 庫,rewrite模組需要 pcre 庫,ssl 功能需要openssl庫),也可以直接執行configure命令,根據提示缺少的軟體庫安裝,下面有缺少相應庫報的錯誤資訊和安裝依賴庫的方法。

[[email protected] software]# cd nginx-1.10.1
[[email protected] nginx-1.10.1]# pwd
/home/software/nginx-1.10.1
[[email protected] nginx-1.10.1]# ./configure --prefix=/usr/local/nginx
1、如果報下面錯誤,說明還沒有安裝gcc編譯環境,可以通過yum線上安裝功能安裝gcc,重新執行configure命令。
[
[email protected]
nginx-1.10.1]# ./configure --prefix=/usr/local/nginx checking for OS + Linux 2.6.32-431.el6.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found
線上安裝gcc:
[[email protected] nginx-1.10.1]# yum install gcc
2、如果報下面的錯誤,說明沒有安裝pcre-devel庫,通過yum線上安裝pcre後,重新執行configure命令。
./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.
線上安裝pcre-devel庫:
[[email protected] nginx-1.10.1]# yum -y install pcre-devel
-y引數表示使用yum線上安裝時,如果需要使用者輸入Y/N時自動輸入Y。
3、如果報下面的錯誤,說明沒有安裝zlib庫,安裝zlib庫後重新執行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庫:
[[email protected] nginx-1.10.1]# yum -y install zlib-devel

4、如果報以下錯誤,說明沒有安裝OpenSSL庫,安裝OpenSSL庫後重新執行configure命令。

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
線上安裝openssl庫:
[[email protected] nginx-1.10.1]# yum install openssl-devel


執行configure命令成功後,顯示如下資訊:
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using builtin md5 code
  + sha1 library is not found
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

四、執行make命令

[[email protected] nginx-1.10.1]# make

五、執行make install命令

[[email protected] nginx-1.10.1]# make install

步驟四和步驟五可以合併執行如下命令,連線符 && 代表前面一個命令如果執行成功則繼續執行後面的命令,如果前面命令執行失敗則不再執行後面的命令。而 || 表示如果前面的命令執行成功則不執行後面的命令,如果前面的命令執行失敗則繼續執行後面的命令
[[email protected] nginx-1.10.1]# make && make install

六、啟動Nginx服務

[[email protected] nginx-1.10.1]# cd /usr/local/nginx/
[[email protected] nginx]# ll
總用量 16
drwxr-xr-x. 2 root root 4096 10月  1 23:35 conf
drwxr-xr-x. 2 root root 4096 10月  1 23:35 html
drwxr-xr-x. 2 root root 4096 10月  1 23:35 logs
drwxr-xr-x. 2 root root 4096 10月  1 23:35 sbin
[[email protected] nginx]# ./sbin/nginx 

通過瀏覽器訪問Nginx,顯示如下welcome to nginx!頁面便表示安裝成功:


注:

如果80埠被佔用,服務將啟動失敗,關閉其他程式重新啟動Nginx即可。

如果開啟著防火牆,需要開放80埠,在/etc/sysconfig/iptables檔案裡新增如下內容:

-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT