HyunJun 기술 블로그

EC2 Apache 배포하기. 본문

AWS/EC2

EC2 Apache 배포하기.

공부 좋아 2023. 8. 3. 09:51
728x90
반응형

2023.08.02 - [AWS/EC2] - EC2 인스턴스 생성하기. 글에서는 EC2 인스턴스 생성하는 방법을 알아보았다. 이번 시간에는 간단하게 HTML 페이지를 Apache를 사용하여 서버에 배포하는 것을 정리하려고 한다.

 

Apache HTTP Server

Apache2는 웹 서버 소프트웨어인 Apache HTTP Server의 Debian 및 Ubuntu에서의 패키지 이름이다. Apache는 오픈 소스 소프트웨어 그룹인 아파치 소프트웨어 재단에서 만드는 웹 서버 프로그램이다. 팀 버너스 리가 만든 최초의 웹 서버 프로그램인 "NCSA HTTPd"를 기반으로 만들어졌다. 팀 버너스 리의 NCSA HTTPd는 유닉스 기반으로 만들어졌기에, 아파치 HTTP 서버는 NCSA HTTPd를 리눅스에서도 돌리는 것을 목표로 만들어진 프로그램이다. 그 이후 리눅스와 함께 퍼져나갔고, 리눅스가 서버 OS의 최다 점유율을 차지하자 아파치도 자연스럽게 최다 점유율을 차지하게 되었다. 현재 거의 모든 리눅스 배포판이 이 아파치를 지원한다.

 

1) Apache2 설치.

$ sudo apt install apache2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils bzip2 libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 mailcap mime-support
  ssl-cert
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser
  bzip2-doc
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils bzip2 libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 mailcap mime-support
  ssl-cert
0 upgraded, 13 newly installed, 0 to remove and 3 not upgraded.
Need to get 2137 kB of archives.
After this operation, 8505 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libapr1 amd64 1.7.0-8ubuntu0.22.04.1 [108 kB]
Get:2 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libaprutil1 amd64 1.6.1-5ubuntu4.22.04.1 [92.6 kB]
Get:3 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libaprutil1-dbd-sqlite3 amd64 1.6.1-5ubuntu4.22.04.1 [11.3 kB]
Get:4 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 libaprutil1-ldap amd64 1.6.1-5ubuntu4.22.04.1 [9168 B]
Get:5 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 liblua5.3-0 amd64 5.3.6-1build1 [140 kB]
Get:6 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 apache2-bin amd64 2.4.52-1ubuntu4.6 [1345 kB]
Get:7 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 apache2-data all 2.4.52-1ubuntu4.6 [165 kB]
Get:8 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 apache2-utils amd64 2.4.52-1ubuntu4.6 [89.1 kB]
Get:9 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 mailcap all 3.70+nmu1ubuntu1 [23.8 kB]
Get:10 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 mime-support all 3.66 [3696 B]
Get:11 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy-updates/main amd64 apache2 amd64 2.4.52-1ubuntu4.6 [97.8 kB]
Get:12 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 bzip2 amd64 1.0.8-5build1 [34.8 kB]
Get:13 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu jammy/main amd64 ssl-cert all 1.1.2 [17.4 kB]
Fetched 2137 kB in 0s (8708 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libapr1:amd64.
(Reading database ... 94089 files and directories currently installed.)
Preparing to unpack .../00-libapr1_1.7.0-8ubuntu0.22.04.1_amd64.deb ...
Unpacking libapr1:amd64 (1.7.0-8ubuntu0.22.04.1) ...
Selecting previously unselected package libaprutil1:amd64.
Preparing to unpack .../01-libaprutil1_1.6.1-5ubuntu4.22.04.1_amd64.deb ...
Unpacking libaprutil1:amd64 (1.6.1-5ubuntu4.22.04.1) ...
Selecting previously unselected package libaprutil1-dbd-sqlite3:amd64.
Preparing to unpack .../02-libaprutil1-dbd-sqlite3_1.6.1-5ubuntu4.22.04.1_amd64.deb ...
Unpacking libaprutil1-dbd-sqlite3:amd64 (1.6.1-5ubuntu4.22.04.1) ...
Selecting previously unselected package libaprutil1-ldap:amd64.
Preparing to unpack .../03-libaprutil1-ldap_1.6.1-5ubuntu4.22.04.1_amd64.deb ...
Unpacking libaprutil1-ldap:amd64 (1.6.1-5ubuntu4.22.04.1) ...
Selecting previously unselected package liblua5.3-0:amd64.
Preparing to unpack .../04-liblua5.3-0_5.3.6-1build1_amd64.deb ...
Unpacking liblua5.3-0:amd64 (5.3.6-1build1) ...
Selecting previously unselected package apache2-bin.
Preparing to unpack .../05-apache2-bin_2.4.52-1ubuntu4.6_amd64.deb ...
Unpacking apache2-bin (2.4.52-1ubuntu4.6) ...
Selecting previously unselected package apache2-data.
Preparing to unpack .../06-apache2-data_2.4.52-1ubuntu4.6_all.deb ...
Unpacking apache2-data (2.4.52-1ubuntu4.6) ...
Selecting previously unselected package apache2-utils.
Preparing to unpack .../07-apache2-utils_2.4.52-1ubuntu4.6_amd64.deb ...
Unpacking apache2-utils (2.4.52-1ubuntu4.6) ...
Selecting previously unselected package mailcap.
Preparing to unpack .../08-mailcap_3.70+nmu1ubuntu1_all.deb ...
Unpacking mailcap (3.70+nmu1ubuntu1) ...
Selecting previously unselected package mime-support.
Preparing to unpack .../09-mime-support_3.66_all.deb ...
Unpacking mime-support (3.66) ...
Selecting previously unselected package apache2.
Preparing to unpack .../10-apache2_2.4.52-1ubuntu4.6_amd64.deb ...
Unpacking apache2 (2.4.52-1ubuntu4.6) ...
Selecting previously unselected package bzip2.
Preparing to unpack .../11-bzip2_1.0.8-5build1_amd64.deb ...
Unpacking bzip2 (1.0.8-5build1) ...
Selecting previously unselected package ssl-cert.
Preparing to unpack .../12-ssl-cert_1.1.2_all.deb ...
Unpacking ssl-cert (1.1.2) ...
Setting up libapr1:amd64 (1.7.0-8ubuntu0.22.04.1) ...
Setting up bzip2 (1.0.8-5build1) ...
Setting up ssl-cert (1.1.2) ...
Setting up liblua5.3-0:amd64 (5.3.6-1build1) ...
Setting up apache2-data (2.4.52-1ubuntu4.6) ...
Setting up mailcap (3.70+nmu1ubuntu1) ...
Setting up libaprutil1:amd64 (1.6.1-5ubuntu4.22.04.1) ...
Setting up mime-support (3.66) ...
Setting up libaprutil1-ldap:amd64 (1.6.1-5ubuntu4.22.04.1) ...
Setting up libaprutil1-dbd-sqlite3:amd64 (1.6.1-5ubuntu4.22.04.1) ...
Setting up apache2-utils (2.4.52-1ubuntu4.6) ...
Setting up apache2-bin (2.4.52-1ubuntu4.6) ...
Setting up apache2 (2.4.52-1ubuntu4.6) ...
Enabling module mpm_event.
Enabling module authz_core.
Enabling module authz_host.
Enabling module authn_core.
Enabling module auth_basic.
Enabling module access_compat.
Enabling module authn_file.
Enabling module authz_user.
Enabling module alias.
Enabling module dir.
Enabling module autoindex.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module filter.
Enabling module deflate.
Enabling module status.
Enabling module reqtimeout.
Enabling conf charset.
Enabling conf localized-error-pages.
Enabling conf other-vhosts-access-log.
Enabling conf security.
Enabling conf serve-cgi-bin.
Enabling site 000-default.
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Processing triggers for ufw (0.36.1-4ubuntu0.1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
Scanning processes...                                                        
Scanning candidates...                                                       
Scanning linux images...                                                     

Restarting services...
Service restarts being deferred:
 /etc/needrestart/restart.d/dbus.service
 systemctl restart networkd-dispatcher.service
 systemctl restart unattended-upgrades.service
 systemctl restart user@1000.service

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.

 

 

아래와 같이 보라색 바탕화면에 Ok 같은 문구가 나올 경우, 설정을 건드리지 말고 Ok만 누르면 된다.

 

 

브라우저에서 ip 주소로 접근하면 기본적으로 아파치에서 만들어놓은 Default Page가 나온다.

 

일단 간단하게 해당 문서를 수정해 보자.

$ cd /var/www/html 
$ sudo vi index.html

그러면 기본적인 HTML 문서가 나오며, Apache2 Default Page라는 부분을 변경해 보았다.

 

해당 내용을 변경 후 vi editor를 저장, 종료만 하게 되어도 아래처럼 내용이 바로 바뀌게 된다. 그 이유는, 3.35.234.104로 접근 시 /var/www/html/index.html을 응답해 주기 때문이다.

 

2) HTML 파일 서버에 올리기.

Mac, Window 막론하고 서버에 파일을 올리기 위해서는 FileZilla 프로그램을 사용하면 편하다. 아래의 페이지에서 자신의 운영체제에 맞게 FileZilla Client를 설치하면 된다.

https://filezilla-project.org/

 

 

설치 후, 왼쪽 위의 컴퓨터 모양을 클릭한다.

새 사이트를 클릭한다.

 

퍼블릭 IP, 포트 22번, 사용자 ID, 키 파일을 참고하여 설정하고 연결을 클릭한다.

 

아래와 같은 창들이 나오면 그냥 확인을 클릭하면 된다.

 

왼쪽이 로컬 컴퓨터, 오른쪽이 서버 컴퓨터이며 파일을 쉽게 드래그 앤 드롭으로 옮길 수 있다.

 

Apache2를 이용하여 HTML 파일을 서비스하기 위해 /var/www/html/ 디렉터리 안에 파일을 올려보자, 아마 권한이 없기 때문에 올라가지 않을 것이다.

 

html 디렉터리를 확인해 보면 소유자, html 파일을 올려야 하는 html 폴더 자체의 소유 그룹이 root이고 현재 계정은 ubuntu 계정이므로, 일반 사용자의 쓰기 권한(write)이 없으므로 수정을 하지 못한다.

 

다시 터미널에서 원격 접속을 하여, chmod를 이용하여 권한을 넣어준다. -R은 html 폴더뿐 아니라 폴더 내의 모든 파일(디렉터리)에 777 권한을 적용한다.

$ cd /var/www
$ ls
html
$ sudo chmod 777 html -R
$ ls -la

drwxrwxrwx  2 root root 4096 Aug 17 11:26 html

 

 

그럼 이제 간단한 html 파일을 작성하여 드래그 앤 드롭으로 올려보자.

 

아까와 다르게 잘 올라간다. 이번에는 index.html이 아니고 test.html이기 때문에 아래와 같이 브라우저에서 접근해 보자.

잘 나온다!

 

 

 

service 명령어를 사용하면, 현재 백그라운드에서 실행 중인 서비스들을 제어 가능하다. Apache2는 현재 웹 페이지 문서를 80포트에 대해서 요청이 오면 응답해 주는 역할을 한다. 한번 Apache2 프로그램을 종료해 보자.

$ sudo service apache2 stop
$ sudo service apache2 status
○ apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Thu 2023-08-17 11:45:21 UTC; 1s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 33894 ExecStop=/usr/sbin/apachectl graceful-stop (code=exited, status=0/SUCCESS)
   Main PID: 32639 (code=exited, status=0/SUCCESS)
        CPU: 588ms

 

그러면 이제 접근되지 않는다.

 

 

다시 Apache2 프로그램을 돌려보자.

$ sudo service apache2 start
$ sudo service apache2 status
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2023-08-17 11:46:35 UTC; 2s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 33935 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 33939 (apache2)
      Tasks: 55 (limit: 1141)
     Memory: 4.7M
        CPU: 24ms
     CGroup: /system.slice/apache2.service
             ├─33939 /usr/sbin/apache2 -k start
             ├─33944 /usr/sbin/apache2 -k start
             └─33945 /usr/sbin/apache2 -k start

 

Apach2로 인해 이제 다시 80 포트에 대한 요청 및 응답이 가능해진다.

 

 

3) HTTP Server

우리는 지금까지 간단한 Web Server를 구현한 것이다. 이러한 HTTP Server의 경우 다음과 같은 특징이 있다.

 

 

HTTP Server(예: Apache, Nginx)는 웹 서비스를 제공하는 데 있어 다른 역할을 합니다. HTTP Server는 주로 정적인 컨텐츠를 제공하는 데 특화되어 있다. HTTP Server만을 사용하여 웹 서비스를 배포했을 때의 특징은 다음과 같다.

 

  1. 정적 컨텐츠 제공: HTTP Server는 HTML, CSS, JavaScript, 이미지와 같은 정적 파일을 효율적으로 제공한다.
  2. 빠른 응답 시간: 동적인 처리가 없기 때문에, 정적 파일을 빠르게 로드하고 클라이언트에게 전송할 수 있다.
  3. 낮은 리소스 사용: 동적 컨텐츠를 처리하는 WAS에 비해, HTTP Server는 상대적으로 적은 메모리와 CPU를 사용한다.
  4. 간단한 구성: 동적 애플리케이션 로직이나 데이터베이스 연결 없이 웹 페이지를 제공하기 때문에 설정이 간단하다.
  5. 확장성: 정적 파일 제공에 최적화된 HTTP Server는 대량의 트래픽을 처리하는 데 유리하며, 캐싱과 같은 기능을 활용하여 성능을 더욱 향상시킬 수 있다.
  6. 제한된 기능: 동적인 기능(예: 사용자 인증, 데이터베이스 쿼리, 서버 사이드 로직 처리)을 제공하려면 WAS가 필요하다. HTTP Server만 사용하는 경우 이러한 기능을 제공하기 어렵다.
  7. 보안: 동적 처리가 없기 때문에 일부 보안 위협(예: SQL 인젝션)에서는 안전할 수 있지만, HTTP Server 자체의 보안 설정과 관련된 취약점에는 주의해야 한다.
  8. 캐싱 최적화: HTTP Server는 정적 컨텐츠의 캐싱을 쉽게 구성할 수 있어, 반복적인 요청에 대한 응답 시간을 크게 줄일 수 있다.

 

결론적으로, HTTP Server만을 사용하여 웹 서비스를 배포하는 것은 정적인 웹 페이지나 컨텐츠를 제공하는 데 적합하다. 동적인 기능이나 복잡한 애플리케이션 로직이 필요한 경우 WAS와 함께 사용하는 것이 좋다.

 

또한 apache로 대비되는 것에는 nginx가 있다. nginx는 최신 배포에 사용되고 매우 빠르다. 하지만 그렇다고 해서 apache가 안 좋기만 한 것은 아니다 apache도 꾸준히 업데이트되고 있다.

 

4) Apache 설정

아래의 디렉터리에서 apache에 대한 설정이 가능하다.

cd /etc/apache2

 

Apache2는 매우 유연하고 확장 가능한 웹 서버로, 다양한 구성 파일과 디렉터리를 사용하여 서버의 동작을 정의하고 관리한다. 아래는 주요 구성 파일 및 디렉터리에 대한 간략한 설명이다.

 

  • apache2.conf
    • Apache2의 주 구성 파일이다.
    • 전반적인 서버 설정, 모듈, 전역 환경, 디렉터리 설정 등을 포함한다.
    • 이 파일은 다른 구성 파일을 포함(Include 지시문 사용)하여 서버의 전반적인 동작을 정의한다.
  • ports.conf
    • Apache2가 어떤 IP 주소와 포트에서 수신 대기할지를 정의하는 파일이다.
    • 기본적으로 80 포트(HTTP)와 443 포트(HTTPS)를 사용하도록 설정되어 있다.
  • conf-available / conf-enabled
    • conf-available: 웹 서버의 전반적인 동작과 관련된 추가 구성 파일을 포함하는 디렉터리이다.
    • conf-enabled: conf-available에서 활성화된 구성의 심볼릭 링크를 포함하는 디렉터리이다.
    • a2enconf와 a2disconf 명령을 사용하여 구성을 활성화하거나 비활성화할 수 있다.
  • mods-available / mods-enabled
    • mods-available: 사용 가능한 모든 Apache2 모듈의 구성 파일과 로드 파일을 포함하는 디렉터리이다.
    • mods-enabled: mods-available에서 활성화된 모듈의 심볼릭 링크를 포함하는 디렉터리이다.
    • a2enmod와 a2dismod 명령을 사용하여 모듈을 활성화하거나 비활성화할 수 있다.
  • sites-available / sites-enabled: 
    • sites-available: 사용 가능한 모든 웹 사이트(가상 호스트)의 구성 파일을 포함하는 디렉터리이다.
      • 이 디렉터리에는 사용 가능한 모든 웹 사이트의 설정 파일들이 포함되어 있다. 즉, 웹 서버에서 호스팅 하려는 모든 웹 사이트에 대한 설정 파일을 여기에 저장할 수 있다. 그러나 이 디렉터리에 있는 설정은 자동으로 활성화되지 않는다. 설정을 활성화하려면 해당 설정 파일을 sites-enabled 디렉터리로 링크해야 한다.
    • sites-enabled: sites-available에서 활성화된 웹 사이트의 심볼릭 링크를 포함하는 디렉터리이다.
      • 이 디렉터리에는 현재 활성화된 웹 사이트의 설정 파일들이 심볼릭 링크로 포함되어 있다. Apache 웹 서버는 시작할 때 이 디렉터리의 설정 파일들을 읽어서 해당 웹 사이트들을 서비스한다.
    • a2ensite와 a2dissite 명령을 사용하여 웹 사이트를 활성화하거나 비활성화할 수 있다.

 

이러한 구성 파일 및 디렉터리 구조를 통해 Apache2는 웹 서버의 설정을 모듈화하고, 개별 사이트, 모듈, 추가 구성을 쉽게 관리하고 활성화/비활성화할 수 있다. 일반적인 작업 흐름은 다음과 같다.

 

  1. 웹 사이트의 설정 파일을 sites-available에 작성한다.
  2. a2ensite 명령어를 사용하여 해당 설정을 활성화하면, 설정 파일에 대한 심볼릭 링크가 sites-enabled에 생성된다.
  3. a2dissite 명령어를 사용하여 설정을 비활성화하면, sites-enabled에서 해당 심볼릭 링크가 제거된다.

 

이러한 구조를 사용함으로써, 사용자는 여러 웹 사이트 설정을 쉽게 관리하고, 필요에 따라 웹 사이트를 활성화 또는 비활성화할 수 있다.

728x90
반응형
Comments