ubuntu 20.04 apache2 - php files downloading - php

php files do not load in the browser, but are downloaded. I have read through many solutions to this problem on both serverfault.com and stackoverflow. (Many of the posts are several years old with older versions of Apache and php, and some of the config files and their locations have changed.) I've found a common set of solutions to the problem, but none of them have worked for me. The following links contain examples of the suggestions I have tried that didn't solve my problem.
apache2 on ubuntu - php files downloading
Apache shows PHP code instead of executing it
https://serverfault.com/questions/25227/why-is-php-script-downloaded-instead-of-executed
https://serverfault.com/questions/286882/apache-is-not-interpreting-php-files
I installed apache2. If I go to "localhost" in my browser, it serves up the "Apache2 Ubuntu Default Page". I installed php. "php7.4.conf" and "php7.4.load" appears in both /etc/apache2/mods-available and /etc/apache2/mods-enabled. I verify that php is enabled with sudo a2enmod php7.4, which gives
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php7.4:
Module php7.4 already enabled
Based off multiple replies in different questions, I have ended up with the following in my /etc/apache2/apache2.conf file (note this is not the entire file):
Include /etc/phpmyadmin/apache.conf
AddType application/x-httpd-php .php
# Use for PHP 7.x:
LoadModule php7_module modules/libphp7.4.so
AddHandler php7-script php
AddType application/x-httpd-php-source .phps
AddHandler application/x-httpd-php .phps
AddHandler application/x-httpd-php .php
AddType application/x-httpd-php .php
# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php
Notes: I have verified that the apache2.conf file is being executed, by adding a bad line to the file and attempting to restart apache, which resulted in an error. libphp7.4.so is located in /usr/lib/apache2/modules.
And my /etc/apache2/mods-available/php7.4.conf and /etc/apache2/mods-enabled/php7.4.conf files look like this:
<FilesMatch ".+\.ph(ar|p|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch ".+\.phps$">
SetHandler application/x-httpd-php-source
# # Deny access to raw php sources by default
# # To re-enable it's recommended to enable access to the files
# # only in specific virtual host or directory
# Require all denied
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(ar|p|ps|tml)$">
# Require all denied
</FilesMatch>
# Running PHP scripts in user directories is disabled by default
#
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
# <IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_flag engine Off
# </Directory>
# </IfModule>
After every change I have made, I have restarted apache with sudo service apache2 restart. I do a test and still php files are downloaded. I'm using a simple php file called verify.php, which contains the following:
<?php
phpinfo();
?>
I've never worked with these things before so I could be missing something obvious? For some additional context, I'm working on a school project where I plan to use a php page to query a database and generate a table with the results.

In my case of similar hardship:
I do not need those statements in apache2.conf:List item
I have similar content in my /etc/apache2/mods-available/php7.4.conf and /etc/apache2/mods-enabled/php7.4.conf
I ran:
sudo a2dismod mpm_event && sudo a2enmod mpm_prefork && sudo a2enmod php7.4
However what got me to work is a misspelled 'php' as 'phd' (in SetHandler application/x-httpd-php of etc/apache2.conf):
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
I think that is the only needed command to call PHP as handler.
p.s. I have more than one instance of messed up config files.. i haven't found a good control template for all those linux configuration process. (this time the install was started by somebody, i took over the second part)
(This section contains notes and hints specific to Apache 2.x installs of PHP on Unix systems.
https://www.php.net/manual/en/install.unix.apache2.php)

Related

Setting specific PHP version with SetHandler in .htacess per directory doesn't work

Using Ubuntu 18 & Apache, I am trying to achieve state in which a PHP handler of my choice is used in each directory.
There is a .htaccess directive for that which itself works fine - I can change x-httpd-php to x-httpd-php-source and it is correctly applied
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
So I thought I would set a specific one similary as in tutorials for AddHandler
<FilesMatch \.php$>
SetHandler application/x-httpd-php73
</FilesMatch>
Sadly, this does nothing (raw PHP code is displayed)
I have PHP7.1 - PHP7.3 installed and modules are enabled with sudo a2enmod php7.*
EDIT: I was able to do this with SetHandler "proxy:unix:/var/run/php/php7.3-fpm.sock|fcgi://localhost/" after some extra package installing , however I would like to know how to force PHP version during SetHandler in a way I described.

How to disable arbitrary PHP 7 code in directory?

Question at the bottom.
According to the official Mediawiki security guide, I have to
<Directory "/Library/MediaWiki/web/images">
# Ignore .htaccess files
AllowOverride None
# Serve HTML as plaintext, don't execute SHTML
AddType text/plain .html .htm .shtml .phtml .php .php3 .php4 .php5 .php7
# Don't run arbitrary PHP code.
php_admin_flag engine off
# If you've other scripting languages, disable them too.
</Directory>
However with Apache 2.4.29 and PHP 7.1.15 I get
Invalid command 'php_admin_flag', perhaps misspelled or defined by a module not included in the server configuration
According to this post the solution is
You cannot use php_admin_value/php_admin_flag with PHP compiled as CGI
(suPHP), because these options are only supported when PHP is compiled
as a module of Apache. Feel free to use php.ini sections to change the
settings (http://php.net/manual/en/ini.sections.php). Otherwise - just
switch to mod_ruid2+mod_php and you'll be able to use php_admin_value
in Apache configuration files.
Even when I have installed
dnf -y install httpd php php-mysqlnd php-gd php-xml php-mbstring mod_ruid2
I get the error, despite having mod_php and mod_ruid2.
Question
Can someone translate the above into what I actually need to do in my case in plain English?
For a single directory:
<Directory "/Library/MediaWiki/web/images">
php_admin_value engine Off
</Directory>
For multiple directories under the same path:
<DirectoryMatch ^/Library/MediaWiki/web/(images|other_folder|other_folder)>
php_admin_value engine Off
</DirectoryMatch>
There's also <LocationMatch "path"> - more directives here:
http://httpd.apache.org/docs/current/mod/core.html#locationmatch

I need libphp7.so module to configure apache on Centos

I follow the tutorial of this link to use php7 or phpng on my Centos 6.5 with apache.
I can execute php scripts in the console but I would like to be able to run php scripts using the Apache Server.
I need some help because I can't find the libphp7.so module. I don't know if I have to build it or what.
I believe you need to add --with-apxs2 to your configure script. According to the link you provided I do not see that in the configure flags. --with-apxs2 will "Build shared Apache 2.0 Handler module". You may also need to make sure in your apache configuration you have:
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
The first time I built php7 it just displayed the source rather than execute it, but adding that caused it to execute the code.
If you want to try it out with Docker I just created a Docker image for PHP7 at https://registry.hub.docker.com/u/silintl/php7/ You can also just view the Dockerfile which includes all the commands used to install and configure it.
In my config file /etc/httpd/conf.modules.d/15-php.conf which is loaded by the parent config file /etc/httpd/conf/httpd.conf I found the following default configuration:
<IfModule !mod_php5.c>
<IfModule prefork.c>
LoadModule php7_module modules/libphp7.so
</IfModule>
</IfModule>
<IfModule !mod_php5.c>
<IfModule !prefork.c>
LoadModule php7_module modules/libphp7-zts.so
</IfModule>
</IfModule>
Using information provided by #JanePage and #PhilipShipley, i change it to this:
LoadModule php7_module modules/libphp7.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
and Apache started working fine.
Thanks Phillip who gaves some clues for this issue, but in my case I've solved my problem this way :
1 - Copy the Library php7.so that you have into apache module directory with this command :
sudo cp /etc/httpd/modules/libphp7.so /opt/bitnami/apache2/modules
2 - Add in your Apache config file following code :
LoadModule php7_module ./modules/libphp7.so
SetHandler application/x-httpd-php
And PHP worked fine after that !

How install and configure manually php and apache?

Installed PHP like this:
wget http://in1.php.net/distributions/php-5.3.28.tar.bz2
tar -xvf php-5.3.28.tar.bz2
cd php-5.3.28
./configure
make
make install
Installed Apache2 like this:
sudo apt-get install apache2
So, how do I now link Apache to PHP?
PS - I know I can install PHP as a module which will be 100x easier, but I want to know how to link exactly these two in this way.
Thanks.
Edit your httpd.conf to load the PHP module. The path on the right hand side of the LoadModule statement must point to the path of the PHP module on your system. The make install from above may have already added this for you, but be sure to check.
LoadModule php5_module modules/libphp5.so
Tell Apache to parse certain extensions as PHP. For example, let's have Apache parse .php files as PHP. Instead of only using the Apache AddType directive, we want to avoid potentially dangerous uploads and created files such as exploit.php.jpg from being executed as PHP. Using this example, you could have any extension(s) parse as PHP by simply adding them. We'll add .php to demonstrate.
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Or, if we wanted to allow .php, .php2, .php3, .php4, .php5, .php6, and .phtml files to be executed as PHP, but nothing else, we'd use this:
<FilesMatch "\.ph(p[2-6]?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
And to allow .phps files to be handled by the php source filter, and displayed as syntax-highlighted source code, use this:
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
And restart Apache
service httpd restart
You can read how do that in the php documentation
http://php.net/manual/en/install.unix.apache2.php
http://php.net/manual/en/install.unix.apache2.php#92797

Problem with debugging PHP with Eclipse and xdebug

Can you help me with the following problem? I have Eclipse Helios configured with xdebug under Ubuntu. When I start to debug a given page everything seems fine, I can attach to the break point I want, but the problem comes when I tray to step over/into, Firefox (my default browser for debugging) prompt me to "save as" the php page.
Have you checked to ensure that PHP, Apache and MySQL are running?
If they aren't running then you will not be able to run a PHP script.
I think the command in ubuntu (if not running on LAMPP) is "sudo apachectl start" to start apache/php
Then you can put your scripts in /var/www/html/
If you are running LAMPP (http://www.apachefriends.org/en/xampp-linux.html) then you can probably "sudo /opt/lampp/lampp start" to boot everything up.
Then install your scripts to /opt/lampp/htdocs/
After you've done all of that, you should be able to run it.
I assume you're using Apache as your webserver. If so, it seems that Apache doesn't know how to handle *.php files.
Have you added:
# Required modules: dir_module, php5_module
<IfModule dir_module>
<IfModule php5_module>
DirectoryIndex index.php index.html
<FilesMatch "\.php$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
</IfModule>
</IfModule>
to your apache configuration file? This should do the trick.

Categories