이 단계에서는 비표준 디렉토리에서 웹 콘텐츠를 제공하고 SELinux 파일 컨텍스트를 관리하도록 Apache 를 구성하는 방법을 배우게 됩니다. 기본적으로 SELinux 정책은 Apache (httpd) 가 특정 디렉토리, 주로 /var/www/html에서만 파일을 제공하도록 제한합니다. 웹 콘텐츠를 다른 위치에 배치하면 파일 시스템 권한이 올바르더라도 SELinux 가 Apache 가 해당 콘텐츠에 접근하는 것을 방지합니다. 여기서 SELinux 파일 컨텍스트가 작용합니다.
SELinux 파일 컨텍스트는 파일 또는 디렉토리에 적용되어 해당 보안 속성을 정의하는 레이블입니다. Apache 가 사용자 정의 위치에서 콘텐츠를 제공하려면 해당 위치와 해당 콘텐츠에 올바른 SELinux 컨텍스트, 일반적으로 httpd_sys_content_t가 있어야 합니다. semanage fcontext를 사용하여 영구 규칙을 정의하고 restorecon을 사용하여 적용합니다.
먼저, Apache HTTP 서버를 설치해야 합니다.
-
Apache HTTP 서버 설치.
dnf 패키지 관리자를 사용하여 httpd 패키지를 설치합니다.
sudo dnf install -y httpd
httpd 패키지 및 해당 종속성의 성공적인 설치를 나타내는 출력이 표시되어야 합니다.
-
웹 콘텐츠용 사용자 정의 디렉토리와 index.html 파일 생성.
/custom이라는 새 디렉토리를 만들고 그 안에 간단한 index.html 파일을 배치합니다. 이것이 비표준 웹 문서 루트가 됩니다.
sudo mkdir /custom
echo 'This is custom web content.' | sudo tee /custom/index.html
index.html 파일의 내용을 확인합니다.
cat /custom/index.html
This is custom web content.
-
새 문서 루트를 사용하도록 Apache 구성.
Apache 의 주요 구성 파일은 /etc/httpd/conf/httpd.conf입니다. Apache 가 기본 /var/www/html 대신 새 /custom 디렉토리를 가리키도록 이 파일을 편집해야 합니다.
nano를 사용하여 구성 파일을 엽니다.
sudo nano /etc/httpd/conf/httpd.conf
편집기 내에서 DocumentRoot "/var/www/html" 및 <Directory "/var/www/html"> 줄을 찾습니다. /var/www/html의 두 항목을 모두 /custom으로 변경합니다.
수정 후 관련 섹션은 다음과 같이 표시되어야 합니다.
#
## DocumentRoot: The directory out of which you will serve your
## documents. By default, all requests are taken from this directory, but
## symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/custom"
#
## Relax access to content within /var/www.
#
<Directory "/custom">
AllowOverride None
## Allow open access:
Require all granted
</Directory>
파일을 저장하고 종료합니다 (Ctrl+X, Y, Enter).
-
Apache 웹 서비스를 시작하고 활성화합니다.
구성을 수정한 후 httpd 서비스를 시작해야 합니다. 컨테이너 환경에 있으므로 systemctl을 사용할 수 없습니다. httpd를 직접 시작합니다.
sudo /usr/sbin/httpd -DFOREGROUND &
& 기호는 명령을 백그라운드에서 실행하여 터미널을 계속 사용할 수 있도록 합니다. Apache 가 시작됨을 나타내는 다음과 유사한 출력이 표시되어야 합니다.
[1] 5094
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::216:3eff:fe00:63b%eth0. Set the 'ServerName' directive globally to suppress this message
참고: 서버의 정규화된 도메인 이름에 대한 경고 메시지는 이 랩 환경에서 정상이며 안전하게 무시할 수 있습니다.
Apache 가 실행 중인지 확인하려면 httpd 프로세스를 확인하면 됩니다.
ps aux | grep httpd
여러 httpd 프로세스가 실행 중인 것을 볼 수 있습니다.
root ... /usr/sbin/httpd -DFOREGROUND
apache ... /usr/sbin/httpd -DFOREGROUND
...output omitted...
-
웹 페이지에 접근을 시도합니다.
이제 curl을 사용하여 웹 페이지에 접근해 봅니다. 동일한 시스템에 있으므로 localhost를 사용할 수 있습니다.
curl http://localhost/index.html
참고: 이 특정 환경에서는 root_t 컨텍스트로도 웹 페이지에 접근할 수 있음을 알 수 있습니다. 이는 SELinux 가 시행 중이지만 root_t 컨텍스트가 예상보다 더 넓은 권한을 가질 수 있음을 보여줍니다. 그러나 보안 모범 사례 및 적절한 SELinux 구성을 위해 웹 콘텐츠는 여전히 적절한 httpd_sys_content_t 컨텍스트를 사용해야 합니다.
This is custom web content.
-
사용자 정의 디렉토리의 현재 SELinux 컨텍스트를 확인합니다.
ls -Z 명령을 사용하여 /custom 디렉토리와 index.html 파일의 SELinux 컨텍스트를 확인합니다.
ls -Zd /custom /custom/index.html
root_t 컨텍스트가 있음을 알 수 있는데, 이는 Apache 웹 콘텐츠에 권장되는 컨텍스트가 아닙니다.
system_u:object_r:root_t:s0 /custom
system_u:object_r:root_t:s0 /custom/index.html
기본 Apache 문서 루트와 비교합니다.
ls -Zd /var/www/html
/var/www/html에 httpd_sys_content_t 컨텍스트가 있음을 알 수 있습니다. 이것이 사용자 정의 디렉토리에 적용해야 하는 컨텍스트입니다.
system_u:object_r:httpd_sys_content_t:s0 /var/www/html
-
/custom에 대한 영구 SELinux 파일 컨텍스트 규칙을 정의합니다.
semanage fcontext 명령은 SELinux 파일 컨텍스트 매핑 규칙을 관리하는 데 사용됩니다. -a 옵션은 새 규칙을 추가하고, -t는 대상 유형을 지정하며, 정규식 '/custom(/.*)?'는 /custom 디렉토리 자체와 그 안에 있는 모든 파일 및 하위 디렉토리를 일치시킵니다.
sudo semanage fcontext -a -t httpd_sys_content_t '/custom(/.*)?'
이 명령은 SELinux 정책에 규칙을 추가하지만 기존 파일의 컨텍스트를 즉시 변경하지는 않습니다.
-
새 SELinux 컨텍스트를 파일에 적용합니다.
restorecon 명령은 파일 및 디렉토리의 SELinux 컨텍스트를 정책에 정의된 기본값으로 복원하는 데 사용됩니다. -R 옵션은 변경 사항을 재귀적으로 적용하고, -v는 자세한 출력을 제공합니다.
sudo restorecon -Rv /custom
/custom 및 /custom/index.html의 컨텍스트가 다시 레이블링되었음을 나타내는 출력이 표시되어야 합니다.
Relabeled /custom from system_u:object_r:root_t:s0 to system_u:object_r:httpd_sys_content_t:s0
Relabeled /custom/index.html from system_u:object_r:root_t:s0 to system_u:object_r:httpd_sys_content_t:s0
ls -Z를 사용하여 컨텍스트를 다시 확인합니다.
ls -Zd /custom /custom/index.html
이제 httpd_sys_content_t 컨텍스트가 있어야 합니다.
system_u:object_r:httpd_sys_content_t:s0 /custom
system_u:object_r:httpd_sys_content_t:s0 /custom/index.html
-
웹 페이지에 다시 접근합니다.
SELinux 컨텍스트가 올바르게 설정되었으므로 curl을 사용하여 웹 페이지에 다시 접근해 봅니다.
curl http://localhost/index.html
이제 index.html 파일의 내용을 볼 수 있습니다.
This is custom web content.
마지막으로 Apache HTTP 서버 프로세스를 중지합니다.
sudo pkill httpd
httpd 프로세스가 실행 중이지 않은지 확인합니다.
ps aux | grep httpd
grep 프로세스 자체만 표시되어야 합니다.
labex ... grep httpd