介绍 (Introduction)
This tutorial will guide you through setting up multiple domains and websites using 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虚拟主机设置多个域和网站。 在此过程中,您将学习如何根据他们请求的域向不同的访问者提供不同的内容。
For a more detailed version of this tutorial, with more explanations of each step, please refer to How To Set Up Apache Virtual Hosts on Ubuntu 18.04.
有关本教程的更详细版本,以及每个步骤的更多说明,请参考如何在Ubuntu 18.04上设置Apache虚拟主机 。
先决条件 (Prerequisites)
In order to complete this tutorial, you will need access to the following on an Ubuntu 18.04 server:
为了完成本教程,您将需要在Ubuntu 18.04服务器上访问以下内容:
- A sudo user on your server 服务器上的sudo用户
An Apache2 web server, which you can install with
sudo apt install apache2
一个Apache2 Web服务器,您可以使用
sudo apt install apache2
第1步-创建目录结构 (Step 1 — Create the Directory Structure)
We’ll first make a directory structure that will hold the site data that we will be serving to visitors in our top-level Apache directory. We’ll be using example domain names, highlighted below. You should replace these with your actual domain names.
首先,我们将建立一个目录结构,该目录结构将在顶级Apache目录中保存将提供给访问者的站点数据。 我们将使用示例域名,如下所示。 您应该用实际的域名替换它们。
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
第2步-授予权限 (Step 2 — Grant Permissions)
We should now change the permissions to our current non-root user to be able to modify the files.
现在,我们应该更改对当前非root用户的权限,以便能够修改文件。
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
Additionally, we’ll 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
步骤3 —为每个虚拟主机创建演示页面 (Step 3 — Create Demo Pages for Each Virtual Host)
Let’s create some content to serve, we’ll make a demonstration index.html
page for each site. We can open up an index.html
file in a text editor for our first site, using nano for example.
让我们创建一些要服务的内容,为每个站点创建一个演示index.html
页面。 我们可以在文本编辑器中为第一个站点打开index.html
文件,例如使用nano。
nano /var/www/example.com/public_html/index.html
纳米/ var / www / example.com /public_html/index.html
Within this file, create a domain-specific HTML document, like the following:
在此文件中,创建特定于域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, then copy this file to use as the basis for our second site:
保存并关闭文件,然后复制此文件用作我们第二个站点的基础:
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
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
<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.
保存并关闭此文件。
步骤4 —创建新的虚拟主机文件 (Step 4 — Create New Virtual Host Files)
Apache comes with a default virtual host file called 000-default.conf
that we’ll use as a template. We’ll copy it over to create a virtual host file for each of our domains.
Apache带有一个称为000-default.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 (we’re using nano below) with root privileges:
使用root权限在编辑器(我们在下面使用nano)中打开新文件:
sudo nano /etc/apache2/sites-available/example.com.conf
须藤纳米/ etc / apache2 / sites-available / example.com .conf
We will customize this file for our own domain. Modify the highlighted text below for your own circumstances.
我们将为我们自己的域自定义此文件。 您可以根据自己的情况修改以下突出显示的文本。
<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. The final file should look something like this, with highlighted text corresponding to your own relevant domain information.
现在,您需要修改所有信息,以引用您的第二个域。 最终文件应类似于以下内容,突出显示的文本对应于您自己的相关域信息。
<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.
完成后保存并关闭文件。
步骤5 —启用新的虚拟主机文件 (Step 5 — Enable the New Virtual Host Files)
With our virtual host files created, we must enable them. We’ll be using the a2ensite
tool to achieve this goal.
创建虚拟主机文件后,我们必须启用它们。 我们将使用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
Your server should now be set up to serve two websites.
现在,您的服务器应设置为可以服务两个网站。
步骤6 —设置本地主机文件(可选) (Step 6 — 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 test your work by temporarily modifying the hosts
file on your local computer.
如果您没有使用您拥有的实际域名来测试此过程,而是使用了一些示例域,则可以通过临时修改本地计算机上的hosts
文件来测试您的工作。
On a local Mac or Linux machine, type the following:
在本地Mac或Linux计算机上,键入以下内容:
- sudo nano /etc/hosts 须藤nano / etc / hosts
For a local Windows machine, find instructions on altering your hosts file here.
对于本地Windows计算机, 请在此处找到有关更改主机文件的说明 。
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
文本,文件应如下所示:
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.
保存并关闭文件。 这将在我们的计算机上定向对example.com
和test.com
任何请求,并将它们发送到我们的服务器。
第7步-测试结果 (Step 7 — 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 configured two virtual hosts on the same server.
如果这两个站点均按预期工作,则您已在同一服务器上配置了两个虚拟主机。
If you adjusted your home computer’s hosts file, delete the lines you added.
如果您调整了家用计算机的主机文件,请删除添加的行。
相关教程 (Related Tutorials)
Here are links to more additional guides related to this tutorial:
这里是与本教程相关的更多其他指南的链接:
How to Rewrite URLs with mod_rewrite for Apache on Ubuntu 18.04