Parse application/x-httpd-php in php7 without file extention - php

Ran into this while moving some sites from a one server to another:
The .htaccess file only has this in certain directories:
SetHandler application/x-httpd-php
but it does not parse url's like:
domain.com/dir/filenameWithoutExtension/
where 'filenameWithoutExtension' contains the actual content. And should generate the page
The site is hosted on Ubuntu (Directadmin) with:
First PHP: PHP 7 php-fpm
Second PHP: PHP 5.6 suphp
Apache 2.4.33
Does anyone know how to manage this correctly?

Related

Why is php extension served by apache and a php file with proprietary extension served by FPM/FastCGI?

After a recent update of PHP we where confronted with sessions getting lost between two files. The two files are of different extensions but both PHP. After research we found the .php files are served Server API Apache 2.0 in PHP version 7.3.22. The proprietary extension (pext) however is served by Server API FPM/FastCGI also in PHP version 7.3.22. We assume this is the reason the session is not transferred.
We use this rule in htaccess to get our proprietary extension served as PHP:
AddType application/x-httpd-php .php .pext
We would like to know how to setup so both extensions are served by the same Server API so the can share session data.
Found it: from php 7.2 another php.conf file is created: php72-php.conf which do not copy the following setting;
<FilesMatch .(php|pext)$>
SetHandler proxy:fcgi://php-fpm
Therefor - as per default - the new php.conf set Server API Apache to php and pext was left with FPM. After adding above and restarting FPM and Apache all was fine and sessions are shared again.

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

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.

php files are downloaded instead of ran

My problem started when I was working with the .htaccess file to a directory trying to get php to run inside of html files. I tried ALOT of combinations of AddTypes and AddHandlers. I took them out and now my php files are being downloaded by the broswer instead of running. There are a few other questions on here that I have studied up and down, but they are not exactly my problem.
I'm fairly lost now, I've tried just about every combination of AddType and AddHandler. I've since decided to use mod_rewrite and just have all my files end in .php but I can't get them to run.
This is what I'm using to test:
<!DOCTYPE html>
<body>
<?php
echo("php working");
?>
</body>
</html>
Am I missing something? Luckily I made all my changes at a separate directory than the root so that the site still runs, but none of the php files in that folder can be viewed in broswer. What can I do? Any help is appreciated.
EDIT:
Just to make it clear, I was originally trying to be able to put php in my html files so I was messing around with handlers and addtypes. My server does have php. If I call a php script from a .html file, it runs. I just can't get a php file like the example I included above to open in brower ANYMORE, it used to open in browser. My fear is that I have messed something up by trying all the different handlers in .htaccess. I have since cleared my .htaccess file in hopes that would at least get me to square one, but it hasn't.
SECOND EDIT:
I went to the root directory and made a php file and it ran just fine so its definitely something I changed in the .htaccess for the particular directory. Is there anyway I can reset that directory?
have you made sure to install php correctly within apache?
if you are using php as a module, you need the following:
LoadModule php5_module "c:/php/php5apache2.dll"
AddType application/x-httpd-php .php
# configure the path to php.ini
PHPIniDir "C:/php"
or as a CGI binary
ScriptAlias /php/ "c:/php/"
AddType application/x-httpd-php .php
# For PHP 4
Action application/x-httpd-php "/php/php.exe"
# For PHP 5
Action application/x-httpd-php "/php/php-cgi.exe"
other things to consider, are the permissions set correctly on the php binaries, is the php.ini present and correct, have you restarted apache since installing php into apache?
lastly, php doesnt run within html files, you would need to set the following as pretty much the last thing in the apache config
AddType application/x-httpd-php .html .htm
Do you see the line like this in your htaccess file?
php_flag engine off
If yes, delete it.
One more thing that may help you understand what happened
The following piece of htaccess would make Php from being executed but rather downloaded as a text file
<VirtualHost *>
ServerName sourcecode.testserver.me
DocumentRoot /var/www/example
AddType text/plain php
</VirtualHost>
One more thing
I'm guessing that this topic was addressed once here-> htaccess downloading file instead of loading

PHP not interpreted by browser on new webhost

Moving my website to a new webhost, I'm having an issue with apache / server conf files. PHP files aren't interpreted by the browser but only downloaded. I'm quite a beginner in that domain.
MySQL: 5.0.84
PHP Version 5.2.17
Apache 20051115
in .htaccess file, I wrote
AddType application/x-httpd-php5 .php
to make sure every php file is read by php5.
Running a php info in the backend ( frontend doesnt interpret php ... ) shows well, if you need infos from it.
I really need some help, ty
php is interpreted server side. so it's a server problem.
check your .htaccess to make sure it's picking up php.
might need to add a line to it:
AddType application/x-httpd-php .php .php3 .phtml .htm .html

Server Downloading File Instead Of Showing It

I have a Apache server running at Rackspace Cloud Hosting, so I've installed Wordpress manually(setting up the database, wget the latest.zip...) and placed it at /var/www, but now when I try to access it for the first time, it downloads a strange file with the IP as name instead of showing me the Wordpress Installation.
PS: The same happens if I place it on a sub-directory like blog
I think you haven't configured Apache to execute PHP (mod_php?) Right now it's just serving your PHP files like any old file type so what you're getting is your wordpress index.php as a file download.
I'm not sure what the process is for Rackspace, but usually this would mean enabling mod_php.
Do you have any other PHP files that work on that site?
you need to tell Apache to excuate php files. add the following to your Apache configuration file:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .php
make sure PHP module is included in Apache configuration first!

Categories