안녕하세요.
하얀 도하지 남편입니다.
오늘은 리눅스에서 Apache와 Nginx 설정 방법에 대해 공부해 보도록 하겠습니다.
1. 개요
웹 서버는 인터넷 환경에서 웹사이트를 제공하는 핵심 요소입니다. 리눅스에서는 Apache와 Nginx가 가장 널리 사용되는 웹 서버입니다. 이 글에서는 두 웹 서버의 설정 방법과 최적화 전략을 살펴보겠습니다.
2. Apache 웹 서버 설정
2.1 Apache 설치
Apache는 대부분의 리눅스 배포판에서 기본적으로 제공됩니다. Ubuntu에서는 다음 명령어로 설치할 수 있습니다.
sudo apt update
sudo apt install apache2
CentOS에서는 다음과 같이 설치할 수 있습니다.
sudo yum install httpd
2.2 Apache 기본 설정 파일
Apache의 주요 설정 파일은 /etc/apache2/apache2.conf
(Ubuntu) 또는 /etc/httpd/conf/httpd.conf
(CentOS)입니다. 기본적으로 다음 설정이 포함되어 있습니다.
<Directory /var/www/>
AllowOverride None
Require all granted
</Directory>
2.3 가상 호스트 설정
여러 도메인을 운영하려면 가상 호스트(Virtual Host)를 설정해야 합니다. /etc/apache2/sites-available/yourdomain.conf
파일을 생성하고 다음 내용을 추가합니다.
<VirtualHost *:80>
ServerAdmin admin@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
이후 설정을 활성화하고 Apache를 재시작합니다.
sudo a2ensite yourdomain.conf
sudo systemctl restart apache2
3. Nginx 웹 서버 설정
3.1 Nginx 설치
Nginx는 가볍고 빠른 성능을 제공하는 웹 서버입니다. Ubuntu에서는 다음 명령어로 설치할 수 있습니다.
sudo apt update
sudo apt install nginx
CentOS에서는 다음과 같이 설치합니다.
sudo yum install nginx
3.2 Nginx 기본 설정 파일
Nginx의 주요 설정 파일은 /etc/nginx/nginx.conf
입니다. 기본적인 서버 블록 설정은 다음과 같습니다.
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
3.3 설정 적용 및 테스트
설정을 저장한 후 Nginx 구성을 테스트합니다.
sudo nginx -t
문제가 없다면 Nginx를 재시작합니다.
sudo systemctl restart nginx
4. 성능 최적화
4.1 KeepAlive 설정
Apache의 KeepAlive
옵션을 활성화하면 여러 요청을 하나의 연결로 유지할 수 있어 성능이 향상됩니다. /etc/apache2/apache2.conf
파일에서 다음 설정을 확인합니다.
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
Nginx에서는 keepalive_timeout
을 설정합니다.
keepalive_timeout 65;
4.2 캐싱 및 압축
Apache에서는 mod_deflate
를 활성화하고, Nginx에서는 gzip
설정을 추가하면 성능을 최적화할 수 있습니다.
Apache 설정:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml
</IfModule>
Nginx 설정:
gzip on;
gzip_types text/css application/javascript;
4.3 SSL 보안 적용
웹사이트의 보안을 위해 SSL을 적용해야 합니다. Let’s Encrypt를 이용하면 무료 SSL 인증서를 발급받을 수 있습니다.
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
Nginx에서는 다음과 같이 설정합니다.
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Apache와 Nginx는 각각 장점이 있는 강력한 웹 서버입니다. Apache는 유연한 설정이 강점이며, Nginx는 가볍고 빠른 성능을 제공합니다. 프로젝트의 특성에 따라 적절한 웹 서버를 선택하고 최적화하는 방법을 통해 서버를 구성해 보세요.