解决Centos 7中安装LAMP之后Apache无法解析php
我在Centos7系统上安装了Apache,PHP 之后.在输出phpinfo(),检测php脚本的时候,发现我的系统根本没有解析php脚本文件,但是html文件可以解析的。检测文件的用户 组权限,也没有问题,这就奇怪了。
可能是由于apache没有加载php的模块。
执行下面命令,可以检测apache 已经安装的模块
httpd -M
[root@ysqblogsever html]# httpd -M
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.16.58. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
access_compat_module (shared)
actions_module (shared)
alias_module (shared)
allowmethods_module (shared)
......
proxy_wstunnel_module (shared)
ssl_module (shared)
systemd_module (shared)
cgi_module (shared)
perl_module (shared)
没有发现php_module.
再检查/etc/httpd/conf.modules.d/10-php.conf该文件是否存在,不存在的话就得安装mod_php模块。
[root@ysqblogsever httpd]# cd conf.modules.d/
[root@ysqblogsever conf.modules.d]# ls
00-base.conf 00-dav.conf 00-lua.conf 00-mpm.conf 00-proxy.conf 00-ssl.conf 00-systemd.conf 01-cgi.conf 02-perl.conf
[root@ysqblogsever conf.modules.d]# ls -l
total 36
-rw-r--r-- 1 root root 3739 Aug 6 21:44 00-base.conf
-rw-r--r-- 1 root root 139 Aug 6 21:44 00-dav.conf
-rw-r--r-- 1 root root 41 Aug 6 21:44 00-lua.conf
-rw-r--r-- 1 root root 742 Aug 6 21:44 00-mpm.conf
-rw-r--r-- 1 root root 957 Aug 6 21:44 00-proxy.conf
-rw-r--r-- 1 root root 41 Aug 6 21:44 00-ssl.conf
-rw-r--r-- 1 root root 88 Aug 6 21:44 00-systemd.conf
-rw-r--r-- 1 root root 451 Aug 6 21:44 01-cgi.conf
-rw-r--r-- 1 root root 448 Nov 4 20:15 02-perl.conf
我们发现上面没有10-php.conf,我强烈推荐使用yum命令来安装apache的mod_php模块:yum install mod_php71w 如果没有这个安装包,可以尝试着添加一下源:
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
最后执行如下面代码:
[root@ysqblogsever conf.modules.d]# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
Retrieving https://mirror.webtatic.com/yum/el7/epel-release.rpm
Preparing... ################################# [100%]
package epel-release-7-11.noarch (which is newer than epel-release-7-5.noarch) is already installed
[root@ysqblogsever conf.modules.d]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Retrieving https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Preparing... ################################# [100%]
Updating / installing...
1:webtatic-release-7-3 ################################# [100%]
[root@ysqblogsever conf.modules.d]# yum install mod_php71w
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* epel: mirrors.aliyun.com
* remi-safe: mirrors.tuna.tsinghua.edu.cn
* webtatic: us-east.repo.webtatic.com
webtatic | 3.6 kB 00:00:00
webtatic/x86_64/primary_db | 232 kB 00:00:02
Resolving Dependencies
--> Running transaction check
---> Package mod_php71w.x86_64 0:7.1.33-1.w7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================
Installing:
mod_php71w x86_64 7.1.33-1.w7 webtatic 2.9 M
Transaction Summary
==============================================================================================================================
Install 1 Package
Total download size: 2.9 M
Installed size: 9.2 M
Is this ok [y/d/N]: y
Downloading packages:
mod_php71w-7.1.33-1.w7.x86_64.rpm | 2.9 MB 00:04:09
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 1 pre-existing rpmdb problem(s), 'yum check' output follows:
python-requests-2.6.0-1.el7_1.noarch has missing requires of python-urllib3 >= ('0', '1.10.2', '1')
Installing : mod_php71w-7.1.33-1.w7.x86_64 1/1
Verifying : mod_php71w-7.1.33-1.w7.x86_64 1/1
Installed:
mod_php71w.x86_64 0:7.1.33-1.w7
Complete!
[root@ysqblogsever conf.modules.d]# ls -l
total 40
-rw-r--r-- 1 root root 3739 Aug 6 21:44 00-base.conf
-rw-r--r-- 1 root root 139 Aug 6 21:44 00-dav.conf
-rw-r--r-- 1 root root 41 Aug 6 21:44 00-lua.conf
-rw-r--r-- 1 root root 742 Aug 6 21:44 00-mpm.conf
-rw-r--r-- 1 root root 957 Aug 6 21:44 00-proxy.conf
-rw-r--r-- 1 root root 41 Aug 6 21:44 00-ssl.conf
-rw-r--r-- 1 root root 88 Aug 6 21:44 00-systemd.conf
-rw-r--r-- 1 root root 451 Aug 6 21:44 01-cgi.conf
-rw-r--r-- 1 root root 448 Nov 4 20:15 02-perl.conf
-rw-r--r-- 1 root root 299 Oct 26 18:50 10-php.conf
[root@ysqblogsever conf.modules.d]# http -M
-bash: http: command not found
[root@ysqblogsever conf.modules.d]# httpd -M
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.16.58. Set the 'ServerName' directive globally to suppress this message
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
access_compat_module (shared)
actions_module (shared)
alias_module (shared)
allowmethods_module (shared)
auth_basic_module (shared)
auth_digest_module (shared)
authn_anon_module (shared)
authn_core_module (shared)
authn_dbd_module (shared)
authn_dbm_module (shared)
authn_file_module (shared)
authn_socache_module (shared)
authz_core_module (shared)
authz_dbd_module (shared)
authz_dbm_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_owner_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cache_module (shared)
cache_disk_module (shared)
data_module (shared)
dbd_module (shared)
deflate_module (shared)
dir_module (shared)
dumpio_module (shared)
echo_module (shared)
env_module (shared)
expires_module (shared)
ext_filter_module (shared)
filter_module (shared)
headers_module (shared)
include_module (shared)
info_module (shared)
log_config_module (shared)
logio_module (shared)
mime_magic_module (shared)
mime_module (shared)
negotiation_module (shared)
remoteip_module (shared)
reqtimeout_module (shared)
rewrite_module (shared)
setenvif_module (shared)
slotmem_plain_module (shared)
slotmem_shm_module (shared)
socache_dbm_module (shared)
socache_memcache_module (shared)
socache_shmcb_module (shared)
status_module (shared)
substitute_module (shared)
suexec_module (shared)
unique_id_module (shared)
unixd_module (shared)
userdir_module (shared)
version_module (shared)
vhost_alias_module (shared)
dav_module (shared)
dav_fs_module (shared)
dav_lock_module (shared)
lua_module (shared)
mpm_prefork_module (shared)
proxy_module (shared)
lbmethod_bybusyness_module (shared)
lbmethod_byrequests_module (shared)
lbmethod_bytraffic_module (shared)
lbmethod_heartbeat_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_scgi_module (shared)
proxy_wstunnel_module (shared)
ssl_module (shared)
systemd_module (shared)
cgi_module (shared)
perl_module (shared)
php7_module (shared)
再次查看是否已经存在10-php.conf
[root@ysqblogsever conf.modules.d]# ls -al
total 48
drwxr-xr-x 2 root root 4096 Nov 25 10:42 .
drwxr-xr-x 7 root root 4096 Nov 25 11:42 ..
-rw-r--r-- 1 root root 3739 Aug 6 21:44 00-base.conf
-rw-r--r-- 1 root root 139 Aug 6 21:44 00-dav.conf
-rw-r--r-- 1 root root 41 Aug 6 21:44 00-lua.conf
-rw-r--r-- 1 root root 742 Aug 6 21:44 00-mpm.conf
-rw-r--r-- 1 root root 957 Aug 6 21:44 00-proxy.conf
-rw-r--r-- 1 root root 41 Aug 6 21:44 00-ssl.conf
-rw-r--r-- 1 root root 88 Aug 6 21:44 00-systemd.conf
-rw-r--r-- 1 root root 451 Aug 6 21:44 01-cgi.conf
-rw-r--r-- 1 root root 448 Nov 4 20:15 02-perl.conf
-rw-r--r-- 1 root root 299 Oct 26 18:50 10-php.conf
[root@ysqblogsever conf.modules.d]#
恭喜mod_php模块安装成功!
然后在apache的配置文件httpd.cnf中增加下面信息(粗体):
DirectoryIndex index.php index.html
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
最后重新启动httpd.service
sudo systemctl restart httpd.service