HTTPd 显示 PHP 代码而不是执行它

sti*_*tiv 0 centos php apache-httpd

我的设置:

# grep php /etc/httpd/conf/httpd.conf 
LoadModule php5_module  modules/libphp5.so
AddType x-httpd-php .php
AddHandler php5-script .php

# grep PHP /etc/httpd/conf/httpd.conf 
PHPIniDir /etc/

# grep open_tag /etc/php.ini 
; short_open_tag
short_open_tag = On

# /etc/init.d/httpd start
Starting httpd: [Tue Dec 04 03:26:29 2012] [warn] module php5_module is already loaded, skipping
httpd: apr_sockaddr_info_get() failed for holdsworth
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]

# cat /var/log/httpd/error_log 
[Tue Dec 04 03:26:37 2012] [notice] SELinux policy enabled; httpd running as context unconfined_u:system_r:httpd_t:s0
[Tue Dec 04 03:26:37 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Tue Dec 04 03:26:37 2012] [warn] module php5_module is already loaded, skipping
[Tue Dec 04 03:26:45 2012] [notice] Digest: generating secret for digest authentication ...
[Tue Dec 04 03:26:45 2012] [notice] Digest: done
[Tue Dec 04 03:26:53 2012] [warn] ./mod_dnssd.c: No services found to register
[Tue Dec 04 03:26:53 2012] [notice] Apache/2.2.15 (Unix) DAV/2 PHP/5.3.3 configured -- resuming normal operations
[Tue Dec 04 03:28:35 2012] [notice] caught SIGTERM, shutting down
Run Code Online (Sandbox Code Playgroud)

access_log 为空。当我打开 index.php 时,它会显示其内容。

[root@holdsworth steve]# ls -lah /var/www/html/php/index.php 
-rwxrwxrwx. 1 root root 1.7K Oct 31 18:38 /var/www/html/php/index.php
Run Code Online (Sandbox Code Playgroud)

小智 5

看起来你同时启用了suEXECSELinux,所以你需要修改以允许执行php脚本,开始检查CGI脚本是否被允许:

getsebool -a | egrep 'cgi|builtin_scriptin'
Run Code Online (Sandbox Code Playgroud)

如果不:

setsebool -P httpd_enable_cgi 1
setsebool -P httpd_builtin_scripting 1
Run Code Online (Sandbox Code Playgroud)

还要检查 php 脚本是否具有必要的扩展标志,由 apache 进程拥有和可执行,以保持 SElinux 和 suEXEC 满意:

ls -lZ *.php

chcon -t httpd_sys_script_exec_t *.php
chmod +x *.php
chown  xxxx:yyyyyy  *.php

ls -l *.php
Run Code Online (Sandbox Code Playgroud)

或者,如果不是公共服务器,则禁用两者。

请参阅:http : //beginlinux.com/server_training/web-server/976-apache-and-selinux