使用 Apache 的 VPS 上的多个域

发布于 2024-09-14 05:39:04 字数 395 浏览 13 评论 0原文

我计划很快获得一个 VPS,并且有两个我想托管的网站。我使用主机文件为我的开发环境设置了本地虚拟主机以正确移植它。

我的问题希望很简单:当设置两个指向一台 VPS 服务器的独立域时,Apache 是否足够智能(就像在本地环境中一样),可以像本地一样自动过滤对 domain.com 的任何请求到正确的 VHOST?我只是想确定一下:)

谢谢!

我要问的示例:

假设 Domain1.com 和 Domain2.com 都在我的 VPS 上。

当有人请求 www.Domain1.com 时,apache 会看到这一点并将其传递到 domain1.com 虚拟主机文件。 当有人请求 www.Domain2.com 时,apache 会看到这一点并将其传递到 domain2.com 虚拟主机文件。

I am planning on getting a VPS soon and have two sites I want to host. I have my local vhosts setup for my development environment using host file to port it correctly.

My question is hopefully simple: When setting up with two separate domains that point to the one VPS server is Apache smart enough, like on the local environment, to automatically filter any requests for domain.com to the correct VHOST like it does locally? I would just like to be sure :)

Thanks!

Example of what I am asking:

Say Domain1.com and Domain2.com are both on my VPS.

When someone requests www.Domain1.com apache sees this and passes it through to the domain1.com vhost file.
When someone requests www.Domain2.com apache sees this and passes it through to the domain2.com vhost file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷默言语 2024-09-21 05:39:04

重新粘贴我的答案 仅使用一个 VPS 托管两个域? 在这里它更加相关。

作为一个完全的初学者,我一直在尝试在一个 Apache VPS 上托管多个域。教程包含太多信息,让我感到困惑。

下面我将向完全的初学者介绍如何使用 Ubuntu 和 Apache 在一台 VPS 服务器上托管多个域。

重要!您需要使用 root 帐户来执行大多数操作。

重要!如果您之前尝试对 apache 配置进行一些更改,请撤消它们。

创建虚拟主机

在服务器上为您的域创建文件夹。
例如:

/home/apache/domain1

/home/apache/domain2

将index.html 文件与任意文本放在每个文件夹中。

This is domain1
This is domain2

转到 /etc/apache2/sites-available 文件夹。

创建文件domain1.conf

sudo nano domain1.conf

<VirtualHost *:80>
DocumentRoot /home/apache/domain1
ServerName domain1.com
ServerAlias www.domain1.com
</VirtualHost>

创建文件domain2.conf

sudo nano domain2.conf

<VirtualHost *:80>
DocumentRoot /home/apache/domain2
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>

您可以以相同的方式创建子域。

sudo nano blog.conf

<VirtualHost *:80>
DocumentRoot /home/apache/blog
ServerName blog.domain.com
ServerAlias www.blog.domain.com
</VirtualHost>

启用已创建的站点

sudo a2ensite domain1.conf
sudo a2ensite domain2.conf

重新启动 apache

sudo service apache2 reload

将域重定向到服务器

仅当您将域名重定向到服务器 IP 时,创建的 VirtualHosts 才会起作用。域名只是可以转换为 IP 号码的名称。

本地计算机

要在本地计算机上测试您的配置,您需要编辑主机文件。

sudo nano /etc/hosts

它应该看起来像这样。

127.0.0.1       localhost domain1.com domain2.com

Hosts 文件告诉您的计算机域需要重定向到本地计算机。

重要!例如,如果您在现有域的主机文件中创建条目,

127.0.0.1       stackoverflow.com

您将失去对该网站的访问权限。

服务器

为了将域重定向到您的 Web 服务器,您需要为给定域创建或修改“A”型 DNS 记录到您服务器的 IP 地址。您可以通过域名注册商提供的面板控制来完成此操作。

如果您不知道服务器的 IP 地址,请登录该服务器并输入命令行:

ifconfig

Repasting my answer from Hosting two domains using only one VPS? as here it is even more relevant.

As complete beginner, I have been trying to host multiple domains on one Apache VPS. Tutorials had too much information that lead me to confusion.

Below I describe, for complete beginners, how to host multiple domains on one VPS server with Ubuntu and Apache.

IMPORTANT! You need to use root account to execute most operations.

IMPORTANT! If you have been trying to make some changes to apache configuration before, undo them.

Creating VirtualHosts

Create folders for your domains on server.
For example:

/home/apache/domain1

/home/apache/domain2

Put index.html file in each folder with any text.

This is domain1
This is domain2

Go to /etc/apache2/sites-available folder.

Create file domain1.conf

sudo nano domain1.conf

<VirtualHost *:80>
DocumentRoot /home/apache/domain1
ServerName domain1.com
ServerAlias www.domain1.com
</VirtualHost>

Create file domain2.conf

sudo nano domain2.conf

<VirtualHost *:80>
DocumentRoot /home/apache/domain2
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>

You can create subdomains same way.

sudo nano blog.conf

<VirtualHost *:80>
DocumentRoot /home/apache/blog
ServerName blog.domain.com
ServerAlias www.blog.domain.com
</VirtualHost>

Enable created sites

sudo a2ensite domain1.conf
sudo a2ensite domain2.conf

Restart apache

sudo service apache2 reload

Redirecting domain to server

Created VirtualHosts will work only if you redirect your domain name to server IP. Domains are just names that can be translated to IP numbers.

Local computer

To test your configuration on local machine, you need to edit hosts file.

sudo nano /etc/hosts

It should look like this.

127.0.0.1       localhost domain1.com domain2.com

Hosts file tells your computer that domain needs to be redirected to local machine.

IMPORTANT! If you create entry in hosts file for existing domain, for example

127.0.0.1       stackoverflow.com

you will loose access to this website.

Server

In order to redirect domain to you web server, you need to create or modify "A"-type DNS record for given domain to IP address of your server. You can do it by panel control provided by your domain registrar.

If you do not know IP address of your server, log in to that server and type in command line:

ifconfig
享受孤独 2024-09-21 05:39:04

简单的答案是“是”,Apache 就是这么聪明。如果您习惯于将本地虚拟主机文件与主机文件结合使用来模拟本地域,则可以在 VPS 上应用完全相同的技术。您对主机文件所做的部分本质上是创建一个本地名称服务器,除此之外 Apache 不知道其中的区别。只需为每个命名虚拟主机设置 ServerName 指令,您就会发现它的工作方式与本地相同。

The simple answer is 'yes', Apache is that clever. If you are used to using a local vhost file in conjunction with your hosts file to simulate local domains, the exact same technique can be applied on a VPS. The part you are doing with the hosts file is essentially creating a local name server, other than that Apache doesn't know the difference. Simply set the ServerName directive for each named vhost and you should find it working the same as it does locally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文