글 목록으로 돌아가기

Linux

웹 서버

Linux Apache, nginx 웹 서버 설치 및 설정 정리

Dohyeon Kim
Dohyeon Kim 2026년 7월 28일 · 2분 읽기
Linux AutoEverSW

1) Apache Web Server


개요

  • 가장 오래되고 검증된 웹 서버
  • 프로세스/스레드 기반 구조 사용
  • .htaccess 파일을 이용해 디렉토리별로 설정을 세밀하게 제어 가능
  • PHP 기반 웹 사이트나 복잡한 모듈 설정이 필요한 환경에 적합
  • 수많은 모듈이 존재하여 확장성이 뛰어나고 관련 자료가 매우 풍부

설치

sudo apt install -y apache2

# 서비스 확인
sudo systemctl status apache2

접속

  • http://localhost
  • Apache Web Server의 기본 디렉토리: /var/www/html

사용자 계정에 웹 설정

# 설정 파일 수정
sudo nano /etc/apache2/mods-available/userdir.conf
  • #UserDir disabled root → 주석 해제
# 심볼릭 링크 설정
sudo ln -s /etc/apache2/mods-available/userdir.conf /etc/apache2/mods-enabled/userdir.conf
sudo ln -s /etc/apache2/mods-available/userdir.load /etc/apache2/mods-enabled/userdir.load

# 서비스 재시작
sudo systemctl stop apache2
sudo systemctl start apache2

# 사용자 홈 디렉토리에 public_html 생성
mkdir ~/public_html

# 디렉토리 접근 권한 변경
chmod 701 .
chmod 701 public_html
  • public_html 디렉토리에 index.html 파일을 생성하고 작성

웹 서버 설정 파일

  • /etc/apache2/apache2.conf

2) nginx


개요

  • 가볍고 빨라서 동시에 많은 접속자를 처리하는데 특화된 도구
  • 비동기 이벤트 기반 구조

주요 역할

  • 정적 웹 서버: 메모리 사용량이 적어 Apache보다 많이 사용
  • 리버스 프록시: 클라이언트와 백엔드 서버 사이의 중개자
    • 실제 서버 IP 숨김 → 보안 우수
    • 자주 요청되는 데이터를 미리 저장(캐싱) → 서버 부하 감소
  • 로드밸런서: 여러 대의 서버에 요청을 분산

Apache와 차이점

  Apache Nginx
작동 방식 스레드/프로세스 기반 비동기 이벤트 기반
성능 대규모 동시 접속 시 부담 대규모 동시 접속에 매우 강함
유연성 다양한 모듈 제공 가볍고 성능 중심적

설치

sudo apt install -y nginx
sudo systemctl status nginx
  • 80번 포트를 기본 포트로 사용
curl http://localhost

정적 웹 사이트 게시

# 디렉토리 생성
sudo mkdir -p /var/www/example.com/html

# 소유권 변경
sudo chown -R $USER:$USER /var/www/example.com/html
  • 생성한 디렉토리에 HTML 파일 작성 (설정을 변경하지 않으려면 index.html로 작성)

설정 파일 수정

sudo nano /etc/nginx/sites-available/default
server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/html;   # 이 부분 수정
    index index.html;                  # welcome 파일명 변경 시 여기도 수정

    server_name example.com www.example.com;

    location / {
        try_files $uri $uri/ = 404;
    }
}
# 설정 오류 확인
sudo nginx -t

# 설정 다시 읽어오기
sudo systemctl reload nginx

리버스 프록시(Reverse Proxy) 설정

  • 리버스 프록시: 클라이언트 요청을 대신 받아 내부 서버로 전달하고 응답을 다시 클라이언트에게 전달하는 프록시 서버
  • 목적
    • Load Balancing
    • 보안 강화: 실제 서버를 외부로 노출하지 않음, DDoS/WAF와 함께 사용
    • 캐싱
    • 압축 및 최적화
    • URL 라우팅: URL을 받아 각기 다른 서버로 요청 전송

설정 예시 (api.example.com → 내부 3000번 포트)

server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://localhost:3000;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Load Balancing

설정 예시 (3개 서버로 로드밸런싱)

upstream backend_servers {
    server 192.168.0.101:5000;
    server 192.168.0.102:5000;
    server 192.168.0.103:5000;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend_servers;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

댓글