/sc_assets/3664/logo.png

Select a topic:

Choose from one of the topics below to browse other articles

Ubuntu

Articles & Information.

Adding virtual hosts to Apache

If you're using the Apache web server on your Laser Shark VPS, you may want to serve multiple websites from the same server instance. You can do so using Apache's virtual hosts feature, which matches different server setups with different domain names.

You can create as many virtual hosts as you want on your server, as long as you have the resources available. Your server won't indicate that it serves other sites, too—each domain will point to the appropriate site on your server.

This guide assumes you have already installed Apache on your VPS, or have used our LAMP Preinstall image.

Directories

By default, Apache stores your document root (where your website files are) in /var/www/html. You can store your sites anywhere on your system, but to make it easy we'll stay in the /var/www directory. Let's say you're adding a new site called example.com to your VPS. Create a directory in /var/www for your virtual host, and create a public_html directory inside of that.

sudo mkdir -p /var/www/example.com/public_html

If you're not logged in as root (as recommended), you'll need to change ownership of those directories so that you can edit what's inside them.

sudo chown -R $USER:$USER /var/www/example.com/public_html

Also, change the permissions of /var/www to ensure that your web server can read everything inside it.

sudo chmod -R 755 /var/www

Now that you have your directories set up, you may want to create a simple index page just so you can test that it's working.

nano /var/www/example.com/public_html/index.html

You can enter any HTML here, but for simplicity's sake we went with:

<h1>It works!</h1>

Configuration

Now you have to tell Apache to use the new virtual host. As the root user (or with sudo), copy the default virtual host file and edit it.

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
sudo nano /etc/apache2/sites-available/example.com.conf

In this file you'll see the configuration for a virtual host. The default virtual host file is commented to help you understand what it does, but the basic (without comments) structure of the file looks like this:

<VirtualHost *:80>
     ServerAdmin webmaster@localhost
     DocumentRoot /var/www/html
     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

This file describes the default virtual host in Apache that serves its files out of /var/www/html. It doesn't have any domain name matching set up because the default virtual host is the 'if all else fails' option. However, in your new virtual host, you'll need to match the domain name and make a few other changes to your file. First, change ServerAdmin to an email address at your domain:

ServerAdmin webmaster@example.com

Now, add the following two lines to match with your domain:

ServerName example.com
ServerAlias www.example.com

ServerName describes the primary domain for that virtual host, and ServerAlias describes any other domains which should be matched with this virtual host.

Finally, you'll need to change the DocumentRoot to serve files from your virtual host directory.

DocumentRoot /var/www/example.com/public_html

Save and close the file using Ctrl-O and Ctrl-X. Now you'll need to enable the virtual host, which we'll do using Apache's built-in a2ensite tool.

sudo a2ensite example.com.conf

And, last but not least, reload Apache's configuration so your changes take effect.

sudo service apache2 reload

And that's it! You now have a virtual host for example.com configured on your VPS. You can create as many of these as you like using the same method.