hogehoge foobar Blog Style Beta

Web,Mac,Linux,JavaScript,Perl,PHP,RegExp,Git,Vim,Redmineなど技術的なことのメモや、ちょっと便利そうなものの紹介をしています。

Homebrewで nginx + PHP 環境を作ってみる

前回、MacPorts から Homebrew へ移行しましたが、せっかくなので、nginx + PHP 環境を作ってみました。 nginx を触ってみたかったので、勉強がてらにインストールしたときのメモ書きです。

nginx のインストール 設定 起動

これは、前回もやったのですが、一応おさらい。

nginx のインストールオプションを確認

インストールする前に、「brew options」でインストールオプションを確認しておきます。

$ brew options nginx
--with-debug
  Compile with support for debug log
--with-passenger
  Compile with support for Phusion Passenger module
--with-webdav
  Compile with support for WebDAV module

nginx をインストール

brew install」で、パッケージ名を指定して nginx をインストールします。 Xcodeのバージョン古いと警告が出ることがあるみたいなので、事前にバージョンアップしておきます。

(私の場合、Xcode4.2 だと警告がでたので、Xcode4.6 にバージョンアップしました。)

$ brew install nginx

パスの設定

Homebrew でインストールしたアプリケーションは、「/usr/local/sbin/」にインストールされるので、パスを通しておきます。

$ which nginx
/usr/local/sbin/nginx

.bashrc などに記述する場合は、 「$(brew --prefix)/sbin」という形で書くと良いです。

export PATH=$(brew --prefix)/sbin:/opt/local/bin:/opt/local/sbin/:$PATH$

設定ファイルの記述

インストールしたら、今度は設定をします。

設定ファイルは「/usr/local/etc/nginx/nginx.conf」です。

 nginx の設定ファイルは json形式ですが、中身は Apache の設定ファイルに似ているので、Apache を触ったことがあればなんとなくわかると思います。

実際の設定ファイルは、こんな感じになると思います。

http {
    include       mime.types;
    default_type  application/octet-stream;

    access_log  /usr/local/var/log/access.log;
    error_log  /usr/local/var/log/error.log warn;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        root   /usr/local/www/html;

        location / {
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

設定ファイルを書いたら、「nginx -t」でシンタックスエラーとかが無いかをチェックします。 「test is successful」が表示されればOKです。

$ nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

nginx の起動

ここまでできたら、nginx を起動してみます。

起動するときは「nginx」でOKです。

$ sudo nginx

nginx の停止

停止するときは「nginx -s stop」です。

$ sudo nginx -s stop

PHP のインストール

次に PHP をインストールします。

Homebrew のリポジトリを追加

Homebrew 標準だと、PHP のパッケージが無いので、リポジトリを追加します。

リポジトリの追加には、「brew tap」を使います。

PHPリポジトリとして「josegonzalez/php」を追加します。

PHP のインストールで、zlib も必要になるので、「homebrew/dupes」も併せて追加しておきます。

$ brew tap josegonzalez/php
$ brew tap homebrew/dupes

インストールオプションを確認

リポジトリの追加が終わったら、PHP のインストールオプションを確認します。

$ brew options php53
--32-bit
  Build 32-bit only.
--homebrew-apxs
  Build against apxs in Homebrew prefix
--with-cgi
  Enable building of the CGI executable (implies --without-apache)
--with-debug
  Compile with debugging symbols
--with-fpm
  Enable building of the fpm SAPI executable (implies --without-apache)
--with-gmp
  Build with gmp support
--with-homebrew-openssl
  Include OpenSSL support via Homebrew
--with-imap
  Include IMAP extension
--with-intl
  Include internationalization support
--with-libmysql
  Include (old-style) libmysql support
--with-mssql
  Include MSSQL-DB support
--with-pgsql
  Include PostgreSQL support
--with-thread-safety
  Build with thread safety
--with-tidy
  Build with tidy support
--with-unixodbc
  Build with unixodbc support
--without-apache
  Build without shared Apache 2.0 Handler module
--without-bz2
  Build without bz2 support
--without-mysql
  Remove MySQL/MariaDB support
--without-pear
  Build without PEAR

PHP をインストール

さっき確認したオプションの中から、必要なものを指定してインストールします。

nginx との連携で PHP-fpm を使用するので、「--with-fpm」を指定します。

$ brew install php53 --with-fpm --with-intl --with-mysql --with-pgsql --with-homebrew-openssl

しばらく時間がかかるので、珈琲でも飲みながら待ちます。

インストールが終わったら、パスを通します。

nginx の時と同じようにして、「「$(brew --prefix)/bin」」を設定します。(/sbin では無く /bin)

export PATH=$(brew --prefix)/bin:$(brew --prefix)/sbin:/opt/local/bin:/opt/local/sbin/:$PATH$

パスを通したら、バージョンを確認しておきます。

$ php -v
PHP 5.3.21 (cli) (built: Feb 11 2013 12:38:44) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies

launchctl の設定・php-fpm の実行

php-fpm 実行させるのに、launchctl を使います。

launchctl については、Mac の launchctl ってなんだ? - IT戦記 がとても参考になりました。

「/Users/goofy/Library/LaunchAgents」以下に「php-fpm.plist」という設定ファイルを作ります。

$ vim /Users/goofy/Library/LaunchAgents/php-fpm.plist 

設定ファイルの中身はこんな感じです。

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version='1'>
  <dict>
    <key>Label</key><string>org.php.php-fpm</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/sbin/php-fpm</string>
      <string>--fpm-config</string>
      <string>/usr/local/etc/php/5.3/php-fpm.conf</string>
    </array>
    <key>Debug</key><false/>
    <key>RunAtLoad</key><true/>
    <key>KeepAlive</key><false/>
    <key>UserName</key><string>mrgoofy</string>
  </dict>
</plist>

設定ファイルを作ったら、「launchctl」で php-fpm を実行させます。

$ launchctl load -w ~/Library/LaunchAgents/php-fpm.plist

「launchctl list」で実行されているプロセスを確認して「php-fpm」があればOKです。

$ launchctl list | grep php-fpm
PID Status  Label
XXXXX - org.php.php-fpm

nginx と PHP を連携

最後に nginx と PHP を連携させます。 nginx.conf に以下のような内容を追記します。

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

追記したら nginx を再起動します。

$ sudo nginx -s stop
$ sudo nginx

再起動後、公開ディレクトリに、phpinfo が見れるファイルを作成します。 ブラウザでアクセスしてページが見えればOKです。

$ vim /usr/local/www/html/info.php
<?php
    phpinfo();
?>

→ http://localhost/info.php でページが見えればOK

今回の参考サイト