跳转到主要内容

标签(标签)

资源精选(342) Go开发(108) Go语言(103) Go(99) angular(82) LLM(75) 大语言模型(63) 人工智能(53) 前端开发(50) LangChain(43) golang(43) 机器学习(39) Go工程师(38) Go程序员(38) Go开发者(36) React(33) Go基础(29) Python(24) Vue(22) Web开发(20) Web技术(19) 精选资源(19) 深度学习(19) Java(18) ChatGTP(17) Cookie(16) android(16) 前端框架(13) JavaScript(13) Next.js(12) 安卓(11) 聊天机器人(10) typescript(10) 资料精选(10) NLP(10) 第三方Cookie(9) Redwoodjs(9) LLMOps(9) Go语言中级开发(9) 自然语言处理(9) PostgreSQL(9) 区块链(9) mlops(9) 安全(9) 全栈开发(8) ChatGPT(8) OpenAI(8) Linux(8) AI(8) GraphQL(8) iOS(8) 软件架构(7) Go语言高级开发(7) AWS(7) C++(7) 数据科学(7) whisper(6) Prisma(6) 隐私保护(6) RAG(6) JSON(6) DevOps(6) 数据可视化(6) wasm(6) 计算机视觉(6) 算法(6) Rust(6) 微服务(6) 隐私沙盒(5) FedCM(5) 语音识别(5) Angular开发(5) 快速应用开发(5) 提示工程(5) Agent(5) LLaMA(5) 低代码开发(5) Go测试(5) gorm(5) REST API(5) 推荐系统(5) WebAssembly(5) GameDev(5) CMS(5) CSS(5) machine-learning(5) 机器人(5) 游戏开发(5) Blockchain(5) Web安全(5) Kotlin(5) 低代码平台(5) 机器学习资源(5) Go资源(5) Nodejs(5) PHP(5) Swift(5) 智能体(4) devin(4) Blitz(4) javascript框架(4) Redwood(4) GDPR(4) 生成式人工智能(4) Angular16(4) Alpaca(4) 编程语言(4) SAML(4) JWT(4) JSON处理(4) Go并发(4) kafka(4) 移动开发(4) 移动应用(4) security(4) 隐私(4) spring-boot(4) 物联网(4) nextjs(4) 网络安全(4) API(4) Ruby(4) 信息安全(4) flutter(4) 专家智能体(3) Chrome(3) CHIPS(3) 3PC(3) SSE(3) 人工智能软件工程师(3) LLM Agent(3) Remix(3) Ubuntu(3) GPT4All(3) 软件开发(3) 问答系统(3) 开发工具(3) 最佳实践(3) RxJS(3) SSR(3) Node.js(3) Dolly(3) 移动应用开发(3) 低代码(3) IAM(3) Web框架(3) CORS(3) 基准测试(3) Go语言数据库开发(3) Oauth2(3) 并发(3) 主题(3) Theme(3) earth(3) nginx(3) 软件工程(3) azure(3) keycloak(3) 生产力工具(3) gpt3(3) 工作流(3) C(3) jupyter(3) 认证(3) prometheus(3) GAN(3) Spring(3) 逆向工程(3) 应用安全(3) Docker(3) Django(3) R(3) .NET(3) 大数据(3) Hacking(3) 渗透测试(3) C++资源(3) Mac(3) 微信小程序(3) Python资源(3) JHipster(3) 大型语言模型(2) 语言模型(2) 可穿戴设备(2) JDK(2) SQL(2) Apache(2) Hashicorp Vault(2) Spring Cloud Vault(2) Go语言Web开发(2) Go测试工程师(2) WebSocket(2) 容器化(2) AES(2) 加密(2) 输入验证(2) ORM(2) Fiber(2) Postgres(2) Gorilla Mux(2) Go数据库开发(2) 模块(2) 泛型(2) 指针(2) HTTP(2) PostgreSQL开发(2) Vault(2) K8s(2) Spring boot(2) R语言(2) 深度学习资源(2) 半监督学习(2) semi-supervised-learning(2) architecture(2) 普罗米修斯(2) 嵌入模型(2) productivity(2) 编码(2) Qt(2) 前端(2) Rust语言(2) NeRF(2) 神经辐射场(2) 元宇宙(2) CPP(2) 数据分析(2) spark(2) 流处理(2) Ionic(2) 人体姿势估计(2) human-pose-estimation(2) 视频处理(2) deep-learning(2) kotlin语言(2) kotlin开发(2) burp(2) Chatbot(2) npm(2) quantum(2) OCR(2) 游戏(2) game(2) 内容管理系统(2) MySQL(2) python-books(2) pentest(2) opengl(2) IDE(2) 漏洞赏金(2) Web(2) 知识图谱(2) PyTorch(2) 数据库(2) reverse-engineering(2) 数据工程(2) swift开发(2) rest(2) robotics(2) ios-animation(2) 知识蒸馏(2) 安卓开发(2) nestjs(2) solidity(2) 爬虫(2) 面试(2) 容器(2) C++精选(2) 人工智能资源(2) Machine Learning(2) 备忘单(2) 编程书籍(2) angular资源(2) 速查表(2) cheatsheets(2) SecOps(2) mlops资源(2) R资源(2) DDD(2) 架构设计模式(2) 量化(2) Hacking资源(2) 强化学习(2) flask(2) 设计(2) 性能(2) Sysadmin(2) 系统管理员(2) Java资源(2) 机器学习精选(2) android资源(2) android-UI(2) Mac资源(2) iOS资源(2) Vue资源(2) flutter资源(2) JavaScript精选(2) JavaScript资源(2) Rust开发(2) deeplearning(2) RAD(2)

category

How To Set Up Apache Virtual Hosts on Ubuntu 20.04

Introduction

Apache HTTP服务器是一款流行的开源web服务器,它为开发人员提供了灵活性、强大功能和广泛支持。Apache服务器配置不是在一个单一的文件中进行的,而是通过模块化设计进行的,在模块化设计中,可以根据需要添加和修改新文件。在这个模块化设计中,您可以创建一个单独的站点或域,称为虚拟主机。

使用虚拟主机,一个Apache实例可以为多个网站提供服务。使用Apache配置的每个域或单个站点都会将访问者引导到保存该站点信息的特定目录。这是在没有向访问者指示同一服务器也负责其他站点的情况下完成的。这个方案是可扩展的,没有任何软件限制,只要你的服务器可以处理负载。

在本指南中,您将在Ubuntu 20.04服务器上设置Apache虚拟主机。在此过程中,您将学习如何通过创建两个虚拟主机站点,根据不同访问者请求的域,为他们提供不同的内容。

Prerequisites

Before you begin this tutorial, you will need:

If you are using DigitalOcean, you can learn how to set up domains by following our product documentation, How to Add Domains.

In order to successfully complete this tutorial, you will need two domains with:

  • An A record with your_domain pointing to your server’s public IP address.
  • An A record with www.your_domain pointing to your server’s public IP address.

For other providers, please refer to their relevant product documentation.

Note: If you do not have domains available at this time, you can use test values locally on your computer. Step 6 of this tutorial will show you how to test and configure your test values. This will allow you to validate your configuration even though your content won’t be available to other visitors through the domain name.

Step 1 — Creating the Directory Structure

The first step is to create a directory structure that will hold the site data that you will be serving to visitors.

Your 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. You will create a directory here for each of the virtual hosts.

Within each of these directories, you will create a public_html directory. The public_html directory contains the content that will be served to your visitors. The parent directories, named here as your_domain_1 and your_domain_2, will hold the scripts and application code to support the web content.

Use these commands, with your own domain names, to create your directories:  

  1. sudo mkdir -p /var/www/your_domain_1/public_html
  2. sudo mkdir -p /var/www/your_domain_2/public_html

Be sure to replace your_domain_1 and your_domain_2 with your own respective domains. For example, if one of your domains was example.com you would create this directory structure: /var/www/example.com/public_html.

Step 2 — Granting Permissions

You’ve created the directory structure for your files, but they are owned by the root user. If you want your regular user to be able to modify files in these web directories, you can change the ownership with these commands:  

  1. sudo chown -R $USER:$USER /var/www/your_domain_1/public_html
  2. sudo chown -R $USER:$USER /var/www/your_domain_2/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, the regular user now owns the public_html subdirectories where you will be storing your content.

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

  1. sudo chmod -R 755 /var/www

Your web server now has the permissions it needs to serve content, and your user should be able to create content within the necessary folders. The next step is to create content for your virtual host sites.

Step 3 — Creating Default Pages for Each Virtual Host

With your directory structure in place, you can start focusing on each individual virtual host site and the content within that site. Start by creating an index.html page for your first site your_domain_1.

Open and create the index.html file with your preferred text editor. This example uses nano:  

  1. nano /var/www/your_domain_1/public_html/index.html

Within this file, create an HTML file that indicates to visitors which site they are connected to:

/var/www/your_domain_1/public_html/index.html

<html>
  <head>
    <title>Welcome to your_domain_1!</title>
  </head>
  <body>
    <h1>Success! The your_domain_1 virtual host is working!</h1>
  </body>
</html>

To save and close the file in nano, start by pressing CTRL+X. Press Y when prompted to save the file, then press ENTER when you are finished to exit.

Next, copy this file to use as the base for your second site by typing:  

  1. cp /var/www/your_domain_1/public_html/index.html /var/www/your_domain_2/public_html/index.html

Then open this new file and modify the relevant pieces of information using your text editor like before:

  1. nano /var/www/your_domain_2/public_html/index.html

/var/www/your_domain_2/public_html/index.html

<html>
  <head>
    <title>Welcome to your_domain_2!</title>
  </head>
  <body> <h1>Success! The your_domain_2 virtual host is working!</h1>
  </body>
</html>

Save and close this file. You now have one page for each site that you can use to test the virtual host configuration.

Step 4 — Creating New Virtual Host Files

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

Apache comes with a default virtual host file called 000-default.conf. You can copy this file to create virtual host files for each of your domains.

Copy the default configuration file over to the first domain:

  1. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your_domain_1.conf

Be aware that the default Ubuntu configuration requires that each virtual host file end in .conf.

Open the new file in your preferred text editor with root privileges:  

  1. sudo nano /etc/apache2/sites-available/your_domain_1.conf

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

/etc/apache2/sites-available/your_domain_1.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, customize the items for your first domain and add some additional directives. This virtual host section matches any requests that are made on port 80, the default HTTP port.

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

/etc/apache2/sites-available/your_domain_1.conf

ServerAdmin admin@your_domain_1

After this, add two additional directives. The first, called ServerName, establishes the base domain for the virtual host definition. The second, called ServerAlias, defines further names that should match as if they were the base name. This is useful for matching additional hosts you defined. For instance, if you set the ServerName directive to example.com you could define a ServerAlias to www.example.com, and both will point to this server’s IP address.

Add these two directives to your configuration file after the ServerAdmin line:

/etc/apache2/sites-available/your_domain_1.conf

<VirtualHost *:80>
  ...
    ServerAdmin admin@your_domain_1
    ServerName your_domain_1
    ServerAlias www.your_domain_1
    DocumentRoot /var/www/html
    ...
</VirtualHost>

Next, change your virtual host file location for the document root for this domain. Edit the DocumentRoot directive to point to the directory you created for this host:

/etc/apache2/sites-available/your_domain_1.conf

DocumentRoot /var/www/your_domain_1/public_html

Here is an example of the virtual host file with all of the adjustments made above:

/etc/apache2/sites-available/your_domain_1.conf

<VirtualHost *:80>
  ...
    ServerAdmin admin@your_domain_1
    ServerName your_domain_1
    ServerAlias www.your_domain_1
    DocumentRoot /var/www/your_domain_1/public_html
    ...
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ...
</VirtualHost>

Save and close the file.

Create your second configuration file by copying over the file from your first virtual host site: 

  1. sudo cp /etc/apache2/sites-available/your_domain_1.conf /etc/apache2/sites-available/your_domain_2.conf

Open the new file in your preferred editor:  

  1. sudo nano /etc/apache2/sites-available/your_domain_2.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/your_domain_2.conf

<VirtualHost *:80>
  ...
    ServerAdmin admin@your_domain_2
    ServerName your_domain_2
    ServerAlias www.your_domain_2
    DocumentRoot /var/www/your_domain_2/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 5 — Enabling the New Virtual Host Files

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

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

Use the following commands to enable your virtual host sites:

  1. sudo a2ensite your_domain_1.conf
  2. sudo a2ensite your_domain_2.conf

There will be output for both sites, similar to the example below, reminding you to reload your Apache server:

Output

Enabling site example.com. To activate the new configuration, you need to run: systemctl reload apache2

Before reloading your server, disable the default site defined in 000-default.conf by using the a2dissite command:

  1. sudo a2dissite 000-default.conf

Output

Site 000-default disabled. To activate the new configuration, you need to run: systemctl reload apache2

Next, test for configuration errors:  

  1. sudo apache2ctl configtest

The should receive the following output:  

Output

. . . Syntax OK

When you are finished, restart Apache to make these changes take effect.  

  1. sudo systemctl restart apache2

Optionally, you can check the status of the server after all these changes with this command:  

  1. sudo systemctl status apache2

Your server should now be set up to serve two websites. If you’re using real domain names, you can skip Step 6 and move on to Step 7. If you’re testing your configuration locally, follow Step 6 to learn how to test your setup using your local computer.

Step 6 — (Optional) Setting Up Local Hosts File

If you haven’t been using actual domain names that you own to test this procedure, and have been using example domains instead, you can still test the functionality of your virtual host sites by temporarily modifying the hosts file on your local computer. This will intercept any requests for the domains that you configured and point them to your Virtual Private Server (VPS), just as the DNS system would do if you were using registered domains. This will only work from your local computer and is only for testing purposes.

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.

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

  1. sudo nano /etc/hosts

If you are on a Windows machine, open the Command Prompt and type:

  1. notepad %windir%\system32\drivers\etc\hosts

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. Using the domains used in this guide, and replacing your server IP for the your_server_IP text, your file should look like this:

/etc/hosts

127.0.0.1   localhost
127.0.1.1   guest-desktop
your_server_IP your_domain_1
your_server_IP your_domain_2

This will direct any requests for your two domains on your computer and send them to your server at the designated IP address.

Save and close the file.

Step 7 — Testing 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:

http://your_domain_1

An example of the virtual host web page with edited content

You can also visit your second host page and view the file you created for your second site:

http://your_domain_2

An example of a different virtual host web page with edited content

如果这两个站点都按预期工作,那么您已经在同一台服务器上成功配置了两个虚拟主机。

注意:如果您调整了本地计算机的主机文件,如本教程的步骤6中所述,您可能需要删除您添加的行,因为您已经验证了您的配置是否有效。这将防止您的主机文件中填充不再需要的条目。

结论

现在,您有一个单独的服务器来处理两个独立的域名。您可以按照上面概述的步骤来扩展此过程,以添加其他虚拟主机。Apache可以处理的域名数量没有软件限制,所以可以随意制作服务器能够处理的虚拟主机。

文章链接