ubuntu化した玄箱にとりあえずnginxをインストールしたが、使う使わないは別としてやっぱりPHP5はインストールして使えるようにしておきたい所。ということで、今回はnginxでphp5を動かすお話。
nginxのインストール方法についてはこちらのエントリーをどうぞ。
玄箱 UbuntuにHTTPサーバー(nginx)をインストールした
なんとなく先ずは mysqlをインストール
php5の前にaptでサクッとmysqlもインストールしておいた
# apt-get install mysql
# apt-get install mysql-server
# apt-get install php5-mysql
本題 PHP5のインストール
そしてphp5のインストールとnginxで使えるようにする方法
ほとんど、下記ページの受け売りですが…
nginx x fastcgiでphp5を動かす on debian
php5のインストール
パッケージがあるのでaptで簡単インストール
# apt-get install php5-cli php5-cgi php5-gd
spawn-fcgiのインストール
パッケージが存在しないのでソースからインストール
# wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
# tar zxvf spawn-fcgi-1.6.3.tar.gz
# cd spawn-fcgi-1.6.3
# ./configure prefix=/usr/local/spawn-fcgi-1.6.3
# make
# make install
# ln -s /usr/local/spawn-fcgi-1.6.3/bin/spawn-fcgi /usr/bin/spawn-fcgi
php-fastcgiの作成
# vi /usr/bin/php-fastcgi
php-fastcgiの内容は下記の通り
#! /bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u www-data -f /usr/bin/php5-cgi
php-fastcgiに実行権限を与えておく
# chmod 755 /usr/bin/php-fastcgi
/etc/init.d/php-fastcgiを作成
# vi /etc/init.d/php-fastcgi
php-fastcgiの内容は下記の通り
#!/bin/bash
### BEGIN INIT INFO
# Provides: php-fastcgi
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the php-fastcgi
# Description: starts the php-fastcgi
### END INIT INFO
PHP_SCRIPT=/usr/bin/php-fastcgi
FASTCGI_USER=www-data
RETVAL=0
PIDFILE=/var/run/php5-cgi.pid
case "$1" in
start)
su - $FASTCGI_USER -c $PHP_SCRIPT
pidof php5-cgi > $PIDFILE
RETVAL=$?
;;
stop)
killall -9 php5-cgi
echo '' > $PIDFILE
RETVAL=$?
;;
restart)
killall -9 php5-cgi
su - $FASTCGI_USER -c $PHP_SCRIPT
pidof php5-cgi > $PIDFILE
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
console output
実行権限とOS起動時の設定
# chmod 755 /etc/init.d/php-fastcgi
# update-rc.d php-fastcgi defaults
# /etc/init.d/php-fastcgi start
nginxでphp5を使えるようにする
「」の方法でインストールすると htmlドキュメントのパスは
/usr/local/nginx-1.4.3/html
となっているはずなので、このパスをドキュメントルートとして話を進める。
/var/www/html などに置きたい方は適宜読み替えてください
アクセス権限の設定
# chown -R www-data:www-data /usr/local/nginx-1.4.3/html
nginx.confの編集
# vi /usr/local/nginx-1.4.3/conf/nginx.conf
nginx.confの内容は下記の通り
logファイルのパスとして/var/log/nginx を指定しているので、nginxディレクトリがないときは
# makedir /var/log/nginx
で作成しておく
user www-data;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
error_log /var/log/nginx/localhost.error.log;
location / {
root html;
index index.html index.htm index.php mtview.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME html$fastcgi_script_name;
}
}
}
nginxの再起動
# service nginx restart
説明に抜けがなければ、これでphpがnginxでも使えるようになるはず。

コメント