Linux 安装 httpd 服务器

安装 httpd

运行下面的命令:

yum install httpd

开放防火墙的 HTTP 端口

针对 Apache 的 httpd 服务器,可以使用下面的命令开放防火墙端口。

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent

开放防火墙后,使用下面的命令让防火墙生效

firewall-cmd --reload

启动 httpd

使用下面的命令来启动 httpd 服务器

systemctl restart httpd

校验服务器

使用网页访问服务器的 IP 地址,如果能看到下面的界面,则说明服务器已经正常启动了。

配置 HTTPD AH 00558 错误

重启的时候可能会导致 HTTPD 启动失败,如果有 AH00558 错误,请参考链接:Apache Httpd 安装 AH00558错误

安装完成 Httpd 后出现错误:

[root@vps-f2a02f66 vhosts]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
       └─php-fpm.conf
   Active: failed (Result: exit-code) since Sun 2020-05-24 16:56:11 UTC; 12min ago
 Docs: man:httpd.service(8)
  Process: 17168 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
 Main PID: 17168 (code=exited, status=1/FAILURE)
   Status: "Reading configuration..."
  CPU: 44ms
 
May 24 16:56:11 vps-f2a02f66.vps.ovh.ca systemd[1]: Starting The Apache HTTP Server...
May 24 16:56:11 vps-f2a02f66.vps.ovh.ca httpd[17168]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using vps-f2a02f66.vps.ovh.ca. Set the 'ServerName' directive globally to suppress this >
May 24 16:56:11 vps-f2a02f66.vps.ovh.ca systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
May 24 16:56:11 vps-f2a02f66.vps.ovh.ca systemd[1]: httpd.service: Failed with result 'exit-code'.
May 24 16:56:11 vps-f2a02f66.vps.ovh.ca systemd[1]: Failed to start The Apache HTTP Server.

服务器的错误提示信息如下:

httpd-error-01

这是因为你的服务器没设置域名。

你可以设置本地域名后解决这个问题。

编辑 vi /etc/httpd/conf/httpd.conf 文件。

然后修改 ServerName localhost:80 为 localhost 或者取消注释这行。

httpd-error-02

然后重启服务器后,就可以修正上面的错误。

配置 HTTPD AH00091 错误

在你配置 HTTPD 的时候可能会有这个错,这个错误信息是提示你的日志可能没有办法被写入。

请参考链接:Apache 安装运行测时候提示错误 (13)Permission denied: AH00091: httpd: could not open error log file

Apache 提示错误信息为日志不能写入。

具体是:

(13)Permission denied: AH00091: httpd: could not open error log file /var/www/html/vhosts/ossez.com/wiki.ossez.com/logs/errors.log. AH00015: Unable to open logs

导致这个问题的原因是 SELinux 运行在 enforcing 模式。

这个将会禁止 Apache 将日志写入到费标准的日志目录中。

你需要运行下面的命令将日志配置到 SELinux 中。

semanage fcontext -a -t httpd_log_t "/home/vhosts(/.*)?"
semanage fcontext -a -t httpd_sys_content_t "/home/vhosts(/.*)?"
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www(/.*)?"
restorecon -Rv /var/www

然后重启服务器,你应该可以看到没有这个错误了。

解决目录不能访问的错误,可能是因为 SELinux 导致的,你可以尝试运行下面的命令。

sudo chcon -t httpd_sys_rw_content_t /var/www/ossez.com -R

上面的命令可以解决目录没有写入权限的问题。