This article will help you to create Virtual hosts in Apache2 server on Ubuntu, Debian & LinuxMint systems.
Step 1: Install Apache
You may skip this option if you already have installed Apache server in your system. If not use the following command to install Apache server on Ubuntu system. Also as a best practice don’t forget to update repositories metadata.$ sudo apt-get update $ sudo apt-get install apache2
Step 2: Create Apache VirtualHost
In Apache on Ubuntu all the virtual host configuration files are stored under /etc/apache2/sites-available directory. With the new Apache installation you will find a default virtual host file there. Create a new Virtual Host configuration file by copying default file.$ cd /etc/apache2/sites-available/ $ sudo cp 000-default.conf site1.example.com.confNow Edit new virtual host configuration file and update as per your requirement. My site1.example.com configuration file looks like below.
$ vim site1.example.com.conf
<VirtualHost *:80> ServerAdmin webmaster@site1.example.com ServerName site1.example.com DocumentRoot /var/www/site1.example.com/httpdocs <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>Now create a directory structure in your system, as per defined in above virtual host, and assign proper ownership and file permission.
$ mkdir -p /var/www/site1.example.com/httpdocs $ chmod 755 /var/www/site1.example.com/httpdocs $ chown www-data.www-data /var/www/site1.example.com/httpdocsNow upload your project files on /var/www/site1.example.com/httpdocs/ directory. For this example we have created a index.html file for testing purpose like below.
$ echo "<h1>site1.example.com</h1>" > /var/www/site1.example.com/httpdocs/index.html
Step 3: Enable First Virtual Host
Now we have successfully created our first virtual host in Apache. Now use the following command to enable this Virtual Host, So that Apache can load this configuration file on next reload.$ a2ensite site1.example.comBasically this creates a soft link of Virtual Host configuration file in directory /etc/apache2/sites-enabled/. To activate the new configuration, we need to run:
$ service apache2 reloadNow you can access http://site1.example.com in your browser. If you don’t have dns configured for your domain, do a local mapping by adding an entry in /etc/hosts files.
... 192.168.1.100 site1.example.com ...
Step 4: Create Another Virtual Host
If you want to add another virtual host on your server, just repeat step 2 and 3 with changing your site name like site2.example.com.Enjoy Virtual Hosting!
0 Comments