Ubuntu的apache2配置PHP不能运行问题

好久没写过了,在总算考完试并且获得一堆烂成绩后,是时候要回归到学习自己的兴趣点

并且填补一下基础

首先记录一下这个问题吧

Ubuntu中配置好apache2后php代码没执行

首先我是准备在虚拟机本地搭个sqli-labs来学习sql injection

因为在之前也搭过,所以应该没什么大问题

结果,就出现了一开始点setup databases时没有反应,即没有任何输出,但其他html元素均能正常显示

我这里配置是

ubuntu gnome 15.10

php5

apache2

mysql

简单来说,就是PHP代码根本就没有运行

此时查看网页源码也能看到php代码,其实还有另外一种情况是网页源码中根本就没有显示php代码

分类讨论下

ps:因为我没有学过多少web的内容,所以以下一些概念性的解释不一定正确,最好自己去搜一下,倘若有错误,还请指出。

  1. 网页源码根本看不到PHP代码

    这里我猜测是因为lamp没有装完整

    首先,php要装上php-cgi,CGICommon Gateway Interface,通过这个借口,php程序可以对服务器和客户端交换信息做一些事情

    另外,还有php-cli,这个是用于在命令行运行php脚本的

    还有php-gd库,用于处理图像。在网站上GD库通常用来生成缩略图,或者用来对图片加水印,或者用来生成汉字验证码,或者对网站数据生成报表等。

    另外,要链接数据库,还要装上相应的php的数据库支持,例如php-mysql

    倘若还不能解决,可以参考一下这篇解答

    http://stackoverflow.com/questions/3555681/why-are-my-php-files-showing-as-plain-text

    You’ll need to add this to your server configuration:

    1
    AddType application/x-httpd-php .php

    that is assuming you have installed php properly, which may not be the case since it doesn’t work where it normally would immediately after installing.

    It is entirely possible that you’ll also have to add the php so/dll to your apache config using a LoadModule directive (usually in httpd.conf)

  2. 网页源码中能看到PHP代码

    这个就可以参考这篇解答了

    http://askubuntu.com/questions/451708/php-script-not-executing-on-apache-server

    Typing:

    1
    sudo apt-get install apache2 php5 libapache2-mod-php5

    will install everything you need and will start the apache server with support for PHP.

    To verify that the php module is loaded, type:

    1
    a2query -m php5

    if not enabled, then load with:

    1
    sudo a2enmod php5

    and restart apache:

    1
    sudo service apache2 restart

    Update for php7.0, tested on Ubuntu 16.04 and 16.10

    Thanks to the comments, I update the answer for php7.

    Install:

    1
    sudo apt-get install apache2 php7.0 libapache2-mod-php7.0 

    Verify:

    1
    a2query -m php7.0

    Load:

    1
    sudo a2enmod php7.0

    Restart apache:

    1
    sudo service apache2 restart

    这个原因似乎是因为apache安装后默认还缺一个php的支持库

    因为我就是这个原因,按照上面安装了libapache2-mod-php5后,嗨呀,一切正常了

在配置过程中,apache2的默认根目录是/var/www/html,并不是以前的/var/www,当然,这个可以到配置文件中进行设置。

为了测试php代码是否正常执行,可以通过最简单的一个php脚本测试

1
2
3
<?php
phpinfo();
>

因为我并没有学过php,对于web服务器的配置也一知半解,所以在这个过程耗费了我好多的时间

不过,感谢网上各种大牛的帮助,也是解决了这个问题。


假期别颓啊喂,好好学习!!!

×

赞助gif换电脑、吃双皮奶(逃

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. Ubuntu中配置好apache2后php代码没执行
    1. 1.0.0.1. 假期别颓啊喂,好好学习!!!
,