Operating environment:
Ubuntu: 16.04
PHP: 5.6.36
the Apache: 2.4.18
Appear /var/www/html
under the folder can execute php file
vhost configuration file DocumentRoot
path format is /home/USERNAME/public_html
, the access, the output php source file or download php
Solution :
- Do not use
/home/USERNAME/public_html
this format, such as read:/home/USERNAME/www
this format - Modify the
/etc/apache2/mods-available/php5.6.conf
file, use the#
numbers to comment out从<IfModule ...> 到 </IfModule>
several lines
The last restart apache
The reason: /etc/apache2/mods-available/php5.6.conf
file some content is:
# 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>
My own translation of the above comments
# 在用户的目录中运行php脚本是默认禁止的
#
# 为了在用户的目录重新启用php 注释以下行
# (从 <IfModule ...> 到 </IfModule>.) 不要将它设置为 On
# 预防 .htaccess 文件来禁止它
<IfModule mod_userdir.c>
<Directory /home/*/public_html>
php_admin_flag engine Off
</Directory>
</IfModule>
In /etc/apache2/sites-available
folder added a www.test.com.conf
vhost on file
<VirtualHost *:80>
ServerName www.test.com
ServerAlias www.test.com test.com
DocumentRoot /home/test/public_html
<Directory /home/test/public_html>
Options FollowSymLinks MultiViews
AllowOverride All
Require all granted
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/domain/www.test.com.error.log
CustomLog /var/log/apache2/domain/www.test.comaccess.log combined
</VirtualHost>
In /home/test/public_html/
case there is a info.php
file
<?php
phpinfo();
Access http://www.test.com/info.php time, has shown php source code, no execute php
Later the vhost configuration file /home/test/public_html
is public_html
changed to www
the corresponding folder rename
mv /home/test/public_html /home/test/www
After the changes, /etc/apache2/sites-available/www.test.com.conf
the contents of the file is
<VirtualHost *:80>
ServerName www.test.com
ServerAlias www.test.com test.com
# only replace public_html to www
DocumentRoot /home/test/www
<Directory /home/test/www>
Options FollowSymLinks MultiViews
AllowOverride All
Require all granted
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/domain/www.test.com.error.log
CustomLog /var/log/apache2/domain/www.test.comaccess.log combined
</VirtualHost>
Then restart apache
sudo service apache2 restart
And then try to access http://www.test.com/info.php on the information output php