본문 바로가기
아이티 공부/리눅스

nginx (엔진X) 리눅스에 설치하는 방법

by Klero 2017. 2. 25.

Nginx (엔진X) 란?

 

Nginx (엔진X 라고 부름) 웹 서버 소프트웨어 중 하나로, 가볍고 높은 성능 이라는 장점을 가지고 있다.

다만 확장 모듈이 Apache httpd 에 비해 부족하긴 한데, 확장 모듈이 엄청 많고 제대로 사용 하는 사람도 드물다.

그래서 호환성 문제가 없다면 Nginx 로 갈아타는 추세이기도 하다. Apache httpd 에서 많이 사용하는 필수 모듈은

어차피 Nginx 에서도 존재하고 사용할 수 있다. 그리고 Nginx의 일부 모듈은 Apache httpd 보다 훨씬 빠르다고 한다.

 

즉 Nginx는 Apache httpd 를 대체할 대안으로 급 떠오르는 가벼우며 높은 성능을 보여주는 웹 서버이다.




 

*테스트 환경 : RHEL 6.5 64Bit 에 Nginx-1.9.12 버전 설치

nginx를 다운로드 받을 수 있는 공식 홈페이지 : http://nginx.org/en/download.html

 

1. Mainline Version인 nginx-1.9.12 다운로드

- 공식 홈페이지에서 다운로드 후 리눅스 서버에 파일을 옮겨도 되고 아래의 명령어를 사용하여도 다운로드 받을 수 있다.

# wget http://nginx.org/download/nginx-1.9.12.tar.gz

 

 

2. Nginx 설치를 위해 의존 패키지들을 설치한다.

# yum -y install gcc g++ cpp gcc-c++ pcre-devel openssl openssl-devel gd gd-devel

 

 

3. 압축 해제

# tar xvf nginx-1.9.12.tar.gz
# cd nginx-1.9.12


 

4. 컴파일 하기 전 옵션을 설정한다. (Nginx 공식 홈페이지에 옵션 내용들이 있다.)

# ./configure --prefix=/opt/nginx-1.9.12 \
--conf-path=/opt/nginx-1.9.12/conf/nginx.conf \
--sbin-path=/opt/nginx-1.9.12/sbin/nginx \
--lock-path=/var/lock/nginx.lock \
--pid-path=/var/run/nginx.pid \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-log-path=/opt/nginx-1.9.12/log/access.log \
--error-log-path=/opt/nginx-1.9.12/log/error.log \
--with-http_addition_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_realip_module \
--user=nobody \
--group=nobody

 

 

5. 컴파일 및 설치를 진행한다.

# make && make install

 

 

6. Nginx 실행에 필요한 디렉토리를 생성한다.

# mkdir -p /var/lib/nginx


7. Nginx를 서비스로 실행할 수 있도록 서비스 파일을 편집한다.

# vi /etc/init.d/nginx
# chmod 755 /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:      /opt/nginx-1.9.12/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="/opt/nginx-1.9.12/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx-1.9.12/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

 


8. 서버 재시작 후에도 자동으로 실행되도록 설정한다.

# chkconfig nginx on
# chkconfig --list nginx

 

 

9. 방화벽을 설정한다
# vi /etc/sysconfig/iptables

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

 

# service iptables restart
# service iptables save



10. Nginx를 실행해보자

# service nginx start
# service nginx status

- 파이어폭스를 실행하고 주소창에 http://서버IP를 입력하여 정상적으로 Nginx가 실행되는지 확인한다.


 

 

 

 

조금이나마 도움이 되셨다면 밑에 공감 한 번 클릭해주시면 감사하겠습니다.

반응형

댓글