LNMP 환경 구축 가이드

하나. 방화벽 설정

CentOS 7.x 기본 방화벽인 firewall 대신 iptables를 사용하도록 설정합니다.

  1. firewall 비활성화:
systemctl stop firewalld.service # firewall 중지
systemctl disable firewalld.service # 부팅 시 firewall 자동 실행 비활성화
  1. iptables 설치
yum install iptables-services # 설치
vi /etc/sysconfig/iptables # 방화벽 설정 파일 편집
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
systemctl restart iptables.service # 설정 적용을 위해 방화벽 재시작
systemctl enable iptables.service # 부팅 시 방화벽 자동 활성화
/usr/libexec/iptables/iptables.init restart # 방화벽 재시작

둘. SELinux 비활성화

vi /etc/selinux/config
#SELINUX=enforcing # 주석 처리
#SELINUXTYPE=targeted # 주석 처리
SELINUX=disabled # 추가
 
setenforce 0 # 설정 즉시 적용

셋. 소프트웨어 다운로드

1. nginx 다운로드
http://nginx.org/download/nginx-1.10.1.tar.gz
2. MySQL 다운로드
http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.33.tar.gz
3. PHP 다운로드
http://cn2.php.net/distributions/php-5.6.26.tar.gz
4. cmake (MySQL 빌드 도구)
https://cmake.org/files/v3.6/cmake-3.6.2.tar.gz
5. pcre (nginx 가상 정적 지원)
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
6. openssl (nginx 확장)
https://www.openssl.org/source/openssl-1.0.2j.tar.gz
7. zlib (nginx 확장)
http://zlib.net/zlib-1.2.8.tar.gz
8. libmcrypt (PHP 확장)
http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
9. yasm (PHP 확장)
http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
10. t1lib (PHP 확장)
ftp://sunsite.unc.edu/pub/Linux/libs/graphics/t1lib-5.1.2.tar.gz
11. gd 라이브러리 설치 패키지
https://bitbucket.org/libgd/gd-libgd/downloads/libgd-2.1.1.tar.gz
12. libvpx (gd 라이브러리 필요)
http://ftp.osuosl.org/pub/blfs/conglomeration/libvpx/libvpx-1.6.0.tar.bz2
13. tiff (gd 라이브러리 필요)
http://download.osgeo.org/libtiff/tiff-4.0.6.tar.gz
14. libpng (gd 라이브러리 필요)
ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng16/libpng-1.6.25.tar.gz
15. freetype (gd 라이브러리 필요)
http://download.savannah.gnu.org/releases/freetype/freetype-2.7.tar.gz
16. jpegsrc (gd 라이브러리 필요)
http://www.ijg.org/files/jpegsrc.v9b.tar.gz

넷. 빌드 도구 및 라이브러리 설치

yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libxml* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel

MySQL, PHP, Nginx 설치:

하나. MySQL 설치

  1. cmake 설치
cd /usr/local/src
tar zxvf cmake-3.6.2.tar.gz
cd cmake-3.6.2
./configure
make
make install
  1. MySQL 설치
groupadd mysql # mysql 그룹 생성
useradd -g mysql mysql -s /bin/false # mysql 사용자 생성 및 그룹 추가, 직접 로그인 금지
mkdir -p /data/mysql # MySQL 데이터베이스 저장 디렉토리 생성
chown -R mysql:mysql /data/mysql # MySQL 데이터베이스 디렉토리 권한 설정
mkdir -p /usr/local/mysql # MySQL 설치 디렉토리 생성
cd /usr/local/src # 소프트웨어 패키지 디렉토리로 이동
tar zxvf mysql-5.6.33.tar.gz # 압축 해제
cd mysql-5.6.33 # 디렉토리 이동
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc # 설정
make # 컴파일
make install # 설치
rm -rf /etc/my.cnf # 기본 설정 파일 삭제 (기본으로 없으면 생략)
cd /usr/local/mysql # MySQL 설치 디렉토리로 이동
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql # MySQL 시스템 데이터베이스 생성
ln -s /usr/local/mysql/my.cnf /etc/my.cnf # /etc 디렉토리에 심볼릭 링크 추가
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld # MySQL을 시스템 서비스로 등록
chmod 755 /etc/init.d/mysqld # 실행 권한 부여
chkconfig mysqld on # 부팅 시 자동 실행 설정
vi /etc/rc.d/init.d/mysqld # 편집
basedir=/usr/local/mysql # MySQL 프로그램 설치 경로
datadir=/data/mysql # MySQL 데이터베이스 저장 경로
service mysqld start # 시작
vi /etc/profile # MySQL을 시스템 환경 변수에 추가: 마지막에 아래 줄 추가
export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile # 설정 즉시 적용
아래 두 줄은 MySQL 라이브러리 파일을 시스템 기본 위치에 연결하여 PHP 등 컴파일 시 라이브러리 경로를 지정할 필요가 없도록 합니다.
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
mkdir /var/lib/mysql # 디렉토리 생성
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock # 심볼릭 링크 추가
mysql_secure_installation # MySQL 비밀번호 설정, 안내에 따라 Y 입력 후 2번 비밀번호 입력

둘. Nginx 설치

  1. pcre 설치
cd /usr/local/src
mkdir /usr/local/pcre
tar zxvf pcre-8.39.tar.gz
cd pcre-8.39
./configure --prefix=/usr/local/pcre
make
make install
  1. openssl 설치
cd /usr/local/src
mkdir /usr/local/openssl
tar zxvf openssl-1.0.2j.tar.gz
cd openssl-1.0.2j
./config --prefix=/usr/local/openssl
make
make install
vi /etc/profile
export PATH=$PATH:/usr/local/openssl/bin
:wq!
source /etc/profile
  1. zlib 설치
cd /usr/local/src
mkdir /usr/local/zlib
tar zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=/usr/local/zlib
make
make install
  1. Nginx 설치
groupadd www
useradd -g www www -s /bin/false
cd /usr/local/src
tar zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.2j --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.39
참고: --with-openssl=/usr/local/src/openssl-1.0.2j --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.39는 소스 패키지 압축 해제 경로를 가리키며, 설치 경로가 아닙니다. 그렇지 않으면 오류가 발생합니다.
make
make install
/usr/local/nginx/sbin/nginx # Nginx 시작

Nginx 부팅 시 자동 실행 설정

vi /etc/rc.d/init.d/nginx # 시작 파일 편집하여 아래 내용 추가
############################################################
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
chmod 775 /etc/rc.d/init.d/nginx # 파일 실행 권한 부여
chkconfig nginx on # 부팅 시 자동 실행 설정
/etc/rc.d/init.d/nginx restart # 재시작

브라우저에서 서버 IP 주소를 열면 아래와 같은 화면이 표시되며, 이는 Nginx 설치 성공을 의미합니다.

셋. PHP 설치

  1. yasm 설치
cd /usr/local/src
tar zxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure
make
make install
  1. libmcrypt 설치
cd /usr/local/src
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make
make install
  1. libvpx 설치

cd /usr/local/src

tar xvf libvpx-1.6.0.tar.bz2

cd libvpx-1.6.0

./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9

make

make install

  1. tiff 설치
cd /usr/local/src
tar zxvf tiff-4.0.6.tar.gz
cd tiff-4.0.6
./configure --prefix=/usr/local/tiff --enable-shared
make
make install
  1. libpng 설치
cd /usr/local/src
tar zxvf libpng-1.6.25.tar.gz
cd libpng-1.6.25
./configure --prefix=/usr/local/libpng --enable-shared
make
make install
  1. freetype 설치
cd /usr/local/src
tar zxvf freetype-2.7.tar.gz
cd freetype-2.7
./configure --prefix=/usr/local/freetype --enable-shared
make # 컴파일
make install # 설치
  1. jpeg 설치
cd /usr/local/src
tar zxvf jpegsrc.v9b.tar.gz
cd jpeg-9b
./configure --prefix=/usr/local/jpeg --enable-shared
make # 컴파일
make install # 설치
  1. libgd 설치
cd /usr/local/src
tar zxvf libgd-2.1.1.tar.gz # 압축 해제
cd libgd-2.1.1 # 디렉토리 이동
./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/ --with-tiff=/usr/local/tiff  --with-vpx=/usr/local/libvpx/ # 설정
make # 컴파일
make install # 설치
참고: libgd 컴파일 실패 시, 먼저 건너뛰고 시스템 기본 2.1.0 버전을 사용하며, PHP 컴파일 시 --with-gd=/usr/local/libgd 매개변수를 --with-gd로 변경
  1. t1lib 설치
cd /usr/local/src
tar zxvf t1lib-5.1.2.tar.gz
cd t1lib-5.1.2
./configure --prefix=/usr/local/t1lib --enable-shared
make without_doc
make install
  1. PHP 설치

참고: 시스템이 64비트인 경우 아래 두 명령을 실행해야 합니다(그렇지 않으면 PHP 설치 오류 발생). 32비트 시스템은 실행할 필요가 없습니다.

\cp -frp /usr/lib64/libltdl.so* /usr/lib/
\cp -frp /usr/lib64/libXpm.so* /usr/lib/
cd /usr/local/src
tar -zvxf php-5.6.26.tar.gz
cd php-5.6.26
export LD_LIBRARY_PATH=/usr/local/libgd/lib
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd=/usr/local/libgd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/lib64 --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype  # 설정
make # 컴파일
make install # 설치

컴파일 오류 발생 시, 정리 후 다시 컴파일할 수 있습니다.

make clean # 정리
make  # 컴파일
cp php.ini-production /usr/local/php/etc/php.ini # PHP 설정 파일을 설치 디렉토리로 복사
rm -rf /etc/php.ini # 시스템 기본 설정 파일 삭제
ln -s /usr/local/php/etc/php.ini /etc/php.ini # /etc 디렉토리에 심볼릭 링크 추가
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf # 템플릿 파일을 PHP-FPM 설정 파일로 복사
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf # /etc 디렉토리에 심볼릭 링크 추가
vi /usr/local/php/etc/php-fpm.conf # 편집
user = www # PHP-FPM 실행 계정을 www로 설정
group = www # PHP-FPM 실행 그룹을 www로 설정
pid = run/php-fpm.pid # 앞에 있는 세미콜론 제거

PHP-FPM 부팅 시 자동 실행 설정

cp /usr/local/src/php-5.6.26/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm # PHP-FPM을 시작 디렉토리로 복사
chmod +x /etc/rc.d/init.d/php-fpm # 실행 권한 부여
chkconfig php-fpm on # 부팅 시 자동 실행 설정
vi /usr/local/php/etc/php.ini # 설정 파일 편집
찾기: disable_functions =
수정: disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname

# PHP에서 비활성화할 함수 목록. 특정 프로그램에서 이 함수가 필요한 경우 삭제하여 비활성화 해제할 수 있습니다.

찾기: ;date.timezone =
수정: date.timezone = PRC # 시간대 설정
찾기: expose_php = On
수정: expose_php = Off # PHP 버전 정보 표시 금지
찾기: short_open_tag = Off
수정: short_open_tag = ON # PHP 짧은 태그 지원
찾기opcache.enable=0
수정opcache.enable=1 # PHP opcode 캐시 지원
찾기: ;opcache.enable_cli=1 # PHP opcode 캐시 지원
수정: opcache.enable_cli=0
마지막 줄에 추가: zend_extension=opcache.so # opcode 캐시 기능 활성화

Nginx에서 PHP 지원 설정

vi /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf 설정 파일을 수정해야 합니다.
user www www; # 첫 번째 줄의 주석을 제거하고 Nginx 실행 그룹을 www www로 변경합니다. /usr/local/php/etc/php-fpm.conf의 user, group 설정과 동일해야 하며, 그렇지 않으면 PHP 실행 오류가 발생합니다.
index index.html index.htm index.php; # index.php 추가
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# FastCGI server 부분의 location 주석을 해제합니다. fastcgi_param 행의 매개변수를 $document_root$fastcgi_script_name으로 변경하거나 절대 경로를 사용합니다.

/etc/init.d/nginx restart # Nginx 재시작
service php-fpm start # PHP-FPM 시작

테스트:

cd /usr/local/nginx/html/ # Nginx 기본 웹사이트 루트 디렉토리로 이동
rm -rf /usr/local/nginx/html/* # 기본 테스트 페이지 삭제
vi index.php # 새 index.php 파일 생성
<?php
phpinfo();
?>
chown www.www /usr/local/nginx/html/ -R # 디렉토리 소유자 설정
chmod 700 /usr/local/nginx/html/ -R # 디렉토리 권한 설정

태그: LNMP nginx MySQL PHP CentOS

7월 15일 21:30에 게시됨