如何在Ubuntu 18.04上设置Apache虚拟主机

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值