显示纯PHP代码

时间:2017-10-28 09:44:20

标签: php apache amazon-ec2 centos centos7

我使用的是Amazon EC2,CentOS 7 x64_86,1GB RAM。

(1)运行命令

php -v

结果

PHP 7.0.24 (cli) (built: Sep 30 2017 10:10:28) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies

(2)运行命令

httpd -v

结果

Server version: Apache/2.4.6 (CentOS)
Server built:   Oct 19 2017 20:39:16

(3)运行命令

vi /etc/httpd/conf.d/welcome.conf

结果

#
# This configuration file enables the default "Welcome" page if there
# is no default index page present for the root URL.  To disable the
# Welcome page, comment out all the lines below.
#
# NOTE: if this file is removed, it will be restored on upgrades.
#
#<LocationMatch "^/+$">
#    Options -Indexes
#    ErrorDocument 403 /.noindex.html
#</LocationMatch>

<Directory /usr/share/httpd/noindex>
    AllowOverride None
    Require all granted
</Directory>

Alias /.noindex.html /usr/share/httpd/noindex/index.html
Alias /noindex/css/bootstrap.min.css /usr/share/httpd/noindex/css/bootstrap.min.css
Alias /noindex/css/open-sans.css /usr/share/httpd/noindex/css/open-sans.css
Alias /images/apache_pb.gif /usr/share/httpd/noindex/images/apache_pb.gif
Alias /images/poweredby.png /usr/share/httpd/noindex/images/poweredby.png

错误

enter image description here

enter image description here

如何解决?

1 个答案:

答案 0 :(得分:1)

你实际上看到了两个不同的问题。

问题1

查看目录列表,而不是返回index.php。这必须是因为您没有指定DirectoryIndex。检查你的vhost配置(类似/etc/httpd/sites-available/your-site.conf 而不是 welcome.conf),并在vhost块中添加一个,如:

<VirtualHost *:80>
    DirectoryIndex index.php index.html
    // ... rest of your config
</VirtualHost>

问题2

Apache正在显示index.php的内容,而不是处理它。这意味着未启用PHP支持。

  • 在Apache配置中查找LoadModule,并确保php有一些变体,例如LoadModule php5_module,并且未注释掉。

  • 检查您是否在Apache配置中配置了MIME类型,例如AddType application/x-httpd-php .php,并且未注释掉它。

此问题的参考:

PHP code is not being executed, instead code shows on the page

HTTPd shows PHP code instead of executing it