运行环境:

Ubuntu : 16.04

PHP: 5.6.36

Apache: 2.4.18

出现/var/www/html 文件夹下的 php文件能够执行

vhost 配置文件的DocumentRoot的路径格式是/home/USERNAME/public_html的,访问时,输出 php源码或者下载php文件

解决方法:

  1. 不使用/home/USERNAME/public_html这样的格式,改为如:/home/USERNAME/www 这样格式
  2. 修改 /etc/apache2/mods-available/php5.6.conf文件,使用#号注释掉 从<IfModule ...> 到 </IfModule>的行数

最后重启 apache

原因是: /etc/apache2/mods-available/php5.6.conf 文件有一段内容是:

# 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>

我自己对上面注释的翻译

# 在用户的目录中运行php脚本是默认禁止的
#
# 为了在用户的目录重新启用php 注释以下行
# (从 <IfModule ...> 到 </IfModule>.) 不要将它设置为 On
# 预防 .htaccess 文件来禁止它
<IfModule mod_userdir.c>
<Directory /home/*/public_html>
php_admin_flag engine Off
</Directory>
</IfModule>

/etc/apache2/sites-available 文件夹下添加了一个 www.test.com.conf的vhost文件

<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>

/home/test/public_html/下有一个 info.php文件

<?php
phpinfo();

访问 http://www.test.com/info.php 的时候,却显示出 php 源码,没有执行 php

后来将 vhost配置文件中的/home/test/public_html 中的public_html 改为为www,对应的文件夹重命名

mv /home/test/public_html /home/test/www

修改之后,/etc/apache2/sites-available/www.test.com.conf文件的内容是

<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>

然后重启 apache

sudo service apache2 restart

然后再试访问 http://www.test.com/info.php 就输出 php信息了

Ubuntu Apache vhost不执行php小记的更多相关文章

  1. Ubuntu 解决:当执行`sudo apt-get update`命令时 出现的 “apt-get 404 Not Found Package Repository Errors” 问题

    Ubuntu 解决:当执行sudo apt-get update或者sudo apt-get install命令是出现的 "apt-get 404 Not Found Package Rep ...

  2. Ubuntu+Apache+PHP+Mysql环境搭建(完整版)

    Ubuntu+Apache+PHP+Mysql环境搭建(完整版) 一.操作系统Ubuntu 14.04 64位,阿里云服务器 二.Apache 1.安装Apache,安装命令:sudo apt-get ...

  3. Ubuntu+Apache+PHP+Mysql环境搭建(完整版)(转)

    http://www.2cto.com/os/201505/401588.html Ubuntu+Apache+PHP+Mysql环境搭建(完整版) 一.操作系统Ubuntu 14.04 64位,阿里 ...

  4. ubuntu apache svn 参考

    Ubuntu下Subversion服务器的安装配置     本文涉及的范围 要通过 HTTP 协议访问 Subversion 文件仓库,需要安装并配置好 Web 服务器.Apache2 被证实可以很好 ...

  5. ubuntu apache nginx 启动 关闭

    转载自:http://www.comflag.com/2011/05/01/apache-web.htm 电影<社交网络>中,facebook创始人马克.扎克失恋后入侵哈佛大学宿舍楼服务器 ...

  6. 什么是伪静态,以及ubuntu + apache 如何实现伪静态

    原文链接:http://www.cnblogs.com/ainiaa/archive/2010/07/25/1784564.html php伪静态 一直在做php的开发工作.在开发的过程中老早就听说了 ...

  7. Ubuntu 16.04应用布署小记

    本系列文章记录了升级Ubuntu 16.04的布署过程 回到目录 10. Dokuwiki 将虚拟目录配置文件链接到Apache2的可用的配置文件库中 sudo ln -s /opt/dokuwiki ...

  8. Ubuntu 16.04环境布署小记

    本系列文章记录了升级Ubuntu 16.04的布署过程 回到目录 10. 安装Mono, Xsp 当前版本16.04.1的系统源的Mono版本为4.2.1,如需使用最新版本(本文书写时稳定版本为4.6 ...

  9. Ubuntu+Apache+PHP+Mysql环境搭建

    一.操作系统Ubuntu 14.04 64位,虚拟机服务器 二.Apache 1.安装Apache,安装命令:sudo apt-get install apache2 2.环境配置: 1)配置文件:路 ...

随机推荐

  1. 使用PHP如何去除字符串结尾的字符

    前言 在工作中遇到一个需求:一串字符串,如"迅雷官方下载"."快播5.0下载",需要去掉他们结尾的"官方下载"和"下载" ...

  2. Linux下的Ngnix服务器部署静态页

    一.安装FTP vsftpd 的名字代表”very secure FTP daemon”, 安全是它的开发者 Chris Evans 考虑的首要问题之一.在这个 FTP 服务器设计开发的最开始的时候, ...

  3. 标准 I/O (带缓冲)

    标准IO中分文本流和二进制流 windows系统中:(1) 二进制流 - 换行符 '\n'    (2) 文本流 - 换行符 ‘\r' '\n' linux 系统中:不区分文本流和二进制流:换行符 - ...

  4. centos6.5下,使用虚拟ftp用户

    因为业务的问题,有位客户的账户总是出现各种问题,本人对于acl的使用又不是很会,所以和同事一起研究了一下这个虚拟ftp用户. Centos6.5 1 需求 为了保证系统的安全性,现对系统中vsftpd ...

  5. final和abstract关键字的作用

    final和abstract关键字的作用 final和abstract是功能相反的两个关键字,可以对比记忆 abstract可以用来修饰类和方法,不能用来修饰属性和构造方法:使用abstract修饰的 ...

  6. Centos6.5 安装 LAMP

    Centos 安装 LAMP 系统: Centos 6.5 Apache 2.4 + PHP 7.2 + Mysql 5.7 准备工作 centos 查看版本 查看 centos版本 How to C ...

  7. Eclipse+Marven + spring mvc 新建一个 Hello world 项目

    1. 打开Eclipse,菜单 File->New->Marven Project.               2. 点击 Next,                3. 选择 marv ...

  8. Mysql 编译报错 g++: internal compiler error: Killed (program cc1plus) 解决办法

    g++: internal compiler error: Killed (program cc1plus) 解决办法 g++: internal compiler error: Killed (pr ...

  9. php注册和登陆与数据库的链接

    注册和登陆其实是从一个表中进行提取和写入数据 1.(1)先建立一个注册页面 1 2 3 4 5 6 7 8 9 <body>         <h1>注册页面</h1&g ...

  10. LUOGU P3723 [AH2017/HNOI2017]礼物 (fft)

    传送门 解题思路 首先我们设变化量为\(r\),那么最终的答案就可以写成 : \[ ans=min(\sum\limits_{i=1}^n(a_i-b_i+r)^2) \] \[ ans=min(\s ...