CentOS APM 설치 (yum)

리눅스 APM 설치
리눅스 Apache, PHP, MySQL 설치
리눅스 httpd, php, mysqld 설치

1 설치 전 확인

명령어
Bash
Copy
rpm -qa | egrep "^(httpd|php|mysql)"
실행 예시
bat
Copy
[root@jmnote ~]# rpm -qa | egrep "^(httpd|php|mysql)"
mysql-libs-5.1.61-4.el6.x86_64
→ 설치되지 않았다.[1]

2 설치

여기서는 Daum repo를 사용한다.

명령어
Bash
Copy
yum install httpd mysql mysql-server php php-mysql
실행결과
bat
Copy
[root@jmnote ~]# yum install httpd -y
... (생략)
============================================================================================================
 Package                     Arch                Version                            Repository         Size
============================================================================================================
Installing:
 httpd                       x86_64              2.2.15-26.el6.centos               Daum              821 k
 mysql                       x86_64              5.1.66-2.el6_3                     Daum              885 k
 mysql-server                x86_64              5.1.66-2.el6_3                     Daum              8.6 M
 php                         x86_64              5.3.3-22.el6                       Daum              1.1 M
 php-mysql                   x86_64              5.3.3-22.el6                       Daum               81 k
Installing for dependencies:
 apr                         x86_64              1.3.9-5.el6_2                      Daum              123 k
 apr-util                    x86_64              1.3.9-3.el6_0.1                    Daum               87 k
 apr-util-ldap               x86_64              1.3.9-3.el6_0.1                    Daum               15 k
 httpd-tools                 x86_64              2.2.15-26.el6.centos               Daum               72 k
 mailcap                     noarch              2.1.31-2.el6                       Daum               27 k
 perl-DBD-MySQL              x86_64              4.013-3.el6                        Daum              134 k
 perl-DBI                    x86_64              1.609-4.el6                        Daum              705 k
 php-cli                     x86_64              5.3.3-22.el6                       Daum              2.2 M
 php-common                  x86_64              5.3.3-22.el6                       Daum              524 k
 php-pdo                     x86_64              5.3.3-22.el6                       Daum               75 k
Updating for dependencies:
 mysql-libs                  x86_64              5.1.66-2.el6_3                     Daum              1.2 M

Transaction Summary
============================================================================================================
Install      15 Package(s)
Upgrade       1 Package(s)

Total download size: 17 M
Is this ok [y/N]:
y Enter
bat
Copy
... (생략)
Installed:
  httpd.x86_64 0:2.2.15-26.el6.centos mysql.x86_64 0:5.1.66-2.el6_3   mysql-server.x86_64 0:5.1.66-2.el6_3
  php.x86_64 0:5.3.3-22.el6           php-mysql.x86_64 0:5.3.3-22.el6

Dependency Installed:
  apr.x86_64 0:1.3.9-5.el6_2                          apr-util.x86_64 0:1.3.9-3.el6_0.1                     
  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1              httpd-tools.x86_64 0:2.2.15-26.el6.centos             
  mailcap.noarch 0:2.1.31-2.el6                       perl-DBD-MySQL.x86_64 0:4.013-3.el6                   
  perl-DBI.x86_64 0:1.609-4.el6                       php-cli.x86_64 0:5.3.3-22.el6                         
  php-common.x86_64 0:5.3.3-22.el6                    php-pdo.x86_64 0:5.3.3-22.el6                         

Dependency Updated:
  mysql-libs.x86_64 0:5.1.66-2.el6_3                                                                        

Complete!

3 설치 후 확인

bat
Copy
[root@jmnote ~]# rpm -qa | egrep "^(httpd|php|mysql)" | sort -n
httpd-2.2.15-26.el6.centos.x86_64
httpd-tools-2.2.15-26.el6.centos.x86_64
mysql-5.1.66-2.el6_3.x86_64
mysql-libs-5.1.66-2.el6_3.x86_64
mysql-server-5.1.66-2.el6_3.x86_64
php-5.3.3-22.el6.x86_64
php-cli-5.3.3-22.el6.x86_64
php-common-5.3.3-22.el6.x86_64
php-mysql-5.3.3-22.el6.x86_64
php-pdo-5.3.3-22.el6.x86_64

4 서비스 등록 확인

명령어
Bash
Copy
service httpd status
service mysqld status
php -v
실행예시
bat
Copy
[root@jmnote ~]# service httpd status
httpd is stopped
bat
Copy
[root@jmnote ~]# service mysqld status
mysqld is stopped
bat
Copy
[root@jmnote ~]# php -v
PHP 5.3.3 (cli) (built: Feb 22 2013 02:51:11) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
→ 아파치와 MySQL이 서비스에 등록되어 있다. PHP를 쉘에서 사용할 수 있다.

5 httpd 시작 및 설정

bat
Copy
[root@jmnote ~]# service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 111.222.33.44 for ServerName
                                                           [  OK  ]
→ 정상이지만, 경고 메시지를 없애고 싶다.
명령어
Bash
Copy
cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.default
sed -i 's/\#ServerName www.example.com:80/ServerName 도메인:80/g' /etc/httpd/conf/httpd.conf
diff /etc/httpd/conf/httpd.conf.default /etc/httpd/conf/httpd.conf
→ "도메인"에 자신의 도메인을 기입한다. 도메인이 없다면 IP를 적거나 그냥 www.example.com을 기입해도 무방하다.
실행예시 (jmnote)
bat
Copy
[root@jmnote ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.default
[root@jmnote ~]# sed -i 's/\#ServerName www.example.com:80/ServerName www.jmnote.com:80/g' /etc/httpd/conf/httpd.conf
[root@jmnote ~]# diff /etc/httpd/conf/httpd.conf.default /etc/httpd/conf/httpd.conf
276c276
< #ServerName www.example.com:80
---
> ServerName www.jmnote.com:80

이제 httpd를 다시 시작해보면...

bat
Copy
[root@jmnote ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
→ 경고 메시지가 사라졌다.

6 mysqld 시작 테스트

bat
Copy
[root@jmnote ~]# service mysqld start
Initializing MySQL database:  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 jmnote password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

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 /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
→ 정상이다. 1) 서버 재부팅시 자동시작 설정과, 2) root 패스워드 지정 안내이다. 다음 문단에서 처리할 것이므로 대충 보고 넘어가자.

7 MySQL 패스워드 지정, 테스트

MySQL 패스워드를 지정하고 테스트해보자.

명령어
Bash
Copy
/usr/bin/mysqladmin -u root password '패스워드'
mysql -u root -p
→ 원하는 패스워드를 기입한다.
실행예시
bat
Copy
[root@jmnote ~]# /usr/bin/mysqladmin -u root password 'P@ssw0rd'
[root@jmnote ~]# mysql -u root -p
Enter password:
→ 지정한 패스워드 입력 후 Enter
bat
Copy
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.52 Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
show databases; 입력 후 Enter
bat
Copy
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql>
quit 입력 후 Enter
bat
Copy
mysql> quit
Bye

8 재부팅시 자동시작 설정

명령어
Bash
Copy
chkconfig | egrep "(mysqld|httpd)"
chkconfig mysqld on
chkconfig httpd on
chkconfig | egrep "(mysqld|httpd)"
실행예시
bat
Copy
[root@jmnote ~]# chkconfig | egrep "(mysqld|httpd)"
httpd          	0:off	1:off	2:off	3:off	4:off	5:off	6:off
mysqld         	0:off	1:off	2:off	3:off	4:off	5:off	6:off
→ 서버 재부팅시 아파치와 MySQL이 자동으로 시작되지 않는다.
bat
Copy
[root@jmnote ~]# chkconfig mysqld on
[root@jmnote ~]# chkconfig httpd on
[root@jmnote ~]# chkconfig | egrep "(mysqld|httpd)"
httpd          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
→ 런레벨 3~5일 때 데몬을 자동시작하도록 설정되었다.
bat
Copy
[root@jmnote ~]# shutdown -r now
→ 실제로 재부팅하여 자동시작되는지 확인해보자.
bat
Copy
[root@jmnote ~]# service httpd status
httpd (pid  1278) is running...
[root@jmnote ~]# service mysqld status
mysqld (pid  1174) is running...
→ 자동시작되었다.

9 PHP 동작 확인

명령어
Bash
Copy
php -r 'echo "Hello World\n";'
실행예시
bat
Copy
[root@jmnote ~]# php -r 'echo "Hello World\n";'
Hello World

10 브라우저 확인 (1)

명령어
Bash
Copy
echo "<?php echo 'Hello';?>" > /var/www/html/hello.php
cat /var/www/html/hello.php
실행예시
bat
Copy
[root@jmntote ~]# echo "<?php echo 'Hello';?>" > /var/www/html/hello.php
[root@jmntote ~]# cat /var/www/html/hello.php
<?php echo 'Hello';?>

11 브라우저 확인 (2) 오류 메시지 출력

오류가 있는 PHP 코드를 넣어보자.

명령어
Bash
Copy
echo "<?php Hello ?>" > /var/www/html/hello_err.php
cat /var/www/html/hello_err.php
실행예시
bat
Copy
[root@jmnote ~]# echo "<?php Hello ?>" > /var/www/html/hello_err.php
[root@jmnote ~]# cat /var/www/html/hello_err.php
<?php Hello ?>

php.ini의 display_errors를 On으로 바꾸고 httpd를 재시작하자.

명령어
Bash
Copy
cp /etc/php.ini /etc/php.ini.default
sed -i 's/\display_errors = Off/display_errors = On/g' /etc/php.ini
diff /etc/php.ini.default /etc/php.ini
service httpd restart
실행예시
bat
Copy
[root@jmnote ~]# cp /etc/php.ini /etc/php.ini.default
[root@jmnote ~]# sed -i 's/\display_errors = Off/display_errors = On/g' /etc/php.ini
[root@jmnote ~]# diff /etc/php.ini.default /etc/php.ini
530c530
> display_errors = Off
---
< display_errors = On
[root@jmnote ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

다시 브라우저에서 http://서버IP/hello_err.php 에 접속해보자. 이번에는 아래와 같이 오류 메시지가 나타날 것이다.

text
Copy
Notice: Use of undefined constant Hello - assumed 'Hello' in /var/www/html/hello_err.php on line 1

12 PHP-MySQL 연동 확인

명령어
Bash
Copy
echo "<?php" > /var/www/html/hello_db.php
echo "\$db = new mysqli('localhost', 'root', '패스워드', 'test');" >> /var/www/html/hello_db.php
echo "if (\$db->connect_errno) die('Connect failed: '.\$db->connect_error);" >> /var/www/html/hello_db.php
echo "\$result = \$db->query('SHOW DATABASES;');" >> /var/www/html/hello_db.php
echo "echo '<xmp>';" >> /var/www/html/hello_db.php
echo "while (\$row = \$result->fetch_object()) print_r(\$row);" >> /var/www/html/hello_db.php
echo "echo '</xmp>';" >> /var/www/html/hello_db.php
echo "\$result->close();" >> /var/www/html/hello_db.php
echo "\$db->close();" >> /var/www/html/hello_db.php
echo "?>" >> /var/www/html/hello_db.php
cat /var/www/html/hello_db.php
→ 앞서 지정한 MySQL 패스워드를 기입한다.
실행예시
bat
Copy
[root@jmnote ~]# echo "<?php" > /var/www/html/hello_db.php
[root@jmnote ~]# echo "\$db = new mysqli('localhost', 'root', 'P@ssw0rd', 'test');" >> /var/www/html/hello_db.php
[root@jmnote ~]# echo "if (\$db->connect_errno) die('Connect failed: '.\$db->connect_error);" >> /var/www/html/hello_db.php
[root@jmnote ~]# echo "\$result = \$db->query('SHOW DATABASES;');" >> /var/www/html/hello_db.php
[root@jmnote ~]# echo "echo '<xmp>';" >> /var/www/html/hello_db.php
[root@jmnote ~]# echo "while (\$row = \$result->fetch_object()) print_r(\$row);" >> /var/www/html/hello_db.php
[root@jmnote ~]# echo "echo '</xmp>';" >> /var/www/html/hello_db.php
[root@jmnote ~]# echo "\$result->close();" >> /var/www/html/hello_db.php
[root@jmnote ~]# echo "\$db->close();" >> /var/www/html/hello_db.php
[root@jmnote ~]# echo "?>" >> /var/www/html/hello_db.php
[root@jmnote ~]# cat /var/www/html/hello_db.php
<?php
$db = new mysqli('localhost', 'root', 'P@ssw0rd', 'test');
if ($db->connect_errno) die('Connect failed: '.$db->connect_error);
$result = $db->query('SHOW DATABASES;');
echo '<xmp>';
while ($row = $result->fetch_object()) print_r($row);
echo '</xmp>';
$result->close();
$db->close();
?>
브라우저 결과

브라우저에서 http://서버IP/hello_db.php 에 접속해보자.

text
Copy
stdClass Object
(
    [Database] => information_schema
)
stdClass Object
(
    [Database] => mysql
)
stdClass Object
(
    [Database] => test
)
→ mysql 설치시 기본으로 생성되는 DB 3개가 보인다.

13 기타 설정

필요시 설정하자.

웹 소프트웨어 설치

14 같이 보기

15 주석

  1. CentOS 6 최소 설치 209개 패키지 중에 mysql-libs가 있을 뿐 mysql이 설치된 것은 아니다.