FLYING

/* TODO: 気の利いた説明を書く */

VPSを導入してやったこと(PHP,MySQLインストール編)

VPSを導入してやったこと(Rubyインストール編)の続きです。

デフォルトのリポジトリに入っているPHPMySQLのバージョンが少々古いので,最新のバージョンをインストールするために,yumレポジトリにremiとepelを追加します*1

$ sudo wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
$ sudo wget http://rpms.famillecollet.com/el5.i386/remi-release-5-7.el5.remi.noarch.rpm
$ sudo rpm -Uvh remi-release-5-7.el5.remi.noarch.rpm epel-release-5-4.noarch.rpm
$ rm remi-release-5-7.el5.remi.noarch.rpm epel-release-5-4.noarch.rpm

remiのリポジトリを有効にするため,設定ファイルを次のように編集します。

$ sudo vi /etc/yum.repos.d/remi.repo
[remi]
name=Les RPM de remi pour Enterprise Linux 5 - $basearch
baseurl=http://rpms.famillecollet.com/enterprise/5/remi/$basearch/
        http://iut-info.univ-reims.fr/remirpms/enterprise/5/remi/$basearch/
# enabled: 0 => 1
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
failovermethod=priority

PHPをインストールします。オプションにはひとまず必要そうなものを指定してみました。後から必要なオプションをyumでインストールすることもできると思います。

$ sudo yum install php-devel php-mbstring php-mysql php-gd php-mcrypt php-pdo php-xml
$ php -v
PHP 5.3.5 (cli) (built: Jan 22 2011 10:11:01) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

PHPの設定ファイルを編集します。

$ sudo vi /etc/php.ini

次のように設定を変更しました。最近のバージョンだと「magic_quotes_gpc」や「short_open_tag」などの項目はデフォルトでオフになっているので,古いスクリプトを実行する場合は明示的にオンにした方がいいかもしれません(セキュリティ的にはなるべくオフにするべき)。

; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-log
; Example:
;error_log = php_errors.log
; Log errors to syslog (Event Log on NT, not valid in Windows 95).
;error_log = syslog
error_log = "/var/log/php_errors.log"

; Maximum size of POST data that PHP will accept.
; http://www.php.net/manual/en/ini.core.php#ini.post-max-size
; post_max_size = 8M
post_max_size = 16M

; Maximum allowed size for uploaded files.
; http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize
; upload_max_filesize = 2M
upload_max_filesize = 16M

[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
;date.timezone =
date.timezone = "Asia/Tokyo"

httpdを再起動して,動作確認用のスクリプトを作成します。PHPスクリプトはドキュメントルート以下のディレクトリに配置してください。

$ sudo /etc/init.d/httpd restart
$ vi ~/public_html/test.php

スクリプトの内容はなんでもいいですが,次のようにしておくといいと思います。

<?php
	phpinfo();
?>

ブラウザからスクリプトにアクセスして,設定情報が表示されることを確認します。うまく表示されたらPHPのインストールは完了です。

続いて,MySQLをインストールします。

$ sudo yum install mysql-server
$ mysql -V
mysql  Ver 14.14 Distrib 5.1.54, for redhat-linux-gnu (i686) using readline 5.1

MySQLの設定ファイルを編集します。

$ sudo vi /etc/my.cnf

文字コード関連の設定を追記しました。ついでに,メモリ節約のためにInno DBを無効にしてみました。

[mysql]
# Specify default character set
default-character-set=utf8

[mysqld]
# Specify default character set
character-set-server=utf8
# Skip inno db (for saving memory)
skip-innodb

MySQL自動起動を有効にして,MySQLを起動します。

$ sudo /sbin/chkconfig mysqld on
$ sudo /etc/init.d/mysqld start

MySQLのユーザーを編集します。

$ mysql -u root

不要なユーザーを削除して,rootユーザーにパスワードを設定します。

mysql> select User,Host,Password from mysql.user;
+------+------------+----------+
| User | Host       | Password |
+------+------------+----------+
| root | localhost  |          |
| root | tondol-vps |          |
| root | 127.0.0.1  |          |
|      | localhost  |          |
|      | tondol-vps |          |
+------+------------+----------+
5 rows in set (0.00 sec)

mysql> delete from mysql.user where User='';
Query OK, 2 rows affected (0.21 sec)

mysql> update mysql.user set Password=password('YOUR_PASSWORD') where User='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select User,Host,Password from mysql.user;
+------+------------+-------------------------------------------+
| User | Host       | Password                                  |
+------+------------+-------------------------------------------+
| root | localhost  | ***************************************** |
| root | tondol-vps | ***************************************** |
| root | 127.0.0.1  | ***************************************** |
+------+------------+-------------------------------------------+
3 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

続いて,データベースの管理用にphpMyAdminを導入します。ソースをsourceforgeからダウンロードしてきて,ドキュメントルート以下のディレクトリに展開します。

$ cd ~/public_html/
$ wget http://sourceforge.net/projects/phpmyadmin/files%2FphpMyAdmin%2F3.3.9%2FphpMyAdmin-3.3.9-all-languages.tar.gz/download
$ tar -xvf phpMyAdmin-3.3.9-all-languages.tar.gz
$ mv phpMyAdmin-3.3.9-all-languages/ phpmyadmin
$ rm phpMyAdmin-3.3.9-all-languages.tar.gz

設定ファイルを作成します。

$ cd phpmyadmin
$ cp config.sample.inc.php  config.inc.php
$ vi config.inc.php

設定ファイルのパスフレーズ部分を埋めます。

/*
 * This is needed for cookie based authentication to encrypt password in
 * cookie
 */
$cfg['blowfish_secret'] = 'YOUR_PASSWORD'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

この段階でphpMyAdminを設置したディレクトリにブラウザからアクセスして,phpMyAdminのログイン画面が表示されることをチェックしてみてください。

このままでは誰でもphpMyAdminにアクセスできてしまうので,安全のためBasic認証を設定しましょう。.htaccessファイルの設定を有効にするため,先にApacheの設定ファイルを編集します。

$ sudo vi /etc/httpd/conf/httpd.conf

「AllowOverride None」となっている箇所を「AllowOverride All」に変更します。このように編集することで,.htaccessファイルに記述した設定がすべて有効になります*2

#
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
# features.
#
<Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/home/tondol/public_html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#    Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride All

#
# Controls who can get stuff from this server.
#
    Order allow,deny
    Allow from all

</Directory>

Apacheを再起動します。

$ sudo /etc/init.d/httpd restart

Apacheの設定が終わったので,.htaccessファイルを作成します。

$ vi .htaccess

.htaccessファイルの内容は次のようにすればいいでしょう。

AuthUserFile /home/tondol/.htpasswd
AuthGroupFile /dev/null
AuthName "Only administrators are allowed to access this directory"
AuthType Basic
require valid-user

.htpasswdファイルを作成します。

cd ~
htpasswd -c .htpasswd YOUR_ID
New password: YOUR_PASSWORD
Re-type new password: YOUR_PASSWORD

Basic認証が動作することを確認できたら,phpMyAdminの導入は完了です!

*1:wgetコマンドで404エラーが出たときは,rpmパッケージの置いてあるディレクトリをブラウザで閲覧してみて,一覧からそれっぽいパッケージを指定してやり直してみてください

*2:.htaccessファイルではOverrideさせたくない項目がある場合は,項目を絞ってOverrideを許可することもできます。詳しくはApacheのマニュアルを参照してね