How can we make the creation of Virtual host easy?

| | 2 min read

What do you usually go to create a virtual host? If I am right, then you will copy the default file, change the contents, add entry in host file and then you will enable the site, right? Well, if you are thorough with the above steps, then I would advise you not to waste your time any more by repeating the steps each time you need to create a virtual host, because I am going to provide you a script using which you can easily create a virtual host.

  • Step 1 : Copy the following script and save as .sh file.
    
    #!/bin/bash
    
    name=$1
    WEB_ROOT_DIR=$2
    
    email='webmaster@localhost'
    sitesEnable='/etc/apache2/sites-enabled/'
    sitesAvailable='/etc/apache2/sites-available/'
    userDir='/var/www/'
    sitesAvailabledomain=$sitesAvailable$name.local
    echo "Creating a vhost for $sitesAvailabledomain with a webroot $WEB_ROOT_DIR"
    
    ### create virtual host rules file
    echo "
    		<VirtualHost *:80>
    			ServerAdmin $email
    			ServerName $name.local
    			DocumentRoot $WEB_ROOT_DIR
    			<Directory $WEB_ROOT_DIR/>
    				Options Indexes FollowSymLinks
    				AllowOverride all
    				
    			</Directory>
    		</VirtualHost<" > $sitesAvailabledomain
    echo -e $"\nNew Virtual Host Created\n"
    		
    sudo sed -i "1s/^/127.0.0.1 $name.local\n/" /etc/hosts
    
    sudo a2ensite $name.local
    sudo service apache2 reload
    
    echo "Done, please browse to http://$name.local to check!"
    
  • Step 2 : Open terminal and navigate to the folder where the script file resides. Grant executable permission to this file(chmod a+x filename.sh).
  • Step 3 : Type sudo ./filename.sh virtual_host_name /directory-path.

    This will create a virtual host "virtual_host_name.local" which will point to the directory-path.I would advice you not to use this script if you don't know the steps to create a virtual host, because you shouldn't always use shortcuts in your life. Learn the correct way and then use this.

Note : The above code works if the apache version is less than 2.4. If your Apache version is 2.4, then add the following code inside the script file.


#!/bin/bash

### First parameter is the domain name and second parameter is the web-root.
name=$1
WEB_ROOT_DIR=$2

email='webmaster@localhost'
sitesAvailable='/etc/apache2/sites-available/'
sitesAvailabledomain=$sitesAvailable$name.local.conf
echo "Creating a vhost for $name.local with a webroot $WEB_ROOT_DIR"

### create virtual host rules file.
echo "
		<VirtualHost *:80>
			ServerAdmin $email
			ServerName $name.local
			DocumentRoot $WEB_ROOT_DIR
			<Directory $WEB_ROOT_DIR/>
				Options Indexes FollowSymLinks
				AllowOverride all
				Require all granted
			</Directory>
		</VirtualHostlt;" > $sitesAvailabledomain
echo -e $"\nNew Virtual Host Created\n"

### Add the domain name to hosts file.
sudo sed -i "1s/^/127.0.0.1 $name.local\n/" /etc/hosts

### Enable the site and reload the apache.
sudo a2ensite $name.local.conf
sudo service apache2 reload

echo "Done, please browse to http://$name.local to check!"

Hope this helps, happy coding. Do you need any web hosting support or need any services, connect with us.