LAMPサーバ構築
WEBサービスを提供する環境としてLAMPサーバを構築する。
目的
LAMPサーバとは、「Linux」+「Apache」+「MySQL」+「PHP」の頭文字を取った言葉。
WEBサービスを提供する際のプラットホームとして現在最も使用されている環境である。
つまり、LAMP構成を組むことにより主要なWEBアプリを動かすことが可能になる。
今回、WEBアプリを動かすにあたり、プラットホームとしてLAMPサーバを構築する。
構築条件
構築機器
- 仮想サーバ SUN VirtualBox 4.0 で構築
作成環境
OS | CentOS 5.5 64bit版 |
サーバ作成条件
サーバIP | 192.168.0.10/24 |
ホスト名 | hogehogeServer |
user | hogehoge |
userpasswd | hogehogepass |
mysqldb名 | アプリ名 |
mysqluser名 | アプリ名user |
mysqluserpasswd | アプリ名pass |
手順記入ルール
- コマンド行頭記号で 「$」はユーザ権限 「#」はスーパーユーザー権限 を表示
- コマンドが長くなる場合は、行末に「\」を記述し複数行に記述。
前準備
- CentOSインストール 実施
- yumリポジトリ設定 実施
- yumリポジトリ設定(独自) 実施 ★ネット接続不可環境時
LAMPインストール
(1)インストール
【Telnet】
- rootでログイン
$ su パスワード:
- 確認
# yum list install httpd php mysql-server
httpd.x86_64 2.2.3-43.el5.centos installed Available Packages mysql-server.x86_64 5.0.77-4.el5_4.2 c5-media php.x86_64 5.1.6-27.el5 c5-media
- インストール
# yum install httpd php mysql-server
:
Complete!
追加モジュールインストール
(1)インストール
【Telnet】
- 確認
# yum list install mod_auth_mysql php-mysql php-mbstring php-gd perl-Net-SSLeay expect
Available Packages expect.i386 5.43.0-5.1 c5-media expect.x86_64 5.43.0-5.1 c5-media mod_auth_mysql.x86_64 1:3.0.0-3.2.el5_3 c5-media perl-Net-SSLeay.x86_64 1.30-4.fc6 c5-media php-gd.x86_64 5.1.6-27.el5 c5-media php-mbstring.x86_64 5.1.6-27.el5 c5-media php-mysql.x86_64 5.1.6-27.el5 c5-media
- インストール
# yum install mod_auth_mysql php-mysql php-mbstring php-gd perl-Net-SSLeay expect
:
Complete!
yumに暗号化モジュールを登録
★サーバ環境にあわせて以下rpmのyumリポジトリ登録を実施
- http://ftp.iij.ad.jp/pub/linux/centos/5.5/extras/x86_64/RPMS/php-mcrypt-5.1.6-15.el5.centos.1.x86_64.rpm
- http://ftp.iij.ad.jp/pub/linux/centos/5.5/extras/x86_64/RPMS/libmcrypt-2.5.8-4.el5.centos.x86_64.rpm
環境1:インターネットにアクセス可能
★以下サイト内の最新版リポジトリデータを確認。rpm入手はyumで実施
http://packages.sw.be/rpmforge-release/
- 確認
# yum list all
Loaded plugins: fastestmirror Determining fastest mirrors * addons: ftp.iij.ad.jp ←「addons」 があることを確認 * base: ftp.iij.ad.jp ←「base」 があることを確認 * extras: ftp.iij.ad.jp ←「extras」 があることを確認 * updates: ftp.iij.ad.jp ←「updates」があることを確認
:
環境2:インタネットへのアクセス不可
★手順 yumリポジトリ設定(独自)#rpm にて独自リポジトリにrpmを登録
[今回実施手順]
- ユーザエリア「/home/hogehoge」にrpmファイルをアップ
- ファイルコピー
# cp libmcrypt-* /home/yum/rpm/
# cp php-mcrypt-* /home/yum/rpm/
- リポジトリ登録
# createrepo -v /home/yum/rpm/
環境3:イントラ内にリポジトリサーバ有り
★手順 yumリポジトリサーバ#server にてリポジトリサーバにrpmを登録
[今回実施手順(yumリポジトリサーバ側)]
- ユーザエリア「/home/hogehoge」にrpmファイルをアップ
- ファイルコピー
# cp libmcrypt-* /var/www/html/yum/rpm/
# cp php-mcrypt-* /var/www/html/yum/rpm/
- リポジトリ登録
# createrepo -v /var/www/html/yum/rpm/
インストール
【Telnet】
- yumキャッシュクリア
# yum clean all
- 確認
# yum list install php-mcrypt
php-mcrypt.x86_64 5.1.6-15.el5.centos.1 local-rpm
- インストール
# yum install php-mcrypt
:
Complete!
Apache設定
(1)httpd.conf編集
- ホスト名確認
# cat /etc/hosts
127.0.0.1 hogehogeServer localhost.localdomain localhost
※登録サーバを確認 「localhost.localdomain localhost」以外
→上記例の場合は 「 hogehogeServer」
- ホスト名登録
# vi /etc/httpd/conf/httpd.conf
- ホスト名追加
#ServerName www.example.com:80 ServerName hogehogeServer:80 ←追加
MySQL設定
(1)my.cnf編集
【telnet】
- my.cnfの編集
# vi /etc/my.cnf
- 以下6行を最終行に追加
[mysqld] default-character-set=utf8 character_set_server=utf8 skip-character-set-client-handshake [mysql] default-character-set=utf8
- MYSQL再起動
# /etc/rc.d/init.d/mysqld start
MySQL を起動中: [ OK ] #
(2)Mysql設定確認及びパスワード設定
- MySQLにrootでログイン
# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
- 文字セット確認
mysql> show variables like 'char%';
- 文字セットがutf8を確認
+--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+
- ユーザ、ホスト、パスワード確認
mysql> select user,host,password from mysql.user;
- パスワード未設定を確認
+------+----------------+----------+ | user | host | password | +------+----------------+----------+ | root | localhost | | | root | hogehogeServer | | | root | 127.0.0.1 | | | | localhost | | | | hogehogeServer | | +------+----------------+----------+ 5 rows in set (0.00 sec)
- localhostパスワード設定
mysql> set password for root@localhost = password('mysqlpass');
Query OK, 0 rows affected (0.00 sec)
- hogehogeServerパスワード設定
mysql> set password for root@hogehogeServer = password('mysqlpass');
Query OK, 0 rows affected (0.00 sec)
- 127.0.0.1パスワード設定
mysql> set password for root@127.0.0.1 = password('mysqlpass');
Query OK, 0 rows affected (0.00 sec)
- 再度ユーザ、ホスト、パスワード確認
mysql> select user,host,password from mysql.user;
- パスワードが設定されていることを確認
+------+----------------+------------------+ | user | host | password | +------+----------------+------------------+ | root | localhost | 6cbbdf9b35eb7db1 | | root | hogehogeServer | 6cbbdf9b35eb7db1 | | root | 127.0.0.1 | 6cbbdf9b35eb7db1 | | | localhost | | | | hogehogeServer | | +------+----------------+------------------+ 5 rows in set (0.00 sec)
- MySQLログオフ
mysql> exit
Bye #
(3)動作確認
- パスワード無しでログインできないことを確認
# mysql -u root -h localhost
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- パスワード無しでログインできないことを確認
# mysql -u root -h hogehogeServer
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- rootでログインできるか確認
# mysql -u root -h localhost -pmysqlpass
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
- rootでログインできるか確認
# mysql -u root -h hogehogeServer -pmysqlpass
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
- MySQLログオフ
mysql> exit
Bye #
起動
(1)Apache起動
- 再ログイン
# su -
- Apache起動
# /etc/rc.d/init.d/httpd start
httpd を起動中: [ OK ]
- Apache自動起動設定
# chkconfig httpd on
- 確認
# chkconfig --list httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
(2)MySQL起動
- MySQL起動
# /etc/rc.d/init.d/mysqld restart
MySQL を停止中: [ OK ] MySQL を起動中: [ OK ]
- MySQL自動起動設定
# chkconfig mysqld on
- 確認
# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
確認
(1)WEB動作確認
【WEB】
http://192.168.0.10/ →初期画面が起動
(2)動作試験データ作成
- web動作試験データ作成
# vi /var/www/html/index.html
- 試験ページ表示データ全面追加
<html> <head> <title>テストページ</title> </head> <body> 初めてのホームページです。 </body> </html>
- php動作試験データ作成
$ vi /var/www/html/info.php
- 試験ページ表示データ全面追加
<?php phpinfo(); ?>
- 動作試験
$ php /var/www/html/info.php
phpinfo() PHP Version => 5.1.6 :
(多量に出力されればOK)
- mysql動作試験データ作成
$ vi /var/www/html/mysql.php
- 試験ページ表示データ全面追加
<?php mysql_connect("localhost","root","mysqlpass") or die("Error MySQL First Test"); echo "Success MySQL First Test"; ?>
- 動作試験
$ php /var/www/html/mysql.php
Success MySQL First Test
(この文字が出力されればOK)
$ ls -l /var/www/html/
-rw-r--r-- 1 root root 128 2月 6 21:37 index.html -rw-r--r-- 1 root root 23 2月 6 21:38 info.php -rw-r--r-- 1 root root 117 2月 6 21:40 mysql.php
(3)動作試験
他のPCから以下URLアクセス
初めてのホームページです。
(PHP設定情報出力)
Success MySQL First Test
★上記のとおり出力されればOK
(4)試験データ削除
# rm /var/www/html/index.html
# rm /var/www/html/info.php
# rm /var/www/html/mysql.php
- 終了
exit
logout
$
遭遇したトラブル
Apache再起動時に「Could not reliably ~」のエラー
問題:
Apache再起動時に「Could not reliably ~」のエラーが出る場合がある
Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
原因:
Apacheの定義ファイルhttp.confへのホスト名登録忘れ。
独自にサーバ名を設定した場合この問題が出やすい
# view /etc/hosts 127.0.0.1 hogehogeServer localhost.localdomain localhost
で登録サーバを確認 「localhost.localdomain localhost」以外
※上記例の場合は 「 hogehogeServer」
# vi /etc/httpd/conf/httpd.conf
#ServerName www.example.com:80 ServerName hogehogeServer:80 ←追加
のようにServerNameに追加しておく
php /var/www/html/mysql.phpでエラー発生
問題:
以下コマンドでエラー発生
# php /var/www/html/mysql.php
PHP Fatal error: Call to undefined function mysql_connect() in /var/www/html/mysql.php on line 2
原因:
mysql起動忘れ
謝辞
テキスト作成に当たり、以下サイトを参考にさせて頂きました。ありがとうございます。
http://centossrv.com/vsftpd.shtml
http://centossrv.com/mysql.shtml
http://blog.cheki.net/archives/349
http://i-am.web777.net/2007/03/mysql_utf8.html