본문 바로가기
자격증

[리눅스마스터 1급] 10. 웹 서비스 (Apache)

by LimSeongHyeon 2025. 4. 28.


흐름

파일/디렉토리 주제 주요 설정
httpd.conf 패치 기본 설정 Listen, ServerName, DocumentRoot, DirectoryIndex, ErrorDocument,  접근제어
conf.modules.d/*.conf 모듈 로드 mod_userdir.so, mod_vhost_alias.so 등
conf.d/*.conf 세부 기능 설정 ssl.conf (SSL), welcome.conf (기본 페이지), 인증관련 conf 파일들
conf/extra/httpd-userdir.conf 사용자 디렉터리 설정 UserDir 설정, <Directory /home/*/www> 접근 허용
conf/extra/httpd-vhosts.conf 가상호스트 설정  설정, ServerName별 DocumentRoot 분기
.htaccess 인증 설정
(UserDir 내에서 활용)
AllowOverride AuthConfig, 인증설정

 


기본 설정 (httpd.conf)

항목 설명 예시
ServerName 서버 로터의 대신으로 표시할 공용 단위를 설정 ServerName www.ihd.or.kr:80
Listen 서버가 수신할 포트 번호 Listen 80, Listen 8080
DocumentRoot 메인 문서 디렉터리 경로 DocumentRoot "/usr/local/apache/htdocs"
DirectoryIndex 여러 파일이 있을 때 개인적으로 로드할 파일 DirectoryIndex index.html index.htm index.php
ErrorDocument 서버 에러 발생 시, 보여줄 파일 설정 ErrorDocument 404 /error_pages/404.html
LoadModule 모듈 활성화 LoadModule userdir_module modules/mod_userdir.so
Include 관련 설정 파일 포함 Include conf/extra/httpd-userdir.conf
Directory 특정 디렉토리에 권한, 인증, 옵션을 설정 <Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

 

httpd -L 활용
$ httpd -L | grep -i servername
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
ServerName (core.c)
	How to work out the ServerName : Port when constructing URLs

 

 


 

사용자 디렉터리 설정 (UserDir)

키워드 설명 예시
UserDir 사용자 문서 디렉터리 명칭 UserDir www

 

작업 순서

순서 항목 내용 (예시)
1 mod_userdir 모듈 활성화 LoadModule userdir_module modules/mod_userdir.so
2 설정 파일 로드 Include conf/extra/httpd-userdir.conf
3 설정 파일 수정 UserDir public_html
$ locate -i userdir
/etc/httpd/conf.d/userdir.conf
/usr/lib64/httpd/modules/mod_userdir.so

 

설정 파일 수정
<Directory "/home/*/public_html">
    AllowOverride FileInfo AuthConfig Limit
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS
</Directory>
설정 파일 수정 시, UserDir로 허용한 디렉토리 및 파일에 대해서 접근 허용 설정을 해준다.

가상 호스트 (VirtualHost) 설정

항목 설명 예시
VirtualHost 호스트 구분 설정 <VirtualHost 192.168.5.13:80>
ServerAdmin 관리자 이메일 ServerAdmin admin@ihd.or.kr
DocumentRoot 가상 호스트의 문서 경로 DocumentRoot "/usr/local/apache/htdocs"
관련 모듈 mod_vhost_alias 모듈 LoadModule vhost_alias_module modules/mod_vhost_alias.so
관련 설정 httpd-vhosts.conf 포함 Include conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /var/www/example

    ErrorLog logs/example-error_log
    CustomLog logs/example-access_log combined
</VirtualHost>

 

httpd -L 활용
$ httpd -L | grep -i host
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
<VirtualHost (core.c)
	Container to map directives to a particular virtual host, takes one or more host addresses
    ...


$ httpd -L | grep -i admin
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
ServerAdmin (core.c)
	The email address of the server administrator
    
    
$ httpd -L | grep -i document
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
DocumentRoot (core.c)
	Root directory of the document tree
    ...

 


 

인증 설정 (Authentication)

항목 설명 예시
htpasswd 사용자 인증 파일 생성 htpasswd -c /etc/password ihduser
AllowOverride .htaccess 파일 사용 허용 AllowOverride AuthConfig
.htaccess 인증 관련 파일 작성 /usr/local/apache/htdocs/.htaccess

 

작업 순서

순번 내용 설명
1 인증 계정 파일 생성 htpasswd -c 명령어로 최초 사용자 아이디/비밀번호 저장
2 .htaccess 또는 Directory 설정 작성 인증 방식을 명시하고 사용자 파일 경로를 지정
3 AllowOverride 설정 허용 .htaccess 파일 사용을 허용하기 위해 AllowOverride AuthConfig 지정
4 파일 퍼미션 설정 Apache가 사용자 파일과 웹 파일을 읽을 수 있게 권한 조정
5 설정 문법 검사 및 적용 apachectl configtest → systemctl reload httpd

 

htpasswd (HTTP + Password)
htpasswd는 HTTP 프로토콜용 비밀번호를 관리하는 도구이다. man 페이지를 살펴보면 "htpasswd - Manage user files for basic authentication"라고 정의 되어있다. 기본적으로 특정 디렉토리 안에 유저별로 비밀번호 정보를 보관하고 관리한다. 때문에 해당 명령어를 사용할 때, 비밀번호 정보가 관리되고 있는 디렉토리를 제시해 주어야 한다.

 

.htaccess
Apache가 특정 디렉터리에 접근할 때 적용할 로컬 설정 파일이며 설정 파일에서 AllowOverride가 허용된 디렉터리에서만 작동한다. 통상 htpasswd로 인해 처음 User Password 디렉토리가 지정될때, 해당 디렉토리에 생성된다. 주요 사용 목적에는 사용자 인증, 리다이렉션, 접근 제한, URL 재작성, MIME 타입 설정, 디렉터리 인덱싱 설정 등이 있다.

 

예시

사용자 인증 설정 AuthType Basic
AuthName "Private"
AuthUserFile /etc/httpd/conf/.htpasswd
Require valid-user
특정 파일 접근 금지 <Files secret.txt>
    Order Allow,Deny
    Deny from all
</Files>
기본 문서 변경 DirectoryIndex index.php index.html
URL 리다이렉트 Redirect /old.html /new.html

 


 

오류 페이지 (ErrorDocument)

항목 설명 예시
ErrorDocument 오류에 따라 페이지 및 메시지 지정 ErrorDocument 404 /not_found.html
ErrorDocument 404 "죄송합니다. 페이지를 찾을 수 없습니다."

 

설정 가능 위치
httpd.conf (전체 서버 기본 설정) 전체 사이트 공통 적용
vhosts.conf (가상호스트별 설정) 도메인마다 다른 에러 페이지 적용 가능
.htaccess 파일 특정 디렉터리만 별도로 설정 가능 (단 AllowOverride FileInfo 필요)

 


 

접근 제어 (Access Control)

누구는 접속할 수 있고, 누구는 접속하면 안 된다.

항목 설명 예시
Order Deny,Allow 순서의 설정 Order Deny,Allow
Deny from 거부할 IP/도메인 명시 Deny from All
Allow from 허용할 IP/도메인 명시 Allow from 192.168.22.0/24

 

예시
# Apache 2.4 이전
Order Deny,Allow
Deny from All
Allow from 192.168.22.0/24

# Apache 2.4 이후
Require all denied
Require ip 192.168.22.0/24
기본적으로 모두 거부, 192.168.22.0/24 대역은 허용  → White-List 스타일로 작동됨

 

# Apache 2.4 이전
Order Allow,Deny
Allow from All
Deny from 192.168.1.100

# Apache 2.4 이후
Require all granted
Require not ip 192.168.1.100
기본적으로 모두 허용, 192.168.1.100은 거부 → Black-List 스타일로 작동됨

 

 


 

패치 설정 변경 후 필요 작업

목표 명령
문법 검증 apachectl configtest 또는 httpd -t
변경 데이터 회수 apachectl graceful 또는 httpd -k graceful
서비스 재시작 systemctl restart httpd
프로세스 재시작 apachectl restart 또는 httpd -k restart
systemctl을 이용한 재시작은 서비스 데몬 자체가 재시작 되지만 apachectl, httpd 명령어를 사용하는 경우 서비스는 그대로 켜져있고 서비스 내부 프로세스가 재시작하는 방식으로 진행된다.

문제

메인 서버 설정 (httpd.conf) 및 기본 설정

 

메인 설정 파일 절대경로를 작성하세요.

더보기

# Apache 2.4 이전

/usr/local/apache/conf/httpd.conf

 

# Apache 2.4 이후

/etc/httpd/conf/httpd.conf

 

도메인 www.ihd.or.kr의 80포트로 접속할 수 있는 설정 방법을 작성하세요.

더보기

ServerName www.ihd.or.kr:80

 

웹 문서 저장 경로를 /usr/local/apache/html로 변경하세요.

더보기

DocumentRoot "/usr/local/apache/html"

 

index.htm → index.html index.php 순서로 파일을 찾아 제공하는 설정하는 방법을 작성하세요.

더보기

DirectoryIndex index.htm index.html index.php

 

서버가 8080 포트로 요청을 수신하도록 설정하세요.

더보기

Listen 8080

 


 

접근 제어 설정

 

[ Apache 2.4 이전 기준 ]

기본적으로 모든 접근을 차단하고, 특정 IP 대역 (192.168.22.0/24)에 속한 클라이언트만 접속할 수 있도록 웹서버 접근 통제 정책을 설정하세요.

더보기

Order Deny,Allow

Deny from All

Allow from 192.168.22.0/24

 

[ Apache 2.4 이전 기준 ]

기본적으로 모든 접근을 허용하고, 특정 IP 대역 (192.168.22.0/24)에 속한 클라이언트만 차단할 수 있도록 웹서버 접근 통제 정책을 설정하세요.

더보기

Order Allow, Deny

Allow from All

Deny from 192.168.22.0/24

 

[ Apache 2.4 이후 기준 ]

기본적으로 모든 접근을 차단하고, 특정 IP 대역 (192.168.22.0/24)에 속한 클라이언트만 접속할 수 있도록 웹서버 접근 통제 정책을 설정하세요.

더보기

Require All denied

Require ip 192.168.22.0/24

 

[ Apache 2.4 이후 기준 ]

기본적으로 모든 접근을 허용하고, 특정 IP 대역 (192.168.22.0/24)에 속한 클라이언트만 차단할 수 있도록 웹서버 접근 통제 정책을 설정하세요.

더보기

Require All granted

Require not ip 192.168.22.0/24

 

 


 

사용자 디렉터리 설정 (UserDir)

 

웹서버가 각 사용자의 홈 디렉터리 안에 있는 개인 웹페이지를 서비스할 수 있도록, 관련 모듈을 활성화하세요.

더보기

LoadModule userdir_module modules/mod_userdir.so

 

사용자별 웹 디렉터리 기능을 설정하기 위해 추가로 포함해야 할 설정 파일의 전체 경로를 작성하세요.

더보기

# Apache 2.4 이전

/usr/local/apache/conf/extra/httpd-userdir.conf

 

# Apache 2.4 이후

/etc/httpd/conf.d/userdir.conf

 

사용자의 홈 디렉터리 하위에 위치한 웹문서 디렉터리 이름(public_html)을 지정하는 설정 지시어를 작성하세요.

더보기

UserDir public_html

 


 

가상호스트 설정 (VirtualHost)

 

하나의 서버에서 여러 도메인을 운영할 수 있도록, 가상호스트 기능을 활성화할 수 있도록, 관련 모듈을 활성화하세요.

더보기

LoadModule vhost_alias_module modules/mod_vhost_alias.so

 

가상호스트 설정을 적용하기 위해 추가로 포함해야 할 설정 파일 경로를 작성하세요.

더보기

# Apache 2.4 이전

/usr/local/apache/conf/extra/httpd-vhosts.conf

 

# Apache 2.4 이후

/usr/share/doc/httpd-core/httpd-vhosts.conf

 

84 포트로 접속하는 내용을 정의하기 위한 가상호스트를 설정하는 지시어를 작성하세요.

더보기

<VirtualHost *:84>

</VirtualHost>

 

특정 IP(192.168.0.10)의 80 포트로 접속하는 내용을 정의하기 위한 가상호스트를 설정하는 지시어를 작성하세요.

더보기

<VirtualHost 192.168.0.10:80>

</VirtualHost>

 

www.example.com으로 접근하고, 80 포트로 접속하는 내용을 정의하기 위한  가상호스트를 설정하는 지시어를 작성하세요.

더보기

<VirtualHost *:80>

ServerName www.example.com

</VirtualHost>


 

오류 페이지 설정 (ErrorDocument)

 

사용자가 존재하지 않는 페이지에 접근했을 때 제공할 별도의 404 오류 페이지를 설정하는 방법을 작성하세요.

더보기

ErrorDocument 404 /not_found.html

 


 

사용자 인증 설정

 

Basic Authentication을 위한 유저 파일을 관리하는 명령어를 작성하세요.

더보기

htpasswd

 

인증 사용자 파일을 특정 경로(/etc/httpd/conf/.htpasswd)에 새로 생성하면서, 사용자 계정을 추가하는 명령어를 작성하세요.

더보기

htpasswd -c /etc/httpd/conf/.htpasswd ihduser

 

디렉터리 접근 제한을 위해 .htaccess 파일 사용을 허용하도록 설정하세요.

더보기

AllowOverride AuthConfig

 

디렉터리 인증 설정 시, 생성해야 할 혹은 생성되는 설정 파일명을 작성하세요.

더보기

.htaccess

 


 

웹서버 제어 명령

 

웹서버의 변경된 설정을 적용하기 위해 서버를 완전히 재시작하는 명령어를 작성하세요.

더보기

httpd -k restart

apachectl restart

/usr/local/apache/bin/apachectl restart

 

웹서버 데몬을 수동으로 실행하는 명령어를 작성하세요.

더보기

httpd -k start

apachectl start

/usr/local/apache/bin/apachectl start

 


 

설정 적용/점검 명령어

 

설정 파일 변경 후 문법 오류를 사전에 점검하는 명령어를 작성하세요.

더보기

httpd -t

service httpd configtest

 

웹서버를 중단하지 않고 설정 변경 사항을 적용하는 명령어를 작성하세요.

더보기

httpd -k graceful

service httpd graceful