What is a VirualHost?
In apache a VirtualHost is a way to host multiple websites on a single apache instance. If you're a developer that works on more than one website this is the best way to test all those websites on one machine.Creating a VirtualHost
Creating a VirtualHost is simple, we can achieve this in four easy steps. In the following example we will be creating a VirtualHost named "website.demo":
-
Create the VirtualHost file
sudo nano /etc/apache2/sites-available/website.demo.conf
<VirtualHost *:80> DocumentRoot /home/username/Software/website ServerName website.demo </virtualhost>
- Create the hosts file entry
$ sudo nano /etc/hosts
127.0.0.1 website.demo
- Enable the new VirtualHost
$ a2ensite website.demo
- Restart Apache
$ sudo service apache2 restart
Now you can go to http://website.demo and you website (which is located at /home/username/Software/website) will be there.
The DocumentRoot entry tells apache where the files for this VirtualHost are located. The ServerName entry tells apache what name to serve the website as.
The VirtualHost file
The VirtualHost file (/etc/apache2/sites-available/website.demo.conf) is loaded by apache on startup and serves website.demo on port 80 which means you can visit it via your web browser.The DocumentRoot entry tells apache where the files for this VirtualHost are located. The ServerName entry tells apache what name to serve the website as.
a2ensite and a2dissite
a2ensite enables a VirtualHost inside your sites-available folder by creating a symbolic link to it from the sites-enabled folder (which apache loads during startup). a2dissite disables a VirtualHost by removing the symbolic link created by a2ensite.
And that's it, you should now be able to create your own VirtualHosts easily.
No comments:
Post a Comment