반응형
    
    
    
  nginx와 php-fpm을 사용하는 경우 *.html 파일에서도 PHP 코드를 실행하도록 설정하는 방법
테스트 환경
- 운영체제 버전 정보
 
$ lsb_release -d
Description:	Ubuntu 22.04 LTS
- NGINX 버전 정보
 
$ nginx -v
nginx version: nginx/1.24.0
- PHP-FPM 버전 정보
 
$ php-fpm8.1 -v
PHP 8.1.18 (fpm-fcgi) (built: Apr 14 2023 04:39:44)
Copyright (c) The PHP Group
Zend Engine v4.1.18, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.18, Copyright (c), by Zend Technologies
nginx와 php-fpm을 사용하는 경우, *.html 파일에서도 PHP 코드를 실행하도록 설정하려면 다음과 같이 작업해야합니다.
1. nginx 설정 파일 수정
nginx의 설정 파일을 열고 location 디렉티브를 사용하여 HTML 파일에서도 PHP 코드를 실행하도록 지시합니다.
- location ~ \.(php|html|htm)$
 
vim /etc/nginx/conf.d/default.conf
# Settings for a TLS enabled server.
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}
# Settings for a TLS enabled server.
server {
    listen 443 ssl http2;
    server_name example.com;
    root /var/www/html;
    index index.php index.html index.htm;
    charset utf-8;
    access_log /var/log/nginx/example.com-accesss.log main;
    error_log /var/log/nginx/example.com-error.log;
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    # pass PHP scripts to FastCGI server
    location ~ \.(php|html|htm)$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    ssl_certificate "/etc/nginx/ssl/wildcard_example_com/_wildcard_unified_example_com.crt";
    ssl_certificate_key "/etc/nginx/ssl/wildcard_example_com/_wildcard_example_com_SHA256WITHRSA.key";
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    error_page 404 /404.html;
        location = /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
위 설정에서 location ~ \.html$ 디렉티브가 *.html 파일을 처리하도록 지시합니다.
2. php-fpm 설정 수정
php-fpm 설정 파일을 열고 security.limit_extensions 디렉티브를 사용하여 PHP 코드를 실행할 수 있는 확장자 목록에 .html을 추가합니다.
- security.limit_extensions = .php .html .htm
 
vim /etc/php/8.1/fpm/pool.d/www.conf
security.limit_extensions = .php .html .htm
이제 nginx와 php-fpm이 모두 설정되었으므로 *.html 파일에서도 PHP 코드를 실행할 수 있습니다.
- nginx php8.1-fpm 재기동
 
systemctl restart nginx php8.1-fpm
- test.html 파일
 
vim test.html
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <?php echo "Hello, World!"; ?>
    </body>
</html>
- 브라우저에서 확인
 

728x90
    
    
  반응형
    
    
    
  '리눅스' 카테고리의 다른 글
| 리눅스에서 프록시를 지정하는 방법 (0) | 2023.05.12 | 
|---|---|
| docker proxy 설정하는 방법(환경 변수 구성) (0) | 2023.05.11 | 
| 우분투에서 최신 버전의 ansible을 설치하는 방법 (0) | 2023.05.08 | 
| Packer 명령어의 자동 완성을 활성화하는 방법 (0) | 2023.05.02 | 
| Nginx 및 Apache 웹 서버에서 HTTP/2를 적용하는 방법 (0) | 2023.04.28 |