Nginx 設定 · 1 min read · Dec 13, 2025
Fedora 13 における PHP5 と MySQL サポート付き Nginx のインストール - ページ 2
4 PHP5 のインストール
Nginx で PHP5 を FastCGI を通じて動作させることができます。Fedora 用のスタンドアロン FastCGI デーモンパッケージは存在しないため、lighttpd の FastCGI パッケージ (lighttpd-fastcgi) を使用し、php-cli や MySQL を PHP スクリプトから使用するために必要な php-mysql などの PHP5 モジュールと一緒にインストールします:
yum install lighttpd-fastcgi php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy次に、/etc/php.ini を開き、ファイルの最後に cgi.fix_pathinfo = 1 の行を追加します:
vi /etc/php.ini| [...] cgi.fix_pathinfo = 1 |
lighttpd-fastcgi パッケージには、FastCGI プロセスを開始するために使用できる実行可能ファイル /usr/bin/spawn-fcgi が含まれています。詳細を知るには、
spawn-fcgi --helpを確認してください。
localhost のポート 9000 でリッスンし、ユーザーおよびグループ nginx として実行される PHP FastCGI デーモンを開始するには、次のコマンドを実行します:
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pidもちろん、システムを起動するたびにそのコマンドを手動で入力したくないので、システムが起動時に自動的にコマンドを実行するようにするために、/etc/rc.local を開きます…
vi /etc/rc.local… そしてファイルの最後にコマンドを追加します:
| [...] /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u nginx -g nginx -f /usr/bin/php-cgi -P /var/run/fastcgi-php.pid |
5 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 5; [...] keepalive_timeout 2; [...] |
仮想ホストは server {} コンテナ内で定義されています。デフォルトの vhost を次のように変更しましょう:
| [...] 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 /usr/share/nginx/html$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;) に変更してください。fastcgi_param 行を fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; に変更することを確認してください。そうしないと、PHP インタープリタはブラウザで呼び出す PHP スクリプトを見つけられません。
ファイルを保存し、nginx を再起動します:
/etc/init.d/nginx restart次に、ドキュメントルート /usr/share/nginx/html に次の PHP ファイルを作成します…
vi /usr/share/nginx/html/info.php| |
次に、そのファイルをブラウザで呼び出します (例: http://192.168.0.100/info.php):

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

6 リンク
- nginx: http://nginx.net/
- nginx Wiki: http://wiki.codemongers.com/Main
- PHP: http://www.php.net/
- MySQL: http://www.mysql.com/
- Fedora: http://fedoraproject.org/
新しい投稿を受信箱で受け取る
スパムはありません。いつでも購読を解除できます。