메뉴 닫기

[CentOS 6.x] Nginx mysql php 설치 및 연동

안녕하세요.

기술지원팀 박용휘 입니다. 

이번 포스팅 에서는 NGINX 와 Mysql php 설치 및 연동하는 방법을 포스팅 하도록 하겠습니다.

APM의 경우 많이 사용하는 방법이라 익숙하시겠지만 NGINX의 경우 사용률이 증가하는 추세 이긴 하나 아직 APM의 비해 적은 듯 합니다.

 

설치 방법은 APM 과 많이 다르지 않습니다.

[설치환경]

CentOs 6.8
NGINX 1.8
PHP 5.6
MYSQL 5.6

 

1. 필수 라이브러리 설치

[root@~]# yum -y install gcc* make libtool-ltdl-devel openssl-devel pcre-devel ncurses-devel libxml2-devel
bzip2-devel curl-devel gdbm-devel libjpeg-devel libpng-devel freetype-devel imap-devel libc-client-
devel krb5-devel libmcrypt libmcrypt-devel libmhash-devel flex icu libicu libicu-devel gd gd-devel

 

2. 설치 파일 다운로드
[ 아래 파일들을 각페이지 에서   다운로드 받습니다. ]

[root@~]# cd /usr/local/src
[root@~]#wget nginx-1.71.tar.gz
[root@~]#wget libmcrypt-2.5.8.tar.gz
[root@~]#wget php-5.6.14.tar.gz
[root@~]#wget mhash-0.9.9.9.tar.gz
[root@~]#wget libiconv-1.14.tar.gz
[root@~]#wget mysql-5.6.12.tar.gz

3. NGINX 설치

[root@~]# tar zxvf nginx-1.7.10.tar.gz
[root@~]# cd nginx-1.7.10
[root@~]# ./configure –prefix=/usr/local/nginx \
> –conf-path=/usr/local/nginx/conf/nginx.conf \
> –sbin-path=/usr/local/nginx/sbin/nginx \
> –lock-path=/usr/local/nginx/nginx.lock \
> –pid-path=/usr/local/nginx/nginx.pid \
> –http-client-body-temp-path=/usr/local/nginx/tmp/body \
> –http-proxy-temp-path=/usr/local/nginx/tmp/proxy \
> –http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi \
> –http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi \
> –http-scgi-temp-path=/usr/local/nginx/tmp/scgi \
> –http-log-path=/usr/local/nginx/logs/access.log \
> –error-log-path=/usr/local/nginx/logs/error.log \
> –with-http_addition_module \
> –with-http_degradation_module \
> –with-http_flv_module \
> –with-http_gzip_static_module \
> –with-http_image_filter_module \
> –with-http_mp4_module \
> –with-http_random_index_module \
> –with-http_ssl_module \
> –with-http_stub_status_module \
> –with-http_sub_module \
> –with-http_realip_module \
> –with-http_xslt_module \
> –with-http_dav_module \
> –with-http_auth_request_module \
> –with-http_spdy_module \
> –user=nobody \
> –group=nobod
[root@~]# make && make install

 

4. NGINX 실행스크립 작성
nginx 처음 설치 후 정상적으로 실행이 안될 수 있으므로 vi 편집기를 이용하여 실행파일 스크립트를 작성 합니다.
vi /etc/init.d/nginx

#!/bin/sh
#
# nginx – this script starts and stops the nginx daemin
#
# chkconfig: – 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /var/run/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”
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
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

 

 

5. NGINX 실행 및 확인

[root@~]# /etc/init.d/nginx start
nginx (을)를 시작 중:
[ OK ]
[root@~]# netstat -nlpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q    Local Address    Foreign Address  State       PID/Program name
tcp         0                       0 0.0.0.0:80         0.0.0.0:*             LISTEN   14446/nginx

 

 

6. MySQl
유저 및 그룹 생성

[root@~]# groupadd mysql
[root@~]# useradd -g mysql mysql

CMAKE 설치
(mysql 5.5.x ver 이상 부터는 cmake 를 이용하여 설치)

[root@~]# cd /usr/local/src
[root@~]# tar zxvfp ./cmake-2.8.5.tar.gz
[root@~]# cd ./cmake-2.8.5
[root@~]#./bootstrap
[root@~]#make
[root@~]#make install

MYSQL 컴파일

[root@~]# cd /usr/local/src
[root@~]# tar zxvfp ./mysql-5.6.12.tar.gz
[root@~]# cd ./mysql-5.6.12
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DENABLE_DOWNLOADS=1
[root@~]# make
[root@~]# make install

MYSQL 환경설정
(support-files 에서 “my.cnf” 파일을 복사후 “mysql_install_db” 실행 합니다.)

[root@~]# cp -arp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
[root@~]#/usr/local/mysql/scripts/mysql_install_db \
–defaults-file=/etc/my.cnf \
–user=mysql \
–basedir=/usr/local/mysql/ \
–datadir=/usr/local/mysql/data/
[root@~]# cp -arpf /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

권한변경 및 환경변수 등록

[root@~]#chown -R mysql:mysql /usr/local/mysql/
[root@~]#chmod 700 /etc/init.d/mysqld
[root@~]#chmod 711 /usr/local/mysql
[root@~]#chmod 700 /usr/local/mysql/data
[root@~]#chmod 751 /usr/local/mysql/bin
[root@~]#chmod 750 /usr/local/mysql/bin/*
[root@~]#chmod 755 /usr/local/mysql/bin/mysql
[root@~]#chmod 755 /usr/local/mysql/bin/mysqldump
[root@~]#echo ‘export PATH=$PATH:/usr/local/mysql/bin’ >> /etc/profile
[root@~]#source /etc/profile

 

[root@~]#/usr/local/mysql/bin/mysqladmin -u root password
New password: 패스워드
Confirm new password: 패스워드
[root@~]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@~]# netstat -nlpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address     Foreign Address   State      PID/Program name
tcp          0               0 0.0.0.0:80                   0.0.0.0:*          LISTEN     14446/nginx
tcp          0               0 :::3306                        :::*                   LISTEN     18825/mysqld

7. PHP 설치

Mhash 설치
[root@~]# cd /usr/local/src
[root@~]# tar zxvf mhash-0.9.9.9.tar.gz
[root@~]# cd mhash-0.9.9.9
[root@~]#./configure
[root@~]# make && make install
[root@~]# cd /usr/local/src
[root@~]# tar zxvf libiconv-1.14.tar.gz
[root@~]# cd libiconv-1.14
[root@~]#./configure
[root@~]# make && make install
[root@~]# cd /usr/local/src
[root@~]# tar zxvf libmcrypt-2.5.8.tar.gz
[root@~]# cd libmcrypt-2.5.8
[root@~]#./configure
[root@~]# make && make install

라이브러리 링크 추가

[root@~]# ln -s /usr/local/mysql/lib/ /usr/local/mysql/lib64
[root@~]# ln -s /usr/local/lib/libmcrypt.so.4 /usr/local/lib64/libmcrypt.so.4
[root@~]# ln -sf /usr/lib64/libldap.so /usr/lib/libldap.so

PHP 컴파일
[root@~]# cd /usr/local/src
[root@~]# tar zxvf php-5.6.14.tar.gz
[root@~]# cd php-5.6.14
[root@~]# ./configure \
–prefix=/usr/local/php \
–with-config-file-path=/usr/local/php/etc \
–disable-debug \
–enable-fpm \
–enable-bcmath \
–enable-exif \
–enable-ftp \
–enable-gd-native-ttf \
–enable-inline-optimization \
–enable-intl \
–enable-mbregex \
–enable-mbstring \
–enable-mod-charset \
–enable-sigchild \
–enable-soap \
–enable-sockets \
–enable-sysvsem=yes \
–enable-sysvshm=yes \
–enable-xml \
–enable-zip \
–with-bz2 \
–with-iconv=/usr/local/lib \
–with-curl \
–with-zlib \
–with-gd \
–with-gettext \
–with-mcrypt \
–with-mhash \
–with-mysql=/usr/local/mysql \
–with-mysqli \
–with-openssl \
–with-xmlrpc \
–with-freetype-dir=/usr/lib64 \
–with-jpeg-dir=/usr/lib64 \
–with-libxml-dir=/usr/lib64 \
–with-png-dir=/usr/lib64 \
–with-zlib-dir=/usr/lib64 \
–with-fpm-user=nobody \
–with-fpm-group=nobody \
–with-ldap
make
make install
[root@~]# /usr/local/php/bin/pear install PHP_Archive-0.11.4
[root@~]# cp /usr/local/src/php-5.6.14/php.ini-development /usr/local/nginx/conf/php.ini

 

 NGINX PHP 연동

1) NGINX 의 경우 php-fpm 이 설치 되어 있어야 연동이 가능 합니다 .
Php-fpm PHP FastCGI Process Manager 의 약자
[root@~]# cd /usr/local/php/etc/
[root@~]# cp -arp php-fpm.conf.default ./php-fpm.conf
[root@~]# /usr/local/php/sbin/php-fpm → php-fpm 실행

nginx 와 php-fpm 이 연동 할 수 있도록 nginx.conf 파일을 수정 합니다.
[root@~]# vi /usr/local/nginx/conf/nginx.conf
#아래 내용을 주석 해제 또는 conf 파일 중간에 삽입 합니다.#
location ~ \.php$ {
root
/usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include
fastcgi_params;
}
3) phpinfo 페이지를 확인 할 수 있도록 php 파일을 html 폴더에 작성 합니다.
[root@~]#vi /usr/local/nginx/html/phpinfo.php
<?php
phpinfo();
?>
[root@~]# netstat -nlpt
Active Internet connections (only servers)
Proto      Recv-Q Send-Q        Local Address    Foreign Address     State     PID/Program name
tcp                0                          0 0.0.0.0:80              0.0.0.0:*        LISTEN    14446/nginx
tcp                0                          0 :::3306                    :::*                LISTEN    18825/mysqld

 

NGIXN

 

PHP

 

이상 NGINX 와 MYSQL PHP 연동 하는 포스팅 이였습니다.

 

감사합니다.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x