CentOS 7에 Nginx, MariaDB, PHP(LNMP) 스택을 설치하는 방법을 단계별로 설명합니다.
1. CentOS 서드파티 Yum 저장소 설정
CentOS 기본 저장소에는 Nginx 패키지가 포함되어 있지 않으므로, Atomic 서드파티 저장소를 추가합니다.
[root@localhost ~]# yum install wget -y[root@localhost ~]# wget http://www.atomicorp.com/installers/atomic[root@localhost ~]# sh ./atomic[root@localhost ~]# yum check-update
2. 개발 도구 및 라이브러리 설치
LNMP 스택 구축에 필요한 필수 개발 패키지와 라이브러리를 한 번에 설치합니다.
[root@localhost ~]# yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel
3. 기존 Apache, MySQL, PHP 제거
이전에 설치된 웹 서버 관련 패키지를 제거하여 충돌을 방지합니다.
[root@localhost ~]# yum remove httpd[root@localhost ~]# yum remove mysql[root@localhost ~]# yum remove php
4. Nginx 설치 및 시작
[root@localhost ~]# yum install nginx -y[root@localhost ~]# systemctl start nginx.service[root@localhost ~]# systemctl status nginx.service[root@localhost ~]# systemctl enable nginx.service
5. MariaDB 설치 및 보안 설정
[root@localhost ~]# yum install mariadb-devel mariadb mariadb-server[root@localhost ~]# systemctl start mariadb.service[root@localhost ~]# systemctl status mariadb.service[root@localhost ~]# systemctl enable mariadb.service
MariaDB 보안 설정을 위해 mysql_secure_installation 스크립트를 실행합니다.
[root@localhost ~]# mysql_secure_installation
이 스크립트는 root 비밀번호 설정, 익명 사용자 제거, 원격 root 로그인 비활성화, 테스트 데이터베이스 제거 등의 작업을 대화형으로 진행합니다.
6. PHP 및 PHP-FPM 설치
[root@localhost ~]# yum -y install php php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-mssql php-snmp php-soap[root@localhost ~]# yum install php-tidy php-common php-devel php-fpm php-mysql -y[root@localhost ~]# systemctl start php-fpm.service[root@localhost ~]# systemctl status php-fpm.service[root@localhost ~]# systemctl enable php-fpm.service
7. Nginx에서 PHP 지원 설정
Nginx 설정 파일을 백업한 후, PHP-FPM과 연동하도록 편집합니다.
[root@localhost ~]# cp /etc/nginx/nginx.conf{,.bak}[root@localhost ~]# vim /etc/nginx/nginx.conf
Nginx 메인 설정에서 user nginx nginx; 행을 확인하거나 추가합니다.
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
기본 서버 블록을 다음과 같이 수정합니다.
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
8. PHP 설정 최적화
[root@localhost ~]# cp /etc/php.ini{,.bak}[root@localhost ~]# vim /etc/php.ini
보안을 위해 expose_php = Off로 설정하여 PHP 버전 정보 노출을 차단합니다.
9. 서비스 재시작
[root@localhost ~]# systemctl restart nginx.service[root@localhost ~]# systemctl restart php-fpm.service
10. 작동 테스트
Nginx 기본 웹 루트 디렉토리에 PHP 정보 출력 파일을 생성하여 동작을 확인합니다.
[root@localhost ~]# cd /usr/share/nginx/html/[root@localhost html]# vim index.php
다음 내용을 추가합니다:
<?php phpinfo(); ?>
디렉토리 소유권을 Nginx 사용자로 설정합니다.
[root@localhost html]# chown nginx.nginx /usr/share/nginx/html/ -R
방화벽에서 HTTP(80) 및 MySQL(3306) 포트를 허용합니다.
[root@localhost html]# firewall-cmd --zone=public --add-port=80/tcp --permanent[root@localhost html]# firewall-cmd --zone=public --add-port=3306/tcp --permanent[root@localhost html]# systemctl restart firewalld.service[root@localhost html]# systemctl enable firewalld.service
웹 브라우저에서 서버 IP 주소로 접속하면 PHP 정보 페이지가 표시됩니다.
참고 사항
- Nginx 기본 웹 루트:
/usr/share/nginx/html/ - MariaDB 데이터 디렉토리:
/var/lib/mysql - 디렉토리 권한 설정 예시:
chown nginx.nginx /usr/share/nginx/html/ -R