架設 Nginx + PHP FastCGI 於 Ubuntu Linux 10.04

Nginx 是比較輕巧的 HTTP Server, 此篇主要架設環境是於 Ubuntu Linux 10.04, Debian Linux.

Nginx 與 Apache 效能比較

Nginx 文件參考

Nginx + PHP CGI 安裝步驟

下述是 Nginx + PHP5 CGI 安裝步驟, 假設 Web server 的專案目錄是 /var/www/example.

  1. apt-get update
  2. apt-get install php5-cgi
  3. apt-get install nginx
  4. vim /etc/init.d/php-fcgi # 建立 PHP CGI 開機啟動 Script
    1. #!/bin/bash
    2. BIND=127.0.0.1:9000
    3. USER=www-data
    4. PHP_FCGI_CHILDREN=15
    5. PHP_FCGI_MAX_REQUESTS=1000
    6. PHP_CGI=/usr/bin/php-cgi
    7. PHP_CGI_NAME=`basename $PHP_CGI`
    8. 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"
    9. RETVAL=0
    10. start() {
    11. echo -n "Starting PHP FastCGI: "
    12. start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
    13. RETVAL=$?
    14. echo "$PHP_CGI_NAME."
    15. }
    16. stop() {
    17. echo -n "Stopping PHP FastCGI: "
    18. killall -q -w -u $USER $PHP_CGI
    19. RETVAL=$?
    20. echo "$PHP_CGI_NAME."
    21. }
    22. case "$1" in
    23. start)
    24. start
    25. ;;
    26. stop)
    27. stop
    28. ;;
    29. restart)
    30. stop
    31. start
    32. ;;
    33. *)
    34. echo "Usage: php-fastcgi {start|stop|restart}"
    35. exit 1
    36. ;;
    37. esac
    38. exit $RETVAL
  5. chmod +x /etc/init.d/php-fcgi
  6. update-rc.d php-fcgi defaults # 開機自動啟動 php-fcgi
  7. mkdir /var/www/example # Web 根目錄
  8. vim /var/www/example/index.php
  9. vim /etc/nginx/sites-available/www # 設定 Nginx VirtualHost,下述包含常用設定範例
    1. server {
    2. listen   80;
    3. server_name  example.com;
    4. root /var/www/example;
    5. charset utf-8;
    6. error_page  404  /404.html;
    7. access_log  /var/log/nginx/example.access.log;
    8. error_log  /var/log/nginx/example.error.log;
    9. location / {
    10. # root   /var/www/example;
    11. index  index.html index.htm;
    12. }
    13. # 只有 127.0.0.1 可以存取, 其它擋掉
    14. location /doc {
    15. root   /usr/share;
    16. autoindex on;
    17. allow 127.0.0.1;
    18. deny all;
    19. }
    20. # 開啟檔案索引列表
    21. location /images {
    22. root   /usr/share;
    23. autoindex on;
    24. }
    25. # 設定靜態檔案 expires 時間
    26. location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    27. access_log   off;
    28. expires         30d;
    29. }
    30. # rewrite 下述寫法, 執行 "/forum/" 時, 一樣可以跑到下面的 "location /forum/", 而不會被上面的 "/" 裡面的 "^/.*" 影響
    31. # 而 "/" 裡面的 "^/.*", 一樣可以把所有沒寫到 location 的檔案全部納進來.
    32. # rewrite /
    33. location / {
    34. if (!-e $request_filename) {
    35. rewrite ^/form3/topic/(.*)$ /index.php?q=$1 last;
    36. rewrite ^/form3/forum/(.*)$ /forums/a.php?q=$1 last;
    37. rewrite ^/form3 /index.php?q=xxx last;
    38. rewrite ^/.* /index.php?q=xxx last;
    39. break;
    40. }
    41. }
    42. # rewrite /forum/
    43. location /forum/ {
    44. if (!-e $request_filename) {
    45. rewrite ^/forum/topic/(.*)$ /index.php?q=$1&$query_string last;
    46. rewrite ^/forum/forum/(.*)$ /forums/a.php?q=$1&$query_string last;
    47. rewrite ^/forum/ /index.php?$query_string last;
    48. break;
    49. }
    50. }
    51. location ~ \.php$ {
    52. fastcgi_pass   127.0.0.1:9000;
    53. fastcgi_index  index.php;
    54. #fastcgi_param  SCRIPT_FILENAME  /var/www/example$fastcgi_script_name;
    55. fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    56. #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    57. include fastcgi_params;
    58. # 若上傳的目錄, 怕使用者傳 *.php 上來, 可以用此設定來避免危險 (網址是 /images/, 就不會經過 PHP 編譯執行)
    59. if ($uri !~ "^/images/") {
    60. fastcgi_pass 127.0.0.1:9000;
    61. }
    62. }
    63. }
  10. ln -s /etc/nginx/sites-available/www /etc/nginx/sites-enabled/www # 設定啟動 www
  11. /etc/init.d/nginx configtest # 測試設定檔是否有錯誤
  12. /etc/init.d/php-fcgi start # 啟動 PHP cgi
  13. /etc/init.d/nginx restart # 啟動 Nginx
  14. http://example.com/index.php # 就可以看到標準 phpinfo() 的畫面囉~ 到此即完成.

問題排除

  1. 若出現 No input file specified. 的錯誤, 可做下述設定試試看

    $ vim /etc/php5/conf.d/nginx.ini # 寫入下述內容
    cgi.fix_pathinfo=1
    doc_root=

  2. 若發現 $_SERVER['DOCUMENT_ROOT'] 跟正常應該出現的 DOCUMENT_ROOT 值不同, 則於設定檔加入: (於 "server {}" 層級, 上述範例已經有加入)

    root /var/www/example; # "/var/www/example" 請替換成實際路徑

  3. 要使用像 Apache ServerAlias 的話, 要怎麼設定?

    server_name   example.com alias.example.com;

  4. nginx.conf 的 worker_processes 要設多少?

    worker_processes 一般是與 CPU n核 的數量相同即可.

  5. nginx.conf 的 event 設定?
    1. events {
    2. use epoll; # Linux 效能最佳的 event 模式
    3. worker_connections  1024; # 每個 process 允許最大的同時連線數
    4. }
  6. 用 php cgi 跑的模式, 於 $_GET, $_POST, $_REQUEST 取得的值, 需要做 urldecode().

    特別是參數列有遇到空白, 會變成 "+", 若這個值有需要去找某些資料, 就會出問題~

關於 Nginx 效能 的 相關網頁

關於 Nginx + Awstats 分析 的 相關網頁

相關網頁

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

在〈架設 Nginx + PHP FastCGI 於 Ubuntu Linux 10.04〉中有 5 則留言

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料