본문 바로가기

CentOS

Nginx 설치하기

Let's Encrypt에서 SSL 인증서를 발급받으려니 HTTP Server가 필요해졌다. 아파치의 HTTP Server를 설치하려다 보다 가볍다는 NginX를 설치를 해보기로 했다.

 

패키지 매니저엔 1.14.1 버전이 올라와 있다. 현재 최신버전은 1.17.9 버전이고 Stable 버전은 1.16.1 버전이다. 1.16.1 버전을 직접 컴파일 해보자. 웹 서버는 워낙 공격이 많이 들어오는 곳이다보니 Stable한 최신버전을 쓰는것이 좋다고 생각한다.

 

https://nginx.org/en/download.html

 

nginx: download

 

nginx.org

 

우선 적당한 경로에 최신 버전을 다운로드 하고 압축을 풀어준다.

# cd /home/build
# curl https://nginx.org/download/nginx-1.17.9.tar.gz > nginx-1.17.9.tar.gz
# tar -zxvf nginx-1.17.9.tar.gz

 

컴파일 전 필요한 라이브러리를 설치하자. 필요한 라이브러리에 대한 소개는 아래 페이지에 잘 나와있다.

https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/#installing-nginx-dependencies

 

NGINX Docs | Installing NGINX Open Source

Install NGINX Open Source either as a prebuilt package or from source, following step-by-step instructions for all supported Linux distributions.

docs.nginx.com

필요한 라이브러리 중 PCRE만 미설치 상태였다. 나머지 두 라이브러리(zlib, OpenSSL)은 버전까지 일치했기 때문에 PCRE만 설치한다. (저 문서에 있는 ftp 경로에서 다운로드가 안되어서 pcre 공식페이지의 링크를 가져왔다.)

# cd /home/build
# curl https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz > pcre-8.43.tar.gz
# tar -zxf pcre-8.43.tar.gz
# cd pcre-8.43
# ./configure
# make && make install

 

...nginx를 설치하다보니 libz.a 를 찾지 못하는 문제가 생겼다. zlib도 얌전히 설치하자.

# cd /home/build
# curl https://www.zlib.net/zlib-1.2.11.tar.gz > zlib-1.2.11.tar.gz
# tar -zxvf zlib-1.2.11.tar.gz
# cd zlib-1.2.11
# ./configure
# make && make install

 

당황스럽게도 configure 스크립트에선 각 옵션에 대한 기본값을 보여주질 않는다. 공식 문서에서 확인해보자. 덤으로 필요한 모듈들도 정리해서 설정하자. 준비가 완료되었다면 컴파일 한다.

# ../configure --conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/ngi nx/access.log \
--with-pcre=../pcre-8.43 --with-pcre-jit \
--with-zlib=../zlib-1.2.11 \
--with-http_ssl_module --with-stream

 

서비스에 등록하자. 해당 스크립트는 NginX 공식 홈페이지에서 가져왔다.

https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/

 

NGINX Init Scripts | NGINX

Have a 🍪? :) nginx.com uses cookies to provide functionality and performance. Privacy Policy.

www.nginx.com

# vi /etc/rc.d/init.d/nginx
#
# 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
# 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="/etc/nginx/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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 $prog -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 +x ./nginx

서비스를 리로딩 해준다.

# systemctl daemon-reload

 

이제 실행이 되는지 확인해보자.

# systemctl start nginx

 

방화벽을 열어주고

# firewall-cmd --permanent --add-service http

 

웹브라우저로 Welcome 화면이 뜨는지 체크해보면 끝!

http://xxx.xxx.xxx.xxx 으로 접속하면 Welcome 화면이 반겨준다.

 

 

 

 

 

 

'CentOS' 카테고리의 다른 글

Ruby 설치하기  (0) 2020.03.08
SSL 무료 인증서 발급하기  (0) 2020.03.06
RRDtool 설치하기  (0) 2020.03.01
MariaDB 튜닝하기  (0) 2020.03.01
Redis Server 설치하기  (0) 2020.02.29