
Ubuntu 24.04에서 Apache2를 설치했다면, 기본적인 사용 방법은 서비스 관리 → 웹 문서 배치 → 가상 호스트 설정 → 로그 확인 → 보안 설정 순으로 익히면 됩니다.
1. Apache2 상태 확인
서비스가 정상 실행 중인지 확인합니다.
sudo systemctl status apache2
실행 중이면 다음과 비슷하게 표시됩니다.
active (running)
시작, 중지, 재시작 명령:
# 시작
sudo systemctl start apache2
# 중지
sudo systemctl stop apache2
# 재시작
sudo systemctl restart apache2
# 설정만 다시 읽기
sudo systemctl reload apache2
# 부팅 시 자동 시작
sudo systemctl enable apache2
2. 웹 서버 접속 확인
서버 IP 확인:
ip addr
또는
hostname -I
브라우저에서
http://서버IP
로 접속하면 Apache 기본 페이지가 보입니다.
예:
http://192.168.0.100
3. 웹 페이지 올리기
기본 웹 루트:
/var/www/html
기본 페이지 확인:
ls -al /var/www/html
기본 파일:
index.html
새 페이지 작성:
sudo nano /var/www/html/index.html
예시:
<!DOCTYPE html>
<html>
<head>
<title>My Apache Server</title>
</head>
<body>
<h1>Apache2 정상 동작</h1>
</body>
</html>
저장 후 브라우저 새로고침하면 변경 내용이 보입니다.
4. Apache 설정 파일 구조
주요 디렉터리:
/etc/apache2/
├── apache2.conf
├── ports.conf
├── sites-available/
├── sites-enabled/
├── mods-available/
├── mods-enabled/
주요 파일
메인 설정
/etc/apache2/apache2.conf
포트 설정
/etc/apache2/ports.conf
사이트 설정
/etc/apache2/sites-available/
활성 사이트
/etc/apache2/sites-enabled/
5. 가상 호스트(VirtualHost)
한 서버에서 여러 웹사이트를 운영할 때 사용합니다.
예:
sudo nano /etc/apache2/sites-available/example.conf
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
<Directory /var/www/example>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>
디렉터리 생성:
sudo mkdir -p /var/www/example
사이트 활성화:
sudo a2ensite example.conf
설정 검사:
sudo apache2ctl configtest
재시작:
sudo systemctl reload apache2
비활성화:
sudo a2dissite example.conf
6. 모듈 관리
Apache는 기능을 모듈로 관리합니다.
현재 활성 모듈:
apache2ctl -M
예를 들어 Rewrite 모듈 활성화:
sudo a2enmod rewrite
sudo systemctl restart apache2
비활성화:
sudo a2dismod rewrite
7. 로그 확인
접속 로그:
tail -f /var/log/apache2/access.log
오류 로그:
tail -f /var/log/apache2/error.log
실시간 확인:
sudo journalctl -u apache2 -f
8. 설정 문법 검사
설정 변경 후 반드시 확인:
sudo apache2ctl configtest
정상이면:
Syntax OK
9. 방화벽(UFW) 허용
UFW 사용 시:
sudo ufw allow 'Apache Full'
상태 확인:
sudo ufw status
10. HTTPS(SSL) 적용
모듈 활성화:
sudo a2enmod ssl
Let's Encrypt 사용 시:
sudo apt install certbot python3-certbot-apache
인증서 발급:
sudo certbot --apache
자동으로 HTTPS 설정이 적용됩니다.
자주 사용하는 명령 모음
# 상태 확인
sudo systemctl status apache2
# 재시작
sudo systemctl restart apache2
# 설정 검사
sudo apache2ctl configtest
# 사이트 활성화
sudo a2ensite example.conf
# 사이트 비활성화
sudo a2dissite example.conf
# 모듈 활성화
sudo a2enmod rewrite
# 로그 확인
tail -f /var/log/apache2/error.log
example.conf를 수정한 경우에는 설정 검사 → 적용(reload 또는 restart) → 로그 확인 순서로 진행하는 것이 일반적입니다.
1. 설정 문법 검사
가장 먼저 문법 오류가 없는지 확인합니다.
sudo apache2ctl configtest
정상이면:
Syntax OK
오류가 나오면 먼저 수정해야 합니다.
2. 설정 적용
대부분의 경우 reload만으로 충분합니다.
sudo systemctl reload apache2
reload는 현재 연결을 끊지 않고 설정만 다시 읽어옵니다.
만약 모듈 추가/제거(a2enmod, a2dismod)나 Apache 자체 설정을 크게 변경한 경우에는:
sudo systemctl restart apache2
를 사용합니다.
3. 사이트 설정을 새로 추가했거나 이름을 변경한 경우
예를 들어:
/etc/apache2/sites-available/example.conf
를 새로 만들었거나 파일명을 변경했다면:
sudo a2ensite example.conf
sudo systemctl reload apache2
가 필요합니다.
이미 활성화된 사이트의 내용을 수정한 것이라면 a2ensite를 다시 실행할 필요는 없습니다.
활성화 여부 확인:
ls -l /etc/apache2/sites-enabled/
또는
apache2ctl -S
4. 적용 확인
Apache가 어떤 설정을 사용 중인지 확인:
sudo apache2ctl -S
가상 호스트 목록이 표시됩니다.
예:
*:80 example.com (/etc/apache2/sites-enabled/example.conf:1)
5. 문제 발생 시 로그 확인
tail -f /var/log/apache2/error.log
또는
sudo journalctl -u apache2 -n 50
실제 운영 시 가장 많이 사용하는 순서
sudo nano /etc/apache2/sites-available/example.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
sudo apache2ctl -S
이 4개 명령만 기억해도 대부분의 VirtualHost 설정 변경 작업은 처리할 수 있습니다.
