How To Set Up Apache Virtual Hosts in Linux

by Ismail.EL
0 comment


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:

b. Grant Permissions:

We need to grant ownership of the directory to the interested user:

Additionally, it is important to make sure that everyone will be able to read our new files.

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

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:

Open up the new config file and add a following script:

#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_html

ErrorLog ${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:

And:

e. Restart Apache:

Finally Use this command to restart apache:

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 🙂

20270cookie-checkHow To Set Up Apache Virtual Hosts in Linux

Related Posts

Leave a Comment