CentOS6 에 APM 설치하기

새로운 PHP 설치 방법 안내

– 현재는 https://github.com/php79/stack 스크립트로 자동 설치하는 방법을 권장하고 있습니다.
– 본 문서는 2013년도에 작성된 CentOS 6 + Apache + PHP 5.3 + MySQL 조합으로 수동 설치하는 방법에 대해 다룹니다.


yum 으로 APM 패키지를 설치하고, 부팅시 자동 시작, 보안을 위한 설정을 다루겠습니다.

먼저 yum 으로 Apache + PHP + MySQL 패키지를 설치합니다.

yum install httpd mysql-server mysql php php-devel php-pear php-mysql php-mbstring php-gd

1. Apache 설정

– 먼저 설정 파일을 백업합니다.

cp -av /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.ori

 
– KeepAlive 항목은 Off 에서 On 으로 변경합니다.  중소규모에선 웹서버 사용량이 너무 많아 서비스가 멈출 정도가 아니라면 대부분의 경우 On 이 빠른 응답으로 유리합니다.

sed -i 's/KeepAlive Off/KeepAlive On/' /etc/httpd/conf/httpd.conf

 

– ServerName 은 아파치 시작시 경고 문구가 나오지 않도록 127.0.0.1 으로라도 입력해 둡니다.  (물론 실제 주소를 입력해도 됩니다.)

sed -i 's/#ServerName www.example.com:80/ServerName 127.0.0.1:80/' /etc/httpd/conf/httpd.conf

 

– AddDefaultCharset 항목은 UTF-8이 아닌 페이지에서 문제가 될 수 있으므로 주석 처리합니다.

sed -i 's/AddDefaultCharset UTF-8/#AddDefaultCharset UTF-8/' /etc/httpd/conf/httpd.conf

 

– 외부에서 Apache 버전을 알 수 없도록 숨깁니다.

sed -i 's/ServerTokens OS/ServerTokens Prod/' /etc/httpd/conf/httpd.conf
sed -i 's/ServerSignature On/ServerSignature Off/' /etc/httpd/conf/httpd.conf

 

– 이제 수정된 결과가 다음과 일치하는지 확인하면 됩니다.

grep -P 'KeepAlive O|^ServerName|AddDefaultCharset|ServerTokens|ServerSignature' /etc/httpd/conf/httpd.conf
ServerTokens Prod
KeepAlive On
ServerName 127.0.0.1:80
ServerSignature Off
#AddDefaultCharset UTF-8

 

2. PHP 설정

– 먼저 설정 파일을 백업합니다.

cp -av /etc/php.ini /etc/php.ini.ori

 
– /etc/php.ini 의 기본값은 다음과 같습니니다.

grep -P 'short_open_tag =|expose_php =|date.timezone =|register_globals =|register_long_arrays =|magic_quotes_gpc =|allow_call_time_pass_reference =|error_reporting =|display_errors =|display_startup_errors =' /etc/php.ini
short_open_tag = Off
allow_call_time_pass_reference = Off
expose_php = On
error_reporting = E_ALL & ~E_DEPRECATED
display_errors = Off
display_startup_errors = Off
register_globals = Off
register_long_arrays = Off
magic_quotes_gpc = Off
;date.timezone =

 

– 다음처럼 일반적인 설정을 적용해 줍니다.

sed -i 's/short_open_tag = Off/short_open_tag = On/' /etc/php.ini
sed -i 's/;date.timezone =/date.timezone = "Asia\/Seoul"/' /etc/php.ini
sed -i 's/allow_call_time_pass_reference = Off/allow_call_time_pass_reference = On/' /etc/php.ini

 

– 외부에서 PHP 버전을 숨깁니다.

sed -i 's/expose_php = On/expose_php = Off/' /etc/php.ini

 

– 장애 발생시 쉬운 해결을 위해 PHP 에러를 화면에 바로 보여줍니다. (보안에 민감하신 분은 Off로 그대로 두셔도 됩니다.)

sed -i 's/display_errors = Off/display_errors = On/' /etc/php.ini
sed -i 's/display_startup_errors = Off/display_startup_errors = On/' /etc/php.ini

 

– 일부 솔루션의 호환성 경고를 보이지 않기 위해 에러 레벨을 변경합니다.

sed -i 's/error_reporting = E_ALL \& ~E_DEPRECATED/error_reporting = E_ALL \& ~E_NOTICE \& ~E_DEPRECATED \& ~E_USER_DEPRECATED/' /etc/php.ini

 

– PHP 솔루션마다 필수 조건이 On, Off 2가지중 하나일 수 있지만, 국내 솔루션들은 아래와 같이 설정하면 무난합니다. 특히 사용하려는 PHP 솔루션에서 권장하는 magic_quotes_gpc 값을 모르실 경우, 일단 On 으로 두셔야 보안상 안전합니다.
참고) 1개 서버내에서 사이트마다 아래 값을 다르게 지정하는 방법도 있습니다. – http://www.php.net/manual/kr/configuration.changes.php
주의) 아래 값을 Off 로 두실 경우, 사용하려는 솔루션이 Off 환경에서 정상 동작하는지 반드시 확인하는 것이 좋습니다.

sed -i 's/register_globals = Off/register_globals = On/' /etc/php.ini
sed -i 's/magic_quotes_gpc = Off/magic_quotes_gpc = On/' /etc/php.ini

 

– (선택, 비권장) 아주 오래전에 제작한 솔루션에서 $HTTP_SERVER_VARS 등의 $HTTP_*_VARS 변수를 사용하는 경우에만 On 으로 변경합니다.
참고) 그누보드, XE 등의 솔루션들은 아래 항목을 Off 로 그대로 두면 됩니다.

sed -i 's/register_long_arrays = Off/register_long_arrays = On/' /etc/php.ini

 

– 이제 수정된 결과가 다음과 일치하는지 확인하면 됩니다.

grep -P 'short_open_tag =|expose_php = |date.timezone =|register_globals =|register_long_arrays =|magic_quotes_gpc =|allow_call_time_pass_reference =|error_reporting =|display_errors =|display_startup_errors =' /etc/php.ini
short_open_tag = On
allow_call_time_pass_reference = On
expose_php = Off
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_USER_DEPRECATED
display_errors = On
display_startup_errors = On
register_globals = On
register_long_arrays = Off
magic_quotes_gpc = On
date.timezone = "Asia/Seoul"

 

3. Apache 시작

– Apache 웹서버를 시작합니다.

/etc/init.d/httpd start

 
– Apache 웹서버를 부팅시 자동 시작되도록 합니다.

/sbin/chkconfig httpd on

 
– 이제 웹브라우저에서 서버 IP를 입력하여, 테스트 웹페이지가 열리는지 확인하면 됩니다.
만약 테스트 웹페이지가 열리지 않는다면, 안전한 CentOS를 위한 방화벽을 확인해보시면 됩니다.
 

4. MySQL 설정 및 시작

– 먼저 설정 파일을 백업합니다.

cp -av /etc/my.cnf /etc/my.cnf.ori

 

– 서버 메모리가 2GB 이상이라면, 1~2GB 에 최적화된 설정 파일로 교체합니다.

cp -av /usr/share/mysql/my-huge.cnf /etc/my.cnf

 

– 설정 파일에서 차기 버전에서 변경되어 에러 가능성이 있는 항목을 미리 변경해 둡니다.

sed -i 's/skip-locking/skip-external-locking/' /etc/my.cnf

 

– MySQL DB서버를 시작합니다.

/etc/init.d/mysqld start

 

– MySQL DB서버를 부팅시 자동 시작되도록 합니다.

/sbin/chkconfig mysqld on

 

– MySQL 보안 설정을 시작합니다. mysql root 비밀번호만 입력하고, 나머지는 모두 엔터만 입력하면 기본값(불필요한 권한 및 DB 삭제)으로 처리됩니다.

/usr/bin/mysql_secure_installation

 




NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] 
New password: *** 여기서 mysql root 비밀번호 입력 ***
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] 
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] 
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] 
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] 
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

 

– 입력했던 mysql root 비밀번호로 접속되는지 확인하면 모든 작업이 완료됩니다.

mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.1.69-log 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)

mysql> quit

 
이상입니다.

다음에는 솔루션 설치를 위한 Apache VirtualHost(가상 호스트) 설정, MySQL DB 계정 생성 등을 해보겠습니다.