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
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:
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>
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
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.
Related
I am moving from a server with PHP 5 to a new server with PHP 7, and I am having issues to tell Apache to parse .html as PHP scripts.
In my .htaccess file I have this line working correctly on my current server with PHP 5:
AddType application/x-httpd-php .html
But on the new server, that directive makes any .html file being downloaded instead than executed as a PHP script.
I know that on the current server with PHP 5 installed, PHP is configured with API set as "Apache 2.0 Handler" whereas on this new server it is configured as "FPM/FastCGI" and I guess that maybe that's the problem? If so, how can I overcome it without having to change that API setting?
I look forward to hearing from you. Thank you in advance for any help!
After hours of research, I ended up reading this page:
https://www.digitalocean.com/community/questions/php-fpm-security-limit_extension-issue
And I fixed the problem by adding this code inside .htaccess:
<FilesMatch ".+\.html$">
SetHandler "proxy:unix:/run/php/php7.0-fpm-[myhostname].sock|fcgi://localhost"
</FilesMatch>
And to avoid the server to give me an 'access denied' error I had to put this line inside /etc/php/7.0/fpm/php-fpm.conf:
security.limit_extensions = .php .htm .html .inc .rss .rdf .xhtml
Then restarted PHP daemon with:
service php7.0-fpm restart
Now everything seems to work fine! Quite complex I'd say, and I think that'd due to the fact PHP is configured with the FPM/FastCGI API.
Comments and thoughts are welcome!
Thanks again to everyone.
I'm stuck trying to figure out what directives my htaccess file for a directory needs. To give a little background, I was messing around with AddType and AddHandler to try to get html files to run php in them. I was adding directives like:
AddType application/x-httpd-php .php .html
and
AddType application/x-httpd-php5 .php .html
That didn't work, so I tried adding handlers as well:
AddHandler x-httpd-php .php .html
and
AddHandler application/x-httpd-php .php .html
That still didn't give me what I wanted so I tried many combinations, like:
AddType application/x-httpd-php .php .html
AddHandler php-script .php .html
I even tried combinations with SetHandler
AddType application/x-httpd-php .php .html
AddHandler php-script .php .html
SetHandler application/x-httpd-php .php html
I tried every imaginable combination of AddType and AddHandler found on the web.
!===== This is important ======!
I realized that its not good practice to have php run your html files just so you can run php in the html. I am no longer trying to do that. The only reason I'm posting this part above is to give you an idea on some things I've changed. These changes were made to the htaccess file at a directory above the root.
!==============================!
Now that I'm no longer trying to run my php in html files I cleared the AddType, AddHandler, and SetHandler directives from the htaccess file. But now I get an internal server error when trying to run php files.
So I tried putting some stuff back. If I had a handle, the browser now downloads the php instead of running it.
I should note that any php running below the directory I changed works fine. But everything in that directory or subdirectory of that directory has this same problem with php files.
I have tried deleting the htaccess file in that directory, but that does not work, I get an internal error.
If I turn Override off, I get an internal error.
Something I changed is persisting in that directory and I'm wondering how to either
a. turn it off
b. add the correct handler to that htaccess file so that php files are run again (like they are on the rest of the server)
I did have the exact same issue so I deleted all the content of .htaccess and started trying only with handlers until I figured it out. My local .htaccess copy had the following SetHandler:
AddHandler x-httpd-php5-3 .php
In my case it worked after changing it to:
AddHandler application/x-httpd-php5 .php
It seems the whole trick is only the handler, I tried this solution in 2 different servers with different configurations and in both cases just changing AddHandler worked. If this doesn't work for you try a different handler for php.
Hope it helps.
Just realized I left this up unanswered after I solved the problem. If someone has a similar problem and is reading this : Check the permissions on all associated folders. I can't remember the specifics but one of the directories involved (I believe it was the htaccess directory) had permissions that persisted after changing some things and then changing them back.
Recently I configured Apache 2.2 server to run PHP and have configured to load PHP 5 as a module. I am able to start Apache with no problems but when I try to run PHP files it pops up would you like to open or save file?
Running on Windows XP SP3
Internet Explorer browser but not related
Simply trying to open it as a file not as my index.php and haven't set Apache to look for that as my index. If it helps I think my issue might be here:
;AddType application/x-httpd-php .php
;AddType application/x-httpd-php .php5
;AddType application/x-httpd-php .phtml
I tried to modify in my Apache conf file addtype and addhandler but no change and apache still starts both ways.
I tried putting these inside the mime configuration part and outside of it same both ways but not sure where in doc that should go my loadmodule part is also entered.
Im not sure but the only other place i could see being an issue would be in my php.ini file to which i have made no modifications.
Can someone point me in the right direction? What I am missing here?
Edit httpd.conf and add (or remove the ; at the begin of line):
# PHP 5.x module
LoadModule php5_module modules/libphp5.so
AddHandler php5-script .php
# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php
AddType text/html .php
# PHP Syntax Coloring
# (optional but useful for reading PHP source for debugging):
AddType application/x-httpd-php-source phps
I don't know, but I think you need to restart the Apache.
From http://dan.drydog.com/apache2php.html (step 11)
can't for the life of me work out why this isn't working - on a dreamhost server, I've created an htaccess file and added AddHandler php5-cgi .html .htm to allow php in html files, as per the dreamhost docs. On an identical dreamhost package, I've done the same thing for another site and it worked perfectly, but in this case it just won't parse the php! Is there anything I could have missed here which could be causing the problem? The htaccess is in the web root and isn't being overridden by anything else.
Try this
AddType application/x-httpd-php .html .htm
The above setting will work if the apache is picking php as a module.
AddHandler application/x-httpd-php .html .htm
or
AddHandler application/x-httpd-php5 .html .htm
above will work if apache is executing php as CGI?
there is no harm i would say to put both of the above conditions together but try one over other and see if it work for you.
read more here in this article I wrote.
Is your server using suPHP rather than mod_php or plain php-cgi?
Try using
AddHandler x-httpd-php .html .htm
or
AddType application/x-httpd-php .html .htm
instead.
Postscript
Shared Hosting services use UID based separation on individual accounts from each others. Most use suPHP, but some use suEXEC. They will use one of these. Both act as a su wrapper around php-cgi so you can't tell from the phpinfo()as its PHP scripting engine as this will report Server API CGI/FastCGI in both cases. For CGI initiated scripts, phpinfo doesn't report on the Apache config. You need either to look at your hosting provider's FAQ or possibly try:
<?php
header( "Content-Type: text/plain");
echo system('grep -iR LoadModule /etc/httpd /etc/apache2');
The hosting provider's support forums / FAQ might give specific configuration advice here. Have you tried them?
I've got the same problem as you but I just put this AddType php .html .htm to my .htaccess, it's working.
For me it is working for only like this:
AddHandler php-cgi .php .htm
I have many years experience of working with Apache/PHP/MySQL directly and have only just started using WAMP.
I installed WAMP last week and had my first 4 PHP/MySQL websites up and running immediately. How much easier is this I began to think! That is until I added a fifth site and cannot get php to parse any php file below the websites root directory.
I have a test.php file containing just the word 'hello', ho HTML tags and no PHP directives.
If I place this into /wamp/www/ob/test.php it works, if I place it into /wamp/www/ob/html/test.php the browser comes up with 'You have chosen to open test.php, what should Firefox do with this file? Basically php failed to parse it.
My other sites are working fine with identical setup and this site plus three others were downloaded using Filezilla.
Can anybody help please before I uninstall WAMP and go back to installing Apache/MySQL/PHP manually.
Either the php handler is not turned on for that directory, or the content-type of the returned data is set wrong.
My WampDeveloper Pro setup has this...
<Directory "C:/WampDeveloper/Websites/*/webroot">
AddType text/html .php .php4 .php5 .phps
AddHandler application/x-httpd-php .php .php4 .php5
AddHandler application/x-httpd-php-source .phps
</Directory>
This turns on PHP for all website folders.
Has a DefaultType text/html in httpd.conf for fallback.
Also check any .htaccess files, they might be setting content-type to something other than 'text/html'.
The pages on the Internet that explain how to make Apache parse the PHP that you add to a file with the extension HTML suggest this:
AddType application/x-httpd-php .html .php .htm
AddHandler application/x-httpd-php .html .php .htm
If your Apache server is configured for php5 but not php, then the result, I believe, is the refusal described. I changed what I added to .htaccess to this (notice the digit 5):
AddType application/x-httpd-php5 .html .php .htm
AddHandler application/x-httpd-php5 .html .php .htm
That fixed the problem for me.