Saturday, May 28, 2016

How to create virtual host on Apache web server


Creating virtual host is solution if you have multiple website running on one web server, even if your web server is on local computer not online at all. In this article i will show you how to create virtual host for apache web server on lubuntu.

Alright, for this tutorial we are going to create a new virtual host and we are going to name it lubuntu.howto, so when open http://lubuntu.howto/ it will shows it's own web page, it will be different with the http://localhost/ or any other local domain.

Step by step how to create virtual host on apache web server
  • Install apache web server, read here for more info (skip if you already did this)
  • Enable apache mod rewrite, read here for more info (skip if you already did this)
  • Create new folder for the virtual host, let's say the folder name is my_lubuntu, since apache2 default document root is on /var/www/html/ so create the new folder on that path, it will be /var/www/html/my_lubuntu/
sudo mkdir /var/www/html/my_lubuntu
  • Create a new web page on /var/www/html/my_lubuntu/ called index.html
  • sudo leafpad /var/www/html/my_lubuntu/index.html
    index.html
    <html> 
    <head> 
    <title>lubuntu</title> 
    </head> 
    <body> 
    <h1>Welcome to lubuntu.howto</h1> 
    <marquee>This is your new virtual host</marquee> 
    <marquee>put your web page inside /var/www/html/my_lubuntu/</marquee> 
    <marquee>It will be available through local domain http://lubuntu.howto/</marquee> 
    <a href="http://lubuntuhowto.blogspot.com/">dont forget to visit lubuntuhowto.blogspot.com</a> 
    </body> 
    </html>
  • Create lubuntu.howto.conf file inside /etc/apache2/sites-available/
  • sudo leafpad /etc/apache2/sites-available/lubuntu.howto.conf
  • Copy paste this code into lubuntu.howto.conf file

  • lubuntu.howto.conf
    <VirtualHost *:80> 
    ServerName lubuntu.howto 
    DocumentRoot /var/www/html/my_lubuntu/ 
    <Directory /var/www/html/my_lubuntu/> 
    AllowOverride all 
    </Directory> 
    </VirtualHost>
  • Activate the new virtual host using a2ensite command
  • sudo a2ensite lubuntu.howto.conf

  • Restart the apache web server to take effect of the changes
  • sudo service apache2 restart

  • Edit /etc/hosts file, insert new domain called lubuntu.howto
  • sudo leafpad /etc/hosts

  • put this somewhere inside your /etc/hosts file
127.0.0.1 lubuntu.howto

  • Open your favorite web browser and go to address http://lubuntu.howto/

That's it guys, that's how you create a virtual host on apache web server, very easy isn't it? you can create as many virtual host as you like, have fun !

NOTE: leafpad is default text editor on lubuntu, so if you are trying this tutorial using ubuntu, change leafpad into gedit.


1 comment: