Nginx 設定 · 1 min read · Feb 14, 2026
CentOS 6.3 に Nginx と PHP5 (および PHP-FPM) と MySQL サポートをインストールする - ページ 2
6 Nginx の設定
Nginx の設定は /etc/nginx/nginx.conf にあり、今開きます:
vi /etc/nginx/nginx.conf設定は理解しやすいです(詳細についてはここを参照してください: http://wiki.codemongers.com/NginxFullExample とここ: http://wiki.codemongers.com/NginxFullExample2)
まず(これはオプションです)、ワーカープロセスの数を増やし、keepalive_timeout を適切な値に設定できます:
| [...] worker_processes 4; [...] keepalive_timeout 2; [...] |
仮想ホストは /etc/nginx/conf.d ディレクトリ内の server {} コンテナで定義されています。デフォルトの vhost を次のように変更しましょう(/etc/nginx/conf.d/default.conf 内):
vi /etc/nginx/conf.d/default.conf| [...] server { listen 80; server_name _; #charset koi8-r; #access_log logs/host.access.log main; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /usr/share/nginx/html; try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$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; } } |
servername ; はこれをデフォルトのキャッチオール vhost にします(もちろん、ここに www.example.com のようなホスト名を指定することもできます)。
location / 部分では、index 行に index.php を追加しました。root /usr/share/nginx/html; はドキュメントルートが /usr/share/nginx/html ディレクトリであることを意味します。
PHP にとって重要な部分は location ~ .php$ {} スタンザです。これを有効にするためにコメントを外してください。root 行をウェブサイトのドキュメントルートに変更してください(例: root /usr/share/nginx/html;)。ゼロデイの脆弱性を防ぐために try_files $uri =404; 行を追加したことに注意してください(http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHP と http://forum.nginx.org/read.php?2,88845,page=3 を参照)。fastcgi_param 行を fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; に変更してください。そうしないと、PHP インタープリタはブラウザで呼び出す PHP スクリプトを見つけられません($document_root は /usr/share/nginx/html に変換されます。これは私たちが設定したドキュメントルートです)。
PHP-FPM はデフォルトで 127.0.0.1 のポート 9000 で待機しているため、nginx に 127.0.0.1:9000 に接続するように指示します。fastcgi_pass 127.0.0.1:9000; の行です。PHP-FPM が Unix ソケットを使用するようにすることも可能です - これについては第 7 章で説明します。
ファイルを保存して nginx をリロードします:
/etc/init.d/nginx reload次に、ドキュメントルート /usr/share/nginx/html に次の PHP ファイルを作成します…
vi /usr/share/nginx/html/info.php| |
今、ブラウザでそのファイルを呼び出します(例: http://192.168.0.100/info.php):

ご覧の通り、PHP5 は動作しており、Server API 行に示されているように FPM/FastCGI を介して動作しています。さらに下にスクロールすると、MySQL モジュールを含む、すでに有効になっているすべてのモジュールが表示されます:

7 PHP-FPM を Unix ソケットを使用するように設定
デフォルトでは PHP-FPM は 127.0.0.1 のポート 9000 で待機しています。TCP のオーバーヘッドを回避するために、PHP-FPM を Unix ソケットを使用するように設定することも可能です。これを行うには、/etc/php-fpm.d/www.conf を開きます…
vi /etc/php-fpm.d/www.conf… そして listen 行を次のようにします:
| [...] ;listen = 127.0.0.1:9000 listen = /tmp/php5-fpm.sock [...] |
次に PHP-FPM をリロードします:
/etc/init.d/php-fpm reload次に、nginx の設定とすべての vhost を確認し、行 fastcgi_pass 127.0.0.1:9000; を fastcgi_pass unix:/tmp/php5-fpm.sock; に変更します。次のように:
vi /etc/nginx/conf.d/default.conf| [...] location ~ \.php$ { root /usr/share/nginx/html; try_files $uri =404; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } [...] |
最後に nginx をリロードします:
/etc/init.d/nginx reload8 リンク
- nginx: http://nginx.org/
- nginx Wiki: http://wiki.nginx.org/
- PHP: http://www.php.net/
- PHP-FPM: http://php-fpm.org/
- MySQL: http://www.mysql.com/
- CentOS: http://www.centos.org/
著者について
ファルコ・ティメは、Timme Hosting(超高速 Nginx ウェブホスティング)のオーナーです。彼は HowtoForge のリードメンテイナー(2005 年以来)であり、ISPConfig のコア開発者の一人です(2000 年以来)。彼はまた、O’Reilly の書籍「Linux システム管理」にも貢献しています。
新しい投稿を受信箱で受け取る
スパムはありません。いつでも購読を解除できます。