일반적으로 아파치 웹서버에서는 .php 확장자를 가진 파일에서만 PHP 코드가 실행됩니다. 하지만 때로는 .html 파일에서도 PHP 코드를 실행해야 하는 경우가 있습니다. 오늘은 아파치 설정을 통해 HTML 파일에서 PHP 코드를 인식하도록 하는 방법을 알아보겠습니다.

🎯 언제 이 설정이 필요한가요?

  • 기존 HTML 파일에 PHP 기능을 추가해야 할 때
  • URL에서 .php 확장자를 숨기고 싶을 때
  • 레거시 시스템에서 HTML 파일에 동적 기능이 필요할 때
  • SEO 목적으로 URL 구조를 유지해야 할 때

📁 설정 파일 위치

아파치의 MIME 타입 설정은 다음 파일에서 관리됩니다:

/etc/apache2/mods-enabled/mime.conf

🔧 현재 설정 확인

먼저 현재 설정 파일의 내용을 확인해보겠습니다:

sudo nano /etc/apache2/mods-enabled/mime.conf

기본 설정 내용:

#AddHandler cgi-script .cgi

        #
        # For files that include their own HTTP headers:
        #
        #AddHandler send-as-is asis

        #
        # For server-parsed imagemap files:
        #
        #AddHandler imap-file map

        #
        # For type maps (negotiated resources):
        # (This is enabled by default to allow the Apache "It Worked" page
        #  to be distributed in multiple languages.)
        #
        AddHandler type-map var

        #
        # Filters allow you to process content before it is sent to the client.
        #
        # To parse .shtml files for server-side includes (SSI):
        # (You will also need to add "Includes" to the "Options" directive.)
        #
        AddType text/html .shtml
<IfModule mod_include.c>
        AddOutputFilter INCLUDES .shtml
</IfModule>

</IfModule>

✏️ 설정 수정하기

HTML 파일에서 PHP 코드를 인식하도록 하려면 다음 라인을 추가해야 합니다:

📝 추가할 코드

AddType application/x-httpd-php .html

📋 수정된 전체 설정

#AddHandler cgi-script .cgi

        #
        # For files that include their own HTTP headers:
        #
        #AddHandler send-as-is asis

        #
        # For server-parsed imagemap files:
        #
        #AddHandler imap-file map

        #
        # For type maps (negotiated resources):
        # (This is enabled by default to allow the Apache "It Worked" page
        #  to be distributed in multiple languages.)
        #
        AddHandler type-map var

        #
        # Filters allow you to process content before it is sent to the client.
        #
        # To parse .shtml files for server-side includes (SSI):
        # (You will also need to add "Includes" to the "Options" directive.)
        #
        AddType text/html .shtml
        AddType application/x-httpd-php .html
<IfModule mod_include.c>
        AddOutputFilter INCLUDES .shtml
</IfModule>

</IfModule>

🔄 서버 재시작

설정 변경 후 아파치 서버를 다시 로딩해야 합니다:

sudo service apache2 reload

또는

sudo systemctl reload apache2

🧪 설정 테스트

설정이 제대로 적용되었는지 테스트해보겠습니다:

1. 테스트 파일 생성

sudo nano /var/www/html/test.html

2. 테스트 코드 작성

<!DOCTYPE html>
<html>
<head>
    <title>PHP in HTML Test</title>
</head>
<body>
    <h1>PHP 코드 테스트</h1>
    <p>현재 시간: <?php echo date('Y-m-d H:i:s'); ?></p>
    <p>서버 정보: <?php echo $_SERVER['SERVER_SOFTWARE']; ?></p>
</body>
</html>

3. 브라우저에서 확인

브라우저에서 http://your-domain/test.html에 접속하여 PHP 코드가 실행되는지 확인합니다.

⚠️ 주의사항

1. 보안 고려사항

  • HTML 파일에서 PHP 실행은 보안 위험을 증가시킬 수 있습니다
  • 사용자가 업로드하는 HTML 파일에 대한 검증이 필요합니다
  • 적절한 파일 권한 설정이 중요합니다

2. 성능 영향

  • 모든 HTML 파일이 PHP 파서를 거치게 되어 성능에 영향을 줄 수 있습니다
  • 정적 HTML 파일도 동적으로 처리되어 캐싱 효율성이 떨어질 수 있습니다

3. 대안 방법

특정 디렉토리나 가상 호스트에만 적용하고 싶다면:

<Directory "/var/www/html/dynamic">
    AddType application/x-httpd-php .html
</Directory>

🔍 문제 해결

PHP 코드가 실행되지 않는 경우

  1. PHP 모듈 확인:
    sudo a2enmod php8.1  # PHP 버전에 맞게 조정
    
  2. 설정 문법 검사:
    sudo apache2ctl configtest
    
  3. 에러 로그 확인:
    sudo tail -f /var/log/apache2/error.log
    

🎯 추가 활용 방법

1. 여러 확장자 지원

AddType application/x-httpd-php .html .htm .shtml

2. 조건부 적용

<FilesMatch "\.html$">
    SetHandler application/x-httpd-php
</FilesMatch>

📚 마무리

HTML 파일에서 PHP 코드를 실행하도록 설정하는 것은 간단하지만, 보안과 성능에 미치는 영향을 충분히 고려해야 합니다.

특히 프로덕션 환경에서는:

  • 꼭 필요한 디렉토리에만 적용
  • 적절한 보안 조치 구현
  • 정기적인 보안 점검 수행

이런 사항들을 염두에 두고 설정하시기 바랍니다.


💡 : 개발 환경에서 먼저 충분히 테스트한 후 프로덕션에 적용하는 것을 권장합니다!