본문 바로가기

리눅스

[draft] 우분투 22.04에서 Apache를 소스 컴파일하여 설치하는 방법

우분투 22.04에서 Apache HTTP Server를 소스 컴파일하여 설치하는 방법

Apache HTTP Server 는 가장 널리 사용되는 오픈소스 웹 서버 중 하나이다. 일반적으로 Ubuntu에서는 apt 패키지 관리자를 통해 설치하지만, 특정 모듈 활성화 또는 최신 버전 사용을 위해 소스 컴파일 방식으로 설치할 수도 있다.

1. 의존성 패키지 설치

컴파일에 필요한 빌드 도구와 라이브러리를 먼저 설치한다.

sudo apt-get update
sudo apt-get install -y build-essential
sudo apt-get install -y zlib1g-dev libssl-dev libpcre3-dev
sudo apt-get install -y libnghttp2-dev wget

2. Apache 설치

2.1 Apache 소스 코드 다운로드

  • Apache 소스 코드를 다운로드할 작업 디렉토리로 이동한다.
cd /usr/local/src
  • Apache HTTP Server 다운로드
wget -q https://dlcdn.apache.org/httpd/httpd-2.4.59.tar.gz

2.2 APR 및 APR-util 다운로드

  • Apache는 내부적으로 APR(Apache Portable Runtime) 라이브러리를 사용한다.

apr_recommended

  • APR 다운로드
wget -q --no-check-certificate https://dlcdn.apache.org/apr/apr-1.7.4.tar.gz
  • APR-util 다운로드
wget -q --no-check-certificate https://dlcdn.apache.org/apr/apr-util-1.6.3.tar.gz

2.3 Apache 소스 압축 해제

tar -xzf httpd-2.4.59.tar.gz

2.4 APR 디렉토리 구성

  • Apache 소스 내부에 APR 라이브러리를 포함하도록 디렉토리를 생성한다.
mkdir httpd-2.4.59/srclib/{apr,apr-util}
  • APR 압축 해제
tar xfz apr-1.7.4.tar.gz -C httpd-2.4.59/srclib/apr --strip-components=1
  • APR-util 압축 해제
tar xfz apr-util-1.6.3.tar.gz -C httpd-2.4.59/srclib/apr-util --strip-components=1

2.5 Apache 소스 디렉토리 이동

cd httpd-2.4.59

2.6 Configure 설정

  • 컴파일 옵션을 지정하여 configure 스크립트를 실행한다.
./configure \
--prefix=/usr/local/apache2 \
--bindir=/usr/local/apache2/bin \
--sbindir=/usr/local/apache2/sbin \
--sysconfdir=/usr/local/apache2/conf \
--enable-http2 \
--enable-so \
--enable-rewrite \
--enable-mods-shared=all \
--enable-ssl \
--with-included-apr \
--with-mpms-shared=all \
--with-mpm=worker

2.7 컴파일 및 설치

  • CPU 코어를 활용하여 병렬 빌드를 진행한다.
make -j $(( $(nproc) / 2 ))
  • 설치
sudo make install

2.8 Apache 버전 확인

  • 설치가 완료되면 버전 정보를 확인한다.
/usr/local/apache2/sbin/apachectl -v
Server version: Apache/2.4.59 (Unix)
Server built:   May 10 2024 08:33:40
  • 빌드 옵션 확인
/usr/local/apache2/sbin/apachectl -V
Server version: Apache/2.4.59 (Unix)
Server built:   May 10 2024 08:33:40
Server's Module Magic Number: 20120211:131
Server loaded:  APR 1.7.4, APR-UTIL 1.6.3, PCRE 8.39 2016-06-14
Compiled using: APR 1.7.4, APR-UTIL 1.6.3, PCRE 8.39 2016-06-14
Architecture:   64-bit
Server MPM:     worker
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_PROC_PTHREAD_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/usr/local/apache2"
 -D SUEXEC_BIN="/usr/local/apache2/bin/suexec"
 -D DEFAULT_PIDLOG="logs/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

3. 설치 디렉토리 구조 확인

tree -L 1 /usr/local/apache2
/usr/local/apache2
├── bin
├── build
├── cgi-bin
├── conf
├── error
├── htdocs
├── icons
├── include
├── lib
├── logs
├── man
├── manual
├── modules
└── sbin

14 directories, 0 files

4. Apache 설정 수정

설정 파일 백업

sudo cp /usr/local/apache2/conf/httpd.conf /usr/local/apache2/conf/httpd.conf_$(date +"%Y%m%d-%H%M%S")
sudo cp /usr/local/apache2/conf/extra/httpd-vhosts.conf \
/usr/local/apache2/conf/extra/httpd-vhosts.conf_$(date +"%Y%m%d-%H%M%S")
sudo cp /usr/local/apache2/conf/extra/httpd-ssl.conf \
/usr/local/apache2/conf/extra/httpd-ssl.conf_$(date +"%Y%m%d-%H%M%S")

ServerName 설정

sudo sed -i 's/^#ServerName.*/ServerName localhost/' /usr/local/apache2/conf/httpd.conf

주요 모듈 활성화

  • ssl, socache_shmcb, rewrite, http2 모듈 활성화
sed -i 's/^#\(LoadModule ssl_module modules\/mod_ssl.so\)/\1/' /usr/local/apache2/conf/httpd.conf
sed -i 's/^#\(LoadModule socache_shmcb_module modules\/mod_socache_shmcb.so\)/\1/' /usr/local/apache2/conf/httpd.conf
sed -i 's/^#\(LoadModule rewrite_module modules\/mod_rewrite.so\)/\1/' /usr/local/apache2/conf/httpd.conf
sed -i 's/^#\(LoadModule http2_module modules\/mod_http2.so\)/\1/' /usr/local/apache2/conf/httpd.conf

5. 가상 호스트(VirtualHost) 테스트

테스트용 웹 페이지 생성

sudo mkdir -p /usr/local/apache2/htdocs/site1
echo "<html><body><h1>Welcome to the website</h1></body></html>" \
  > /usr/local/apache2/htdocs/site1/index.html

가상 호스트(httpd-vhosts.conf) 설정

vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

SSL(httpd-ssl.conf) 설정

vim /usr/local/apache2/conf/extra/httpd-ssl.conf

6. Apache 실행

Apache 구성 파일 구문 검사

/usr/local/apache2/sbin/apachectl -t
Syntax OK

Apache 실행

sudo /usr/local/apache2/bin/apachectl start

7. 방화벽 설정

방화벽에서 포트 80(HTTP)과 443(HTTPS)를 허용합니다.

8. 웹 브라우저 테스트

브라우저에서 다음 주소 접속

http://localhost

또는

http://서버_IP

웹 페이지가 정상적으로 표시되면 설치가 완료된 것이다.

9. Apache 모듈 확인

로드된 모듈 확인

/usr/local/apache2/sbin/apachectl -M
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_worker_module (static)
 authn_file_module (shared)
 authn_core_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_core_module (shared)
 access_compat_module (shared)
 auth_basic_module (shared)
 reqtimeout_module (shared)
 filter_module (shared)
 mime_module (shared)
 log_config_module (shared)
 env_module (shared)
 headers_module (shared)
 setenvif_module (shared)
 version_module (shared)
 ssl_module (shared)
 http2_module (shared)
 unixd_module (shared)
 status_module (shared)
 autoindex_module (shared)
 dir_module (shared)
 alias_module (shared)
 rewrite_module (shared)

 

컴파일을 통해 Apache2를 설치하는 것은 패키지 관리자를 통해 설치하는 것보다 더 많은 관리 부담이 따릅니다. 시스템에 새로운 버전을 설치하거나 업데이트할 때 의존성 문제가 발생할 수 있습니다.

 

참고URL

- Apache : HTTP Server Project

- Apache : Portable Runtime (APR) Project