홈/엔지니어/네트워킹 / 서버/Apache 가상호스트 설정 — Virtual Ho…
네트워킹 / 서버초급UbuntuDebianCentOSapachevirtualhosthttpd

Apache 가상호스트 설정 — Virtual Host·모듈 관리

Apache에서 도메인별 가상호스트를 구성하고, 모듈 활성화, 리다이렉트, SSL 연동, 로그 분리까지 실무 설정 방법을 설명합니다.

목차
  1. 설치
  2. 디렉터리 구조 (Ubuntu)
  3. 기본 가상호스트 설정
  4. HTTP → HTTPS 리다이렉트
  5. 모듈 관리
  6. .htaccess와 mod_rewrite
  7. 리버스 프록시 (Node.js 앱 연동)
  8. 설정 검증 및 재로드

설치

Bash
# Ubuntu / Debian
sudo apt update && sudo apt install -y apache2

# CentOS / RHEL
sudo yum install -y httpd

# 서비스 시작
sudo systemctl enable --now apache2   # Ubuntu
sudo systemctl enable --now httpd     # CentOS

디렉터리 구조 (Ubuntu)

CODE
/etc/apache2/
├── apache2.conf        # 메인 설정
├── sites-available/    # 가상호스트 설정 파일
├── sites-enabled/      # 활성화된 사이트 (심볼릭 링크)
├── mods-available/     # 사용 가능한 모듈
└── mods-enabled/       # 활성화된 모듈

기본 가상호스트 설정

APACHE
# /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerName  example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog  ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
Bash
# 사이트 활성화
sudo a2ensite example.com.conf
sudo systemctl reload apache2

# 사이트 비활성화
sudo a2dissite 000-default.conf

HTTP → HTTPS 리다이렉트

APACHE
<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example.com

    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

    Header always set Strict-Transport-Security "max-age=31536000"
</VirtualHost>

모듈 관리

Bash
# 모듈 활성화
sudo a2enmod rewrite    # URL 재작성
sudo a2enmod ssl        # HTTPS
sudo a2enmod headers    # 헤더 조작
sudo a2enmod proxy proxy_http  # 리버스 프록시

# 모듈 비활성화
sudo a2dismod autoindex

# 활성화된 모듈 확인
apache2ctl -M

.htaccess와 mod_rewrite

APACHE
# /var/www/example.com/.htaccess
Options -Indexes
RewriteEngine On

# www → non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

# SPA 라우팅 (React, Vue 등)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]

리버스 프록시 (Node.js 앱 연동)

APACHE
<VirtualHost *:443>
    ServerName api.example.com

    ProxyPreserveHost On
    ProxyPass        / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/

    RequestHeader set X-Forwarded-Proto "https"
</VirtualHost>

설정 검증 및 재로드

Bash
# 문법 검사
sudo apache2ctl configtest
# 또는
sudo apachectl configtest

# 무중단 재로드
sudo systemctl reload apache2
#apache#virtualhost#httpd#웹서버#ssl
편집 안내 · Editorial Note

AI 도구가 초안 작성에 사용될 수 있습니다. 명령어의 실행 여부와 검증 환경은 각 가이드에 기재된 근거를 확인하세요. 적용 전 사용 중인 버전의 공식 문서를 확인하고, 오류는 이메일로 알려주세요. 이메일로 제보

주제 관련 공식 문서(자동 연결 · 문장별 근거 아님)OpenSSL 공식 문서 ↗

질문 & 답변 (Q&A)

이 가이드에 대해 궁금한 점을 질문해보세요. 확인 후 답변드립니다.