編譯安裝Nginx

Nginx官方站點:http://nginx.org

Nginx下載
wget http://nginx.org/download/nginx-1.6.2.tar.gz

1)安裝支持軟件:
Nginx的配置及運行需要pcre、zlib等軟件包的支持,因此應預先安裝這些軟件的開發包(devel),以便提供相應的庫和頭文件,確保Nginx的安裝順利完成。

[root@nginx ~]# service iptables stop
[root@nginx ~]# setenforce 0
[root@nginx ~]# yum -y install pcre-devel zlib-devel

2)創建運行用戶、組:
Nginx服務程序默認以nobody身份運行,建議爲其創建專門的用戶賬號,以便更準確地控制其訪問權限,增加靈活性、降低安全風險。如:創建一個名爲nginx的用戶,不建立宿主目錄,也禁止登錄到shell環境。

[root@nginx ~]# useradd -M -s /sbin/nologin nginx

3)編譯安裝nginx:
釋放nginx源碼包
[root@nginx ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src/

編譯前配置:
[root@nginx ~]# cd /usr/src/nginx-1.6.2/
[root@nginx nginx-1.6.2]# ./configure –prefix=/usr/local/nginx –user=nginx –group=nginx –with-http_stub_status_module –with-http_ssl_module –with-http_flv_module –with-http_gzip_static_module

注:配置前可以參考:./configure –help給出說明
–prefix 設定Nginx的安裝目錄
–user和—group 指定Nginx運行用戶和組
–with-http_stub_status_module 啓用http_stub_status_module模塊以支持狀態統計
–with-http_ssl_module 啓用SSL模塊
–with-http_flv_module 啓用FLV模塊,提供尋求內存使用基於時間的偏移量文件

編譯 安裝:
[root@nginx nginx-1.6.2]# make && make install

爲了使Nginx服務器的運行更加方便,可以爲主程序nginx創建鏈接文件,以便管理員直接執行nginx命令就可以調用Nginx的主程序。

[root@nginx nginx-1.6.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@nginx nginx-1.6.2]# ll /usr/local/bin/nginx
lrwxrwxrwx 1 root root 27 12-29 07:24 /usr/local/bin/nginx -> /usr/local/nginx/sbin/nginx

Nginx的運行控制:
與Apache的主程序httpd類似,Nginx的主程序也提供了”-t”選項用來對配置文件進行檢查,以便找出不當或錯誤的配置。配置文件nginx.conf默認位於安裝目錄/usr/local/nginx/conf/目錄中。若要檢查位於其他位置的配置文件,可使用”-c”選項來指定路徑。

[root@nginx conf]# nginx -t
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:
直接運行nginx即可啓動Nginx服務器,這種方式將使用默認的配置文件,若要改用其他配置文件,需添加”-c 配置文件路徑”選項來指定路徑。需要注意的是,若服務器中已安裝有httpd等其他WEB服務軟件,應採取措施(修改端口,停用或卸載)避免部突。

[root@nginx conf]# service httpd stop
[root@nginx conf]# netstat -anpt |grep :80
[root@nginx conf]# nginx
[root@nginx conf]# netstat -anpt |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6810/nginx: master

通過檢查 Nginx程序的監聽狀態,或者在瀏覽器中訪問此WEB服務(默認頁面將顯示”Welcome to nginx!”),可以確認Nginx服務是否正常運行。

[root@nginx ~]# elinks –dump http://localhost
Welcome to nginx!

主程序Nginx支持標準的進程信號,通過kill或者killall命令傳送

HUP 重載配置 等同於-1
QUIT 退出進程 等同於-3
KILL 殺死進程
USR1 重新寫入日誌
USR2 平滑重啓

[root@nginx ~]# killall -s HUP nginx
[root@nginx ~]# killall -s QUIT nginx
[root@nginx ~]# netstat -anpt |grep :80

http://urchin.blog.51cto.com/4356076/988860/

當Nginx進程運行時,PID號默認存放在logs/目錄下的nginx.pid文件中,因此若改用kill命令,也可以根據nginx.pid文件中的PID號來進行控制。

爲了使Nginx服務的啓動、停止、重載等操作更加方便,可以編寫Nginx服務腳本,並使用chkconfig和service工具來進行管理,也更加符合RHEL系統的管理習慣。

[root@nginx ~]# vim /etc/init.d/nginx

腳本
bin/bash
chkconfig: 2345 99 20
PROG=”/usr/local/nginx/sbin/nginx”
PIDF=”/usr/local/nginx/logs/nginx.pid”
case “1instart) PROG
;;
stop)
kill -s QUIT (cat PIDF)
;;
restart)
0stop 0 start
;;
reload)
kill -s HUP (cat PIDF)
;;
*)
echo “Usage: $0 {start|stop|restart|reload}”
exit 1
esac
exit 0

[root@nginx ~]# chmod +x /etc/init.d/nginx
[root@nginx ~]# chkconfig –add nginx
[root@nginx ~]# chkconfig nginx on
[root@nginx ~]# chkconfig –list nginx
nginx 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉

這樣就可以通過nginx腳本來啓動、停止、重啓、重載Nginx服務器了。

3、配置文件nginx.conf:
在Nginx服務器的主配置文件nginx.conf中,包括全局配置、I/O事件配置、HTTP配置這三大塊內容,配置語句的格式爲”關鍵字值;”(末尾以分號表示結束),以”#”開始的部分表示註釋。

1)全局配置
由各種配置語句組成,不使用特定的界定標記。全局配置部分包括運行用戶、工作進程數、錯誤日誌、PID存放位置等基本設置。
常用配置項:

user nginx; //運行用戶,Nginx的運行用戶實際是編譯時指定的nginx,若編譯時未指定則默認爲nobody
worker_processes 2; //指定nginx啓動的工作進程數量,建議按照cpu數目來指定,一般爲它的倍數
worker_cpu_affinity 00000001 00000010; //爲每個進程分配cpu,上例中將2個進程分配到兩個cpu,當然可以寫多個,或者將一個
進程分配到多個cpu
worker_rlimit_nofile 102400; //這個指令是指當一個nginx進程打開的最多文件數目,理論值應該是最多打開文件數(ulimit
-n)與nginx進程數相除,但是nginx分配請求並不是那麼均勻,所以最好與ulimit -n的值
保持一致。(通過”ulimit –n 數值”可以修改打開的最多文件數目)
error_log logs/error.log; //全局錯誤日誌文件的位置
pid logs/nginx.pid; //PID文件的位置

2)I/O事件配置:
使用”events {}”界定標記,用來指定Nginx進程的I/O響應模型,每個進程的連接數等設置
events {
use epool; //使用epool模型,對於2.6以上的內核,建議使用epool模型以提高性能
worker_connections 4096; //每個進程允許的最多連接數(默認爲1024),每個進程的連接數應根據實際需要來定,一般在10000以下,理論上每臺nginx服務器的最大連接數爲worker_processes*worker_connections,具體還要看服務器的硬件、帶寬等。
}

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