How to Setup Apache Virtual Hosts on Ubuntu 18.04 or 20.04

In this tutorial, you will learn how To setup Apache Virtual Host In Ubuntu 18.04 or 20.04 easy way. The Apache web server is the most popular way of serving web content on the internet. Apache Virtual Hosts (Vhost) are used to run more than one website (domain) using a single IP address. In other words, you can have multiple websites (domains) but a single server. Different sites will be shown depending on the user’s requested URL. So we will show you how to How To Setup Apache Virtual Hosts In Ubuntu. Learn Create & Setup Virtual Host in Windows with XAMPP Server.

How to Create Virtual Host in Ubuntu Server

Here we are going to focus on IP-based virtual hosting. To set up a virtual host you should have the following.

Open the terminal add this command:

sudo gedit /etc/hosts

Now you need to add the hostname same like the one below we have added.	localhost	trilok-Vostro-15-3568

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Now you need to update the gedit /etc/apache2/sites-available/000-default.conf file with your server name and project path.

<VirtualHost *:80>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

	<VirtualHost *:80>
		DocumentRoot /var/www/laravel-app/public // laravel app is your project inside your root

	<VirtualHost *:80>
		DocumentRoot /var/www/laravel-project/public

Basically, the server name is the same as you added in the host file and the document root is showing which folder you want to show your browser.

Now open the terminal and restart your apache2 server.

sudo service apache2 restart

Now you run your project in url via your hostname.

1 thought on “How to Setup Apache Virtual Hosts on Ubuntu 18.04 or 20.04”

Leave a Comment