가상 호스트와 SSL이 포함된 웹 서버 배포
1. 환경 준비
이제 모든 것을 하나로 모을 때가 되었어. 오늘은 여러 가상 호스트를 가진 서버를 배포하고 HTTPS를 연결할 거야. 우리는 테스트하고 디버깅하며 최종적으로 설정할 거야.
우리의 목표
이번 프로젝트에서는 하나의 웹 서버에 두 개의 사이트를 배포할 거야:
- example.com
- test.com
각 사이트에 대해 가상 호스트를 설정하고, HTTPS를 연결하며, 디버깅 및 작동 확인 단계를 시연할 거야. 준비됐어? 시작하자!
준비
시작하기 전에, 모든 것이 제대로 설정되었는지 확인하자:
웹 서버(Nginx 또는 Apache)가 이미 설치되어 있는지 확인.
Nginx를 사용하는 경우:
sudo apt-get update sudo apt-get install nginx
아니면 Apache:
sudo apt-get update sudo apt-get install apache2
SSL 작업을 위한 Certbot 패키지가 설치되어 있어야 해.
Nginx의 경우:
sudo apt-get install certbot python3-certbot-nginx
Apache의 경우:
sudo apt-get install certbot python3-certbot-apache
example.com 및 test.com 도메인에 대해 DNS가 설정된 서버에 접근할 수 있거나, 테스트를 위해
/etc/hosts
를 사용할 수 있는지 확인해.
2. 각 사이트에 대한 디렉토리 설정
사이트를 위한 디렉토리와 파일을 만드는 것부터 시작하자.
1단계: 사이트용 폴더 생성
sudo mkdir -p /var/www/example.com
sudo mkdir -p /var/www/test.com
2단계: 테스트용 HTML 파일 생성
각 사이트용 간단한 HTML 파일을 만들어 보자:
example.com
sudo nano /var/www/example.com/index.html
내용:
<!DOCTYPE html>
<html>
<head>
<title>example.com에 오신 것을 환영합니다</title>
</head>
<body>
<h1>여기는 example.com입니다</h1>
<p>example.com에 오신 것을 환영합니다! 사이트가 SSL로 안전하게 실행되고 있습니다!</p>
</body>
</html>
test.com
sudo nano /var/www/test.com/index.html
내용:
<!DOCTYPE html>
<html>
<head>
<title>test.com에 오신 것을 환영합니다</title>
</head>
<body>
<h1>여기는 test.com입니다</h1>
<p>test.com에 오신 것을 환영합니다! 사이트가 SSL로 안전하게 실행 중입니다!</p>
</body>
</html>
3. 가상 호스트 설정
각 사이트에 대해 별도의 구성 파일을 생성할 거야.
1단계: Nginx용 설정
example.com
sudo nano /etc/nginx/sites-available/example.com
내용:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
test.com
sudo nano /etc/nginx/sites-available/test.com
내용:
server {
listen 80;
server_name test.com www.test.com;
root /var/www/test.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
구성을 활성화하기:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/
서버 재시작:
sudo nginx -t
sudo systemctl reload nginx
2단계: Apache용 설정
example.com
sudo nano /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>
</VirtualHost>
test.com
sudo nano /etc/apache2/sites-available/test.com.conf
내용:
<VirtualHost *:80>
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com
<Directory /var/www/test.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
구성 활성화하기:
sudo a2ensite example.com.conf
sudo a2ensite test.com.conf
sudo systemctl reload apache2
4. 사이트에 대한 HTTPS 설정
이제 Let’s Encrypt를 사용해서 두 사이트에 SSL을 연결해 보자.
sudo certbot --nginx
# 또는 Apache의 경우:
sudo certbot --apache
프로세스에서 어떤 사이트에 인증서를 발급할지 선택하라고 묻는다. example.com
와 test.com
을 선택해. Certbot은 서버 설정을 자동으로 업데이트해 줄 거야.
5. 사이트 작동 확인
설정을 완료한 후, 다음 사이트들을 방문해봐:
HTML 페이지가 잘 표시되는지 확인해봐. 브라우저 주소창에 "안전함" (Secure)이 표시되는지도 확인해야 해.
6. 디버깅 및 확인
뭔가 잘못되었다면 로그를 확인해봐:
Nginx용:
sudo tail -f /var/log/nginx/error.log
Apache용:
sudo tail -f /var/log/apache2/error.log
또는 curl
을 사용하여 접근성을 확인해봐:
curl -I http://example.com
curl -I https://example.com
7. 인증서 갱신
Certbot은 시스템 cron
을 통해 인증서를 자동으로 갱신해줘. 확인하려면 수동으로 다음 명령을 실행하면 돼:
sudo certbot renew --dry-run
이렇게 해서, 하나씩 단계적으로, 여러 사이트와 HTTPS를 지원하는 웹 서버를 설정했어. 너는 이제 빈 서버에서 시작해서 가상 호스트와 SSL이 작동하는 서버를 구성하는 모든 과정을 마쳤어. 이제 네 서버는 세상에 네 사이트들을 보여줄 준비가 됐어. 멋지지 않아?
GO TO FULL VERSION