Nginx 是比較輕巧的 HTTP Server, 此篇主要架設環境是於 Ubuntu Linux 10.04, Debian Linux.
Nginx 與 Apache 效能比較
- Nginx 跟 Apache 效能到底差多少, 此篇文章有清楚的圖文說明: [原]nginx折騰記(HTTP性能測試,與 Apache 對比)
Nginx 文件參考
- Nginx Configuration - Config
- Nginx VirtualHostExample - Virtualhost
- Nginx PHPFcgiExample - 設定 php cgi 支援(推薦)
- Nginx Modules
- Nginx FullExample - Full example
- Nginx RewriteMultiCondExample - Rewirte
Nginx + PHP CGI 安裝步驟
下述是 Nginx + PHP5 CGI 安裝步驟, 假設 Web server 的專案目錄是 /var/www/example.
- apt-get update
- apt-get install php5-cgi
- apt-get install nginx
- vim /etc/init.d/php-fcgi # 建立 PHP CGI 開機啟動 Script
- #!/bin/bash
- BIND=127.0.0.1:9000
- USER=www-data
- PHP_FCGI_CHILDREN=15
- PHP_FCGI_MAX_REQUESTS=1000
- PHP_CGI=/usr/bin/php-cgi
- PHP_CGI_NAME=`basename $PHP_CGI`
- PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
- RETVAL=0
- start() {
- echo -n "Starting PHP FastCGI: "
- start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
- RETVAL=$?
- echo "$PHP_CGI_NAME."
- }
- stop() {
- echo -n "Stopping PHP FastCGI: "
- killall -q -w -u $USER $PHP_CGI
- RETVAL=$?
- echo "$PHP_CGI_NAME."
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart)
- stop
- start
- ;;
- *)
- echo "Usage: php-fastcgi {start|stop|restart}"
- exit 1
- ;;
- esac
- exit $RETVAL
- chmod +x /etc/init.d/php-fcgi
- update-rc.d php-fcgi defaults # 開機自動啟動 php-fcgi
- mkdir /var/www/example # Web 根目錄
- vim /var/www/example/index.php
- <?php
- phpinfo();
- ?>
- vim /etc/nginx/sites-available/www # 設定 Nginx VirtualHost,下述包含常用設定範例
- server {
- listen 80;
- server_name example.com;
- root /var/www/example;
- charset utf-8;
- error_page 404 /404.html;
- access_log /var/log/nginx/example.access.log;
- error_log /var/log/nginx/example.error.log;
- location / {
- # root /var/www/example;
- index index.html index.htm;
- }
- # 只有 127.0.0.1 可以存取, 其它擋掉
- location /doc {
- root /usr/share;
- autoindex on;
- allow 127.0.0.1;
- deny all;
- }
- # 開啟檔案索引列表
- location /images {
- root /usr/share;
- autoindex on;
- }
- # 設定靜態檔案 expires 時間
- location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
- access_log off;
- expires 30d;
- }
- # rewrite 下述寫法, 執行 "/forum/" 時, 一樣可以跑到下面的 "location /forum/", 而不會被上面的 "/" 裡面的 "^/.*" 影響
- # 而 "/" 裡面的 "^/.*", 一樣可以把所有沒寫到 location 的檔案全部納進來.
- # rewrite /
- location / {
- if (!-e $request_filename) {
- rewrite ^/form3/topic/(.*)$ /index.php?q=$1 last;
- rewrite ^/form3/forum/(.*)$ /forums/a.php?q=$1 last;
- rewrite ^/form3 /index.php?q=xxx last;
- rewrite ^/.* /index.php?q=xxx last;
- break;
- }
- }
- # rewrite /forum/
- location /forum/ {
- if (!-e $request_filename) {
- rewrite ^/forum/topic/(.*)$ /index.php?q=$1&$query_string last;
- rewrite ^/forum/forum/(.*)$ /forums/a.php?q=$1&$query_string last;
- rewrite ^/forum/ /index.php?$query_string last;
- break;
- }
- }
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- #fastcgi_param SCRIPT_FILENAME /var/www/example$fastcgi_script_name;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- include fastcgi_params;
- # 若上傳的目錄, 怕使用者傳 *.php 上來, 可以用此設定來避免危險 (網址是 /images/, 就不會經過 PHP 編譯執行)
- if ($uri !~ "^/images/") {
- fastcgi_pass 127.0.0.1:9000;
- }
- }
- }
- ln -s /etc/nginx/sites-available/www /etc/nginx/sites-enabled/www # 設定啟動 www
- /etc/init.d/nginx configtest # 測試設定檔是否有錯誤
- /etc/init.d/php-fcgi start # 啟動 PHP cgi
- /etc/init.d/nginx restart # 啟動 Nginx
- http://example.com/index.php # 就可以看到標準 phpinfo() 的畫面囉~ 到此即完成.
問題排除
- 若出現 No input file specified. 的錯誤, 可做下述設定試試看
$ vim /etc/php5/conf.d/nginx.ini # 寫入下述內容
cgi.fix_pathinfo=1
doc_root= - 若發現 $_SERVER['DOCUMENT_ROOT'] 跟正常應該出現的 DOCUMENT_ROOT 值不同, 則於設定檔加入: (於 "server {}" 層級, 上述範例已經有加入)
root /var/www/example; # "/var/www/example" 請替換成實際路徑
- 要使用像 Apache ServerAlias 的話, 要怎麼設定?
server_name example.com alias.example.com;
- nginx.conf 的 worker_processes 要設多少?
worker_processes 一般是與 CPU n核 的數量相同即可.
- nginx.conf 的 event 設定?
- events {
- use epoll; # Linux 效能最佳的 event 模式
- worker_connections 1024; # 每個 process 允許最大的同時連線數
- }
- 用 php cgi 跑的模式, 於 $_GET, $_POST, $_REQUEST 取得的值, 需要做 urldecode().
特別是參數列有遇到空白, 會變成 "+", 若這個值有需要去找某些資料, 就會出問題~
關於 Nginx 效能 的 相關網頁
- WordPress and bbPress nice URL rewrite for Nginx
- nginx rewrite 參數
- nginx rewrite 的一些參數
- nginx rewrite 參數和例子
- Nginx rewrite rules with wpmu, wp super cache, bbpress and sitemaps support
關於 Nginx + Awstats 分析 的 相關網頁
相關網頁
- Top 20 Nginx WebServer Best Security Practices - Nginx 安全該注意的設定(推薦)
- Nginx 建置小記
- nginx 中文網
- nginx 小設置
- Nginx 0.5.31 + PHP 5.2.4(FastCGI)搭建可承受3萬以上併發連接數,勝過Apache 10倍的Web服務器[原創]
- Nginx 0.5.33 + PHP 5.2.5(FastCGI)搭建勝過Apache 10倍的Web服務器(第2版)[原創]
- nginx + PHP + FastCGI:輕型網頁伺服器
- linux(CentOS 5.3) 下LNMP(linux+nginx+mysql+php)環境的搭建(原創)
- Nginx and PHP-FastCGI on Ubuntu 10.10 (Maverick)
請問一下
標題「架設 Nginx + PHP FastCGI 於 Ubuntu Linux 10.04」
FastCGI和php5-CGI是一樣的東西嗎?
請你指教
算是一樣的, 只是 FastCGI 另外有 Apache module.
可以參考這篇: http://michiel.vanbaak.info/docsphp5fcgi.htm