Contents
How To Set Up Apache Virtual Hosts Linux
1- What is virtual host?
If you’re using Apache for your development server, knowing how to configure Apache will be important.
Because you might run multiple sites on one Apache server, you need to tell Apache which directory contains the web files (the “web root” or “document root”) per website.
* Virtual hosts are the bread and butter of Apache. They allow you to run multiple websites off of one web server as well as customize settings for each site.
2- Setup Apache Virtual Host Configuration:
a. Create a New Directory
The first step in creating a virtual host is to a create a directory where we will keep the new website’s pages and files.
This location will be your Document Root in the Apache virtual configuration file later on.
Here we will add a new virtual host named example.com:
1 |
[user@localhost $] sudo mkdir -p /var/www/html/example.com/public_html |
b. Grant Permissions:
We need to grant ownership of the directory to the interested user:
1 |
[user@localhost $] sudo chown -R $USER:$USER /var/www/example.com/public_html |
Additionally, it is important to make sure that everyone will be able to read our new files.
1 |
[user@localhost $] sudo chmod -R 755 /var/www |
Now you are all done with permissions.
c. Create the Page:
Within our configurations directory, we will now to create a new file called index.html
1 |
[user@localhost $] sudo vim /var/www/example.com/public_html/index.html |
We can add some text to the file so we will have something to look at when the IP redirects to the virtual host.
<html>
<head>
<title>www.example.com</title>
</head>
<body>
<h1>Success: You Have Set Up a Virtual Host</h1>
</body>
</html><
Save and Exit (w+q+!)
d. Virtual Host Config Files:
Your best bet for a starting place is to copy Apache’s default /etc/apache/sites-available/default
:
1 |
[user@localhost $] sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example.com |
Open up the new config file and add a following script:
1 |
[user@doctorlinux $] sudo nano /etc/apache2/sites-available/example.com |
#The ServerName specifies the domain name that the virtual host uses.
ServerName example.com
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /home/user/example.com/public_htmlErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
Create links symbolic config files containing server of example.com in the sites-enabled to sites-available directories
The last step is to activate the host:
1 |
[user@localhost $] sudo ln -s /etc/apache2/sites-available/example.com/sites-enabled/ |
And:
1 |
[user@localhost $] sudo a2ensite example.com |
e. Restart Apache:
Finally Use this command to restart apache:
1 |
[user@localhost $] sudo service apache2 restart |
You may see an error along the lines of:
Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName
3. check in the browser if all is it works:
Once you have finished setting up your virtual host, then type your ip address into the browser (http://192/168.1.10)
4. Conclusion
In this post, i have explained How To Set Up Apache Virtual Hosts in Linux.
If you have any questions or feedback, feel free to leave a comment.
As always, if you found this post useful, then click like and share it 🙂