Setting Up Virtual Host - Linux

by Devansh


Posted on 30 Mar 2018 10:03:27 (4 months ago)


In this section I will only share a commands to setup a virtual host in linux. These commands I have used in Ubuntu 16.04 LTS with APACHE2 Server. Hope these commands will also work on all Linux OS as well. So keeping the post short and sweet below are the commands. Test it with your new project and have fun.

1. cd /var/www

2. Create your project directory here
   => sudo mkdir -p example1.com/public_html

3. cd /var/www/example1.com/public_html
   => Paste all your codes in this directory

4. cd /etc/apache2/sites-available/

5. Copy Default Configuration for your project
   => sudo cp 000-default.conf example1.com.conf

6. sudo nano example1.com.conf
   => Paste the below codes in conf file
	  
	  ServerAdmin webmaster@example1.com
	  ServerName example1.com
	  ServerAlias www.example1.com
	  DocumentRoot /var/www/example1.com/public_html
	  ErrorLog ${APACHE_LOG_DIR}/error.log
	  CustomLog ${APACHE_LOG_DIR}/access.log combined
	  

7. Enable your site conf
   => sudo a2ensite example1.com.conf

8. Disable your default conf
   => sudo a2dissite 000-default.conf

9. Restart your apache server
   => sudo service apache2 restart

10. /etc/hosts
   => Assign your IP for domain
      127.0.0.1 example1.com