How do I process HTML files as PHP on AWS EC2 Linux?

842 Views Asked by At

I'm trying to move over to AWS EC2 and I've hit a sticking point. I've spent a full day trying every possible solution I could find on Stack Overflow and elsewhere, but to no avail.

I want to process .htm files as PHP files. Files ending in .php are processed just fine, but I can't get .htm files to be processed as PHP.

If I use this "AddHandler" syntax in .htaccess, nothing happens:

AddHandler application/x-httpd-php .htm

"x-httpd-php" can literally be anything. It doesn't matter. Even this does nothing:

AddHandler application/its-a-fish .htm

Using this "AddType" syntax, on the other hand, always causes the file to be downloaded by the browser instead of parsed as code:

AddType application/x-httpd-php .htm

Here again, what follows "application/" doesn't matter. All of these cause the file to be downloaded instead of processed:

AddType application/its-a-fish .htm
AddType application/x-http-php7 .htm
AddType application/x-http-php73 .htm

When the file is downloaded, the Content-Type in the Response header is whatever comes after "AddType" in .htaccess, e.g.:

Content-Type: application/x-http-php73

So maybe I just haven't found the "application" identifier my PHP is running under?

I've tried literally every code example I can find over a ~10-hour period (especially in these threads) but nothing has worked:

Server not parsing .html as PHP

Parsing HTML files as PHP

http://kb.cloudblue.com/en/115773

I suspect the reason it worked on all my previous servers and not on AWS is because PHP is running as FastCGI on AWS, not an Apache Handler, but I can't figure out how to make it work with FastCGI.

Here are the relevant packages I currently I have installed:

[root@ip-172-31-30-111 etc]# rpm -qa | egrep 'http|php'
libnghttp2-1.31.1-1.amzn2.0.2.x86_64
httpd-tools-2.4.39-1.amzn2.0.1.x86_64
mod_http2-1.14.1-1.amzn2.x86_64
php-pdo-7.3.6-1.amzn2.0.1.x86_64
generic-logos-httpd-18.0.0-4.amzn2.noarch
httpd-filesystem-2.4.39-1.amzn2.0.1.noarch
httpd-2.4.39-1.amzn2.0.1.x86_64
php-json-7.3.6-1.amzn2.0.1.x86_64
php-mysqlnd-7.3.6-1.amzn2.0.1.x86_64
php-cli-7.3.6-1.amzn2.0.1.x86_64
php-common-7.3.6-1.amzn2.0.1.x86_64
php-fpm-7.3.6-1.amzn2.0.1.x86_64
1

There are 1 best solutions below

0
Shane Pike On

I finally figured this out, thanks primarily to this post: https://talk.plesk.com/threads/cant-get-php-versions-to-serve-html-as-php.342045/page-2#post-854770

Here's what to do specifically on AWS EC2 Linux:

  1. Add these lines to .htaccess, changing the "Files" section to specify the extensions you want to process as PHP:

    <IfModule mod_proxy_fcgi.c>
    <Files ~ (\.htm$)>
    SetHandler proxy:unix:/run/php-fpm/www.sock|fcgi://127.0.0.1:9000
    </Files>
    </IfModule>
    
  2. Change the security.limit_extensions setting in /etc/php-fpm.d/www.conf to allow the extensions you added in .htaccess (plus .php):

    security.limit_extensions = .php .htm
    
  3. Restart the php-fpm service (restarting httpd will not force a reread of www.conf):

    service php-fpm restart
    

If you do #1 without doing #2, you will get an "Access denied" error. Step #2 is what fixes that.