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

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

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

설치

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)

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