The Apache HTTP Web server is a powerful, free and open source web server that has been, and remains the leading web server platform ahead of others such as Nginx and Microsoft IIS.

Apache HTTP Web服务器是一种功能强大,免费和开放源代码的Web服务器,它已经并且一直领先于Nginx和Microsoft IIS等领先的Web服务器平台。

According to Opensource.com Apache enjoys a market share of 45.9%, and powers popular sites such as PayPal, Apple, Craigslist, Adobe, and BBC to mention just but a few.

据Opensource.com称,Apache占有45.9%的市场份额,并且为PayPal,Apple,Craigslist,Adobe和BBC等受欢迎的网站提供了强大的支持。

什么是虚拟主机? (What is Virtual Host?)

Apache web server allows us to create individual units which can be configured and customized independently to serve multiple sites on the same server. This is known as virtual hosting.

Apache Web服务器使我们能够创建可以独立配置和自定义的单个单元,以在同一服务器上为多个站点提供服务。 这称为虚拟主机。

This happens when multiple domain names, each handled separately, are hosted on a single server. The server is thereby able to share its resources such as disk space and memory utilization.

在单个服务器上托管多个域名(每个域名分别处理)时,会发生这种情况。 服务器因此能够共享其资源,例如磁盘空间和内存利用率。

Each domain is configured to direct traffic to a unique directory containing information specific to that domain name.

每个域都配置为将流量定向到包含该域名特定信息的唯一目录。

A widely used application of virtual hosting is in shared web hosting where multiple customers are hosted on a single server. This is a cheaper alternative than dedicated hosting which is far more expensive.

虚拟主机的一种广泛使用的应用程序是共享Web主机,其中将多个客户托管在一台服务器上。 这比专用托管要便宜得多,专用托管要昂贵得多。

In this guide, we will delve in setting up and configuration of Apache Virtual hosts on Ubuntu 18.04 LTS.

在本指南中,我们将深入研究在Ubuntu 18.04 LTS上设置和配置Apache虚拟主机。

安装Apache Web服务器 (Installing Apache Web Server)

Before installing Apache, it is a good idea to update the package repository. You can do this by running the following commands:

在安装Apache之前,最好先更新软件包存储库。 您可以通过运行以下命令来执行此操作:

# sudo apt update

Thereafter, install the Apache web server.

此后,安装Apache Web服务器。

# sudo apt install apache2

The two commands mark the first of a very important process. For illustration purposes, we will use ubuntu.biz and debian.edu as our virtual hosts.

这两个命令标志着一个非常重要的过程的第一步。 出于说明目的,我们将使用ubuntu.bizdebian.edu作为我们的虚拟主机。

Following this guide will also take you through testing both domains and see if the configurations are working.

遵循本指南还将引导您测试两个域,并查看配置是否有效。

为虚拟主机创建目录结构 (Creating directory structures for virtual hosts)

We need a directory that will hold the data that can be accessed by users. Apache usually has a root document directory that serves users seeking information from it. Content within the root directory is assigned to individual users under the /var/www directory.

我们需要一个目录来保存用户可以访问的数据。 Apache通常有一个根文档目录,该目录服务于从中寻找信息的用户。 根目录中的内容分配给/var/www目录下的各个用户。

Within each of these directories, we need a folder public folder (public_html) to hold all the uploaded files.

在每个目录中,我们需要一个文件夹公用文件夹(public_html)来保存所有上载的文件。

Run the following commands to create the two directories:

运行以下命令以创建两个目录:

# sudo mkdir -p /var/www/ubuntu.biz/public_html
# sudo mkdir -p /var/www/debian.edu/public_html

授予权限 (Granting Permissions)

Now that we have the directory structures in place, we need to assign ownership to regular users so that they can modify files contained in the directories. To accomplish this, we need to run the commands as shown.

现在我们已经有了目录结构,我们需要将所有权分配给普通用户,以便他们可以修改目录中包含的文件。 为此,我们需要运行如下所示的命令。

# sudo chown -R $USER:$USER /var/www/ubuntu.biz/public_html
# sudo chown -R $USER:$USER /var/www/debian.edu/public_html

The $USER variable takes the value of the currently logged on user upon pressing ‘Enter’. This allows the regular user to own the subdirectories contained in public_html which will host our content.

$USER变量在按“ Enter”键时获取当前登录用户的值。 这允许普通用户拥有public_html中包含的子目录,这些子目录将托管我们的内容。

To serve the webpages of the virtual hosts properly, we need to assign read permissions to the general www directory which will recursively assign read permissions to the files and folders therein. To achieve this, run

为了正确地为虚拟主机的网页提供服务,我们需要将读取权限分配给常规www目录,该目录将递归地将读取权限分配给其中的文件和文件夹。 为此,请运行

# sudo chmod -R 755 /var/www

At this point, our web server should have the requisite permissions to serve the content and users should also be able to create their own content within the respective directories.

此时,我们的Web服务器应该具有提供内容的必要权限,并且用户还应该能够在相应目录中创建自己的内容。

创建新的虚拟主机文件 (Create new virtual host files)

We need to copy the file to the first domain:

我们需要将文件复制到第一个域:

# sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ubuntu.biz.conf

Let us open the new file using nano as shown:

让我们使用nano打开新文件,如下所示:

# sudo nano /etc/apache2/sites-available/ubuntu.biz.conf

You can either choose to leave it the way it is or make it more presentable for the first timer in VPS installation (note that the original file has # which denotes comments explaining what every line stands for).

您可以选择保留它的状态,也可以使其在VPS安装中的第一个计时器中更具表现力(请注意,原始文件带有#,该注释表示解释每一行所代表的意思)。

From the file above the virtual host will process any request made on port 80 with the default protocol used here is the HTTP.

从上面的文件中,虚拟主机将使用默认协议(此处为HTTP)来处理在端口80上发出的任何请求。

We need to assign a working email to the ServerAdmin attribute. In this case, we will use winnie@example.com

我们需要将有效的电子邮件分配给ServerAdmin属性。 在这种情况下,我们将使用winnie@example.com

We will then add two lines:

然后,我们将添加两行:

  1. ServerName, which matches the base domain for the particular virtual host.ServerName ,与特定虚拟主机的基本域匹配。
  2. ServerAlias that defines any other name that should closely match the base domain name. In this case, we will use www.ServerAlias定义任何其他名称,这些其他名称应与基本域名紧密匹配。 在这种情况下,我们将使用www。

For the DocumentRoot attribute, change the path to reflect the virtual host file path.

对于DocumentRoot属性,更改路径以反映虚拟主机文件路径。

All the three changes can be summarized by editing the virtual host file to look like this:

通过将虚拟主机文件编辑为以下形式,可以概括所有三个更改:

Save the changes and exit.

保存更改并退出。

Let’s copy the first Virtual host and customize for debian.edu (the other domain)

让我们复制第一个虚拟主机并为debian.edu (另一个域)自定义

We copy it by:

我们通过以下方式复制它:

# sudo cp /etc/apache2/sites-available/ubuntu.biz.conf /etc/apache2/sites-available/debian.edu.conf

Open and edit the file as shown:

打开并编辑文件,如下所示:

# sudo nano /etc/apache2/sites-available/debian.edu.conf

Open the configuration file and make the corresponding changes where needed

打开配置文件,并在需要的地方进行相应的更改

Save and exit the configuration file.

保存并退出配置文件。

启用新创建的主机文件 (Enabling newly created hosts files)

Apache has some inbuilt tools that enable us to “activate” files. You can use the a2ensite tool to accomplish this as shown:

Apache有一些内置工具,使我们能够“激活”文件。 您可以使用a2ensite工具来完成此任务,如下所示:

# sudo a2ensite ubuntu.biz.conf
# sudo a2ensite debian.edu.conf

Next, restart the Apache web server for the changes to take effect.

接下来,重新启动Apache Web服务器以使更改生效。

# sudo systemctl restart apache2

To test the functionality of your VPS, you need to modify the hosts file on the local computer. This helps re-direct domain requests to your VPS servers. This works the same way DNS systems operate when registering new domains.

要测试VPS的功能,您需要修改本地计算机上的主机文件。 这有助于将域请求重定向到您的VPS服务器。 这与注册新域时DNS系统运行的方式相同。

The following steps should be performed on the local workstation and not the VPS server. You need to log in as the root for this to work. Edit the /etc/hosts file as shown.

以下步骤应在本地工作站而不是VPS服务器上执行。 您需要以root用户身份登录才能正常工作。 如图所示,编辑/etc/hosts文件。

# sudo vim /etc/hosts

On this file, you need to add the public address (IP) of your VPS server then the domain name used to access the VPS. In my case, my VPS address is 192.168.43.195; consequently, I will edit my hosts’ file as shown:

在此文件上,您需要添加VPS服务器的公共地址(IP),然后添加用于访问VPS的域名。 就我而言,我的VPS地址为192.168.43.195; 因此,我将编辑主机文件,如下所示:

Save and exit the text editor.

保存并退出文本编辑器。

The above changes mean that any visitor’s request to ubuntu.biz and debian.edu will be directed through 192.168.43.195. This is how domains are redirected on Virtual Hosts.

上述更改意味着任何访问ubuntu.bizdebian.edu的请求都将通过192.168.43.195定向。 这就是在虚拟主机上重定向域的方式。

测试虚拟服务器 (Testing the virtual servers)

Now that we are done with configuring of our virtual hosts it’s time to verify if all went all. For a start, you can check the accessibility of the two virtual hosts on the terminal using the ping command

现在我们已经完成了虚拟主机的配置,是时候验证所有虚拟主机是否全部可用了。 首先,您可以使用ping命令检查终端上两个虚拟主机的可访问性

Wonderful! We can reach the virtual host. You can now proceed and open your browser and visit the URL.

精彩! 我们可以到达虚拟主机。 现在,您可以继续打开浏览器并访问URL。

https://ubuntu.biz

Great! Our first virtual host is working. Let’s try pinging our second virtual host

大! 我们的第一台虚拟主机正在运行。 让我们尝试ping第二台虚拟主机

Similarly, open your browser and visit the virtual host’s URL

同样,打开浏览器并访问虚拟主机的URL

https://debian.edu

结论 (Conclusion)

Apache HTTP web server can be easily configured to serve multiple websites from a single server. In this tutorial, we learned how to set up multiple virtual hosts configuration files in the apache web server.

可以轻松配置Apache HTTP Web服务器,以通过单个服务器为多个网站提供服务。 在本教程中,我们学习了如何在apache Web服务器中设置多个虚拟主机配置文件。

翻译自: https://www.journaldev.com/30055/apache-virtual-hosts-ubuntu-18-04

如何在Ubuntu 18.04上设置Apache虚拟主机相关推荐

  1. ubuntu配置mta_如何在Ubuntu 18.04上使用Apache为您的域配置MTA-STS和TLS报告

    ubuntu配置mta The author selected Electronic Frontier Foundation Inc to receive a donation as part of ...

  2. eclipse theia_如何在Ubuntu 18.04上设置Eclipse Theia Cloud IDE平台[快速入门]

    eclipse theia 介绍 (Introduction) Eclipse Theia is an extensible cloud IDE running on a remote server ...

  3. linux joomla安装教程,如何在Ubuntu 18.04上使用Apache安装Joomla

    Joomla是为数十万个网站提供支持的最受欢迎的开源内容管理系统之一.它是用PHP编写的,并且包含大量功能,可以使用免费和高级扩展以及主题进行扩展.使用Joomla,您可以轻松建立自己的电子商务商店, ...

  4. centos8 配置 dns_如何在Ubuntu 18.04上设置DNS名称服务器 | linux资讯

    域名系统(DNS)是网络基础设施的核心部分,提供了将域名转换为IP地址的方法.您可以将DNS视为Internet的电话簿. 连接到Internet的每个设备都由其IP地址唯一标识.当您在浏览器中输入要 ...

  5. 如何在Ubuntu 18.04上设置Mattermost

    介绍 (Introduction) Mattermost is an open source collaboration and messaging platform created with sec ...

  6. eclipse theia_如何在Ubuntu 18.04上设置Eclipse Theia Cloud IDE平台

    eclipse theia 介绍 (Introduction) With developer tools moving to the cloud, adoption of cloud IDE (Int ...

  7. 如何在Ubuntu 18.04上安装Apache Kafka

    介绍 (Introduction) Apache Kafka is a popular distributed message broker designed to efficiently handl ...

  8. 如何在ubuntu 18.04上设置设置sougo拼音输入法

    一.下载sougou拼音deb文件 下载地址:http://pinyin.sogou.com/linux/?r=pinyin 二.在ubuntu上安装相应的配套程序 1.sudo apt-get up ...

  9. 如何在Ubuntu 18.04上安装和配置NFS服务器

    网络文件系统(NFS)是一种分布式文件系统协议,使您可以通过网络共享远程目录.使用NFS,您可以在系统上挂载远程目录,并像对待本地文件一样使用远程计算机上的文件. NFS协议默认情况下未加密,并且与S ...

最新文章

  1. LoadRunner模拟Json请求
  2. JavaScript 的DOM操作
  3. Win2003下Asp配置技巧 http 500内部服务器错误
  4. uniapp 单元测试_单元测试不够验收测试凑,编写验收测试有什么技巧?
  5. 自己的总结(你必须知道的C 495个问题)
  6. Andriod开发中正确引入jar包的方式
  7. python新手入门英文词汇笔记(1-1)_Python新手入门英文词汇(1-1)
  8. item 24: 区分右值引用和universal引用
  9. 已知网友建立html,职称计算机模拟试题:Dreamweaver网页设计模拟试题及答案(5)...
  10. 如何打开屏幕坏的手机_手机屏幕坏了怎么打开usb调试
  11. Fiddler的使用介绍及抓包分析(详解)
  12. Java 打印Hello World
  13. 智能语音的扫地机器人介简_米家扫地机器人1S评测:升级支持智能语音操控
  14. Linux ARM平台开发系列讲解(摄像头V4L2子系统) 2.12.4 V4L2子设备操作函数结构体分析
  15. 手机地理位置共享引发的不安全因素
  16. 纯前端实现文件下载功能
  17. 动手学ocr·十讲--学习笔记一
  18. 【集体智慧编程】第二章、提供推荐
  19. PTA 使我精神焕发
  20. 我们的实践:事理图谱,下一代知识图谱

热门文章

  1. php中0, ,null和false的区别
  2. 【NOIP2016提高A组模拟10.15】算循环
  3. Ubuntu 14.04卸载安装失败的Mysql数据库,以及重新安装配置
  4. 大型网站首页执行时间0.3秒,性能算好还是算坏?
  5. Ubuntu 左边栏和顶栏都不见了,ctrl+alt+t 也调用不出terminal
  6. sprintf 函数
  7. 《程序设计实践》读书笔记第五至六章
  8. [转载] Python round四舍五入精度缺失的解决
  9. 用框架名唬人谁都会,那你知道Web开发模式吗?——莫问前程莫装逼
  10. 固定日期时间倒计时,倒计时不可点击,普通倒计时