Nginx インストール · 4 min read · Jan 26, 2026

Fedora 19 における PHP5 (および PHP-FPM) と MySQL サポートを持つ Nginx のインストール

Nginx (発音は「エンジン x」) は、無料でオープンソースの高性能 HTTP サーバーです。Nginx は、その安定性、豊富な機能セット、シンプルな設定、低リソース消費で知られています。このチュートリアルでは、PHP5 サポート (PHP-FPM 経由) と MySQL サポートを持つ Fedora 19 サーバーに Nginx をインストールする方法を示します。

私は、これがあなたにとって機能するという保証はしません!

1 予備ノート

このチュートリアルでは、ホスト名 server1.example.com と IP アドレス 192.168.0.100 を使用します。これらの設定はあなたの環境によって異なる場合があるため、適宜置き換える必要があります。

2 MySQL/MariaDB 5 のインストール

まず、次のように MySQL 5 をインストールします:

yum install mysql mysql-server

次に、MySQL のシステム起動リンクを作成し (システムが起動するたびに MySQL が自動的に起動するように)、MySQL サーバーを起動します:

systemctl enable mysqld.service  
systemctl start mysqld.service

次に、ネットワークが有効になっていることを確認します。次のコマンドを実行します:

netstat -tap | grep mysql

次のような出力が表示されるはずです:

[root@server1 ~]# netstat -tap | grep mysql  
tcp        0      0 *:mysql                     *:*                         LISTEN      1116/mysqld  
[root@server1 ~]#

もし表示されない場合は、/etc/my.cnf を編集し、skip-networking オプションをコメントアウトします:

vi /etc/my.cnf
[...]
#skip-networking
[...]

その後、MySQL サーバーを再起動します:

systemctl restart mysqld.service

次のコマンドを実行して、root ユーザーのパスワードを設定します (そうしないと、誰でもあなたの MySQL データベースにアクセスできてしまいます!):

mysql_secure_installation
[root@server1 ~]# mysql_secure_installation  
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found  
  
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB  
SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!  
  
In order to log into MariaDB to secure it, we'll need the current  
password for the root user.  If you've just installed MariaDB, and  
you haven't set the root password yet, the password will be blank,  
so you should just press enter here.  
  
Enter current password for root (enter for none): <-- ENTER  
OK, successfully used password, moving on...  
  
Setting the root password ensures that nobody can log into the MariaDB  
root user without the proper authorisation.  
  
Set root password? [Y/n] <-- ENTER  
New password: <-- yourrootsqlpassword  
Re-enter new password: <-- yourrootsqlpassword  
Password updated successfully!  
Reloading privilege tables..  
... Success!  
  
By default, a MariaDB installation has an anonymous user, allowing anyone  
to log into MariaDB without having to have a user account created for  
them.  This is intended only for testing, and to make the installation  
go a bit smoother.  You should remove them before moving into a  
production environment.  
  
Remove anonymous users? [Y/n] <-- ENTER  
... Success!  
  
Normally, root should only be allowed to connect from 'localhost'.  This  
ensures that someone cannot guess at the root password from the  
network.  
  
Disallow root login remotely? [Y/n] <-- ENTER  
... Success!  
  
By default, MariaDB comes with a database named 'test' that anyone can  
access.  This is also intended only for testing, and should be removed  
before moving into a production environment.  
  
Remove test database and access to it? [Y/n] <-- ENTER  
- Dropping test database...  
... Success!  
- Removing privileges on test database...  
... Success!  
  
Reloading the privilege tables will ensure that all changes made so far  
will take effect immediately.  
  
Reload privilege tables now? [Y/n] <-- ENTER  
... Success!  
  
Cleaning up...  
  
All done!  If you've completed all of the above steps, your MariaDB  
installation should now be secure.  
  
Thanks for using MariaDB!  
[root@server1 ~]#

3 Nginx のインストール

Nginx は Fedora 19 のパッケージとして利用可能で、次のようにインストールできます:

yum install nginx

次に、nginx のシステム起動リンクを作成し、起動します:

systemctl enable nginx.service  
systemctl start nginx.service

ブラウザにウェブサーバーの IP アドレスまたはホスト名を入力します (例: http://192.168.0.100)。nginx のウェルカムページが表示されるはずです:

4 PHP5 のインストール

PHP5 を nginx で PHP-FPM を通じて動作させることができます (PHP-FPM (FastCGI プロセスマネージャ) は、あらゆるサイズのサイト、特に忙しいサイトに役立つ追加機能を持つ PHP FastCGI の代替実装です)。公式の Fedora 19 リポジトリには php-fpm パッケージがあるため、php-cli や MySQL を PHP スクリプトから使用するために必要な php-mysqlnd などの PHP5 モジュールと一緒に php-fpm をインストールできます:

yum install php-fpm php-cli php-mysqlnd php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy

Zend OPcache は、PHP 中間コードをキャッシュおよび最適化するための無料でオープンな PHP opcode キャッシャーです。他の PHP opcode キャッシャー (APC や Xcache など) と似ています。PHP ページの速度を上げるために、これらのいずれかをインストールすることを強くお勧めします。Zend OPcache は現在 PHP 5.5 に公式に含まれているため、他の opcode キャッシャーの代わりにこれを使用します。

Zend OPcache は次のようにインストールできます:

yum install php-opcache

次のようなエラーを避けるために:

[13-Nov-2011 22:13:16] PHP Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Berlin' for 'CET/1.0/no DST' instead in /usr/share/nginx/html/info.php on line 2

… /var/log/php-fpm/www-error.log で PHP スクリプトをブラウザで呼び出すときに、/etc/php.ini を開いて date.timezone を設定する必要があります:

vi /etc/php.ini
[...]
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Europe/Berlin"
[...]

次に、php-fpm のシステム起動リンクを作成し、起動します:

systemctl enable php-fpm.service  
systemctl start php-fpm.service

PHP-FPM は、ポート 9000 で FastCGI サーバーを実行するデーモンプロセスです。

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  4;
[...]
    keepalive_timeout  2;
[...]

仮想ホストは server {} コンテナ内で定義されます。デフォルトの vhost はファイル /etc/nginx/nginx.conf の下部に定義されています - 次のように修正します:

vi /etc/nginx/nginx.conf
[...]
    server {
        listen       80;
        server_name  _;

        #charset koi8-r;

        #access_log  /var/log/nginx/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.php index.html index.htm;
        }

        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.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_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;
        }

        # 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 $document_root$fastcgi_script_name; に変更することを確認してください。そうしないと、PHP インタープリターはブラウザで呼び出す PHP スクリプトを見つけられません。

try_files $uri =404; 行を追加して、ゼロデイの脆弱性を防ぐことに注意してください (http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHP および http://forum.nginx.org/read.php?2,88845,page=3 を参照)。代わりに、try_files $uri =404; 行を使用したくない場合は、/etc/php5/fpm/php.ini で cgi.fix_pathinfo = 0; を設定できます (その後、PHP-FPM を再読み込みすることを忘れないでください)。

ファイルを保存して nginx を再読み込みします:

systemctl reload nginx.service

次に、ドキュメントルート /usr/share/nginx/html に次の PHP ファイルを作成します…

vi /usr/share/nginx/html/info.php

ブラウザでそのファイルを呼び出します (例: http://192.168.0.100/info.php):

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

6 リンク

Share: X/Twitter LinkedIn

新しい投稿を受信箱で受け取る

スパムはありません。いつでも購読を解除できます。