LAMP 環境を作る

ノン・プログラマのためのPHP入門 10日間コースを買ったので、この本に紹介されてるとおりに LAMP 環境を作ってみる。

MySQL のインストール。

まずは、ダウンロード。

% wget http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.51a.tar.gz/from/http://ftp.iij.ad.jp/pub/db/mysql/


コンフィギャー。

% tar xzvf mysql-5.0.51a.tar.gz

% cd mysql-5.0.51a

% ./configure --prefix=/usr/local/mysql \
--with-charset=utf8 \
--with-extra-charsets=all \
--with-mysqld-user=sato

なんか怒られた。

checking for termcap functions library... configure: error: No curses/termcap library found

調べてみる。

% yum list | grep curses | less

ncurses.i386                             5.6-9.20070812.fc7     installed
ncurses-debuginfo.i386                   5.6-6.20070303.fc7     fedora-debuginfo
ncurses-devel.i386                       5.6-9.20070812.fc7     core
ncurses-static.i386                      5.6-9.20070812.fc7     updates
php-ncurses.i386                         5.2.4-1.fc7            updates

たぶん、ncurses-devel を入れればよいんじゃないかということで入れてみる。

% sudo yum install ncurses-devel


リトライ。

% ./configure --prefix=/usr/local/mysql \
--with-charset=utf8 \
--with-extra-charsets=all \
--with-mysqld-user=sato

...
MySQL has a Web site at http://www.mysql.com/ which carries details on the
latest release, upcoming features, and other information to make your
work or play with MySQL more productive. There you can also find
information about mailing lists for MySQL discussion.

Remember to check the platform specific part of the reference manual for
hints about installing MySQL on your platform. Also have a look at the
files in the Docs directory.

Thank you for choosing MySQL!

よしOK。そして make; make install して一服しながら待つ。

% make
% sudo make install


MySQL 上での rootユーザを作る

% sudo ./scripts/mysql_install_db


オーナーを root に、データディレクトリの所有権をユーザに変更する

% chown -R root /usr/local/mysql
% chown -R sato /usr/local/mysql/var
% chgrp -R sato /usr/local/mysql


MySQL を起動する

% /usr/local/mysql/bin/mysqld_safe &


MySQL が動作しているかを確認する

% sudo /usr/local/mysql/bin/mysqladmin -u root ping
mysqld is alive


root のパスワードを設定する

% /usr/local/mysql/bin/mysql -u root
mysql> SET PASSWORD FOR root@localhost=PASSWORD('hogehoge');
mysql> exit;


パスワードの付加されていないユーザを全て削除する

% /usr/local/mysql/bin/mysql -u root -p
Enter password:
mysql> USE mysql;
mysql> DELETE FROM user WHERE password='';
mysql> FLUSH PRIVILEGES;
mysql> exit;


設定ファイルを /etc/my.cnf にコピー

% sudo cp support-files/my-medium.cnf /etc/my.cnf


MySQL を停止する

% /usr/local/mysql/bin/mysqladmin shutdown

これで停止できなかったら、以下を試す。(参考:Bugzilla - ソースコードからのインストール

# /usr/local/mysql/bin/mysqladmin -u root -p shutdown

Apache のインストール

Apache にモジュール組み込む際に、Apache の DSO(Dynamic Shared Object)っていう機能が必要らしいけど、yum で入れたため、入ってるか不明。(参考:動的共有オブジェクト (DSO) サポート - Apache HTTP サーバ
(既に Apache 入れてある人で、DSO 機能を付加してインストールしてあるかどうか)確かめるには以下のようにやればよいみたい。(自分は、確かめる前に sudo yum remove httpd で消してしまった…)

apxs - APache eXtenSion tool - Apache HTTP Server
$ httpd -l


まずは、ダウンロード。

% wget http://ftp.kddilabs.jp/infosystems/apache/httpd/httpd-2.2.8.tar.gz


解凍する。

% tar xzvf httpd-2.2.8.tar.gz


DSO を使用するオプションを指定してコンフィギャー。

% cd httpd-2.2.8/
% ./configure --enable-so


make; make install する。

make
sudo make install


Apache を起動させる。

% sudo /usr/local/apache2/bin/apachectl start
  httpd: apr_sockaddr_info_get() failed for server_name
  httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

なんかエラー出たので、ググって調べてみたら、「/etc/hosts」に自分の名前(ホスト名)を入れれば OK だった。(参考:Apache の httpd: apr_sockaddr_info_get() failed for エラー


/etc/host ファイルを編集(/etc/hosts ファイルとは?

## もともとの設定
# 127.0.0.1      localhost.localdomain   localhost

## エイリアスに colinux(自分のホスト名) を追加 
127.0.0.1      localhost.localdomain   localhost        colinux


再度トライ。

% sudo /usr/local/apache2/bin/apachectl restart
Password:

うん、OK。


http://colinux/」にアクセスして、It works! と表示されれば Apache は正常に動作している。

PHP のインストール

まずは、ダウンロード。

% wget http://jp.php.net/get/php-5.2.5.tar.gz/from/this/mirror


コンフィギャーの前に、libgd(図形描画ライブラリ)libpng(PNG画像作成用ライブラリ), libjpeg(JPEG画像作成用ライブラリ), zlib(汎用圧縮ライブラリ)が必要らしいので、入ってるか確かめてみる。

% yum list > yum.list
% cat yum.list | grep libgd
libgda.i386                              1:1.9.100-12.fc7       installed
libgda-devel.i386                        1:1.9.100-12.fc7       installed
compat-libgda.i386                       1.2.4-1.fc7            fedora
compat-libgda-debuginfo.i386             1.2.4-1.fc7            fedora-debuginfo
compat-libgda-devel.i386                 1.2.4-1.fc7            fedora
libgda-debuginfo.i386                    1:1.9.100-12.fc7       fedora-debuginfo
libgda-ldap.i386                         1:1.9.100-12.fc7       fedora
libgda-mysql.i386                        1:1.9.100-12.fc7       fedora
libgda-odbc.i386                         1:1.9.100-12.fc7       fedora
libgda-postgres.i386                     1:1.9.100-12.fc7       fedora
libgda-sharp.i386                        1:1.9.100-12.fc7       fedora
libgda-sharp-devel.i386                  1:1.9.100-12.fc7       fedora
libgda-sqlite.i386                       1:1.9.100-12.fc7       fedora
libgdamm.i386                            1.3.7-4.fc6            fedora
libgdamm-debuginfo.i386                  1.3.7-4.fc6            fedora-debuginfo
libgdamm-devel.i386                      1.3.7-4.fc6            fedora
libgdiplus.i386                          1.2.3-1.fc7            fedora
libgdiplus-debuginfo.i386                1.2.3-1.fc7            fedora-debuginfo
libgdiplus-devel.i386                    1.2.3-1.fc7            fedora
libgdl.i386                              0.7.8-2.fc7            updates
libgdl-devel.i386                        0.7.8-2.fc7            updates

% cat yum.list | grep libpng | less
libpng.i386                              2:1.2.22-1.fc7         installed
libpng-devel.i386                        2:1.2.22-1.fc7         installed
libpng-debuginfo.i386                    2:1.2.16-1.fc7         fedora-debuginfo
libpng-static.i386                       2:1.2.22-1.fc7         updates
libpng10.i386                            1.0.29-1.fc7           updates
libpng10-debuginfo.i386                  1.0.21-2.fc7           fedora-debuginfo
libpng10-devel.i386                      1.0.29-1.fc7           updates

% cat yum.list | grep libjpeg | less
libjpeg.i386                             6b-38.fc7              installed
libjpeg-devel.i386                       6b-38.fc7              installed
libjpeg-debuginfo.i386                   6b-37                  fedora-debuginfo
libjpeg-static.i386                      6b-38.fc7              updates

% cat yum.list | grep zlib | less
zlib.i386                                1.2.3-10.fc7           installed
zlib-devel.i386                          1.2.3-10.fc7           installed
arm-gp2x-linux-zlib.noarch               1.2.3-6.fc7            updates
jzlib.i386                               1.0.7-4jpp.1           fedora
jzlib-debuginfo.i386                     1.0.7-4jpp.1           fedora-debuginfo
jzlib-demo.i386                          1.0.7-4jpp.1           fedora
jzlib-javadoc.i386                       1.0.7-4jpp.1           fedora
zlib-debuginfo.i386                      1.2.3-10.fc7           fedora-debuginfo

うん、恐らく全部入ってるみたいだ。


ライブラリの具体的な場所が分からないので、調べてみる。

% sudo find / -name '*libgd.so*'
/usr/lib/libgd.so.2
/usr/lib/libgd.so
/usr/lib/libgd.so.2.0.0

% sudo find / -name '*libpng.so*'
/usr/lib/libpng.so.3.22.0
/usr/lib/libpng.so.3
/usr/lib/libpng.so


% sudo find / -name '*libjpeg.so*'
/usr/lib/libjpeg.so.62.0.0
/usr/lib/libjpeg.so
/usr/lib/libjpeg.so.62

% sudo find / -name '*zlib.so*'
/usr/lib/ruby/1.8/i386-linux/zlib.so
% sudo cp /usr/lib/ruby/1.8/i386-linux/zlib.so /usr/lib

zlib.so だけなんか変な場所にあったので、/usr/lib に無理やりコピーしておいた。これで全ての必要なライブラリが /usr/lib に入っていることになる。


先ほど調べたライブラリの入ってるフォルダなどを指定してコンフィギャー。

% ./configure --with-apxs2=/usr/local/apache2/bin/apxs \
--enable-mbstring \
--with-mysql=/usr/local/mysql \
--with-gd \
--with-jpeg-dir=/usr/lib \
--with-zlib \
--with-zlib-dir=/usr/lib \
--with-png-dir=/usr/lib


make; make install を実行。

make
sudo make install

これで PHP のインストールは完了。


最後に、/usr/local/apache2/conf/httpd.conf の内容の確認・変更を行う。

//53行目付近を確認
#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
LoadModule php5_module        modules/libphp5.so
#

//165行目付近の内容を変更(index.phpを追加)
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

//328行目付近に追加(AddHandler と AddType)
    #
    # AddHandler allows you to map certain file extensions to "handlers":
    # actions unrelated to filetype. These can be either built into the server
    # or added with the Action directive (see below)
    #
    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    #
    #AddHandler cgi-script .cgi

    # For type maps (negotiated resources):
    #AddHandler type-map var

    #
    # Filters allow you to process content before it is sent to the client.
    #
    # To parse .shtml files for server-side includes (SSI):
    # (You will also need to add "Includes" to the "Options" directive.)
    #
    #AddType text/html .shtml
    #AddOutputFilter INCLUDES .shtml
    AddHandler php5-script .php
    AddType application/x-httpd-php .php
</IfModule>


Apache を再起動する。

% sudo /usr/local/apache2/bin/apachectl restart


以下の内容のファイル info.php をドキュメントルート「/usr/local/apache2/htdocs/」に置いてブラウザからアクセスする(http://colinux/info.php)。

<?php
phpinfo();
?>


以下のようなページが表示されればうまく動いている。


はぁー、疲れた。半日使ってしまったよ…。