如何在Ubuntu 18.04上设置Apache虚拟主机[快速入门]

news/2024/7/8 8:28:11

介绍 (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文档,如下所示:

/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, 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

/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.

我们将为我们自己的域自定义此文件。 您可以根据自己的情况修改以下突出显示的文本。

/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. The final file should look something like this, with highlighted text corresponding to your own relevant domain information.

现在,您需要修改所有信息,以引用您的第二个域。 最终文件应类似于以下内容,突出显示的文本对应于您自己的相关域信息。

/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.

完成后保存并关闭文件。

步骤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文本,文件应如下所示:

/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.

保存并关闭文件。 这将在我们的计算机上定向对example.comtest.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:

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

Apache virtual host example

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

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

http://test.com

Apache virtual host test

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.

如果您调整了家用计算机的主机文件,请删除添加的行。

Here are links to more additional guides related to this tutorial:

这里是与本教程相关的更多其他指南的链接:

  • How To Set Up Apache Virtual Hosts on Ubuntu 18.04

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

  • Domains and DNS on DigitalOcean

    DigitalOcean上的域和DNS

  • How to Rewrite URLs with mod_rewrite for Apache on Ubuntu 18.04

    如何在Ubuntu 18.04上使用mod_rewrite为Apache重写URL

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


http://www.niftyadmin.cn/n/3650435.html

相关文章

看Borland IDE向何处去

《乌鸦FANS》再次灵验&#xff0c;今天得到消息称《Borland欲出售IDE部门》&#xff0c;NASDAQ上的BORL股价应声下跌&#xff08;下图为5日走势图&#xff0c;来自NASDAQ&#xff09;&#xff0c;但我觉得这对Borland用户来说&#xff0c;应该是个利好。李维就此写了一篇《“Bo…

Android下的应用编程——用HTTP协议实现文件上传功能

【文章作者】曾健生 【作者邮箱】zengjiansheng1126.com 【作者QQ】190678908 【作者MSN】zengjiansheng1hotmail.com 【作者博客】blog.csdn.net/newjueqi ******************************************************************************* 在Android的客户端编程中&…

如何将Redis数据迁移到DigitalOcean托管数据库

介绍 (Introduction) There are a number of methods you can use to migrate data from one Redis instance to another, such as replication or snapshotting. However, migrations can get more complicated when you’re moving data to a Redis instance managed by a cl…

两种不同的Web应用

对于今天火炬说Donews Blog将换用WordPress的事后&#xff0c;令狐提出了一个技术方面的问题&#xff0c;我们就此讨论了一番&#xff1a;令狐:我倒是不关心WP好不好&#xff0c;而是觉得一些人&#xff08;也许包括我&#xff09;对于“Web应用”这一概念是不是应该反思一下了…

jest测试react组件_如何使用Jest为React组件编写快照测试

jest测试react组件In this tutorial, we will be looking at what snapshot tests are and how we can use snapshot testing to ensure our user interface does not change without the team knowing about it. 在本教程中&#xff0c;我们将研究什么是快照测试以及如何使用快…

搭建支持 OAuth Echo 的第三方 twitter 应用

背景&#xff1a;进入8月中&#xff0c;twitter 已经在每天减少 Basic Auth 的 API limit了&#xff0c;到月底 Basic Auth 将彻底关闭&#xff0c;也就是说到月底&#xff0c;所有的客户端&#xff0c;twitter与第三方服务之间都必须使用 OAuth 来传递帐号密码等信息。 我喜欢…

SVN+HTTP的一个ulgy的错误

原来配置得好好的基于HTTP的SVN忽然就出问题了&#xff0c;新增加的文件都加不上&#xff0c;一提交就出一个什么&#xff1a;302 Found的错误。在Google上找了半天才找到&#xff0c;竟然是这么个ugly的错误。The solution is to disable special 404 error handling for Subv…

Android 模拟HTTP 协议进行表单提交

在Android的客户端编程中&#xff08;特别是SNS 类型的客户端&#xff09;&#xff0c;经常需要实现注册功能Activity&#xff0c;要用户输入用户名&#xff0c;密码&#xff0c;邮箱&#xff0c;照片后注册。但这时就有一个问题&#xff0c;在HTML中用form表单就能实现如上的注…