Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top