# yum install httpd
# yum list installed | grep -i httpd httpd.i386 2.2.3-22.el5.centos.2 installed
# /etc/init.d/httpd start
httpd を起動中: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[ OK ]
# vi /etc/httpd/conf/httpd.conf
#ServerName www.example.com:80
ServerName localhost
# /etc/init.d/httpd restart httpd を停止中: [ OK ] httpd を起動中: [ OK ]
# chkconfig httpd on # chkconfig --list | grep httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
IPアドレスの例)http://192.168.247.136/ ホスト名の例)http://centos/
# system-config-securitylevel
# cat /etc/sysconfig/iptables (略) -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT (略)
# /etc/init.d/iptables stop ファイアウォールルールを適用中: [ OK ] チェインポリシーを ACCEPT に設定中filter [ OK ] iptables モジュールを取り外し中 [ OK ]
# chkconfig iptables off # chkconfig --list | grep iptables iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
# cat /etc/httpd/conf/httpd.conf | grep ^DocumentRoot DocumentRoot "/var/www/html"
# vi /var/www/html/hello.html
<h1>Hello Apache!!</h1>
IPアドレスの例)http://192.168.247.136/hello.html ホスト名の例)http://centos/hello.html
# yum list php-* Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: ftp.yz.yamagata-u.ac.jp * updates: ftp.yz.yamagata-u.ac.jp * addons: ftp.yz.yamagata-u.ac.jp * extras: ftp.yz.yamagata-u.ac.jp Available Packages php.i386 5.1.6-23.2.el5_3 updates php-bcmath.i386 5.1.6-23.2.el5_3 updates php-cli.i386 5.1.6-23.2.el5_3 updates php-common.i386 5.1.6-23.2.el5_3 updates php-dba.i386 5.1.6-23.2.el5_3 updates php-dbase.i386 5.1.6-15.el5.centos.1 extras php-devel.i386 5.1.6-23.2.el5_3 updates php-gd.i386 5.1.6-23.2.el5_3 updates php-imap.i386 5.1.6-23.2.el5_3 updates php-ldap.i386 5.1.6-23.2.el5_3 updates php-mbstring.i386 5.1.6-23.2.el5_3 updates php-mcrypt.i386 5.1.6-15.el5.centos.1 extras php-mhash.i386 5.1.6-15.el5.centos.1 extras php-mssql.i386 5.1.6-15.el5.centos.1 extras php-mysql.i386 5.1.6-23.2.el5_3 updates php-ncurses.i386 5.1.6-23.2.el5_3 updates php-odbc.i386 5.1.6-23.2.el5_3 updates php-pdo.i386 5.1.6-23.2.el5_3 updates php-pear.noarch 1:1.4.9-4.el5.1 base php-pear-Auth-SASL.noarch 1.0.2-4.el5.centos extras php-pear-DB.noarch 1.7.13-1.el5.centos extras php-pear-Date.noarch 1.4.7-2.el5.centos extras php-pear-File.noarch 1.2.2-1.el5.centos extras php-pear-HTTP-Request.noarch 1.4.2-1.el5.centos extras php-pear-Log.noarch 1.9.13-1.el5.centos extras php-pear-MDB2.noarch 2.4.1-2.el5.centos extras php-pear-MDB2-Driver-mysql.noarch 1.4.1-3.el5.centos extras php-pear-Mail.noarch 1.1.14-1.el5.centos extras php-pear-Mail-Mime.noarch 1.4.0-1.el5.centos extras php-pear-Net-SMTP.noarch 1.2.10-1.el5.centos extras php-pear-Net-Sieve.noarch 1.1.5-2.el5.centos extras php-pear-Net-Socket.noarch 1.0.8-1.el5.centos extras php-pear-Net-URL.noarch 1.0.15-1.el5.centos extras php-pecl-Fileinfo.i386 1.0.4-3.el5.centos extras php-pecl-memcache.i386 2.2.3-1.el5_2 extras php-pgsql.i386 5.1.6-23.2.el5_3 updates php-readline.i386 5.1.6-15.el5.centos.1 extras php-snmp.i386 5.1.6-23.2.el5_3 updates php-soap.i386 5.1.6-23.2.el5_3 updates php-tidy.i386 5.1.6-15.el5.centos.1 extras php-xml.i386 5.1.6-23.2.el5_3 updates php-xmlrpc.i386 5.1.6-23.2.el5_3 updates
# yum list installed | grep -i php php.i386 5.1.6-23.2.el5_3 installed php-cli.i386 5.1.6-23.2.el5_3 installed php-common.i386 5.1.6-23.2.el5_3 installed php-mbstring.i386 5.1.6-23.2.el5_3 installed php-mysql.i386 5.1.6-23.2.el5_3 installed php-pdo.i386 5.1.6-23.2.el5_3 installed
# php -v PHP 5.1.6 (cli) (built: Apr 7 2009 08:00:04) Copyright (c) 1997-2006 The PHP Group Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
# vi /var/www/html/hello.php
<h1><?php echo "Hello PHP!!" ?></h1>
IPアドレスの例)http://192.168.247.136/hello.php ホスト名の例)http://centos/hello.php
# vi /etc/httpd/conf.d/php.conf
AddType text/html .php
AddType application/x-httpd-php .php
LoadModule php5_module modules/libphp5.so
[warn] module php5_module is already loaded, skipping
# /etc/init.d/httpd restart httpd を停止中: [ OK ] httpd を起動中: [ OK ]
# vi /etc/php.ini
360行目あたり(エラー報告のレベルを厳密にします)
error_reporting = E_ALL
error_reporting = E_ALL | E_STRICT
367行目あたり(エラーを表示します)
display_errors = Off
display_errors = On
639行目あたり(タイムゾーンの設定をします)
;date.timezone =
date.timezone = Asia/Tokyo
1125行目あたり(マルチバイト文字の設定を日本語にします)
;mbstring.language = Japanese
mbstring.language = Japanese
1130行目あたり(マルチバイト文字の内部文字コードをUTF8にします)
;mbstring.internal_encoding = EUC-JP
mbstring.internal_encoding = UTF-8
# /etc/init.d/httpd restart
# yum install mysql mysql-server
# yum list installed | grep -i mysql mysql.i386 5.0.45-7.el5 installed mysql-server.i386 5.0.45-7.el5 installed perl-DBD-MySQL.i386 3.0007-2.el5 installed php-mysql.i386 5.1.6-23.2.el5_3 installed
# /etc/init.d/mysqld start
MySQL データベースを初期化中: Installing MySQL system tables...
OK
Filling help tables...
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:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h test01 password 'new-password'
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
[ OK ]
MySQL を起動中: [ OK ]
# ps aux | grep mysql
root 8433 0.0 0.1 5420 1148 pts/0 S 03:45 0:00 [↓実際は一行]
/bin/sh /usr/bin/mysqld_safe
--datadir=/var/lib/mysql
--socket=/var/lib/mysql/mysql.sock
--log-error=/var/log/mysqld.log
--pid-file=/var/run/mysqld/mysqld.pid
mysql 8493 0.0 2.3 126120 18536 pts/0 Sl 03:45 0:00 [↓実際は一行]
/usr/libexec/mysqld
--basedir=/usr
--datadir=/var/lib/mysql
--user=mysql
--pid-file=/var/run/mysqld/mysqld.pid
--skip-external-locking
--socket=/var/lib/mysql/mysql.sock
# pstree
# cat /etc/passwd
# chkconfig mysqld on # chkconfig --list | grep mysqld httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.0.45 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec)
mysql> SET PASSWORD FOR root@localhost=PASSWORD('xxxxx');
Query OK, 0 rows affected (0.00 sec)
mysql> exit Bye
# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# mysql -u root -p Enter password:
mysql> CREATE DATABASE lamp DEFAULT CHARACTER SET utf8; Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | lamp | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
mysql> USE lamp Database changed
CREATE TABLE language (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(10) NOT NULL,
note VARCHAR(100) NOT NULL
);
mysql> CREATE TABLE language (
-> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> name VARCHAR(10),
-> note VARCHAR(100)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> DESC language; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | YES | | NULL | | | note | varchar(100) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec)
INSERT INTO language (name, note) VALUES ('PHP', '初心者でも使いやすい');
INSERT INTO language (name, note) VALUES ('Perl', 'かつては一世を風靡した');
INSERT INTO language (name, note) VALUES ('Ruby', 'Railsで火がついた国産言語');
INSERT INTO language (name, note) VALUES ('Python', '米国での利用者はとても多い');
mysql> SELECT * FROM language; +----+--------+-----------------------------------------+ | id | name | note | +----+--------+-----------------------------------------+ | 1 | PHP | 初心者でも使いやすい | | 2 | Perl | かつては一世を風靡した | | 3 | Ruby | Railsで火がついた国産言語 | | 4 | Python | 米国での利用者はとても多い | +----+--------+-----------------------------------------+ 4 rows in set (0.00 sec)
<?php
$db = mysql_connect('localhost', 'root', 'password');
if (!$db) {
die('MySQL接続失敗: ' . mysql_error());
}
$db_selected = mysql_select_db('lamp', $db);
if (!$db_selected) {
die('データベース選択失敗: ' . mysql_error());
}
$result = mysql_query('SELECT * FROM language', $db);
if (!$result) {
die('クエリ選択失敗: ' . mysql_error());
}
while ($row = mysql_fetch_assoc($result)) {
echo 'id: ' . $row['id'] . '<br />';
echo 'name: ' . $row['name'] . '<br />';
echo 'note: ' . $row['note'] . '<br />';
echo '<hr />';
}
*1 実際にはPHPは単独でも動作しますし、Apacheとは別次元でCGIとしても動作します。
*2 本番運用時は、セキュリティも考慮して、ある程度設定を変更する場合が多いです
*3 『Code Craft ~エクセレントなコードを書くための実践的技法:http://www.amazon.co.jp/dp/4839921946/』の第一章「防御的プログラミング」でも述べられています。
*4 ちなみに、このrootユーザーとは、Linuxのrootユーザーとは全く関係がありません。MySQLレベルで、全ての特権をもつユーザーのことです。
*5 私は開発環境の場合、「password」とすることが多いです。覚えるのが面倒なので。
*6 -uはユーザー名、-pは続けてパスワードを入力するというオプションです。
*7 ほとんどのテキストでは、そのようなスタイルになっているはずです。