LoginSignup
19
19

More than 5 years have passed since last update.

Nginx向けにPHPをソースコードからインストール

Last updated at Posted at 2015-04-07

本記事はNginxは既にインストールされている前提としております。Nginxのインストール方法はこちらを参考にしてください。

ソースコードを取得し、展開する

PHPデベロッパーサイトから使用したいバージョンのソースコードを取得し、展開します。

$ wget http://jp1.php.net/get/php-5.6.7.tar.gz/from/this/mirror
$ tar zxvf php-5.6.7.tar.gz

ソースコードをビルドする

実際にコンパイルし、インストールを実行します。

$ ./configure
    --prefix=/usr/local/stow/php-5.6.3
    --with-pear=/usr/local/stow/php-5.6.3/lib/pear
    --with-pgsql=/usr/local
    --with-mysql=/usr/local
    --with-pdo-mysql
    --with-pdo-pgsql
    --with-curl
    --with-zlib
    --with-mcrypt
    --with-openssl
    --disable-fileinfo
    --enable-fpm
$ make
$ make test
$ sudo make install

Apacheの場合は同一プロセスで動作させることが出来るモジュール版PHPが提供されていますが、Nginxには存在しません。Nginx上でPHPを利用する場合はCGIの形式を取り、PHPの処理要求が発生したら常駐しているphp-cgiのプロセスに処理要求を行うことになります。そのため、Nginxからの要求をphp-cgiへ伝えてやるプログラムが間に必要になります。そのプログラムが「PHP-FPM」であり、これを有効にするオプションが「--enable-fpm」となります。

その他オプションについては必要に応じて設定する必要があるので説明は割愛します。

起動ファイルを用意

PHP-FPMの自動起動ファイルを設置、設定します。起動ファイルの雛形はコンパイル時のディレクトリ群の中に在り、それを複製して利用します。

$ sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

PHP-FPMの設定

インストール先に既に設定ファイルの雛形が用意されているので、これを複製して環境に合わせて設定します。

$ cd /usr/local/stow/php-5.6.7/etc
$ cp php-fpm.conf.default php-fpm.conf
$ diff php-fpm.conf php-fpm.conf.default
--------------------------
26d25
< pid = /var/run/php-fpm.pid
34d32
< error_log = /var/log/php-fpm/php-fpm.log
151,154c149,150
< ;user = nobody
< user = nginx
< ;group = nobody
< group = nginx
---
> user = nobody
> group = nobody
--------------------------
$ sudo mkdir /var/log/php-fpm

Nginxの設定

Nginx本体の設定についても、PHPが動くように変更を加えます。

  location / {
<     root   /var/www;
<     index  index.php;
---
>     root   /usr/share/nginx/html;
>     index  index.html index.htm;
  }

> #location ~ \.php$ {
> #    root           html;
> #    fastcgi_pass   127.0.0.1:9000;
> #    fastcgi_index  index.php;
> #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
> #    include        fastcgi_params;
> #}
---
< location ~ \.php$ {
<     root           /var/www;
<     fastcgi_pass   127.0.0.1:9000;
<     fastcgi_index  index.php;
<     fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
<     include        fastcgi_params;
< }

各サービスの起動

実際にWEBサーバを起動して挙動を確認しましょう。

$ /etc/init.d/php-fpm start
$ /etc/init.d/nginx start
19
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
19