Macにnginx+PHP-FPM
nginx+PHP-FPMの記事があまり無いように見えたのでエントリーしておきます。
ちなみにnginxをインストールする事自体も初めてなので合わせて。
■環境の確認
・Mac Mountain Lion
最終的にはAWSにも乗せたいので基本はソースコンパイルで(一部本筋で無い部分はbrewを使用)
・nginx
・PHP-FPM
・CakePHP
■Reference sites
MacOSXLion10.7.5にNginxとPHP環境をソースから構築する - Web学び
GHOST IN THE SUIT - Wordpressをnginx+fastcgiベースに移行した
CakePHP 2.0開発の準備をしよう!(1/6):初心者のためのCakePHP2.0 プログラミング入門
サーバをAWS EC2+nginx+PHP+MySQLに乗り換えてみた | SUSH-i LOG
Network is unreachable
まずはwgetが無いと何も出来ないので取得しておきます
mkdir -p /usr/local/src
cd /usr/local/src
brew install wget
■PCREのインストール
元々私はPerlに慣れているので、PCREを入れておきます。
以下Web用語辞典から引用
>PCRE
>読み:ぴーしーあーるいー
>英語:Perl Compatible Regular Expressions
>意味:
>PCREとはPerl互換正規表現のことです。
>強力な文字処理能力を持つPerlの正規表現は広くなじまれており、Perlの正規表現に慣れた人たちは他の言語でも同じように使えるPCREは重宝します。
>Cなど他の言語にライブラリが提供されています。
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz
tar xfz pcre-8.32.tar.gz
cd /usr/local/src/pcre-8.32
./configure --enable-unicode-properties --enable-utf8
make
sudo make install
■文字コード変換ライブラリICU (International Components for Unicode)のインストール
wget http://download.icu-project.org/files/icu4c/50.1.2/icu4c-50_1_2-src.tgz
tar xfz icu4c-50_1_2-src.tgz
cd /usr/local/src/icu
sh source/configure --prefix=/usr/local
gnumake
sudo make install
■PHPのインストール
wget http://www.php.net/get/php-5.4.11.tar.gz/from/us1.php.net/mirror
tar xfz php-5.4.11.tar.gz※「--enable-fpm」をしっかり入れます
cd /usr/local/src/php-5.4.11
./configure --prefix=/usr/local/php \
--enable-mbstring \
--enable-mbregex \
--enable-json \
--enable-intl \
--enable-pcntl \
--enable-fpm \
--enable-exif \
--enable-sockets \
--with-mysql --with-pdo-mysql --with-mysqli \
--with-snmp=/usr \
--with-openssl \
--with-pcre-regex=/usr/local \
--with-zlib \
--with-mhash \
--with-xmlrpc \
--with-xsl \
--with-curl \
--with-pear
make
make test
sudo make install
■CHECK
php -v
PHP 5.4.11 (cli) (built: Feb 7 2013 14:33:11)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
■ファイルコピーなど
sudo cp /usr/local/src/php/php.ini-production /usr/local/php/lib/php.ini
cd /usr/local/php/etc
sudo cp php-fpm.conf.default php-fpm.confsudo ln -s /usr/local/php/bin/* /usr/local/bin/
sudo ln -s /usr/local/php/sbin/php-fpm /usr/local/bin/export PATH=/usr/local/bin:/usr/local/sbin:$PATH
■php-fpm.confの修正
sudo cp -a /private/etc/php-fpm.conf /private/etc/php-fpm.conf.old
vim /private/etc/php-fpm.conf[global]
pid = /var/run/php-fpm.pid
error_log = /var/log/php/php-fpm.log
log_level = notice
emergency_restart_threshold = 0
emergency_restart_interval = 0
process_control_timeout = 0
daemonize = yes[www]
listen = 127.0.0.1:9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1;listen.owner = nginx ;Macの場合は抜いておく。For AWS用
;listen.group = nginx ;Macの場合は抜いておく。For AWS用
listen.mode = 0666;user=nginx ;Macの場合は抜いておく。For AWS用
;group=nginx ;Macの場合は抜いておく。For AWS用user=[ENTER YOUR USER NAME]
group=staffpm = dynamic
pm.max_children = 10
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 8
pm.max_requests = 500
pm.status_path = /statusrequest_slowlog_timeout = 2
request_terminate_timeout= 5
slowlog = /var/log/php/$pool.log.slow
security.limit_extensions = .php .html .css .js
■nginxのインストール
cd /usr/local/src
wget http://nginx.org/download/nginx-1.2.6.tar.gz
cd /usr/local/src/nginx-1.2.6
./configure \
--sbin-path=/usr/local/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--with-http_ssl_module \
--with-http_stub_status_module \
--without-http_upstream_ip_hash_module \
--without-http_gzip_module \
--without-http_autoindex_module \
--without-http_geo_module \
--without-http_map_module \
--without-http_split_clients_module \
--without-http_referer_module \
--without-http_proxy_module \
--without-http_uwsgi_module \
--without-http_limit_req_module \
--without-http_browser_module \
--with-pcre=../pcre-8.32
make
make install
■ファイルコピーなど
mv /usr/local/nginx-1.2.6 /usr/local/nginx
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
sudo mkdir -p /var/log/nginx
■ここでCakePHPで動かしたいのでダウンロードと設置
cd /usr/local/src
sudo wget https://github.com/cakephp/cakephp/archive/2.2.7.tar.gz
sudo tar zxvf 2.2.7.tar.gz
mv cakephp-2.2.7 cake
mv cake /usr/local/nginx/
※cakePHPは設置するだけで動きますchmod -R 777 /usr/local/nginx/app/tmp
※app/tmpを(たぶんWebサーバの所有者が)読み書きパーミッションを変えてあげる必要がるようです。とりあえず面倒なので777で
ついでにどうせ後から言われるので、「Security.salt」 と 「chiper string」を適当に1文字ぐらい変更しておきます。
vi /usr/local/nginx/cake/app/Config/core.php
CakePHPの配置場所は「/usr/local/nginx/cake」です。
■これ前提で最後にnginx.confの修正
cp -a /usr/local/nginx/nginx.conf /usr/local/nginx/nginx.conf.old
vi /usr/local/nginx/nginx.conf#user nginx; # Macの場合は抜いておく。For AWS用
worker_processes 2;pid /var/run/nginx.pid;
events {
worker_connections 1024;
}http{
include /usr/local/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;sendfile on;
keepalive_timeout 0;
server_tokens off;
# For ELB
set_real_ip_from 10.0.0.0/8;
real_ip_header X-Forwarded-For;server {
listen 80;
server_name localhost;
rewrite_log on;
root /usr/local/nginx/cake/app/webroot; # cakePHPの場合はwebrootをrootに
index index.php index.html index.htm;# Not found this on disk?
# Feed to CakePHP for further processing!
if (!-e $request_filename) {
rewrite ^/(.+)$ /index.php?url=$1 last;
break;
}location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/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/local/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}location = /status {
include /usr/local/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /status;
fastcgi_pass 127.0.0.1:9000;
access_log off;
}location /nginx_status {
stub_status on;
access_log off;
allow all;
deny all;
}# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
}
最後に立ち上げてみます
$ sudo php-fpm
$ sudo nginx
ちなみにnginxのコマンドは
STOP => $ nginx -s stop
RELOAD => $ nginx -s reload
一応プロセスを確認しておきますとこんな感じです
$ ps axu | grep php-fpm
nobody 37727 0.0 0.0 2477312 584 ?? S 11:07AM 0:00.00 php-fpm
nobody 37726 0.0 0.2 2481408 8936 ?? S 11:07AM 0:00.10 php-fpm
root 37725 0.0 0.0 2477312 612 ?? Ss 11:07AM 0:00.00 php-fpm
$ ps axu | grep nginx
nobody 37733 0.0 0.0 2433764 1140 ?? S 11:08AM 0:00.01 nginx: worker process
nobody 37732 0.0 0.0 2433764 1220 ?? S 11:08AM 0:00.01 nginx: worker process
root 37731 0.0 0.0 2433568 316 ?? Ss 11:08AM 0:00.00 nginx: master process nginx
最終的にhttp://localhostにておなじみの画面が出れば成功です。