天天看點

建構基于Nginx的web伺服器

系統平台:RHEL 5

Nginx版本:nginx-0.8.54

一.安裝及配置Nginx

1.安裝pcre軟體包,pcre的作用為nginx提供相容perl的正規表達式庫。以下采用RHEL5CD光牒自帶的rpm包進行安裝,另外也可下載下傳最新的源碼包進行編譯安裝。

[root@localhost~]# rpm -ivh pcre-6.6-2.el5_1.7

[root@localhost~]# rpm -ivh pcre-devel-6.6-2.el5_1.7

2.安裝nginx

[root@localhost~]# tar zxf nginx-0.8.54.tar.gz

[root@localhost~]# cd nginx-0.8.54

[root@localhost nginx-0.8.54]# ./configure \     

> --user=nginx \           定義nginx運作的使用者

> --group=nginx \        定義nginx運作的組

> --with-http_stub_status_module     \\啟用站點狀态統計子產品

\\其他更多配置選項可以使用./configure --help指令進行檢視

[root@localhost nginx-0.8.54]# make && make install

二.Nginx服務的運作控制

1.添加nginx運作的使用者組:

[root@localhost nginx-0.8.54]# useradd -s /sbin/nologin nginx

2.Nginx預設安裝在/usr/local/nginx目錄下,為了友善應用,可以添加一個nginx主程式的符号連結:

[root@localhost nginx-0.8.54]# ln -sf /usr/local/nginx/sbin/nginx  /usr/sbin

3.使用nginx -t指令檢查nginx配置檔案是否有文法錯誤:

執行nginx -t後出現上述提示表示配置檔案文法正确。

4.使用nginx啟動服務,然後使用netstat指令進行檢視:

[root@localhost nginx-0.8.54]# nginx

5.nginx啟動成功後,可以在浏覽器中檢視初始的web頁面:

另外在伺服器指令行下使用文本浏覽器工具elink進行檢視:

6.使用系統信号控制nginx程序:

[root@localhost~]# kill -s HUP nginx   //重新加載配置檔案,等同于“killall -1 nginx”

[root@localhost~]# kill -s QUIT nginx  //安全退出,等同于“kill -3 nginx”

[root@localhost~]# kill -s TERM nginx //快速退出,不等待處理完目前連接配接

另外,為了友善管理,可以添加一個nginx服務腳本,使用chkconfig和service指令管理nginx服務:

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

#!/bin/bash 

#chkconfig: - 99 20 

#description: Nginx Service Control Script 

case "$1" in 

  start) 

         /usr/sbin/nginx    

         ;; 

  stop) 

         /usr/bin/killall -s QUIT nginx 

            ;; 

  restart) 

         $0 stop 

         $0 start 

  reload) 

         /usr/bin/killall -s HUP nginx 

  *) 

    echo "Usage:$0 {start|stop|restart|reload}" 

    exit 1 

esac 

exit 0 

[root@localhost~]# chmod a+x /etc/init.d/nginx    為nginx腳本賦予可執行權限

[root@localhost~]# chkconfig --add nginx

[root@localhost~]# chkconfig --level 2345 nginx on

接下來就可以使用service nginx stop|start|restart|reload對nginx服務進行控制:

三.建構基于域名的虛拟主機

1.修改nginx主配置檔案:

[root@localhost~]# vi /usr/local/nginx/conf/nginx.conf

user  nginx nginx;                 \\運作nginx的使用者

worker_processes  8;           \\工作程序數

error_log  logs/error.log;       \\錯誤日志的位置

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

pid        logs/nginx.pid;            \\程序檔案預設位于/usr/local/nginx/logs/nginx.pid

events {

    use epoll;                               \\參考事件模型,用于2.6以上的核心

    worker_connections  65535;    \\每個工作程序可接受的連接配接數

}

http {

    include       mime.types;        \\檔案擴充名與檔案類型映射表

    default_type  application/octet-stream;   \\預設檔案類型

    charset utf-8;                            \\站點頁面檔案使用的預設字元編碼

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;           \\開啟高效檔案傳輸模式

    tcp_nopush      on;      \\防止網絡阻塞

    tcp_nodelay     on;       \\防止延遲

    #keepalive_timeout  0;

    keepalive_timeout  65;   \\逾時時間

    #gzip  on;

 #第一虛拟主機配置

  server {

        listen       80;                 \\監聽端口

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   /var/www/sjzz;         \\網站的根目錄

            index  index.html index.htm;        \\目錄索引檔案名

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

 location /Status {                 //站點狀态統計

     stub_status on;

     access_log off;

 }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #location ~ \.php$ {

        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #location ~ /\.ht {

        #    deny  all;

    }

#第二個虛拟主機配置

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    server {

         listen       80;         \\監聽端口

    #    listen       somename:8080;

         location / {

             root   /var/www/linux5234;          \\網站的根目錄

             index  index.html index.htm;

         }

  location /Status {        //站點狀态統計

      stub_status on;

      access_log off;

    # HTTPS server

    #server {

    #    listen       443;

    #    server_name  localhost;

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

    #    ssl_prefer_server_ciphers   on;

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

2.重新開機nginx服務:

[root@localhost~]# service nginx restart

3.建立虛拟主機對應網頁目錄及測試頁面:

[root@localhost~]#  mkdir -p /var/www/sjzz   /var/www/linux5234

[root@localhost~]#  echo "This is www.sjzz.com" > /var/www/sjzz/index.html

[root@localhost~]#  echo "This is www.linxu5234.com" > /var/www/linux5234/index.html

4.在客戶浏覽器中通路不同的虛拟主機:

四.配置站點狀态統計

1.修改nginx.conf檔案,在server{........}配置部分分别添加如下配置項:

location /Status {

     stub_status on;            \\啟用狀态統計子產品

     access_log off;             \\關閉日志記錄

2.重新開機nginx服務

3.在用戶端浏覽器中通路“http://www.sjzz.com/Status”和“http://www.sjzz.com/Status”,

即可看到站點的狀态統計資訊:

五.配置FastCGI方式支援的PHP頁面

使用RHEL5系統自帶的php軟體包時,php-cgi工具由php-cli-5.1.6-23.2.el5_3

提供,位于/usr/bin/php-cgi。若使用源碼包編譯安裝php環境,需要在“./configure”時添加“--enable-cgi”選項,同進去掉“--with-apxs2”選項,否則可能無法編譯出php-cgi程式。本文采用系統預設安裝php環境。

可以使用rpm -qa |grep php檢視php-cli-5.1.6-23.2.el5_3是否安裝,如果沒有安裝,就立即挂載CD光牒進行安裝。

1.擷取spawn-fcgi工具

spawn-fcgi從Lighttpd源碼包中獲得:

[root@localhost~]# tar zxf lighttpd-1.4.20.tar.gz

[root@localhost~]# cd lighttpd-1.4.20

[root@localhost~]# ./configure && make

[root@localhost~]# cp src/spawn-fcgi  /usr/sbin/

2.啟動php-cgi

[root@localhost~]# spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi  -C 16    \\啟動16個php-cgi子程序,在127.0.0.1的9000端口監聽服務

3.檢視spawn-fcgi運作狀态:

使用ps aux |grep php-cgi檢視程序:

4.如果需要在每次開機後都運作spawn-fcgi指令,可以将它添加到/etc/init.d/nginx或者/etc/rc.local腳本中:

[root@localhost~]# vi /etc/rc.local

/usr/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi  -C 16   

5.修改nginx.conf配置檔案,配置nginx支援PHP頁面:

location ~ \.php$ {

     root   /var/www/php;

     fastcgi_pass 127.0.0.1:9000;

     fastcgi_index index.php;

     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

     include fastcgi_params;

重新開機nginx服務:

6.建立php網頁目錄和測試頁面:

[root@localhost~]# mkdir /var/www/php

[root@localhost~]# vi /var/www/php/index.php

<?php

echo "PHP is OK!";

phpinfo{};

?>

7.在客戶浏覽器中進行驗證(成功):

本文轉自 kk5234 51CTO部落格,原文連結:http://blog.51cto.com/kk5234/514578,如需轉載請自行聯系原作者

繼續閱讀