PHP file is downloaded, not loaded - php

I have the following php file:
<html>
<body>
<?php
echo "test";
?>
</body>
</html>
My httpd.conf file has the following entry:
grep php /var/apache/conf/httpd.conf
LoadModule php5_module modules/libphp5.so
Why would the file always be downloaded and not loaded in the browser?

You also need to set the correct content handler / MIME-type for .php files. The default Ubuntu php configuration contains:-
<IfModule mod_php5.c>
<FilesMatch "\.ph(p3?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
# 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_value engine Off
</Directory>
</IfModule>
</IfModule>

Related

PHP in userdir not working

My /etc/apache2/mods-enabled/userdir.conf file looks like:
<IfModule mod_userdir.c>
UserDir /var/zpanel/hostdata/*/public_html/
UserDir disabled root
<Directory /var/zpanel/hostdata/*/public_html/*>
AllowOverride All
Options MultiViews Indexes SymLinksIfOwnerMatch
<Limit GET POST OPTIONS>
# Apache <= 2.2:
Order allow,deny
Allow from all
# Apache >= 2.4:
# Require all granted
</Limit>
<LimitExcept GET POST OPTIONS>
# Apache <= 2.2:
Order deny,allow
Deny from all
# Apache >= 2.4:
#Require all denied
</LimitExcept>
</Directory>
</IfModule>
And file /etc/apache2/mods-enabled/php5.conf
<FilesMatch ".+\.ph(p[345]?|t|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(p[345]?|t|tml|ps)$">
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 /var/zpanel/hostdata/*/public_html/*>
# php_admin_value engine On
# </Directory>
#</IfModule>
But php not working when in accessing web site via userdir.
www.example.com/~admin
When i access via domain then it works. I have zPanel installed
Server version: Apache/2.4.18 (Ubuntu)
In my /etc/apache2/mods-available/php7.0.conf I have this section
# 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>
I think now you know what to do.
Try the following actions:
Make sure mod_userdir is enabled (a2enmod userdir).
The $HOME and public_html dirs have the right read permissions.
sudo -Hu SOMEUSER sh -c 'chmod 755 $HOME $HOME/public_html'
Make sure php_admin_value engine is On.
sudo vim /etc/apache2/mods-enabled/php*.conf
Then find the line containing php_admin_flag and change it to:
php_admin_flag engine On
This is due that PHP is disabled by default for user dirs for Apache 2.
In your particular case, you've to uncomment the following lines:
<IfModule mod_userdir.c>
<Directory /var/zpanel/hostdata/*/public_html/*>
php_admin_value engine On
</Directory>
</IfModule>
As mention by #kenorb, this is because PHP is disabled php_admin_value engine Off by default in userdirs for Apache2.
But instead of changing the php_admin_value engine to On in /etc/apache2/mods-enabled/php*.conf, it is advised in the .conf file itself that the lines starting from <IfModule> to </IfModule> be commented out, so that .htaccess can handle the setting separately. This can be done as shown below:
#<IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_flag engine Off
# </Directory>
#</IfModule>

php file wont run properly on my server that I just set up via Cyberduck

I've set up a local server with Vagrant via Cyberduck on my Mac. There seems to be no problem with running a file in general (I guess?) except for php file for some reason. It simply shows what I typed in, which is
<?php
echo "hello from Vagrant";
instead of saying hello from Vagrant
I also did sudo vi /etc/httpd/conf.d/php.conf and see what seems to be the problem that's causing this.
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
<IfModule prefork.c>
LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule !prefork.c>
LoadModule php5_module modules/libphp5-zts.so
</IfModule>
#
# The following lines prevent .user.ini files from being viewed by Web clients.
#
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Deny from all
Satisfy All
</IfModule>
</Files>
#
# Allow php to handle Multiviews
#
AddType text/html .php
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
# mod_php options
<IfModule mod_php5.c>
#
# Cause the PHP interpreter to handle files with a .php extension.
#
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#<FilesMatch \.php$>
# SetHandler application/x-httpd-php-source
#</FilesMatch>
Also here's some quick info about my software version that I've used so far.
PHP 5.6.15
Apache 2.2.15
CentOS 6.7
Cyberduck 4.7.3
Anyone knows any solution to this? Thank you in advance!

Apache downloading php files instead of executing them

I am trying to install phpmyadmin in my Ubuntu Server and when I try to access via browser to it, a php file downloads instead of displaying the web page. I placed a phpinfo testfile in /var/www and it is working fine.
I have already commented lines in php5.conf file:
<FilesMatch ".+\.ph(p[345]?|t|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
Order Deny,Allow
Deny from all
</FilesMatch>
# Deny access to files without filename (e.g. '.php')
<FilesMatch "^\.ph(p[345]?|t|tml|ps)$">
Order Deny,Allow
Deny from all
</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_value engine Off
# </Directory>
#</IfModule>
I am using Apache/2.4.6 and Ubuntu Server 13.10
If you installed phpmyadmin as application, then try to download source code from official website and run it manually (copy into your directory /var/www and add VirtualHost settings in apache config ) It's easier than try to decide your issue.

php to html request without changing browser url

My site is working with .html extension in the URL. However i want to change it to .php. But pages are not working. Is there any way that url is using .php extension and server is treating it as .html request
Most likely you don’t have Apache setup to parse PHP files. Unless I am missing what the question is? You want too rename the .html files to .php and still have them work?
By default in Ubuntu 12.04 PHP is enabled via the php5.conf file in mods-available that should look like this.
<IfModule mod_php5.c>
<FilesMatch "\.ph(p3?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
# 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_value engine Off
</Directory>
</IfModule>
</IfModule>

Disable MOD_PHP in vhosts and activate suphp

I need to desactivate mod_php on a vhost and let it working for other vhosts, I need to disable it in order to activate suphp.
here is the vhost config :
Options +Indexes
ServerName www.native.org
ServerAlias native.org
DocumentRoot /home/user/www/native/current
ServerAdmin info#native.org
UseCanonicalName Off
CustomLog /var/log/apache2/native_access.log combined
ErrorLog /var/log/apache2/native_error.log
<Directory /home/user/www/native/current>
RemoveHandler .php
AllowOverride All
Options FollowSymLinks
Order allow,deny
allow from all
</Directory>
suPHP_Engine on
SuexecUserGroup user native
<IfModule mod_suphp.c>
suPHP_UserGroup user native
AddHandler x-httpd-php .php .php3 .php4 .php5
suPHP_AddHandler x-httpd-php
</IfModule>
NB: mod_php is activated by default for all vhosts
You don't have to remove handlers, types or to turn PHP's engine off.
In your <VirtualHost ...> add the following lines:
<FilesMatch \.php$>
SetHandler None
</FilesMatch>
In this way, you will remove the handler added by /etc/httpd/conf.d/php.conf (or php5.conf, or whatever) which says:
#
# Cause the PHP interpreter to handle files with a .php extension.
#
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Edit: It's better also to disable PHP's engine according to suphp.conf file:
# Disable php when suphp is used, to avoid having both.
<IfModule mod_php5.c>
php_admin_flag engine off
</IfModule>
Your site will run under suPHP now. (Also, if you have installed phpMyAdmin in /usr/share/phpMyAdmin, it will work under mod_php, which is great.)
At the end, take a look of one of my VirtualHosts configuration:
<VirtualHost 1.2.3.4:80>
ServerName site.com
ServerAlias www.site.com
ServerAdmin admin#site.com
DocumentRoot /home/site/public_html
Options -Indexes
suPHP_Engine on
suPHP_UserGroup site site
suPHP_ConfigPath "/home/site/public_html"
suPHP_AddHandler x-httpd-php
AddHandler x-httpd-php .php .php3 .php4 .php5
# Remove the handler added by php.conf
<FilesMatch \.php$>
SetHandler None
</FilesMatch>
# Disable php when suphp is used, to avoid having both.
<IfModule mod_php5.c>
php_admin_flag engine off
</IfModule>
ErrorLog "|cronolog /home/site/.logs/error_%Y_%m.log"
CustomLog "|cronolog /home/site/.logs/access_%Y_%m.log" combined
</VirtualHost>
Final Note:
If your phpMyAdmin located in /usr/share/phpMyAdmin is not working, add the following lines at the end of your httpd.conf or in your master VirtualHost:
<Directory /usr/share/phpMyAdmin>
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
<IfModule mod_php5.c>
php_admin_flag engine on
</IfModule>
</Directory>
For example:
<VirtualHost 1.2.3.4:80>
ServerAdmin admin#master-site.com
DocumentRoot /var/www/html
Options -Indexes
<Directory /usr/share/phpMyAdmin>
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
<IfModule mod_php5.c>
php_admin_flag engine on
</IfModule>
</Directory>
ErrorLog "|cronolog /var/www/.logs/error_%Y_%m.log"
CustomLog "|cronolog /var/www/.logs/access_%Y_%m.log" combined
</VirtualHost>
You should be able to do
<Directory /home/user/www/native/current>
RemoveHandler .php .phtml .php3 .php5
RemoveType .php .phtml .php3 .php5
php_flag engine off
AllowOverride All
Options FollowSymLinks
Order allow,deny
allow from all
</Directory>

Categories