Apache HTTP Server

Apache HTTP Server는 가장 널리 사용되는 오픈 소스 웹 서버 소프트웨어이다.

주요 특징

  1. 크로스 플랫폼 지원: Linux, Windows, macOS 등 다양한 운영 체제에서 실행 가능
  2. 모듈식 구조: 다양한 기능을 모듈로 추가/제거 가능
  3. 가상 호스팅: 하나의 서버에서 여러 웹사이트 호스팅 가능
  4. 보안 기능: SSL/TLS 지원, 접근 제어 등
  5. 다양한 프로그래밍 언어 지원: PHP, Perl, Python 등

주요 기능

  1. 모듈식 구조: 다양한 기능을 모듈로 추가/제거 가능하여 유연성 제공
  2. 가상 호스팅: 하나의 서버에서 여러 웹사이트 호스팅 가능
  3. 보안 기능: SSL/TLS 지원, 접근 제어, mod_security를 통한 침입 탐지 및 방지
  4. 다양한 프로그래밍 언어 지원: PHP, Perl, Python, Lua 등 지원
  5. 로드 밸런싱: 다양한 로드 밸런싱 메커니즘 제공
  6. URL 재작성: mod_rewrite 모듈을 통한 URL 재작성 기능
  7. 압축 지원: mod_gzip을 통한 콘텐츠 압축으로 성능 향상
  8. IPv6 지원: IPv6 호환성 제공
  9. HTTP/2 지원: 최신 HTTP 프로토콜 지원
  10. 동적 설정:.htaccess 파일을 통한 디렉토리별 설정 지원
  11. 리버스 프록시: 캐싱 기능이 있는 리버스 프록시 제공
  12. 다양한 인증 방식: 비밀번호 기반, 디지털 인증서 등 지원

설치 방법

Ubuntu/Debian 기반:

1
2
sudo apt update
sudo apt install apache2

RHEL/CentOS 기반:

1
sudo yum install httpd

기본 설정

  1. 설정 파일 위치: /etc/apache2/apache2.conf (Ubuntu) 또는 /etc/httpd/conf/httpd.conf (CentOS)

  2. 가상 호스트 설정:
    /etc/apache2/sites-available/ 디렉토리에.conf 파일 생성

  3. 모듈 활성화/비활성화:

    1
    2
    
    sudo a2enmod [모듈명]
    sudo a2dismod [모듈명]
    
  4. 서비스 시작/중지:

    1
    2
    
    sudo systemctl start apache2
    sudo systemctl stop apache2
    

기본 사용법

  1. 웹 루트 디렉토리: /var/www/html/
  2. 로그 파일 위치: /var/log/apache2/ 또는 /var/log/httpd/
  3. 설정 변경 후 서비스 재시작: sudo systemctl restart apache2

설정 옵션

기본 구성과 설정

Apache의 주요 설정 파일들과 그 역할:

  1. 메인 설정 파일 (httpd.conf 또는 apache2.conf):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 기본 설정 예시
ServerRoot "/etc/apache2"
Listen 80
ServerAdmin webmaster@localhost
DocumentRoot "/var/www/html"

# 모듈 로드
LoadModule rewrite_module modules/mod_rewrite.so

# 디렉터리 설정
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
  1. 가상 호스트 설정:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# sites-available/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    <Directory /var/www/example.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

SSL/TLS 설정

HTTPS를 위한 SSL/TLS 설정:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# SSL 설정 예시
<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example.com
    
    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/example.com.crt
    SSLCertificateKeyFile /etc/ssl/private/example.com.key
    SSLCertificateChainFile /etc/ssl/certs/chain.crt
    
    # 보안 강화 설정
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
    SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:...
    SSLHonorCipherOrder on
</VirtualHost>

mod_rewrite를 사용한 URL 재작성

URL 재작성 규칙 설정:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# .htaccess 파일 예시
RewriteEngine On

# www 리다이렉트
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

# PHP 파일 확장자 숨기기
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

보안 설정

보안 강화를 위한 설정:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 디렉터리 리스팅 비활성화
Options -Indexes

# 서버 정보 숨기기
ServerSignature Off
ServerTokens Prod

# XSS 보호
Header set X-XSS-Protection "1; mode=block"

# MIME 스니핑 방지
Header set X-Content-Type-Options "nosniff"

# 클릭재킹 방지
Header set X-Frame-Options "SAMEORIGIN"

최적화

성능 향상을 위한 설정:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 캐시 설정
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>

# Gzip 압축
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml
    AddOutputFilterByType DEFLATE text/css application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

로그 관리

로그 설정과 관리:

1
2
3
4
5
6
7
8
9
# 로그 형식 정의
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

# 커스텀 로그 설정
CustomLog ${APACHE_LOG_DIR}/access.log combined
ErrorLog ${APACHE_LOG_DIR}/error.log

# 로그 레벨 설정
LogLevel warn

모니터링과 문제 해결

일반적인 문제 해결 명령어:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 설정 문법 검사
sudo apache2ctl configtest

# 상태 확인
sudo systemctl status apache2

# 오류 로그 확인
tail -f /var/log/apache2/error.log

# 접속 로그 모니터링
tail -f /var/log/apache2/access.log

참고 및 출처