Time: 15 minutes
Want to host websites on your server? Using Apache? Great. This article will show you how to do exactly that using Apache’s “virtual hosts.”
In Apache, you can use virtual hosts to direct
http traffic for a given domain name to a particular directory (i.e. the root directory of the website for the domain in the request). This feature is commonly used to host multiple websites, but we recommend using it for every website on your server including the first.
Throughout this article, we'll use an example domain - coolexample.com - but you should replace it with the domain name or subdomain you want to host on your server.
To get Apache on your server, you can either install it as part of a LAMP stack, or you can install Apache by itself:
If you have the content for the website prepped, you can upload it to the
/public_htmlfolder you created in the last section.
If you don't have content ready to upload, you can create a sample home page (also known as an index file, which is the first page that loads when visitors come to your domain).
<html> <head> <title>Welcome to my site!</title> </head> <body> <h1>Hooray! Your virtual host is working!</h1> </body> </html>
We're going to copy a configuration usually used in Ubuntu/Debian and create two directories: one to store the virtual host files (
sites-available) and another to hold symbolic links to virtual hosts that will be published (
Edit the main configuration file (
httpd.conf) so that Apache will look for virtual hosts in the
We're going to build it from a new file in your
Here's what the whole file could look like after your changes:
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org ServerName www.coolexample.com ServerAlias coolexample.com DocumentRoot /var/www/coolexample.com/public_html ErrorLog /var/www/coolexample.com/error.log CustomLog /var/www/coolexample.com/requests.log combined </VirtualHost>
CustomLog are not required to set up your virtual host, but we've included them, in case you do want to tell Apache where to keep error and request logs for your site.
If your domain name isn't currently loading another website, you should point it to your server to test your new config.
How you do this depends on where your domain name is registered and whose server you're using:
|Domain registered?||Server hosted?||Do this...|
|GoDaddy||GoDaddy||Point your domain name to a server|
|Another company||GoDaddy||Find a server's public IP address and then update your domain name's primary ("@") A record.|
|GoDaddy||Another company||Find your server's IP address, and then change your domain's IP address to use it.|
|Another company||Another company||Find your server's IP address, and then change your domain's IP address to use it.|
Changes to your domain can take up to 48 hours to display across the internet. However, once they do, you can visit your domain name and view the test page you created earlier!
To create additional sites, repeat the following sections:
/etc/httpd/sites-available/, for example: