Nginx 설치와 기본 환경 구성
Nginx를 실제로 사용하려면 운영 환경에 맞게 설치하고 서비스를 관리하는 방법을 숙지해야 합니다. 이 장에서는 Ubuntu·CentOS·macOS·Docker 환경별 설치 방법과 디렉터리 구조, 프로세스 관리 명령을 다룹니다.
Ubuntu / Debian 계열 설치
패키지 관리자(apt)로 설치
# 패키지 목록 업데이트
sudo apt update
# Nginx 설치
sudo apt install -y nginx
# 서비스 시작
sudo systemctl start nginx
# 부팅 시 자동 시작 등록
sudo systemctl enable nginx
# 상태 확인
sudo systemctl status nginx
설치 후 브라우저에서 http://서버IP 로 접속하면 Nginx 기본 환영 페이지가 표시됩니다.
최신 안정 버전 설치 (공식 저장소)
Ubuntu 기본 저장소의 Nginx는 버전이 오래된 경우가 있습니다. 최신 안정(stable) 버전을 설치하려면 공식 Nginx 저장소를 추가합니다.
# 필요 패키지 설치
sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring
# Nginx 공식 서명 키 추가
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
# 안정 버전 저장소 추가
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
sudo apt update
sudo apt install -y nginx
# 버전 확인
nginx -v
CentOS / RHEL / Rocky Linux 계열 설치
# EPEL 저장소 추가 (CentOS 7/8)
sudo yum install -y epel-release
# Nginx 설치
sudo yum install -y nginx
# 서비스 시작 및 자동 시작 등록
sudo systemctl start nginx
sudo systemctl enable nginx
# 방화벽에서 HTTP/HTTPS 허용
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
CentOS 8+ / Rocky Linux (dnf)
# Nginx 공식 저장소 추가
sudo dnf install -y https://nginx.org/packages/rhel/8/x86_64/RPMS/nginx-$(curl -s https://nginx.org/packages/rhel/8/x86_64/RPMS/ | grep -o 'nginx-[0-9.]*-[0-9.]*' | head -1).el8.ngx.x86_64.rpm
# 또는 단순하게
sudo dnf install -y nginx
sudo systemctl start nginx && sudo systemctl enable nginx
macOS 설치 (Homebrew)
# Homebrew가 설치되어 있다면
brew install nginx
# 서비스 시작 (포트 8080 기본, 80은 root 권한 필요)
brew services start nginx
# 또는 직접 실행
nginx
# 설정 파일 위치
# /opt/homebrew/etc/nginx/nginx.conf (Apple Silicon)
# /usr/local/etc/nginx/nginx.conf (Intel)
Docker로 Nginx 실행
개발 환경이나 컨테이너 기반 운영 환경에서는 Docker를 활용합니다.
# 최신 안정 버전 실행 (포트 80 매핑)
docker run -d --name nginx -p 80:80 nginx
# 설정 파일과 웹 루트를 호스트 볼륨으로 마운트
docker run -d \
--name nginx \
-p 80:80 \
-v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro \
-v $(pwd)/html:/usr/share/nginx/html:ro \
nginx
# 실행 중인 컨테이너에서 설정 리로드
docker exec nginx nginx -s reload
docker-compose.yml 예시:
version: '3.8'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./nginx/ssl:/etc/nginx/ssl:ro
- ./html:/usr/share/nginx/html:ro
restart: unless-stopped
Nginx 디렉터리 구조
Nginx 설치 후 파일과 디렉터리가 어디에 있는지 알아야 설정을 올바르게 수정할 수 있습니다.
Ubuntu/Debian 기준
/etc/nginx/ ← 설정 파일 루트
nginx.conf ← 메인 설정 파일
conf.d/ ← 추가 설정 파일 디렉터리 (*.conf 자동 로딩)
default.conf ← 기본 서버 블록
sites-available/ ← 가상 호스트 설정 저장 (비활성 포함)
sites-enabled/ ← 실제 활성화된 가상 호스트 (심볼릭 링크)
snippets/ ← 재사용 가능한 설정 조각
mime.types ← MIME 타입 매핑
fastcgi_params ← FastCGI 파라미터
proxy_params ← 프록시 기본 파라미터
/var/log/nginx/ ← 로그 디렉터리
access.log ← 접근 로그
error.log ← 에러 로그
/var/www/html/ ← 기본 웹 루트 (index.nginx-debian.html)
/usr/share/nginx/html/ ← 또는 이 경로 (배포판마다 다름)
/var/run/nginx.pid ← Nginx 마스터 프로세스 PID 파일
/usr/sbin/nginx ← Nginx 실행 파일
CentOS/RHEL 기준
/etc/nginx/
nginx.conf
conf.d/
default.conf
/var/log/nginx/
/usr/share/nginx/html/ ← 기본 웹 루트
Nginx 프로세스 구조
Nginx는 마스터 프로세스 와 워커 프로세스 로 구성됩니다.
[마스터 프로세스] (root 권한)
│ 설정 파일 읽기, 워커 관리, 시그널 수신
│
├─ [워커 프로세스 1] (nobody 권한)
├─ [워커 프로세스 2] ← 실제 HTTP 요청 처리
└─ [워커 프로세스 N] ← CPU 코어 수만큼 생성
# 프로세스 확인
ps aux | grep nginx
# 출력 예시
root 1234 0.0 0.0 nginx: master process /usr/sbin/nginx
www-data 1235 0.0 0.1 nginx: worker process
www-data 1236 0.0 0.1 nginx: worker process
Nginx 서비스 관리 명령
systemctl 명령 (Linux)
sudo systemctl start nginx # 시작
sudo systemctl stop nginx # 중지
sudo systemctl restart nginx # 재시작 (연결 끊김 발생)
sudo systemctl reload nginx # 설정 리로드 (무중단, 권장)
sudo systemctl status nginx # 상태 확인
sudo systemctl enable nginx # 부팅 시 자동 시작 등록
sudo systemctl disable nginx # 자동 시작 해제
nginx 시그널 명령
nginx -s stop # 즉시 종료 (SIGTERM)
nginx -s quit # 처리 중인 요청 완료 후 종료 (SIGQUIT)
nginx -s reload # 설정 무중단 리로드 (SIGHUP)
nginx -s reopen # 로그 파일 다시 열기 (SIGUSR1)
nginx -t # 설정 파일 문법 검증 (테스트만, 적용 안 함)
nginx -T # 설정 파일 검증 + 전체 내용 출력
nginx -v # 버전 확인
nginx -V # 버전 + 컴파일 옵션 확인
핵심: 운영 중 설정 변경 시
restart대신reload를 사용하세요.restart는 순간적으로 모든 연결을 끊지만,reload는 기존 워커 프로세스가 요청을 마저 처리한 후 새 설정을 반영한 워커로 교체합니다.
설치 확인 및 테스트
# Nginx 버전 및 컴파일 옵션 확인
nginx -V
# 출력 예시
nginx version: nginx/1.24.0
built with OpenSSL 3.0.2 15 Mar 2022
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx ...
# 설정 파일 문법 검사
sudo nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
# 80 포트 리슨 확인
sudo ss -tlnp | grep :80
# 또는
sudo netstat -tlnp | grep nginx
정리
- Ubuntu:
apt install nginx, CentOS:yum/dnf install nginx - Docker: 볼륨 마운트로 설정 파일·웹 루트 분리
- 핵심 디렉터리:
/etc/nginx/(설정),/var/log/nginx/(로그),/var/www/html/(웹 루트) - 설정 변경 후
nginx -t로 문법 검증 →systemctl reload nginx로 무중단 적용