본문 바로가기

Program

Ubuntu 서버에 Apache2 웹 서버 설치 가이드

반응형

Ubuntu 서버를 운영할 때 가장 기본적으로 설치하는 웹 서버 중 하나가 Apache2입니다. 이 가이드는 Ubuntu 공식 문서(Install Apache2 - Ubuntu Documentation)를 기반으로, Apache2 설치 및 기본 구성 방법을 초보자도 이해할 수 있게 단계별로 설명합니다.


1. Apache2란?

Apache2는 오픈소스 웹 서버 소프트웨어로, 클라이언트(브라우저)로부터 들어오는 HTTP 요청을 받아 웹 페이지를 제공하는 역할을 합니다. 전 세계에서 가장 널리 사용되는 웹 서버 중 하나이며, PHP, Python, Perl 등 다양한 언어와 호환됩니다.


2. 설치 전 준비사항

시스템 요건

  • Ubuntu Server 20.04 또는 22.04 이상 버전
  • sudo 권한을 가진 사용자 계정

사전 점검

sudo apt update
sudo apt upgrade

패키지를 최신으로 유지하는 것은 보안과 안정성 측면에서 매우 중요합니다.


3. Apache2 설치

명령어 한 줄로 설치:

sudo apt install apache2 -y

설치가 완료되면 Apache는 자동으로 시작되며, 시스템 재부팅 시 자동으로 다시 실행되도록 설정됩니다.


4. 설치 확인 방법

1) 서비스 상태 확인

sudo systemctl status apache2

아래와 같은 출력이 나타나면 성공적으로 설치된 것입니다:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled)
     Active: active (running)

2) 웹 브라우저에서 확인

서버의 IP 주소를 웹 브라우저에 입력합니다.

예시: http://<서버-IP>

기본 Apache2 환영 페이지가 보이면 정상입니다.


5. 방화벽 설정 (UFW 사용하는 경우)

Apache 설치 후 외부에서 접속 가능하게 방화벽 허용 규칙을 추가해야 합니다.

sudo ufw allow 'Apache'

적용 후 상태 확인:

sudo ufw status

출력 예시:

Status: active

To                         Action      From
--                         ------      ----
Apache                     ALLOW       Anywhere

6. 주요 디렉토리 및 파일 구조

경로 설명

/var/www/html 웹 루트 디렉토리 (기본 HTML 파일 위치)
/etc/apache2/apache2.conf Apache 메인 설정 파일
/etc/apache2/sites-available/ 가상 호스트 설정 파일 위치
/etc/apache2/sites-enabled/ 활성화된 가상 호스트 설정 링크
/var/log/apache2/ Apache 로그 파일 저장소

7. 서비스 관리 명령어

명령어 설명

sudo systemctl start apache2 Apache 시작
sudo systemctl stop apache2 Apache 중지
sudo systemctl restart apache2 Apache 재시작
sudo systemctl reload apache2 설정만 다시 로드
sudo systemctl enable apache2 부팅 시 자동 시작 설정
sudo systemctl disable apache2 부팅 시 자동 시작 해제

8. 간단한 HTML 파일 테스트

아래 명령어로 index.html을 수정해 봅니다:

echo "<h1>Hello, Apache2 on Ubuntu!</h1>" | sudo tee /var/www/html/index.html

웹 브라우저에서 서버 IP를 다시 열어보면, 위에서 작성한 메시지가 표시됩니다.


9. 가상 호스트 구성 (선택 사항)

Apache는 여러 도메인을 하나의 서버에서 운영할 수 있는 Virtual Host 기능을 제공합니다.

예시로 mywebsite.com을 설정한다고 가정하면 다음과 같이 구성합니다.

  1. 설정 파일 생성:
sudo nano /etc/apache2/sites-available/mywebsite.com.conf
  1. 설정 내용 예시:
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName mywebsite.com
    DocumentRoot /var/www/mywebsite.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
  1. 웹 루트 생성 및 권한 설정:
sudo mkdir -p /var/www/mywebsite.com
echo "<h1>Welcome to mywebsite.com!</h1>" | sudo tee /var/www/mywebsite.com/index.html
sudo chown -R www-data:www-data /var/www/mywebsite.com
  1. 사이트 활성화 및 Apache 재시작:
sudo a2ensite mywebsite.com.conf
sudo systemctl reload apache2

10. 마무리

Apache2는 웹 개발자나 서버 관리자에게 기본 중의 기본이라고 할 수 있습니다. 위 단계를 따라 설치하고, 간단한 HTML 페이지부터 시작해보세요. PHP, SSL, 리버스 프록시 등 다양한 확장을 통해 점점 더 복잡한 웹 서비스로 확장할 수 있습니다.

 

반응형