You will also need to
have Apache installed in order to work through these steps. If you haven't
already done so, you can get Apache installed on your server through apt-get:
sudo apt-get update
sudo apt-get install apache2
After these steps are
complete, we can get started.
For the purposes of this
guide, my configuration will make a virtual host for test1.com and another for test2.com. These will be
referenced throughout the guide, but you should substitute your own domains or
values while following along.
Step 1: Create the
Directory Structure
For instance, for our sites, we're going to make our directories
like this:
sudo mkdir -p /var/www/test1.com/public_html
sudo mkdir -p /var/www/test2.com/public_html
The portions in red represent the domain names that we are
wanting to serve from our VPS.
Step 2: Grant
Permissions
Now we have the directory structure for our files, but they are
owned by our root user. If we want our regular user to be able to modify files
in our web directories, we can change the ownership by doing this:
sudo chown -R $USER:$USER /var/www/test1.com/public_html
sudo chown -R $USER:$USER /var/www/test2.com/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, our regular user now owns the public_html
subdirectories
where we will be storing our content.
We should also modify our permissions a little bit to 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:
sudo chmod -R 755 /var/www
Step 3: Create Demo
Pages for Each Virtual Host
nano /var/www/test1.com/public_html/index.html
<html>
<head>
<title>Welcome to Test1.com!</title>
</head>
<body>
<h1>Success! The test1.com virtual host is working!</h1>
</body>
</html>
cp /var/www/test1.com/public_html/index.html /var/www/test2.com/public_html/index.html
<html>
<head>
<title>Welcome to Test2.com!</title>
</head>
<body>
<h1>Success! The test2.com virtual host is working!</h1>
</body>
</html>
Step 4: Create New
Virtual Host Files
Virtual host files are the files that specify the actual
configuration of our virtual hosts and dictate how the Apache web server will
respond to various domain requests.
Apache comes with a
default virtual host file called 000-default.conf
that we can use as a jumping off point. We are going to copy it
over to create a virtual host file for each of our domains.
We will start with one
domain, configure it, copy it for our second domain, and then make the few
further adjustments needed. The default Ubuntu configuration requires that each
virtual host file end in .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/test1.com.conf
Open the new file in your editor with root privileges:
sudo nano /etc/apache2/sites-available/test1.com.conf
The file will look
something like this (I've removed the comments here to make the file more
approachable):
<VirtualHost *:80>
ServerAdmin admin@test1.com
ServerName test1.com
ServerAlias www.test1.com
DocumentRoot /var/www/test1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
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/test1.com.conf /etc/apache2/sites-available/test2.com.conf
Open the new file with
root privileges in your editor:
sudo nano /etc/apache2/sites-available/test2.com.conf
You now need to modify all
of the pieces of information to reference your second domain. When you are
finished, it may look something like this:
<VirtualHost *:80>
ServerAdmin admin@test2.com
ServerName test2.com
ServerAlias www.test2.com
DocumentRoot /var/www/test2.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.
Step 5: Enable the New Virtual Host Files
Now that we have created
our virtual host files, we must enable them. Apache includes some tools that
allow us to do this.
We can use the a2ensite
tool to enable each of our sites like this:
sudo a2ensite test1.com.conf
sudo a2ensite test2.com.conf
Step 6: Disable the Default
Virtual Host Files
sudo a2ensite 000-default.conf
When you are finished, you need to restart Apache to make these
changes take effect:
sudo service apache2 restart
Step 7: Set Up Local
Hosts File (Optional)
sudo nano /etc/hosts
edit hosts file
127.0.0.1 localhost
127.0.0.1 test1.com
127.0.0.1 test2.com
Save and close the file when you are finished.
Step 8: Test your Results
Now that you have your virtual hosts configured, you can test your setup easily by going to the domains that you configured in your web browser:
http://test1.com
You should see a page that looks like this:
Success! The test1.com virtual host is working!
Likewise, if you can visit your second page:
http://test2.com
You should see a page that looks like this:
Success! The test2.com virtual host is working!
You will see the file you created for your second site:
If both of these sites work well, you've successfully configured two virtual hosts on the same server.