Apache2 성능 최적화 (Apache2 Performance Optimization)
성능 최적화 개요 (Overview of Performance Optimization)
Apache HTTP Server의 성능 최적화는 웹 서버의 응답 시간을 단축하고, 동시 접속자를 효율적으로 처리할 수 있도록 설정을 조정하는 과정입니다. 이를 통해 서버의 처리 능력을 향상시키고, 사용자 경험을 개선할 수 있습니다.
기본적인 성능 최적화 방법 (Basic Performance Optimization Methods)
- 최소화된 모듈 사용: 필요한 모듈만 활성화하여 서버의 메모리 사용을 줄입니다.
sudo a2dismod mod_name # 비활성화 예시 - KeepAlive 설정: KeepAlive를 활성화하여 동일한 클라이언트와의 연결을 유지함으로써 성능을 향상시킵니다.
KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 5 - 적절한 작업자 MPM 선택: 서버의 사용 목적과 하드웨어에 맞는 MPM을 선택합니다.
- prefork: 각 요청에 대해 새로운 프로세스를 생성. 메모리가 많고 CPU 코어 수가 적을 때 유리.
- worker: 각 요청에 대해 스레드를 생성. 메모리 사용이 적고 CPU 코어 수가 많을 때 유리.
- event: worker와 비슷하지만 KeepAlive 연결을 더 효율적으로 처리.
<IfModule mpm_worker_module> StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule>
캐싱 및 압축 (Caching and Compression)
캐시 설정 (Cache Configuration with mod_cache)
- mod_cache 모듈 활성화:
sudo a2enmod cache sudo a2enmod cache_disk - 캐시 설정 추가:
<IfModule mod_cache.c> # 기본 캐시 설정 CacheQuickHandler off CacheLock on CacheLockPath /tmp/mod_cache-lock CacheIgnoreHeaders Set-Cookie </IfModule> <IfModule mod_cache_disk.c> # 디스크 기반 캐시 설정 CacheRoot /var/cache/apache2/mod_cache_disk CacheEnable disk / CacheDirLevels 2 CacheDirLength 1 </IfModule> <Directory "/var/www/html"> # 캐시 만료 시간 설정 CacheDefaultExpire 3600 CacheMaxExpire 86400 CacheLastModifiedFactor 0.5 ExpiresActive on ExpiresDefault "access plus 1 day" </Directory>
압축 설정 (Compression Configuration with mod_deflate)
- mod_deflate 모듈 활성화:
sudo a2enmod deflate - 압축 설정 추가:
<IfModule mod_deflate.c> # 텍스트 파일 압축 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css AddOutputFilterByType DEFLATE application/javascript application/json application/xml# 브라우저 호환성을 위한 헤더 설정 BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # 기존 헤더 보존 Header append Vary User-Agent</IfModule>
캐시 및 압축 설정 예제 (Examples of Cache and Compression Settings)
전체적인 Apache2 설정 파일 예제:
# 서버 기본 설정
ServerRoot "/etc/apache2"
Listen 80
# MPM 설정
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
# KeepAlive 설정
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
# 캐싱 설정
<IfModule mod_cache.c>
CacheQuickHandler off
CacheLock on
CacheLockPath /tmp/mod_cache-lock
CacheIgnoreHeaders Set-Cookie
</IfModule>
<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/mod_cache_disk
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
<Directory "/var/www/html">
CacheDefaultExpire 3600
CacheMaxExpire 86400
CacheLastModifiedFactor 0.5
ExpiresActive on
ExpiresDefault "access plus 1 day"
</Directory>
# 압축 설정
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
AddOutputFilterByType DEFLATE application/javascript application/json application/xml
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
이 설정은 서버의 기본 보안과 성능을 향상시키기 위한 다양한 방법을 포함하고 있습니다. KeepAlive를 활성화하고 적절한 MPM을 선택하며, mod_cache와 mod_deflate를 사용하여 캐싱과 압축을 설정하면 성능을 크게 개선할 수 있습니다.
