Intro

在CentOS 8.3中用yum安装完apache2.4.37和PHP7.4.16之后,配置文件满天飞。需要修改apache底下的httpd.conf文件才能让我们的PHP项目完成在apache服务器上的部署。

配置位置的修改

主要是修改/etc/httpd/conf/httpd.conf这个文件。

端口冲突

如果之前装过Nginx的话两个的端口都是80,需要避免端口冲突,如果选apache回避的话需要修改:

1
2
+ Listen 81
- Listen 80

导入PHP模块

找到LoadModule的位置加入:

1
2
3
4
5
6
7
8
9
10
11
12
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
+ LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
+ LoadModule php7_module /usr/lib64/httpd/modules/libphp7.so

如果没有导入第一个mpm的话,应该会报如下错误:

1
2
3
4
Apr 27 15:25:56 iZe4h75o51zvd0Z httpd[255035]: AH00013: Pre-configuration failed
Apr 27 15:25:56 iZe4h75o51zvd0Z systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
Apr 27 15:25:56 iZe4h75o51zvd0Z systemd[1]: httpd.service: Failed with result 'exit-code'.
Apr 27 15:25:56 iZe4h75o51zvd0Z systemd[1]: Failed to start The Apache HTTP Server.

导入了这个之后还会报另一个错误:

1
2
3
4
Apr 27 15:28:49 iZe4h75o51zvd0Z httpd[256073]: AH00534: httpd: Configuration error: More than one MPM loaded.
Apr 27 15:28:49 iZe4h75o51zvd0Z systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
Apr 27 15:28:49 iZe4h75o51zvd0Z systemd[1]: httpd.service: Failed with result 'exit-code'.
Apr 27 15:28:49 iZe4h75o51zvd0Z systemd[1]: Failed to start The Apache HTTP Server.

所以其实apache是有导入mpm的,只是目前发生了冲突。参考帖子在/etc/httpd/conf.modules.d/00-mpm.conf文件中把mpm模块注释掉

1
2
3
4
5
6
# event MPM: A variant of the worker MPM with the goal of consuming
# threads only for connections with active processing
# See: http://httpd.apache.org/docs/2.4/mod/event.html
#
+ #LoadModule mpm_event_module modules/mod_mpm_event.so
- LoadModule mpm_event_module modules/mod_mpm_event.so

从这个文件来看其实上面也有mod_mpm_prefork.so这个模块的导入选择,可以理解为当前版本的apache把模块引入分到了/etc/httpd/conf.modules.d文件夹中。

AddType

回到httpd.conf文件,找到AddType区域,加入:

1
2
3
4
5
6
7
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz

+ AddType application/x-httpd-php .php

让index.php可读

找到DirectoryIndex,在后面加上index.php:

1
2
3
4
5
6
7
8
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
- DirectoryIndex index.html index.htm
+ DirectoryIndex index.html index.htm index.php
</IfModule>

导入PHP.ini

在httpd.conf最后一行加入:

1
+ PHPIniDir /etc/php.ini

创建index.php

在默认的/var/www/html/目录下创建一个测试用的index.php:

1
2
3
4
5
<?php

phpinfo();

?>

一些有用的命令

启动/重启/查看状态——apache服务器:

1
systemctl start/restart/status httpd

查看service的启动日志:

1
journalctl -u httpd.service

寻找配置文件:

1
whereis httpd

检验apache配置文件的语法正确性:

1
apachectl configtest #如果结果是Syntax OK即为无语法错误

After

重启/启动apache,没有报错即启动成功。尝试访问http://ip/index.php (我的是http://112.124.16.130:81/index.php ),如果能出现PHP的信息界面即配置成功:

QQ图片20210427160347