关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

在 Linux 服务器上安装 Apache Web 服务器

发布时间:2021/10/2 11:25:34
香港云服务器

Apache Web 服务器是用于现代操作系统(包括 Linux 和 Windows)的开源 HTTP 服务器。它是 Internet 上最流行的 Web 服务器。Apache 的配置文件和安装方法根据 Linux 的不同发行版而有所不同。但是默认文档根/var/www/html在所有发行版中。



Debian 和 Ubuntu 发行版将 Apache 称为“Apache2”,Apache2 的配置文件是 /etc/apache2/apache2.conf.

CentOS 将 Apache 称为 Apache httpd,httpd 的配置文件是/etc/httpd/httpd.conf.



安装完成后,您可以通过在浏览器地址栏中输入服务器 IP 地址来检查是否安装了 Apache:

http://ip:port

如果安装成功,您可以看到如下所示的默认 Apache 网页。

image.png

请参阅下面有关在 CentOS、Debian 和 Ubuntu 上安装、配置和测试 Apache 服务器的说明。



CentOS 7/8

安装Apache服务器

  1. 运行以下命令安装Apache。

    # yum install httpd
  2. 即使安装完成,Apache 也不会自动启动。运行以下命令启动Apache进程。

    # systemctl start httpd
  3. 通过执行以下命令验证服务是否正在运行。

    # systemctl status httpd
  4. 运行以下命令以重新启动 Apache。

    # systemctl restart httpd



配置Apache服务器

下一步是在 Apache 中为新域添加和更新 VirtualHost。每个域都需要自己的配置文件。配置文件使用.conf extension, 并且需要保存在/etc/httpd/conf.d/目录中。

在下面的示例中,zitian.cn需要替换为网站的实际名称。

  1. 创建一个文件 at/etc/httpd/conf.d/zitian.cn.conf并向其添加以下行。

    # vi /etc/httpd/conf.d/zitian.cn.conf    ServerAdmin root@zitian.cn ServerName zitian.cn ServerAlias www.zitian.cn DocumentRoot /var/www/html/zitian.cn/ ErrorLog /var/log/httpd/zitian.cn/error.log CustomLog /var/log/httpd/zitian.cn/access.log combined 
  2. 为网站创建一个目录,然后index.html为网站创建文件。

    # mkdir /var/www/html/zitian.cn
  3. 添加一些内容到index.html.

    # vi /var/www/html/zitian.cn/index.html
  4. 重启Apache服务使上述更改生效。

    # systemctl restart httpd
  5. 打开任何浏览器并输入网站 URL。

    http://zitian.cn



测试 Apache 服务器

可以通过在浏览器的地址栏中输入服务器 IP 地址来测试 Apache Web 服务器:

http://ip:port

image.png



CentOS 6

安装Apache服务器

  1. 运行以下命令安装Apache。

    # yum install httpd
  2. 运行以下命令启动Apache进程。

    # service httpd start
  3. 通过执行以下命令验证服务是否正在运行。

    # service httpd status
  4. 运行以下命令以重新启动 Apache。

    # service httpd restart