리눅스

리눅스 CentOS에 Nginx 설치하는 방법

귀염둥이채원 2018. 12. 8. 11:21
1. 다운로드

http://nginx.org/en/download.html 에서 최신 nginx 다운로드 파일을 다운로드 받는다.
사이트에 접근해서 보면 아래와 같이 노출된다.
Mainline version (가장 최신버전)
Stable version (안정화된 버전)
Legacy versions (오래된 버전들...)
나는 여기 버전 중에서 가장 최신 버전으로 설치를 진행했다.

nginx 설치전 의존성 파일을 설치해준다. sudo yum install -y gcc pcre-* zlib* wget "http://nginx.org/download/nginx-1.15.2.tar.gz" tar -xvzf nginx-1.15.2.tar.gz


2. configure 
설치는 irteamsu 권한으로 접근하여 진행하도록 한다.

sudo ./configure --prefix=/home1/irteam/apps/nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module
--without-http_rewrite_module 옵션을 지정하고 make install시에 위와 같은 오류가 발생된다면, yum install pcre pcre-devel libpcre3 libpcre3-dev 을 설치한 다음에 make install을 하자.

configure 환경설정 옵션 정보 가이드가 필요할 경우 아래와 같이 입력하여 상세 내용을 확인할 수 있습니다.

./configure --help

주요 옵션 설명
--prefix : Nginx 가 설치되는 기본 디렉토리 (/usr/local/nginx)
--user: Nginx 사용자 (nginx), 지정하지 않으면 nobody
--group: Nginx 그룹 (nginx), 지정하지 않으면 nobody

--sbin-path : 엔진엑스의 바이너리 파일이 설치되는 경로 (/sbin)
--conf-path : 주 환경 설정 파일의 경로 (/conf/nginx.conf)
--error-log-path : 에러 로그 파일 (/log/error.log)
--http-log-path : http 접근 로그의 위치 (/log/access.log)
--pid-path : 엔진엑스 pid 파일 경로 (/run/nginx.pid)
--lock-path : Nginx lock 경로 (/run/nginx.lock)
--with-per_modules_path : 펄 모듈의 경로
--http-client-body-temp-path : 클라이언트 요청에 의한 임시 파일의 저장에 사용되는 디렉토리 (/cache/client_body_temp)
--http-proxy-temp-path: 프록시가 사용하는 임시 파일 위치 (/cache/proxy-temp)
--http-fastcgi-temp-path: (/cache/fastcgi-temp)
--http-uwsgi-temp-path: (/cache/uwsgi-temp)
--http-scgi-temp-path: (/cache/scgi-temp)

3. make install

컴파일시에 오류가 발생하지 않았다면. 설치를 하자.

sudo make install
cp: `conf/koi-win' and `/home1/irteam/apps/nginx/conf/koi-win' are the same file

make install시에 위와 같은 오류가 발생된다면, 컴파일 위치와 설치 위치가 중복되어서 발생된 오류 이므로, 컴파일 위치를 변경한 후에 설치위치를 --prefix로 지정하여 오류가 발생하지 않도록 적용하면 된다.

irteamsu에서 설치한 후에는 아래와 같이 파일 소유권 부여 관련 설정도 실행해줍니다. 

cd /home/irteam/nginx설치경로 chown -R irteam.irteam nginx설치경로 cd nginx설치경로/sbin/ chown root.irteam nginx chmod u+s nginx

nginx 설치 경로에 logs 파일이 생성되어 있지 않다면, 생성해놓는다.
위와 같은 작업을 완료한 후에 nginx를 가동하도록 하자.

cd /home/irteam/nginx설치경로/sbin ./nginx ps -ef | grep nginx root 799 1 0 13:44 ? 00:00:00 /sbin/dhclient -H test-nginx-test001-ncl -1 -q -cf /etc/dhcp/dhclient-eth0.conf -lf /var/lib/dhclient/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid eth0 root 11720 9021 0 14:36 pts/1 00:00:00 sudo chown root:irteam nginx root 80552 1 0 23:39 ? 00:00:00 nginx: master process ./nginx nobody 80553 80552 0 23:39 ? 00:00:00 nginx: worker process irteam 80556 9021 0 23:39 pts/1 00:00:00 grep nginx

상태 체크를 해보고 싶거든 conf/nginx.conf 파일내에 아래 내용을 추가한 후 restart 하여 curl 명령어로 access를 날려보자.

conf/nginx.conf  파일내에 아래 내용을 추가한다.

#status add location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; }

위의 내용 추가 후에  curl 명령어로 access를 날려보자.

curl http://127.0.0.1:80/nginx_status Active connections: 1 server accepts handled requests 1 1 1 Reading: 0 Writing: 1 Waiting: 0
nginx 실행방법은 아래와 같습니다. nginx start : /nginx설치경로/sbin/nginx nginx reload : /nginx설치경로/sbin/nginx -s reload nginx stop : /nginx설치경로/sbin/nginx -s stop