In this tutorial we’ll show you how to run multiple websites one server using apache virtual hosts. Throughout this tutorial we’ll be using two example domains (which we do not own) for illustration purposes. If you happen to already own your domains, you can substitute them in appropriately.
We’ll assume that you’ve set up a non-root user. Check out our “Getting Started with Your Stack” tutorial if you haven’t already.
What is a virtual host?
Apache separates its web serving functionality into modules called virtual hosts. You can host a single website using one virtual host. This allows the owner of a server to host multiple websites off one single interface. You can keep adding virtual hosts as long as your server can handle the load.
Installing Apache
This tutorial is written with Ubuntu 14.04 in mind — however you should be able to replace “apt-get” with the appropriate package manager for your linux distribution and still successfully install apache. If not, check out your package manager’s documentation on how to see the necessary syntax or how to add packages to your repositories.
sudo apt-get update sudo apt-get install apache2
Creating the Virtual Host Directories
By default, apache creates a folder in ‘/var/www’ called ‘html’. This is set as the default DocumentRoot for your application — where apache looks to serve your files. Since we wish to run two sites off one interface, we will remove the generic ‘html’ folder and create a separate folders we can use as the DocumentRoot for both sites. Run the following commands on your Stack.
sudo rm -rf /var/www/html sudo mkdir -p /var/www/stack.com/ sudo mkdir -p /var/www/harbor.com/
We need to assign ownership and group privileges of the directories to the apache user, which is called ‘www-data’. We’ll use the chown command to do so.
sudo chown -R www-data:www-data /var/www/stack.com/ sudo chown -R www-data:www-data /var/www/harbor.com/
Then we need to assign privileges so the appropriate users and read/write/execute the files in the folder
sudo chmod -R 755 /var/www
That’s all we have to do regarding our DocumentRoots for now. Later we’ll come back to the folders and create some content with which we can test out our Virtual Host configuration.
We now need to actually create the virtual host files. Currently, apache has enabled the ‘000-default’ virtual host which comes default with the installation. All we need to do is copy the file, rename it appropriately, and set the respective paths to our document roots for our two sites. Run the following commands:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/stack.conf sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/harbor.conf
Now open each of these in your favourite text editor and find the block that looks as follows:
ServerAdmin webmaster@localhost DocumentRoot /var/www/html
We’ll need to change both these configuration settings as well as add in two of our own. Modify your Virtual Host files to look like the following:
ServerAdmin [email protected] ServerName domain.com ServerAlias www.domain.com DocumentRoot /var/www/domain.com/
You should replace ‘domain’ with the appropriate domain name and DocumentRoot that you want to configure for the virtual host file you are editing. We’re done configuring the virtual host files but we still need to enable the virtual hosts and restart the apache server. Run the following commands replacing the ‘stack’ and ‘harbor’ with your domain names:
sudo a2dissite 000-default sudo a2ensite stack.com sudo a2ensite harbor.com sudo service apache2 restart
Testing the Configuration
Now that we’ve finished configuring our virtual hosts files, we can create a simple index.html page to verify that we’ve set everything up correctly. Run the following commands:
echo "<h1>stack.com!</h1>" > /var/www/stack.com/index.html echo "<h1>harbor.com!</h1>" > /var/www/harbor.com/index.html
These commands will create index pages in each of the virtual hosts’ DocumentRoot that have a single ‘h1′ tag with the appropriate domain name in the content.
If throughout the tutorial you had been using your own domain names in lieu of stack.com and harbor.com and you’ve pointed your domain name to the IP address of your Stack, you should be all set to visit the domain names you configured. However, if you haven’t yet registered a domain name, you can test whether you’ve correctly configured the virtual hosts by editing your local hosts file.
View your host file
If your local machine is a linux or mac, you can edit the local hosts file by running
sudo nano /etc/hosts
add the following lines to your hosts file
YOUR_STACK_IP_ADDRESS stack.com YOUR_STACK_IP_ADDRESS harbor.com
And save the file by pressing CTRL-X, followed by Y, and finally RETURN. These changes will map requests to stack.com and harbor.com to the IP address of your server. Since you’ve configured apache to route the requests to either of these domain names to different DocumentRoots, you’ll be taken to the index.html page of the appropriate virtual host.