LAMPサーバ構築
WEBサービスを提供する環境としてLAMPサーバを構築する。
目的
LAMPサーバとは、「Linux」+「Apache」+「MySQL」+「PHP」の頭文字を取った言葉。
WEBサービスを提供する際のプラットホームとして現在最も使用されている環境である。
つまり、LAMP構成を組むことにより主要なWEBアプリを動かすことが可能になる。
今回、WEBアプリを動かすにあたり、プラットホームとしてLAMPサーバを構築する。
構築条件
構築機器
- 仮想サーバ SUN VirtualBox 4.3/Hyper-V で構築
作成環境
OS | CentOS 6.5 64bit版 |
サーバ作成条件
サーバIP | 192.168.0.10/24 |
ホスト名 | hogehogeServer |
user | hogehoge |
userpasswd | hogehogepass |
mysqldb名 | mysql |
mysqluser名 | mysqluser |
mysqluserpasswd | mysqlpass |
作業エリア | /home/hogehoge |
手順記入ルール
- コマンド行頭記号で 「$」はユーザ権限 「#」はスーパーユーザー権限 を表示
- コマンドが長くなる場合は、行末に「\」を記述し複数行に記述。
前準備
- CentOSインストール 実施
- yumリポジトリ設定 実施
LAMPインストール
【Telnet】
- rootでログイン
$ su パスワード:
(1)インストール
- 確認
# yum list install httpd php mysql-server
httpd.x86_64 2.2.15-29.el6.centos base mysql-server.x86_64 5.1.73-3.el6_5 updates php.x86_64 5.3.3-27.el6_5 updates
- インストール
# yum install httpd php mysql-server
================================================================================ Package Arch Version Repository Size ================================================================================ Installing: httpd x86_64 2.2.15-29.el6.centos base 821 k mysql-server x86_64 5.1.73-3.el6_5 updates 8.6 M php x86_64 5.3.3-27.el6_5 updates 1.1 M Installing for dependencies: apr x86_64 1.3.9-5.el6_2 base 123 k apr-util x86_64 1.3.9-3.el6_0.1 base 87 k apr-util-ldap x86_64 1.3.9-3.el6_0.1 base 15 k httpd-tools x86_64 2.2.15-29.el6.centos base 73 k mailcap noarch 2.1.31-2.el6 base 27 k mysql x86_64 5.1.73-3.el6_5 updates 894 k perl-DBD-MySQL x86_64 4.013-3.el6 base 134 k perl-DBI x86_64 1.609-4.el6 base 705 k php-cli x86_64 5.3.3-27.el6_5 updates 2.2 M php-common x86_64 5.3.3-27.el6_5 updates 525 k Updating for dependencies: mysql-libs x86_64 5.1.73-3.el6_5 updates 1.2 M
追加モジュールインストール
(1)インストール
【Telnet】
- 確認
# yum list install mod_auth_mysql php-mysql php-mbstring php-gd perl-Net-SSLeay expect
expect.x86_64 5.44.1.15-5.el6_4 base mod_auth_mysql.x86_64 1:3.0.0-11.el6_0.1 base perl-Net-SSLeay.x86_64 1.35-9.el6 base php-gd.x86_64 5.3.3-27.el6_5 updates php-mbstring.x86_64 5.3.3-27.el6_5 updates php-mysql.x86_64 5.3.3-27.el6_5 updates
- インストール
# yum install mod_auth_mysql php-mysql php-mbstring php-gd perl-Net-SSLeay expect
================================================================================ Package Arch Version Repository Size ================================================================================ Installing: expect x86_64 5.44.1.15-5.el6_4 base 256 k mod_auth_mysql x86_64 1:3.0.0-11.el6_0.1 base 24 k perl-Net-SSLeay x86_64 1.35-9.el6 base 173 k php-gd x86_64 5.3.3-27.el6_5 updates 107 k php-mbstring x86_64 5.3.3-27.el6_5 updates 455 k php-mysql x86_64 5.3.3-27.el6_5 updates 81 k Installing for dependencies: libXpm x86_64 3.5.10-2.el6 base 51 k php-pdo x86_64 5.3.3-27.el6_5 updates 75 k tcl x86_64 1:8.5.7-6.el6 base 1.9 M
Apache設定
(1)httpd.conf編集
- ホスト名確認
# vi /etc/hosts
192.168.0.10 hogehogeServer ←追加 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- ホスト名登録
# 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] character_set_server=utf8 skip-character-set-client-handshake [mysql] default-character-set=utf8
- MYSQL起動
# /etc/rc.d/init.d/mysqld start
mysqld を起動中: [ 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.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- 文字セット確認
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/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
- ユーザ、ホスト、パスワード確認
mysql> select host,user,password from mysql.user;
- パスワード未設定を確認
+----------------+------+----------+ | host | user | password | +----------------+------+----------+ | localhost | root | | | hogehogeserver | root | | | 127.0.0.1 | root | | | 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 host,user,password from mysql.user;
- パスワードが設定されていることを確認
+----------------+------+-------------------------------------------+ | host | user | password | +----------------+------+-------------------------------------------+ | localhost | root | *CDC006E5FCB22B379747AF6E75DF97FB503F11B3 | | hogehogeserver | root | *CDC006E5FCB22B379747AF6E75DF97FB503F11B3 | | 127.0.0.1 | root | *CDC006E5FCB22B379747AF6E75DF97FB503F11B3 | | 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'@'hogehogeServer' (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 8 Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MySQLログオフ
mysql> exit Bye #
- rootでログインできるか確認
# mysql -u root -h hogehogeServer -pmysqlpass
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MySQLログオフ
mysql> exit Bye #
起動
(1)Apache起動
- 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
mysqld を停止中: [ OK ] mysqld を起動中: [ OK ]
- MySQL自動起動設定
# chkconfig mysqld on
- 確認
# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
- ログオフ
# exit exit $
確認
(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.3.3 :
(多量に出力されれば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 119 2月 16 22:25 2014 index.html -rw-r--r-- 1 root root 20 2月 16 22:25 2014 info.php -rw-r--r-- 1 root root 122 2月 16 22:27 2014 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 exit $
遭遇したトラブル
''http://192.168.0.10/がタイムアウトでアクセスできない''
問題:
http://192.168.0.10/がタイムアウトでアクセスできない
原因:
ファイアウォール設定解除もれ
「ファイアウォール設定解除手順」にてファイアウォール設定を解除する
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起動忘れ
MYSQL起動時にワーニング発生
問題:
以下コマンドでエラー発生
# /etc/rc.d/init.d/mysqld start
Installing MySQL system tables... 140222 7:29:01 [Warning] '--default-character-set' is deprecated and will be removed in a future release. Please use '--character-set-server' instead.
原因:
/etc/my.cnf の「[mysqld]」に以下の記述があるため
default-character-set=utf8
上記記述は古いmyaqlバージョンの記述方法。新しいバージョンでは以下のように記述を置き換え
character-set-server=utf8
謝辞
テキスト作成に当たり、以下サイトを参考にさせて頂きました。ありがとうございます。
http://centossrv.com/apache.shtml
http://centossrv.com/mysql.shtml
http://blog.cheki.net/archives/349
http://i-am.web777.net/2007/03/mysql_utf8.html
http://icondecotter.jp/blog/2013/08/01/yum-update%E3%81%A7php%E3%82%925-3%E3%81%8B%E3%82%895-4%E3%81%AB%E3%82%A2%E3%83%83%E3%83%97%E3%83%87%E3%83%BC%E3%83%88/