CentOS上如何把Web服務器從Apache換到Nginx
Nginx簡介:
Nginx是一個高性能的HTTP服務器和反向代理服務器, 最大的優點是節省資源,適用于處理高并發的請求。
1. Nginx最初是按照反向代理設計的,和Apache不同, nginx關心如何處理url,而不是文件!
2. Apache 是個基于進程處理的web服務器,如果同時有多個請求,必須要啟動多個進程來處理。 這樣在高負載的情況下,資源的消耗和響應的速度都會有很大的問題。 而Nginx是個基于事件(event)的異步處理模式, 下面是Nginx的一個簡單的示意圖,有一個Master進程,Maste進程負責系統配置,管理socket,以及管理一個或是多個Worker進程。 而Worker進程接收和處理來自用戶(瀏覽器)的請求。一般來講,一個worker進程可以同時處理上千個用戶的連接請求。每個worker進程采用異步的,基于event的方式來處理用戶的請求。對于HTML的靜態頁面,Nginx會自行來處理,但對于PHP,JSP, Python等動態頁面,Nginx是通過FastCGI(或者SCGI,UWSGI)來把動態頁面的請求交給相應的處理程序來處理。
安裝Nginx
需要注意的是,在CentOS的YUM的基礎的容器中,并沒有nginx和php-fpm的RPM包。這兩個RMP包在epel的容器中, 雖然你可以從官網下載RPM包來安裝,但我個人建議,如果你的CentOS/Redhat中沒有加入YUM的epel容器,還是先把這個yum容器加上去比較好,以后可以省無數的折騰。epel具體的安裝方法,我在 Redhat/CentOS 軟件安裝之RPM和YUM 這篇文章中有介紹。
yum的容器庫中加入了epel容器后,在CentOS上安裝Nginx就非常簡單,運行下面的命令就可以了。
yum install nginx
安裝玩以后,會發現Nginx的配置文件放在 /etc/nginx目錄下, 一般在缺省的情況下,web的root目錄會在/usr/share/ngxin/html中。
安裝完nginx以后,我們要測試一下是否安裝成功了.
如果之前已經安裝過Apache的話,先要把Apache的服務停掉。
/etc/init.d/httpd stop #停掉apache服務 chkconfig httpd off #開機重啟后,apache服務不再啟動
這時候你在瀏覽器上輸入 http://主機ip, 如果能出現nginx的測試頁面 “Test page for the nginx http server on EPEL”就說明nginx已經正常運行了。
安裝php-fpm
PHP-FPM (PHP-FastCGI Process Manager) 是目前最常用的一個PHP FastCGI的實現。通俗的講,這個模塊在Nginx和PHP之間橋梁,使之可以互相通信和交換。
安裝及啟動過程如下:
yum install php-fpm /etc/init.d/php-fpm start chkconfig php-fpm on
下一步是確認一下,nginx和php-fpm是否已經正常運行. 執行 netstat -tunlp 命令,會看到大約如下的一個界面。
可以看到nginx在監聽80端口,而php-fpm在監聽9000端口。
設置Nginx 和 PHP-FPM
我們假定這個主機上有兩個網站,一個是aaa.com, 普通的PHP站點, 一個是bbb.com,為wordpress的博客。 我們就討論一下在這種情況下,如何設置nginx.
首先為站點建立相應的目錄
mkdir -p /var/www/aaa/html mkdir -p /var/www/bbb/html mkdir -p /var/log/nginx/aaa mkdir -p /var/log/nginx/bbb chown -R nginx:nginx /var/www/aaa/html chown -R nginx:nginx /var/wwww/bbb/html chown -R nginx:nginx /var/log/nginx/aaa chown -R nginx:nginx /var/log/nginx/bbb
為兩個網站分別設置虛擬目錄(virtual directory)
為了保證整個配置更加清晰,我們盡量不修改主配置文件/etc/nginx/nginx.conf , 而是在在/etc/nginx/conf.d目錄下建立兩個文件,一個是aaa.conf, 一個是bbb.conf
其中aaa.conf的內容如下 (aaa是一個普通的php網站):
server { listen 80 default_server; #當輸入ip時,會訪問aaa.com server_name www.aaa.com aaa.com *aaa.com; #這個應該是最好的寫法了 access_log /var/log/nginx/aaa/access.log; #access_log屬于ngx_http_log_module的設置, 缺省level為info error_log /var/log/nginx/aaa/error.log; #error_log屬于core module, 缺省的level是error location / { root /var/www/aaa/html; index index.php index.html index.htm; #由于是PHP類型的動態頁面為主,所以把index.php放在前面效率會更高些 # try_files $uri $uri/ /index.php?$args; #普通php網站因為沒有rewrite的話,這個不需要 } error_page 404 /404.html; #error_page errcode uri (也就是說出現了404錯誤,會請求/404.html) location = /404.html { #這是一個典型的location root /var/www/aaa/html; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/aaa/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # 因為我們不用Nginx做Apache的反向代理,所以不需要這個 #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # 這種寫法可以防止.jpg.php之類的攻擊,應該是最佳寫法了吧 location ~ \.php$ { root /var/www/aaa/html; try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_pass php; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; }
設置完aaa.com的環境后,還需要設置bbb.com的 nginx的配置,因為bbb.com是wordpress的站點, 除了和aaa.com相同的設置外,還有些特殊的設置,具體設置請參考 http://codex.wordpress.org/Nginx
至此,從Apache向Nginx的移植基本完成。
原文鏈接! http://www.androiddev.net/webserver-apache-to-nginx/