Linux Centos 6.5_x86安裝Nginx


一、下載

  地址:http://nginx.org/en/download.html

  或者在linux上使用wget命令下載:

  wget http://124.202.164.5/files/40950000082BD3BE/nginx.org/download/nginx-1.8.1.tar.gz

二、編譯安裝

[root@root3 soft]# tar -zxvf nginx-1.8.0.tar.gz
nginx-1.8.0/
nginx-1.8.0/auto/
nginx-1.8.0/conf/
nginx-1.8.0/contrib/
nginx-1.8.0/src/
nginx-1.8.0/configure
nginx-1.8.0/LICENSE
nginx-1.8.0/README
...
[root@root3 soft]# cd nginx-1.8.0
nginx-1.8.0]# ./configure
checking for OS
 + Linux 2.6.32-431.el6.i686 i686
checking for C compiler ... found
 + using GNU C compiler
 ...
 ./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.

  Nginx的一些模塊需要其他第三方庫的支持,比如gzip模塊需要zlib庫,rewrite模塊需要pcre庫,ssl需要openssl庫等。  

  上述error說明,沒有安裝pcre庫。

  安裝zlib、zlib-devel、openssl、openssl-devel、pcrepcre-devel

[root@root3 ~]# yum install zlib zlib-devle openssl openssl-devel pcre pcre-devel

  重新./configure

[root@root3 nginx-1.8.0]# ./configure
checking for OS
 + Linux 2.6.32-431.el6.i686 i686
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) 
...
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + md5: using system crypto library
  + sha1: using system crypto library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  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"
[root@root3 nginx-1.8.0]# make
[root@root3 nginx-1.8.0]# make install

  安裝成功!。

  按照以上命令,Nginx默認被安裝到/usr/local/nginx下。另外您可以通過./configure --help命令查看Nginx可選擇編譯項目。可參考《Nginx手動編譯的編譯選項解析》。

 

三、啟動、停止、平滑重啟

  a)啟動

    Nginx的安裝目錄是/usr/local/nginx,那么Nginx的啟動命令就是:

    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

    其中參數“-c”指定了配置文件的路徑,如果不加"-c'參數,Nginx會默認加載其安裝目錄的的conf子目錄的nginx.conf文件。在本例中是/usr/local/nginx/conf/nginx.conf。

    如果配置文件正確,則顯示以下信息:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

    測試是否啟動成功:

[root@mch conf]# ps -ef|grep nginx
root      2896     1  0 10:56 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
oracle    2897  2896  0 10:56 ?        00:00:00 nginx: worker process                                          
oracle    2898  2896  0 10:56 ?        00:00:00 nginx: worker process                                          
oracle    2899  2896  0 10:56 ?        00:00:00 nginx: worker process                                          
oracle    2900  2896  0 10:56 ?        00:00:00 nginx: worker process                                          
oracle    2901  2896  0 10:56 ?        00:00:00 nginx: worker process                                          
oracle    2902  2896  0 10:56 ?        00:00:00 nginx: worker process                                          
oracle    2903  2896  0 10:56 ?        00:00:00 nginx: worker process                                          
oracle    2904  2896  0 10:56 ?        00:00:00 nginx: worker process                                          
root      3033  2876  0 11:17 pts/0    00:00:00 grep nginx

   通過瀏覽器訪問:IP地址:Nginx端口號(端口號是在nginx.conf中server模塊中配置的),我的是:http://192.168.159.129:80/

  b) 停止

    Nginx的停止的方法有很多種,一般是通過發送系統信號給Nginx主進程的方式來停止Nginx。

    我們通過ps命令查看Nginx的主進程號:

[root@mch ~]# ps -ef | grep nginx
root      2439     1  0 10:49 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
oracle    2440  2439  2 10:49 ?        00:00:00 nginx: worker process                                          
oracle    2441  2439  2 10:49 ?        00:00:00 nginx: worker process                                          
oracle    2442  2439  2 10:49 ?        00:00:00 nginx: worker process                                          
oracle    2443  2439  2 10:49 ?        00:00:00 nginx: worker process                                          
oracle    2444  2439  2 10:49 ?        00:00:00 nginx: worker process                                          
oracle    2445  2439  2 10:49 ?        00:00:00 nginx: worker process                                          
oracle    2446  2439  2 10:49 ?        00:00:00 nginx: worker process                                          
oracle    2447  2439  2 10:49 ?        00:00:00 nginx: worker process                                          
root      2449  2407  0 10:49 pts/0    00:00:00 grep nginx

    從上邊可以看到:其中備注為master process的進程,表示他是主進程,另外8個“worker process”,表示是子進程,其中2439是主進程號。

    

    a)從容停止Nginx

      kill - QUIT Nginx主進程號。

      或:

      kill -QUTI /usr/local/nginx/nginx.pid

    b)快速停止Nginx

      kill - TERM Nginx主進程號

      kill - TERM /usr/local/nginx/nginx.pid

      或:

      kill - INT Nginx主進程號

      kill - INT /usr/local/nginx/nginx.pid

     c)強制停止所有nginx進程

        kill -9 nginx

  c)平滑重啟

      重啟之前要確認配置文件nginx.conf的語法是否正確,否則nginx不會加載新的配置文件。通過以下命令判斷neginx配置文件是否正確:

      /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

      控制台出現如下結果說明配置文件正確:

     nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
     nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

     查看nginx主進程號:    

[root@mch /]# ps -ef|grep nginx
root      2896     1  0 Oct13 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
oracle    9954  2896  3 15:46 ?        00:00:00 nginx: worker process                                          
oracle    9955  2896  3 15:46 ?        00:00:00 nginx: worker process                                          
oracle    9956  2896  2 15:46 ?        00:00:00 nginx: worker process                                          
oracle    9957  2896  2 15:46 ?        00:00:00 nginx: worker process                                          
oracle    9958  2896  3 15:46 ?        00:00:00 nginx: worker process                                          
oracle    9959  2896  3 15:46 ?        00:00:00 nginx: worker process                                          
oracle    9960  2896  3 15:46 ?        00:00:00 nginx: worker process                                          
oracle    9961  2896  3 15:46 ?        00:00:00 nginx: worker process                                          
root      9975  7915  0 15:46 pts/0    00:00:00 grep nginx

     重啟Nginx:

     kill -HUP Nginx主進程號

     kill -HUP /usr/local/nginx/nginx.pid

 

  參考文檔:

      《實戰Nginx:取代Apache的高性能Web服務器》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM