Mac Air Lion にPHPの開発環境を構築する

MacではPHP開発環境を構築する方法は2つあります。

1つ目は、MAMPをインストールする方法で、
2つ目は、MacPortsからPHPapacheMySQLをインストールする方法です。

今回は2つ目の方法で環境を構築しました。

※あとで知ったことですが、MacPorts以外にも Fink とか homebrew というパッケージ管理ツールもあるみたいです。
Fink は調べてないのでわからないですが、homebrew は Ruby で実装されてたり MacPorts よりも早いという情報があったのでいつか試してみたいです。

MacPortsインストール

OSはLionなので「MacPorts-2.0.1-10.7-Lion.dmg」をダウンロードして、インストールを実行します。

ターミナルを開いて、インストールされているか確認。

$ port version
Version: 2.0.1

以下のコマンドで最新の状態にできます。
それぞれダウンロードと更新を行っているようです。

$ sudo port selfupdate 
$ sudo port sync

PHPPearApacheインストール

(間違えて-vオプションをつけてしまったので結果は残せず…)
ちなみに、Pearのインストールは今後peclでmongoをインストールする可能性があるので、入れてあります。普通は必要ないかな。

$ sudo port install php5 +apache2 +pear

yumみたいに勝手に php が有効にならないんですね。
その辺も含め下記のように修正しました。(diffで表記)

$ sudo vi /opt/local/apache2/conf/httpd.conf

117d116
< LoadModule php5_module modules/libphp5.so
208c207
<     Options ExecCGI FollowSymLinks
---
>     Options Indexes FollowSymLinks
215c214
<     AllowOverride All
---
>     AllowOverride None
230c229
<     DirectoryIndex index.html index.php
---
>     DirectoryIndex index.html
473,474d471
< Include /opt/local/apache2/conf/extra/mod_php.conf

MySQLインストール

$ sudo port install mysql5-devel mysql5-server-devel
Password:
--->  Computing dependencies for mysql5-devel
--->  Fetching archive for mysql5-devel
--->  Attempting to fetch mysql5-devel-5.5.2-m2_1.darwin_11.x86_64.tbz2 from http://packages.macports.org/mysql5-devel
--->  Fetching mysql5-devel
--->  Attempting to fetch mysql-5.5.2-m2.tar.gz from http://mirror.facebook.net/mysql/Downloads/MySQL-5.5/
--->  Attempting to fetch mysql-5.5.2-m2.tar.gz from http://distfiles.macports.org/mysql5
--->  Verifying checksum(s) for mysql5-devel
--->  Extracting mysql5-devel
--->  Applying patches to mysql5-devel
--->  Configuring mysql5-devel
--->  Building mysql5-devel
--->  Staging mysql5-devel into destroot
--->  Installing mysql5-devel @5.5.2-m2_1
The MySQL client has been installed.
If you also want a MySQL server, install the mysql5-server-devel port.
--->  Activating mysql5-devel @5.5.2-m2_1
--->  Cleaning mysql5-devel
--->  Computing dependencies for mysql5-server-devel
--->  Fetching archive for mysql5-server-devel
--->  Attempting to fetch mysql5-server-devel-5.5.2-m2_1.darwin_11.noarch.tbz2 from http://packages.macports.org/mysql5-server-devel
--->  Fetching mysql5-server-devel
--->  Verifying checksum(s) for mysql5-server-devel
--->  Extracting mysql5-server-devel
--->  Configuring mysql5-server-devel
--->  Building mysql5-server-devel
--->  Staging mysql5-server-devel into destroot
--->  Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting mysql5-server-devel with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo port load mysql5-server-devel
###########################################################
--->  Installing mysql5-server-devel @5.5.2-m2_1
******************************************************
* In order to setup the database, you might want to run
* sudo -u _mysql mysql_install_db5
* if this is a new install
******************************************************
--->  Activating mysql5-server-devel @5.5.2-m2_1
--->  Cleaning mysql5-server-devel
$ sudo port install php5-mysql
--->  Computing dependencies for php5-mysql
--->  Fetching archive for php5-mysql
--->  Attempting to fetch php5-mysql-5.3.6_0+mysqlnd.darwin_11.x86_64.tbz2 from http://packages.macports.org/php5-mysql
--->  Fetching php5-mysql
--->  Verifying checksum(s) for php5-mysql
--->  Extracting php5-mysql
--->  Configuring php5-mysql
--->  Building php5-mysql
--->  Staging php5-mysql into destroot
--->  Installing php5-mysql @5.3.6_0+mysqlnd
To use mysqlnd with a local MySQL server, edit /opt/local/etc/php5/php.ini and set
mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket
to /opt/local/var/run/mysql5/mysqld.sock
--->  Activating php5-mysql @5.3.6_0+mysqlnd
--->  Cleaning php5-mysql
MySQL初期化
$ sudo -u _mysql msql_install_db5
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
Installing MySQL system tables...
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
OK
Filling help tables...
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
job-working-directory: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h iwamoto-no-MacBook-Air.local password 'new-password'

Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /opt/local/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!

The latest information about MySQL is available at http://www.mysql.com/
Support MySQL by buying support/licenses from http://shop.mysql.com/
php.iniにsocketを指定

php.ini の mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket
へ/opt/local/var/run/mysql5/mysqld.sockを追記する。

最初にphp.iniがまだないので、用意されているサンプルをコピーする。
developmentとproductionがあり今回は開発用途なのでdevelopmentをコピーしました。

$ sudo cp /opt/local/etc/php5/php.ini-development /opt/local/etc/php5/php.ini

$ sudo vi /opt/local/etc/php5/php.ini
mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
mysqli.default_socket = /opt/local/var/run/mysql5/mysqld.sock
pdo_mysql.default_socket = /opt/local/var/run/mysql5/mysqld.sock
MySQLの root パスワードを設定
sudo /opt/local/lib/mysql5/bin/mysqladmin -u root password 'パスワード'

phpMyAdminインストール

$ sudo port install phpmyadmin
Password:
--->  Computing dependencies for phpmyadmin
--->  Dependencies to be installed: php5-gd freetype jpeg libpng php5-mbstring php5-mcrypt libmcrypt php5-zip
--->  Fetching archive for freetype
--->  Attempting to fetch freetype-2.4.5_1.darwin_11.x86_64.tbz2 from http://packages.macports.org/freetype
--->  Fetching freetype
--->  Attempting to fetch freetype-2.4.5.tar.bz2 from http://jaist.dl.sourceforge.net/freetype
--->  Attempting to fetch freetype-doc-2.4.5.tar.bz2 from http://jaist.dl.sourceforge.net/freetype
--->  Verifying checksum(s) for freetype
--->  Extracting freetype
--->  Applying patches to freetype
--->  Configuring freetype
--->  Building freetype
--->  Staging freetype into destroot
--->  Installing freetype @2.4.5_1
--->  Activating freetype @2.4.5_1
--->  Cleaning freetype
--->  Fetching archive for jpeg
--->  Attempting to fetch jpeg-8c_0.darwin_11.x86_64.tbz2 from http://packages.macports.org/jpeg
--->  Fetching jpeg
--->  Attempting to fetch jpegsrc.v8c.tar.gz from http://distfiles.macports.org/jpeg
--->  Verifying checksum(s) for jpeg
--->  Extracting jpeg
--->  Configuring jpeg
--->  Building jpeg
--->  Staging jpeg into destroot
--->  Installing jpeg @8c_0
--->  Activating jpeg @8c_0
--->  Cleaning jpeg
--->  Fetching archive for libpng
--->  Attempting to fetch libpng-1.4.8_0.darwin_11.x86_64.tbz2 from http://packages.macports.org/libpng
--->  Fetching libpng
--->  Attempting to fetch libpng-1.4.8.tar.bz2 from http://jaist.dl.sourceforge.net/project/libpng/libpng14/1.4.8
--->  Verifying checksum(s) for libpng
--->  Extracting libpng
--->  Configuring libpng
--->  Building libpng
--->  Staging libpng into destroot
--->  Installing libpng @1.4.8_0
--->  Activating libpng @1.4.8_0
--->  Cleaning libpng
--->  Fetching archive for php5-gd
--->  Attempting to fetch php5-gd-5.3.6_0.darwin_11.x86_64.tbz2 from http://packages.macports.org/php5-gd
--->  Fetching php5-gd
--->  Verifying checksum(s) for php5-gd
--->  Extracting php5-gd
--->  Configuring php5-gd
--->  Building php5-gd
--->  Staging php5-gd into destroot
--->  Installing php5-gd @5.3.6_0
--->  Activating php5-gd @5.3.6_0
--->  Cleaning php5-gd
--->  Fetching archive for php5-mbstring
--->  Attempting to fetch php5-mbstring-5.3.6_0.darwin_11.x86_64.tbz2 from http://packages.macports.org/php5-mbstring
--->  Fetching php5-mbstring
--->  Verifying checksum(s) for php5-mbstring
--->  Extracting php5-mbstring
--->  Configuring php5-mbstring
--->  Building php5-mbstring
--->  Staging php5-mbstring into destroot
--->  Installing php5-mbstring @5.3.6_0
--->  Activating php5-mbstring @5.3.6_0
--->  Cleaning php5-mbstring
--->  Fetching archive for libmcrypt
--->  Attempting to fetch libmcrypt-2.5.8_1.darwin_11.x86_64.tbz2 from http://packages.macports.org/libmcrypt
--->  Fetching libmcrypt
--->  Attempting to fetch libmcrypt-2.5.8.tar.bz2 from ftp://ftp.jp.FreeBSD.org/pub/FreeBSD/ports/distfiles/
--->  Attempting to fetch libmcrypt-2.5.8.tar.bz2 from ftp://ftp.jp.FreeBSD.org/pub/FreeBSD/ports/local-distfiles/
--->  Attempting to fetch libmcrypt-2.5.8.tar.bz2 from http://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8
--->  Verifying checksum(s) for libmcrypt
--->  Extracting libmcrypt
--->  Configuring libmcrypt
--->  Building libmcrypt
--->  Staging libmcrypt into destroot
--->  Installing libmcrypt @2.5.8_1
--->  Activating libmcrypt @2.5.8_1
--->  Cleaning libmcrypt
--->  Fetching archive for php5-mcrypt
--->  Attempting to fetch php5-mcrypt-5.3.6_0.darwin_11.x86_64.tbz2 from http://packages.macports.org/php5-mcrypt
--->  Fetching php5-mcrypt
--->  Verifying checksum(s) for php5-mcrypt
--->  Extracting php5-mcrypt
--->  Configuring php5-mcrypt
--->  Building php5-mcrypt
--->  Staging php5-mcrypt into destroot
--->  Installing php5-mcrypt @5.3.6_0
--->  Activating php5-mcrypt @5.3.6_0
--->  Cleaning php5-mcrypt
--->  Fetching archive for php5-zip
--->  Attempting to fetch php5-zip-5.3.6_0.darwin_11.x86_64.tbz2 from http://packages.macports.org/php5-zip
--->  Fetching php5-zip
--->  Verifying checksum(s) for php5-zip
--->  Extracting php5-zip
--->  Configuring php5-zip
--->  Building php5-zip
--->  Staging php5-zip into destroot
--->  Installing php5-zip @5.3.6_0
--->  Activating php5-zip @5.3.6_0
--->  Cleaning php5-zip
--->  Fetching archive for phpmyadmin
--->  Attempting to fetch phpmyadmin-3.4.3.2_0.darwin_11.noarch.tbz2 from http://packages.macports.org/phpmyadmin
--->  Fetching phpmyadmin
--->  Attempting to fetch phpMyAdmin-3.4.3.2-all-languages.tar.bz2 from http://jaist.dl.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.4.3.2
--->  Verifying checksum(s) for phpmyadmin
--->  Extracting phpmyadmin
--->  Configuring phpmyadmin
--->  Building phpmyadmin
--->  Staging phpmyadmin into destroot
--->  Installing phpmyadmin @3.4.3.2_0
--->  Activating phpmyadmin @3.4.3.2_0

phpMyAdmin must be configured before it can be used. Consult the documentation at file:///opt/local/www/phpmyadmin/Documentation.html#config

A sample configuration file is installed at /opt/local/www/phpmyadmin/config.sample.inc.php, but you mustn’t edit this file. The file to edit is the real configuration file at
/opt/local/www/phpmyadmin/config.inc.php.

--->  Cleaning phpmyadmin
httpd.conf の DocumentRoot 修正

apachephpMyAdmin の DocumentRoot がそれぞれ別のものになっているので、
下記のように修正しました。(diffで表記)

sudo vi /opt/local/apache2/conf/httpd.conf
168c167
< DocumentRoot "/opt/local/www"
---
> DocumentRoot "/opt/local/apache2/htdocs"
195c194
< <Directory "/opt/local/www">
---
> <Directory "/opt/local/apache2/htdocs">

その他

Apacheの起動と停止
$ sudo /opt/local/apache2/bin/apachectl start

$ sudo /opt/local/apache2/bin/apachectl stop
MySQLの起動と停止
$ sudo /opt/local/share/mysql5/mysql/mysql.server start
Starting MySQL
. SUCCESS! 

$ sudo /opt/local/share/mysql5/mysql/mysql.server stop
Shutting down MySQL
.110804 02:39:31 mysqld_safe mysqld from pid file /opt/local/var/db/mysql5/MacBook-Air.local.pid ended
 SUCCESS! 

今まで CentOS の yum ばかり使っていたのですが、それと比べるとMacPorts でのインストールは結構手間のかかるかんじでした。

いろいろなところで差があって、それぞれまとめておかないと次にまた同じような苦労しそうですねー。