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

蜗牛vps教程2021-10-022760

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 网页。

在 Linux 服务器上安装 Apache Web 服务器  第1张

请参阅下面有关在 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   <virtualhost *:80=""> 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 </virtualhost>
  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

在 Linux 服务器上安装 Apache Web 服务器  第2张



CentOS 6

安装Apache服务器

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

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

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

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

    # service httpd restart



配置Apache服务器

下一步是为域设置 Web 服务器配置。配置文件名为httpd.conf,Apache 配置目录位置为/etc/httpd/

  1. 打开 apache 配置文件,/etc/httpd/conf/httpd.conf并将以下几行添加到文件底部。

    # vi /etc/httpd/conf/httpd.conf  <virtualhost *:80=""> ServerAdmin root@zitian.cn ServerName 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 </virtualhost>
  2. 为网站创建一个目录,然后index.html为网站创建文件。

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

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

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

    http://zitian.cn



测试 Apache 服务器

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

http://ip:port

在 Linux 服务器上安装 Apache Web 服务器  第3张



Ubuntu 和 Debian

安装Apache服务器

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

    # apt-get install apache2
  2. 运行以下命令启动Apache进程。

    # /etc/init.d/apache2 start
  3. 通过执行以下命令验证服务是否正在运行。

    # /etc/init.d/apache2 status
  4. 运行以下命令以重新启动 Apache。

    # /etc/init.d/apache2 restart



配置Apache服务器

下一步是为域设置 Web 服务器配置。Apache配置目录是/etc/apache2apache2.conf是主要的Apache配置文件。每个域都需要自己的虚拟主机配置文件。

配置文件使用.conf extension, 并且需要保存在/etc/apache2/sites-available/目录中。

  1. 创建一个文件 at/etc/apache2/sites-available/zitian.cn.conf并向其添加以下行。

    # nano /etc/apache2/sites-available/zitian.cn.conf  <virtualhost *:80="">   ServerAdmin webmaster@localhost   ServerName zitian.cn   ServerAlias www.zitian.cn   DocumentRoot /var/www/zitian.cn   ErrorLog ${APACHE_LOG_DIR}/error.log   CustomLog ${APACHE_LOG_DIR}/access.log combined   </virtualhost>
  2. 为网站创建一个目录,然后index.html为网站创建文件。

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

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

    # /etc/init.d/apache2 restart or # sudo systemctl restart apache2
  5. 打开任何浏览器并输入网站 URL。

    http://zitian.cn



测试 Apache 服务器

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

http://ip:port

在 Linux 服务器上安装 Apache Web 服务器  第4张


评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接