Apache 소개와 설치 가이드

Apache HTTP Server 설치와 운영 가이드입니다. VirtualHost, Directory 권한, mod_proxy reverse proxy, configtest, reload, 로그와 403/502 분리 기준을 정리했습니다.

Apache HTTP Server(httpd)는 정적 웹 서버, PHP 같은 동적 애플리케이션 앞단, reverse proxy, TLS termination 용도로 널리 쓰입니다. 설치 자체는 간단하지만 운영에서는 virtual host, 모듈, 로그, 설정 문법 검사, reload 절차를 같이 잡아야 합니다.

설치와 시작

# Ubuntu/Debian
sudo apt update
sudo apt install -y apache2
sudo systemctl enable --now apache2
sudo systemctl status apache2 --no-pager

# RHEL/Rocky/Alma
sudo dnf install -y httpd
sudo systemctl enable --now httpd
sudo systemctl status httpd --no-pager

기본 응답 확인은 서버 내부에서 먼저 합니다.

curl -I http://127.0.0.1/
apache2ctl -v 2>/dev/null || httpd -v

방화벽

# UFW
sudo ufw allow 'Apache Full'

# firewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

VirtualHost 예시

Ubuntu/Debian 기준으로 정적 사이트를 하나 구성하는 예시입니다.

sudo install -d -m 755 /var/www/example
echo 'hello apache' | sudo tee /var/www/example/index.html
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example

    ErrorLog ${APACHE_LOG_DIR}/example-error.log
    CustomLog ${APACHE_LOG_DIR}/example-access.log combined

    <Directory /var/www/example>
        Require all granted
        Options -Indexes
        AllowOverride None
    </Directory>
</VirtualHost>
sudo tee /etc/apache2/sites-available/example.conf >/dev/null
sudo a2ensite example.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

RHEL 계열 설정 위치

RHEL 계열은 보통 /etc/httpd/conf.d/*.conf에 vhost 파일을 둡니다.

sudo httpd -t
sudo systemctl reload httpd

Reverse proxy 예시

Apache 공식 문서의 mod_proxy는 내부 애플리케이션을 외부 URL로 연결할 때 많이 사용됩니다.

# Ubuntu/Debian
sudo a2enmod proxy proxy_http headers
sudo systemctl reload apache2
<VirtualHost *:80>
    ServerName app.example.com

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

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

로그와 문제 해결

# Ubuntu/Debian
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log

# RHEL 계열
sudo tail -f /var/log/httpd/access_log
sudo tail -f /var/log/httpd/error_log

설정 변경은 항상 apache2ctl configtest 또는 httpd -t로 확인한 뒤 reload합니다. 403은 Directory 권한과 SELinux, 404는 DocumentRoot와 경로, 502/503은 reverse proxy backend 상태부터 확인합니다.

공식 문서

BGM EVER