What are the steps to set up virtual host in Ubuntu?

| | 1 min read

Virtual host setup in Ubuntu is very simple, we can do this in few steps. The following commands will help you to download the latest version of all software from its repositories(that is updation) and then install 'apache2', if you have no apache2 software. Otherwise ignore the following commands and go through the main steps for VirtualHost setup.

sudo apt-get update

sudo apt-get install apache2

Main steps for VirtualHost setup.

  • Open your terminal, take a copy of the default .conf file from /etc/apache2/sites-available/ and open it in gedit editor (if you have no gedit application, just use your own editor for modifying the entries in the file.
    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myproject.conf
    sudo gedit /etc/apache2/sites-available/myproject.conf
  • Remove all the default content in myproject.conf, add the following code in it and save it.
    <VirtualHost *:80>
      ServerName myproject.local
      ServerAdmin webmaster@localhost
      DocumentRoot /home/user/public_html/myproject_folder
      <Directory /home/user/public_html/myproject_folder>
        Require all granted
        AllowOverride All
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
  • Enable your currently added virtual host file.
    sudo a2ensite myproject.conf
  • Then restart your Apache using the following command.
    sudo service apache2 restart
  • Its not finish, now set up localhost file by editing the 'hosts' file from 'etc'. For this, first you have to open 'hosts' file in gedit using the following command :
    sudo gedit /etc/hosts

    Now, you have to enter, myproject.local

Ok, lets open your browser and type URL as 'myproject.local' for getting your site. It will work. Checkout other virtual host related articles now. For any other services get in touch with us.