nginx學習(1):編譯、安裝、啟動


一、下載

從官網http://nginx.org/en/download.html 下載穩定版(目前最新穩定版是1.6.2)

二、解壓

tar zxf nginx-1.6.2.tar.gz
cd nginx-1.6.2

三、配置

./configure --prefix=/opt/app/nginx/1.6.2 --user=cargo

注:prefix指定安裝目錄,user指定運行nginx的用戶身份

通常第一次並不會順利成功,如果出現:

./configure: error: the HTTP rewrite module requires the PCRE library.

表示當前機器上沒有PCRE包,可以手動安裝:

sudo yum -y install pcre-devel

再次運行./configure --prefix=/opt/app/nginx/1.6.2 --user=cargo,又有新的錯誤:

./configure: error: the HTTP gzip module requires the zlib library.

類似的,手動安裝zlib包

sudo yum -y install zlib-devel

繼續重復剛才的./configure命名,如果出現:

./configure: error: the HTTP cache module requires md5 functions from OpenSSL library.

繼續手動安裝open-ssl

yum -y install openssl openssl-devel

一切ok后,接下來可以編譯了

四、編譯

make install

注:需要gcc環境,如果沒有安裝gcc,請先安裝gcc,方法 yum -y install gcc

順利的話,會在/opt/app/nginx/1.6.2路徑下生成很多文件

五、啟動

cd /opt/app/nginx/1.6.2/sbin

./nginx

如果出現:

nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

多半是80端口被占用

cd /opt/app/nginx/1.6.2/conf

vi nginx.conf

找到下面的內容:

      server {
          listen       80;
          server_name  localhost;
 
          #charset koi8-r;

將80端口改成其它端口,比如7040(當然也可以把跟80沖突的進程給kill掉),然后再回到sbin目錄,重復./nginx

正常的話,用ps -ef|grep nginx應該可以看到2個進程:

[cargo@vm-vmw1813-app sbin]$ ps -ef|grep nginx
cargo     4180     1  0 14:38 ?        00:00:00 nginx: master process ./nginx
cargo     4181  4180  0 14:38 ?        00:00:00 nginx: worker process

表示啟動正常,可以用瀏覽器 訪問 http://ip:7040/ 如果出現以下圖片:

恭喜,安裝成功!

其它一些有用的啟動參數:

Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/Cellar/nginx/1.8.0/)
  -c filename   : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

特別要提一下-V(大寫),有時候不知道配置文件在哪,用這個參數就能查出來。  

 

六、卸載、停止服務

卸載只要把目錄刪除掉就行了,如果自己為了運維方便,做了其它啟動的腳本,同步刪除

停止服務,直接kill掉nginx進程最直接。

當然也可以 ./nginx -s stop

 


免責聲明!

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



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