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

介绍 (Introduction)

The Apache web server is a popular method for serving websites on the internet. As of 2019, it is estimated to serve 29% of all active websites and it offers robustness and flexibility for developers. Using Apache, an administrator can set up one server to host multiple domains or sites off of a single interface or IP by using a matching system.

Apache Web服务器是一种在Internet上提供网站服务的流行方法。 截至2019年,据估计它将为所有活动网站中的29%提供服务,并且为开发人员提供了鲁棒性和灵活性。 管理员可以使用Apache使用匹配系统将一台服务器设置为承载单个接口或IP以外的多个域或站点。

Each domain or individual site — known as a “virtual host” — that is configured using Apache will direct the visitor to a specific directory holding that site’s information. This is done without indicating that the same server is also responsible for other sites. This scheme is expandable without any software limit as long as your server can handle the load. The basic unit that describes an individual site or domain is called a virtual host.

使用Apache配置的每个域或单个站点(称为“虚拟主机”)都会将访问者定向到保存该站点信息的特定目录。 这样做是在不指示同一服务器也负责其他站点的情况下完成的。 只要您的服务器可以处理负载,此方案就可以扩展而没有任何软件限制。 描述单个站点或域的基本单位称为virtual host

In this guide, we will walk you through how to set up Apache virtual hosts on an Ubuntu 18.04 server. During this process, you’ll learn how to serve different content to different visitors depending on which domains they are requesting.

在本指南中,我们将引导您逐步了解如何在Ubuntu 18.04服务器上设置Apache虚拟主机。 在此过程中,您将学习如何根据他们请求的域向不同的访问者提供不同的内容。

先决条件 (Prerequisites)

Before you begin this tutorial, you should create a non-root user.

在开始本教程之前,您应该创建一个非root用户

You will also need to have Apache installed in order to work through these steps. If you haven’t already done so, you can get Apache installed on your server through the apt package manner:

您还需要安装Apache才能完成这些步骤。 如果尚未这样做,则可以通过apt软件包方式在服务器上安装Apache:

  • sudo apt update

    sudo apt更新
  • sudo apt install apache2

    须藤apt install apache2

If you would like more detailed instructions as well as firewall setup, please refer to our guide How To Install the Apache Web Server on Ubuntu 18.04.

如果您想要更详细的说明以及防火墙设置,请参阅我们的指南“ 如何在Ubuntu 18.04上安装Apache Web服务器”

For the purposes of this guide, our configuration will make a virtual host for example.com and another for test.com. These will be referenced throughout the guide, but you should substitute your own domains or values while following along.

就本指南而言,我们的配置将为example.comtest.com创建一个虚拟主机。 在本指南中将引用所有这些内容,但是在后续操作中,您应该替换自己的域或值。

If you are using DigitalOcean, you can learn how to set up domains by following the product documentation, How to Add Domains. For other providers, refer to their relevant product documentation If you do not have domains available at this time, you can use test values.

如果您使用的是DigitalOcean,则可以按照产品文档如何添加域来学习如何设置 。 对于其他提供商,请参考其相关的产品文档。如果此时您没有可用的域,则可以使用测试值。

We will show how to edit your local hosts file later on to test the configuration if you are using test values. This will allow you to validate your configuration from your home computer, even though your content won’t be available through the domain name to other visitors.

如果您使用测试值,稍后我们将显示如何编辑本地主机文件以测试配置。 即使您的内容无法通过域名提供给其他访问者,这也使您可以从家用计算机验证您的配置。

第一步-创建目录结构 (Step One — Create the Directory Structure)

The first step that we are going to take is to make a directory structure that will hold the site data that we will be serving to visitors.

我们要采取的第一步是建立一个目录结构,该目录结构将保存我们将提供给访问者的站点数据。

Our document root (the top-level directory that Apache looks at to find content to serve) will be set to individual directories under the /var/www directory. We will create a directory here for both of the virtual hosts we plan on making.

我们的document root (Apache用来查找要提供的内容的顶级目录)将设置为/var/www目录下的各个目录。 我们将在此处为我们计划制造的两个虚拟主机创建一个目录。

Within each of these directories, we will create a public_html folder that will hold our actual files. This gives us some flexibility in our hosting.

每个目录中,我们将创建一个public_html ,将举行我们的实际文件的文件夹。 这使我们在托管方面具有一定的灵活性。

For instance, for our sites, we’re going to make our directories as follows. If you are using actual domains or alternate values, swap out the highlighted text for these.

例如,对于我们的站点,我们将按照以下方式创建目录。 如果您使用的是实际域或备用值,请换成突出显示的文本。

  • sudo mkdir -p /var/www/example.com/public_html

    须藤mkdir -p / var / www / example.com / public_html

  • sudo mkdir -p /var/www/test.com/public_html

    须藤mkdir -p / var / www / test.com / public_html

The portions in red represent the domain names that we want to serve from our VPS.

红色部分代表我们要从VPS提供服务的域名。

第二步-授予权限 (Step Two — Grant Permissions)

Now we have the directory structure for our files, but they are owned by our root user. If we want our regular user to be able to modify files in our web directories, we can change the ownership by doing this:

现在我们有了文件的目录结构,但是它们归我们的根用户所有。 如果我们希望普通用户能够修改我们的Web目录中的文件,则可以通过执行以下操作来更改所有权:

  • sudo chown -R $USER:$USER /var/www/example.com/public_html

    须藤chown -R $ USER:$ USER / var / www / example.com / public_html

  • sudo chown -R $USER:$USER /var/www/test.com/public_html

    须藤chown -R $ USER:$ USER / var / www / test.com / public_html

The $USER variable will take the value of the user you are currently logged in as when you press ENTER. By doing this, our regular user now owns the public_html subdirectories where we will be storing our content.

$USER变量将采用您按ENTER时当前登录的用户的值。 通过这样做,我们的普通用户现在拥有将存储内容的public_html子目录。

We should also modify our permissions to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:

我们还应该修改权限,以确保允许对常规Web目录及其包含的所有文件和文件夹进行读取访问,以便可以正确提供页面:

  • sudo chmod -R 755 /var/www

    须藤chmod -R 755 / var / www

Your web server should now have the permissions it needs to serve content, and your user should be able to create content within the necessary folders.

现在,您的Web服务器应该具有提供内容所需的权限,并且您的用户应该能够在必要的文件夹中创建内容。

第三步-为每个虚拟主机创建演示页面 (Step Three — Create Demo Pages for Each Virtual Host)

We now have our directory structure in place. Let’s create some content to serve.

现在,我们有了目录结构。 让我们创建一些要服务的内容。

For demonstration purposes, we’ll make an index.html page for each site.

出于演示目的,我们将为每个站点创建一个index.html页面。

Let’s begin with example.com. We can open up an index.html file in a text editor, in this case we’ll use nano:

让我们从example.com开始。 我们可以在文本编辑器中打开index.html文件,在这种情况下,我们将使用nano:

  • nano /var/www/example.com/public_html/index.html

    纳米/ var / www / example.com /public_html/index.html

Within this file, create an HTML document that indicates the site it is connected to, like the following:

在此文件中,创建一个HTML文档,该文档指示其连接到的站点,如下所示:

/var/www/example.com/public_html/index.html
/var/www/example.com/public_html/index.html
<html>
  <head>
    <title>Welcome to Example.com!</title>
  </head>
  <body>
    <h1>Success! The example.com virtual host is working!</h1>
  </body>
</html>

Save and close the file (in nano, press CTRL + X then Y then ENTER) when you are finished.

完成后,保存并关闭文件(在nano中,按CTRL + X然后按Y然后按ENTER )。

We can copy this file to use as the basis for our second site by typing:

我们可以通过键入以下内容来复制此文件以用作第二个站点的基础:

  • cp /var/www/example.com/public_html/index.html /var/www/test.com/public_html/index.html

    cp / var / www / example.com /public_html/index.html / var / www / test.com /public_html/index.html

We can then open the file and modify the relevant pieces of information:

然后,我们可以打开文件并修改相关信息:

  • nano /var/www/test.com/public_html/index.html

    纳米/ var / www / test.com /public_html/index.html

/var/www/test.com/public_html/index.html
/var/www/test.com/public_html/index.html
<html>
  <head>
    <title>Welcome to Test.com!</title>
  </head>
  <body> <h1>Success! The test.com virtual host is working!</h1>
  </body>
</html>

Save and close this file as well. You now have the pages necessary to test the virtual host configuration.

保存并关闭此文件。 现在,您具有测试虚拟主机配置所需的页面。

第四步-创建新的虚拟主机文件 (Step Four — Create New Virtual Host Files)

Virtual host files are the files that specify the actual configuration of our virtual hosts and dictate how the Apache web server will respond to various domain requests.

虚拟主机文件是用于指定虚拟主机的实际配置并指示Apache Web服务器如何响应各种域请求的文件。

Apache comes with a default virtual host file called 000-default.conf that we can use as a jumping off point. We are going to copy it over to create a virtual host file for each of our domains.

Apache附带了一个默认的虚拟主机文件,称为000-default.conf ,我们可以将其用作起点。 我们将其复制以为我们的每个域创建一个虚拟主机文件。

We will start with one domain, configure it, copy it for our second domain, and then make the few further adjustments needed. The default Ubuntu configuration requires that each virtual host file end in .conf.

我们将从一个域开始,对其进行配置,将其复制到第二个域,然后进行一些必要的进一步调整。 默认的Ubuntu配置要求每个虚拟主机文件都以.conf结尾。

创建第一个虚拟主机文件 (Create the First Virtual Host File)

Start by copying the file for the first domain:

首先复制第一个域的文件:

  • sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

    须藤cp /etc/apache2/sites-available/000-default.conf / etc / apache2 / sites-available / example.com .conf

Open the new file in your editor with root privileges:

使用root特权在编辑器中打开新文件:

  • sudo nano /etc/apache2/sites-available/example.com.conf

    须藤纳米/ etc / apache2 / sites-available / example.com .conf

With comments removed, the file will look similar to this:

删除注释后,该文件将类似于以下内容:

/etc/apache2/sites-available/example.com.conf
/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Within this file, we will customize the items for our first domain and add some additional directives. This virtual host section matches any requests that are made on port 80, the default HTTP port.

在此文件中,我们将为第一个域自定义项目,并添加一些其他指令。 此虚拟主机部分与在端口80(默认HTTP端口)上进行的所有请求匹配。

First, we need to change the ServerAdmin directive to an email that the site administrator can receive emails through.

首先,我们需要将ServerAdmin指令更改为站点管理员可以通过其接收电子邮件的电子邮件。

ServerAdmin admin@example.com

After this, we need to add two directives. The first, called ServerName, establishes the base domain that should match for this virtual host definition. This will most likely be your domain. The second, called ServerAlias, defines further names that should match as if they were the base name. This is useful for matching hosts you defined, like www:

此后,我们需要添加两个指令。 第一个称为ServerName ,建立应与此虚拟主机定义匹配的基本域。 这很可能是您的域。 第二个名称称为ServerAlias ,它定义了其他名称,这些名称应该像基本名称一样进行匹配。 这对于匹配您定义的主机非常有用,例如www

ServerName example.com
ServerAlias www.example.com

The only other thing we need to change for our virtual host file is the location of the document root for this domain. We already created the directory we need, so we just need to alter the DocumentRoot directive to reflect the directory we created:

我们需要为虚拟主机文件更改的唯一另一件事是该域的文档根目录位置。 我们已经创建了所需的目录,因此我们只需要更改DocumentRoot指令以反映我们创建的目录:

DocumentRoot /var/www/example.com/public_html

When complete, our virtual host file should look like this:

完成后,我们的虚拟主机文件应如下所示:

/etc/apache2/sites-available/example.com.conf
/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

At this point, save and close the file.

此时,保存并关闭文件。

复制第一个虚拟主机并为第二个域自定义 (Copy First Virtual Host and Customize for Second Domain)

Now that we have our first virtual host file established, we can create our second one by copying that file and adjusting it as needed.

现在我们已经建立了第一个虚拟主机文件,我们可以通过复制该文件并根据需要对其进行调整来创建第二个虚拟主机文件。

Start by copying it:

首先复制它:

  • sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/test.com.conf

    须藤cp / etc / apache2 / sites-available / example.com .conf / etc / apache2 / sites-available / test.com .conf

Open the new file with root privileges in your editor:

在编辑器中以root特权打开新文件:

  • sudo nano /etc/apache2/sites-available/test.com.conf

    须藤纳米/ etc / apache2 / sites-available / test.com .conf

You now need to modify all of the pieces of information to reference your second domain. When you are finished, it should look like this:

现在,您需要修改所有信息,以引用您的第二个域。 完成后,它应如下所示:

/etc/apache2/sites-available/test.com.conf
/etc/apache2/sites-available/test.com.conf
<VirtualHost *:80>
    ServerAdmin admin@test.com
    ServerName test.com
    ServerAlias www.test.com
    DocumentRoot /var/www/test.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file when you are finished.

完成后保存并关闭文件。

第五步—启用新的虚拟主机文件 (Step Five — Enable the New Virtual Host Files)

Now that we have created our virtual host files, we must enable them. Apache includes some tools that allow us to do this.

现在我们已经创建了虚拟主机文件,我们必须启用它们。 Apache包含一些允许我们执行此操作的工具。

We’ll be using the a2ensite tool to enable each of our sites. If you would like to read more about this script, you can refer to the a2ensite documentation.

我们将使用a2ensite工具启用每个网站。 如果您想了解有关此脚本的更多信息,可以参考a2ensite文档

  • sudo a2ensite example.com.conf

    须藤a2ensite example.com .conf

  • sudo a2ensite test.com.conf

    须藤a2ensite test.com .conf

Next, disable the default site defined in 000-default.conf:

接下来,禁用在000-default.conf定义的默认站点:

  • sudo a2dissite 000-default.conf

    须藤a2dissite 000-default.conf

When you are finished, you need to restart Apache to make these changes take effect and use systemctl status to verify the success of the restart.

完成后,您需要重新启动Apache才能使这些更改生效,并使用systemctl status来验证重新启动是否成功。

  • sudo systemctl restart apache2

    sudo systemctl重新启动apache2
  • sudo systemctl status apache2

    sudo systemctl状态apache2

Your server should now be set up to serve two websites.

现在应该将服务器设置为可以服务两个网站。

第六步—设置本地主机文件(可选) (Step Six — Set Up Local Hosts File (Optional))

If you haven’t been using actual domain names that you own to test this procedure and have been using some example domains instead, you can at least test the functionality of this process by temporarily modifying the hosts file on your local computer.

如果您没有使用您拥有的实际域名来测试此过程,而是使用了某些示例域,则可以至少通过临时修改本地计算机上的hosts文件来测试此过程的功能。

This will intercept any requests for the domains that you configured and point them to your VPS server, just as the DNS system would do if you were using registered domains. This will only work from your local computer though, and only for testing purposes.

这将拦截对您配置的域的任何请求,并将它们指向您的VPS服务器,就像DNS系统在使用注册域时一样。 不过,这只能在您的本地计算机上工作,并且仅用于测试目的。

Make sure you are operating on your local computer for these steps and not your VPS server. You will need to know the computer’s administrative password or otherwise be a member of the administrative group.

请确保您正在本地计算机上执行这些步骤,而不是在VPS服务器上进行操作。 您将需要知道计算机的管理密码或以其他方式成为管理组的成员。

If you are on a Mac or Linux computer, edit your local file with administrative privileges by typing:

如果您使用的是Mac或Linux计算机,请输入以下内容以具有管理权限的方式编辑本地文件:

  • sudo nano /etc/hosts

    须藤nano / etc / hosts

If you are on a Windows machine, you can find instructions on altering your hosts file here.

如果您使用的是Windows计算机,则可以在此处找到有关更改主机文件的说明

The details that you need to add are the public IP address of your server followed by the domain you want to use to reach that server.

您需要添加的详细信息是服务器的公共IP地址,后跟要用于访问该服务器的域。

Using the domains used in this guide, and replacing your server IP for the your_server_IP text, your file should look like this:

使用本指南中使用的域,并将服务器IP替换为your_server_IP文本,文件应如下所示:

/etc/hosts
/ etc / hosts
127.0.0.1   localhost
127.0.1.1   guest-desktop
your_server_IP example.com
your_server_IP test.com

Save and close the file.

保存并关闭文件。

This will direct any requests for example.com and test.com on our computer and send them to our server. This is what we want if we are not actually the owners of these domains in order to test our virtual hosts.

这将在我们的计算机上定向对example.comtest.com任何请求,并将它们发送到我们的服务器。 如果我们实际上不是这些域的​​所有者,这就是我们想要的,以便测试我们的虚拟主机。

第七步-测试结果 (Step Seven — Test your Results)

Now that you have your virtual hosts configured, you can test your setup by going to the domains that you configured in your web browser:

现在您已经配置了虚拟主机,您可以通过在Web浏览器中配置的域来测试设置:

http://example.com

You should see a page that looks like this:

您应该看到一个如下页面:

You can also visit your second page and see the file you created for your second site.

您还可以访问第二页,并查看为第二个站点创建的文件。

http://test.com

If both of these sites work as expected, you’ve successfully configured two virtual hosts on the same server.

如果这两个站点均按预期工作,则说明您已在同一服务器上成功配置了两个虚拟主机。

If you adjusted your home computer’s hosts file, you may want to delete the lines you added now that you verified that your configuration works. This will prevent your hosts file from being filled with entries that are no longer necessary.

如果您调整了家用计算机的主机文件,则在确认配置可以工作之后,可能要删除添加的行。 这将防止您的主机文件被不再需要的条目填充。

If you need to access this long term, consider adding a domain name for each site you need and setting it up to point to your server.

如果您需要长期访问,请考虑为您需要的每个站点添加一个域名,并将其设置为指向您的服务器

结论 (Conclusion)

If you followed along, you should now have a single server handling two separate domain names. You can expand this process by following the steps we outlined above to make additional virtual hosts.

如果继续进行,现在应该有一台服务器处理两个单独的域名。 您可以按照上面概述的步骤制作其他虚拟主机,以扩展此过程。

There is no software limit on the number of domain names Apache can handle, so feel free to make as many as your server is capable of handling.

Apache可以处理的域名数量没有软件限制,因此请随意增加服务器可以处理的域名数量。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-18-04

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值