When you setup Apache by default you can only have one web page per Web Server. If you only have one IP or one Server then what do you do if you want to have two or more domains? Well Apache does let you setup Virtual Domains. This will let you have as many domains as you want.
Before we start, if you do not already have a LAMP installed I would recommend do that first so you are ready. You need to have Apache installed, and depending on what you are doing with your website’s you will most likely need mysql or php also. So if you do not have a LAMP installed to go to my Lamp Setup Page.
Step One – Setup Directory Structure
By default Apache only has one document directory “/var/www/html” we need to setup folders that we will put the content of each domain in so they are separate. So lets make those folders:
# sudo mkdir -p /var/www/html/domain1.com/public_html
# sudo mkdir -p /var/www/html/domain2.com/public_html
In place of “domain1.com or domain2.com” put your domain name for each domain or some abbreviation that lets you know this folder if for this domain. We will be linking to these locations later so make sure to make it easy on your self. We added the “public_html” to hold the actual files for each domain, this gives us some flexibility in our hosting.
Step Two – Folder Permissions
We now want to change the permissions to the folders and who owns them. Right now they are owned by root and if you want to make changes without logging in as root you need to change who has access to those folders and files. To make these changes we will use chown:
# sudo chown -R $USER:$USER /var/www/html/domain1.com/public_html
# sudo chown -R $USER:$USER /var/www/html/domain2.com/public_html
The $USER is a variable that will take the value of the user you are logged in as and make it the owner of those folders.
Now we want to modify our permissions a little bit more to ensure we have read access permitted to the general web directory, and all the files and folders inside it so the pages can be served correctly. The easiest way to do this we just change the Primary Directory that all folders are under by using the following command.
# sudo chmod -R 755 /var/www/html
Now your web server should have permissions it needs to serve content, and you should be able to create content within the appropriate folders.
Step Three – Create Test pages for Each Virtual Host Domain
For testing we want to create a test page for each domain in the appropriate folders so when we make the config changes to Apache we can test it to make sure the Virtual Host setup is working.
So first we will create two “index.html” files one in each domain’s “public_html” folder. Remember where I have “domain1 or domain2” use your folder names for your domains. To creat the index.html files use the following commands:
In the file we will make a simple HTML document that will make it easy to tell what domain you are looking at.
Now we will do the same thing for the Second domain.
Step Four – Creating the Virtual Host Files
The Virtual Host Files are what tells Apache what the domains are and where the files for that domain are located at.
To start we need to setup a directory structure for our Virtual Hosts files to be stored in. The way we will set it up is we will have a “sites-available” this will give us a place to create the config file for each domain and have a kind of staging area for when we create a new domain before making the domain live. Then we will create a folder called “sites-enabled”. Here we will create Symbolic Links to the “sites-available” folder. This will be were the sites at are live will have link to each config file to tell Apache where and what Site’s it has available to display to the internet.
First we will create the two folders by doing these commands.
sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled
Now that we have the folders we need to tell Apache to look in the “sites-enabled” folder for the config files. To do this we need to change the Apache config file:
sudo vim /etc/httpd/conf/httpd.conf
we will add the following line to the bottom of the file.
Save that file once you have the line added. Then we will create the Virtual Host Config File’s for each domain.
Creating the First Virtual Host File
Start by creating the first file with root privileges:
Note: because we specified in the line we added to the httpd.conf file all “Virtual Host Files” have to end in “.conf”
sudo vim /etc/httpd/sites-available/domain1.com.conf
In the conf file we need to specify the domain and location of the files for that domain. Your config file’s will look something like below.
Then you can do the same thing for your second domain.
sudo vim /etc/httpd/sites-available/domain2.com.conf
Step Five – Enable the Virtual Host Files
Now that we have our Virtual Host Files, we need to enable them so Apache knows to serve those domains out to people visiting your site’s. To do this we create symbolic links to each config file we created in the “sites-available” directory.
sudo ln -s /etc/httpd/sites-available/domain1.com.conf /etc/httpd/sites-enabled/domain1.com.conf
sudo ln -s /etc/httpd/sites-available/domain2.com.conf /etc/httpd/sites-enabled/domain2.com.conf
Now we have everything set for the Virtual Domains. For these changes to take effect we need to restart Apache.
sudo apachectl restart
Step Six – Test your Results
Now its time to test things, if you have DNS pointed to your IP or have set your Hosts file to point the domain names to your server’s IP, you should be able to go to your domain and get your test page that we setup for each domain. If you do not verify your config files are correct in your /etc/httpd/sites-available. Please five me feedback if this helped out or if you find something didn’t work correctly.