CentOS APM 설치 (yum)

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

1 설치 전 확인[ | ]

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

2 설치[ | ]

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

명령어
yum install httpd mysql mysql-server php php-mysql
실행결과
[root@zetawiki ~]# yum install httpd mysql mysql-server php php-mysql
... (생략)
============================================================================================================
 Package                     Arch                Version                            Repository         Size
============================================================================================================
Installing:
 httpd                       x86_64              2.2.15-26.el6.centos               base              821 k
 mysql                       x86_64              5.1.66-2.el6_3                     base              885 k
 mysql-server                x86_64              5.1.66-2.el6_3                     base              8.6 M
 php                         x86_64              5.3.3-22.el6                       base              1.1 M
 php-mysql                   x86_64              5.3.3-22.el6                       base               81 k
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-26.el6.centos               base               72 k
 mailcap                     noarch              2.1.31-2.el6                       base               27 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-22.el6                       base              2.2 M
 php-common                  x86_64              5.3.3-22.el6                       base              524 k
 php-pdo                     x86_64              5.3.3-22.el6                       base               75 k
Updating for dependencies:
 mysql-libs                  x86_64              5.1.66-2.el6_3                     base              1.2 M

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

Total download size: 17 M
Is this ok [y/N]: y
... (생략)
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 설치 후 확인[ | ]

[root@zetawiki ~]# 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 서비스 등록 확인[ | ]

명령어
service httpd status
service mysqld status
php -v
실행예시
[root@zetawiki ~]# service httpd status
httpd is stopped
[root@zetawiki ~]# service mysqld status
mysqld is stopped
[root@zetawiki ~]# 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 시작 및 설정[ | ]

[root@zetawiki ~]# 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  ]
→ 정상이지만, 경고 메시지를 없애고 싶다.
명령어
cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.default
sed -i 's/\#ServerName www.example.com:80/ServerName www.example.com:80/g' /etc/httpd/conf/httpd.conf
diff /etc/httpd/conf/httpd.conf.default /etc/httpd/conf/httpd.conf
→ "www.example.com" 대신 자신의 도메인을 기입한다. 도메인이 없다면 그대로 두어도 무방하다.
실행예시 (jmnote)
[root@zetawiki ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.default
[root@zetawiki ~]# sed -i 's/\#ServerName www.example.com:80/ServerName www.jmnote.com:80/g' /etc/httpd/conf/httpd.conf
[root@zetawiki ~]# diff /etc/httpd/conf/httpd.conf.default /etc/httpd/conf/httpd.conf
276c276
< #ServerName www.example.com:80
---
> ServerName www.jmnote.com:80

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

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

6 mysqld 시작 테스트[ | ]

[root@zetawiki ~]# 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 패스워드를 지정하고 테스트해보자.

명령어
/usr/bin/mysqladmin -u root password 'P@ssw0rd'
mysql -u root -p
→ P@ssw0rd 대신 원하는 패스워드를 기입한다.
실행예시
[root@zetawiki ~]# /usr/bin/mysqladmin -u root password 'P@ssw0rd'
[root@zetawiki ~]# mysql -u root -p
Enter password:
→ 지정한 패스워드 입력 후 Enter
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
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql>
quit 입력 후 Enter
mysql> quit
Bye

8 재부팅시 자동시작 설정[ | ]

명령어
chkconfig --list | egrep "mysqld|httpd"
chkconfig mysqld on
chkconfig httpd on
chkconfig --list | egrep "mysqld|httpd"
실행예시
[root@zetawiki ~]# chkconfig --list | 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이 자동으로 시작되지 않는다.
[root@zetawiki ~]# chkconfig mysqld on
[root@zetawiki ~]# chkconfig httpd on
[root@zetawiki ~]# chkconfig --list | 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일 때 데몬을 자동시작하도록 설정되었다.
[root@zetawiki ~]# reboot
→ 실제로 재부팅하여 자동시작되는지 확인해보자.
[root@zetawiki ~]# service httpd status
httpd (pid  1278) is running...
[root@zetawiki ~]# service mysqld status
mysqld (pid  1174) is running...
→ 자동시작되었다.

9 iptables 80포트 열기[ | ]

외부에서 접근할수 있도록 80포트를 연다.

명령어
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/etc/init.d/iptables save
/etc/init.d/iptables restart
실행예시
[root@zetawiki ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@zetawiki ~]# /etc/init.d/iptables save
iptables: 방화벽 규칙을 /etc/sysconfig/iptables에 저장 중: [  OK  ]
[root@zetawiki ~]# /etc/init.d/iptables restart
iptables: 체인을 ACCEPT 규칙으로 설정 중:  filter          [  OK  ]
iptables: 방화벽 규칙을 지웁니다:                          [  OK  ]
iptables: 모듈을 언로드하는 중:                            [  OK  ]
iptables: 방화벽 규칙 적용 중:                             [  OK  ]

10 PHP 동작 확인[ | ]

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

11 브라우저 확인 (1)[ | ]

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

12 브라우저 확인 (2) 오류 메시지 출력[ | ]

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

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

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

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

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

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

13 PHP-MySQL 연동 확인[ | ]

명령어
echo '<?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();
?>' > /var/www/html/hello_db.php
cat /var/www/html/hello_db.php
→ P@ssw0rd 대신 앞서 지정한 MySQL 패스워드를 기입한다.
실행예시
[root@zetawiki ~]# echo '<?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();
> ?>' > /var/www/html/hello_db.php
[root@zetawiki ~]# 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 에 접속해보자.

stdClass Object
(
    [Database] => information_schema
)
stdClass Object
(
    [Database] => mysql
)
stdClass Object
(
    [Database] => test
)
→ mysql 설치시 기본으로 생성되는 DB 3개가 보인다.
→ 만약 Fatal error: Class 'mysqli' not found 오류 나면 해당 글 참조

14 기타 설정[ | ]

필요시 설정하자.

웹 소프트웨어 설치

15 같이 보기[ | ]

16 주석[ | ]

  1. CentOS 6 최소 설치 209개 패키지 중에 mysql-libs가 있을 뿐 mysql이 설치된 것은 아니다.
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}