Linux/CentOS

CentOS 7 에 Nginx Server 1.8 설치하기

후뤼한잉여 2015. 11. 11. 11:31

CentOS 7 에 Nginx Server 1.8 설치하기

1) 개요

Nginx Server 설치 한 내용을 정리하고자 작성되었습니다.

2) 설치방법

  1. 의존패키지 설치하기
    yum -y install gcc g++ cpp gcc-c++ pcre-devel openssl openssl-devel gd gd-devel wget net-tools
    
  2. Nginx 설치파일 다운로드
    wget http://nginx.org/download/nginx-1.8.0.tar.gz
    tar xvfz [설치파일].tar.gz
    
  3. Nginx 설치하기

    cd [설치파일 압축 해제된 디렉토리]
    

    3-1. Configure 설정하기 (prefix등 변경하고 설치했다가 무슨 이유인지 잘 안되서 그냥 저 경로로 설치함..)

    sudo ./configure --prefix=/opt/nginx \
    --conf-path=/opt/nginx/conf/nginx.conf \
    --sbin-path=/opt/nginx/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/log/access.log \
    --error-log-path=/opt/nginx/log/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_realip_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_sub_module \
    --with-http_realip_module \
    --user=nginx \
    --group=nginx
    

    3-2. 컴파일 및 설치

    sudo make
    sudo make install
    

    3-3. 컴파일시 생성안되는 디렉토리 추가 생성

    sudo mkdir -p /var/lib/nginx
    

    3-4. 서비스에 등록하기

    sudo vi /etc/init.d/nginx
    

    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/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/sbin/nginx"
    prog=$(basename $nginx)
    
    NGINX_CONF_FILE="/opt/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
    
  4. 서비스 파일에 실행권한 부여하기

    sudo chmod +x /etc/init.d/nginx
    
  5. 방화벽 설정하기
    firewall-cmd --add-service=http --permanent
    firewall-cmd --reload
    
  • Nginx 실행 / 종료 명령어
    • 실행 명령어
      sudo service nginx start
      
    • 종료 명령어
      sudo service nginx stop
      
    • 재시작 명령어
      sudo service nginx restart