# Linux 安装 httpd 服务器

**URL:** <https://www.isharkfly.com/t/linux-httpd/15231>\
**Category:** DevOps\
**Tags:** linux\
**Created:** [January 9, 2024, 2:31am UTC](https://www.isharkfly.com/t/linux-httpd/15231 "2024-01-09T02:31:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [January 9, 2024, 2:31am UTC](https://www.isharkfly.com/t/linux-httpd/15231/1 "2024-01-09T02:31:32Z")

</div>

## 安装 httpd

运行下面的命令：

`yum install httpd`

## 开放防火墙的 HTTP 端口

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

```auto
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 地址，如果能看到下面的界面，则说明服务器已经正常启动了。

 ![](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/b/bcc6f83d9f82535038406604a79b7f2dca6a00a3.jpeg)

## 配置 HTTPD AH 00558 错误

重启的时候可能会导致 HTTPD 启动失败，如果有 AH00558 错误，请参考链接：[Apache Httpd 安装 AH00558错误](https://www.isharkfly.com/t/apache-httpd-ah00558/89)

安装完成 Httpd 后出现错误：

```auto
[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](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/a/a08e76a71f83b2cca1f4052e9f6ce6c376348eb2.jpeg)](https://cdn.ossez.com/discourse-uploads/original/1X/9c949edf184a3760fae97de2e88285bf918e43f0.jpeg)

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

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

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

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

[![httpd-error-02](https://com-ossez-www-discourse.s3.dualstack.us-east-2.amazonaws.com/discourse-uploads/original/1X/1c1b220f015431e626dde9694824e17a6c3b7801.jpeg)](https://cdn.ossez.com/discourse-uploads/original/1X/1c1b220f015431e626dde9694824e17a6c3b7801.jpeg)

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

## 配置 HTTPD AH00091 错误

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

请参考链接：[Apache 安装运行测时候提示错误 (13)Permission denied: AH00091: httpd: could not open error log file](https://www.isharkfly.com/t/apache-13-permission-denied-ah00091-httpd-could-not-open-error-log-file/91)

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 中。

```auto
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`

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