Virtualhost Hosting multiple domains in single server called virtualhost, you can host N of virtualhost in single apache webserver. See the below digram it will clearly says we can run N of virtualhost in single apache webserver. Make sure your system should have installed LAMP in Ubuntu, LAMP in Linux Mint & LAMP in Debian.
Apache supports two types of virtualhost
- Name-based Virtual Hosts (All the Websites sharing single IP address)
- IP-based Virtual Hosts (Each Websites having Different IP address)
1) Name based Virtual Host.
In Name based virtual host, each and every websites sharing the single IP address. How its work ? For Name based virtual host you need to setup DNS properly, so that the domain map with shared IP. Example hosting environment, Whenever you buying domain and hosting they will ask you to point the domain to hosting provider server (Like, They will provide Two Nameservers for your domain to map their server)
- Easy to Manage
- Easy to configure compare with IP based
- Good for shared & reseller hosting environment
1) How to add Name based virtualhost in Apache
In Apache 2.2 and older we need to mention when we using Namebased Virtualhost, like NameVirtualHost IP:80 but Apache 2.4 its not necessary because Any address/port combination appearing in multiple virtual hosts is implicitly treated as a name-based virtual host, For more details
1a) Creating Virtual Directories
We have already mentioned in our Tested Environment table, we are going to test two domain so that we came to know whether its working as a Name based virtualhost. For that we need to create Virtual Directories under www folder.
# Create virtual directory # $ sudo mkdir -p /var/www/support.2daygeek.com/public_html $ sudo mkdir -p /var/www/dev.2daygeek.com/public_html
2a) Change the ownership permission
While creating virtual directory By default directory assigned to root user. If its root user permission nobody can’t modify anything. So we need to change the ownership permission to corresponding user to making changes themselves.
# Change the Ownership # $ sudo chown -R username:username /var/www/support.2daygeek.com/public_html $ sudo chown -R username:username /var/www/dev.2daygeek.com/public_html
3a) Setting up Proper permission to www directory
Set the proper permission to apache web root (/var/www) so that everbody can read the website.
$ sudo chmod -R 755 /var/www/
4a) Creating sample page for website’s
We need to create the sample page for each websites, so that we can check whether its working with apache or not.
# Create Sample Page for support.2daygeek.com # $ sudo nano /var/www/support.2daygeek.com/public_html/index.html <html> <head> <title>New virtual host created successfully - support.2daygeek.com</title> </head> <body> <h1>New virtual host created successfully - support.2daygeek.com</h1> </body> </html> # Create Sample Page for dev.2daygeek.com # $ sudo nano /var/www/dev.2daygeek.com/public_html/index.html <html> <head> <title>New virtual host created successfully - dev.2daygeek.com</title> </head> <body> <h1>New virtual host created successfully - dev.2daygeek.com</h1> </body> </html>
5a) Creating Virtual Host Files
We need to create the virtual host file for each domain. By default ubuntu having default virtual host file 000-default.conf under /etc/apache2/sites-available. Just copy for your convenient like below. Make sure your vitualhost configuration file extension should be .conf
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/support.2daygeek.conf $ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/dev.2daygeek.conf
6a) Modify virtual host configuration
Open your copied virtual host configuration file and modify like below content (as per your domain name). Make sure you need to modify the domain name and directory name according that.
# Modified Virtual Host for support.2daygeek.com # <VirtualHost 10.0.2.15:80> ServerName support.2daygeek.com ServerAlias www.support.2daygeek.com ServerAdmin [email protected] DocumentRoot /var/www/support.2daygeek.com/public_html ErrorLog /var/www/support.2daygeek.com/public_html/error.log CustomLog /var/www/support.2daygeek.com/public_html/access.log combined </VirtualHost> # Modified Virtual Host for dev.2daygeek.com # <VirtualHost 10.0.2.15:80> ServerName support.dev.com ServerAlias www.dev.2daygeek.com ServerAdmin [email protected] DocumentRoot /var/www/dev.2daygeek.com/public_html ErrorLog /var/www/dev.2daygeek.com/public_html/error.log CustomLog /var/www/dev.2daygeek.com/public_html/access.log combined </VirtualHost>
7a) Enabling / Disabling Virtual Hosts
Use the below commands to Enable/Disable Virtual Hosts.
# Enable Virtual Hosts # $ sudo a2ensite support.2daygeek.conf $ sudo a2ensite dev.2daygeek.conf # Disable Default Virtual Hosts # $ sudo a2dissite 000-default.conf
8a) Run configtest.
Run the below configtest command to check whether any errors in the newly added configuration files
$ sudo apachectl configtest
9a) Reloading Apache
Reload the apache configuration to make it work the new configuration
$ sudo service apache2 reload
10a) Restart Apache
Restart the apache finally.
$ service apache2 restart
11a) Adding domain name to hosts file.
we need to add both above domain to /etc/hosts file to map the domain to associated IP without adding DNS zone. So that we can locally check.
$ sudo nano /etc/hosts 127.0.0.1 localhost 10.0.2.15 support.2daygeek.com 10.0.2.15 dev.2daygeek.com
12a) Flush local DNS cache
Use the below command to flush local DNS cache
$ sudo /etc/init.d/dns-clean start
13a) Access newly setup website
Navigate your browser and access the site’s by hitting the links http://support.2daygeek.com & http://dev.2daygeek.com
2) IP-based Virtual Hosts
In Name based virtual host, Each Websites having Different IP address. You can assign more then one IP address to single NIC card or you can assign each dedicated IP to Separate NIC card and Practically its not good and hard to manage. For SSL certificate we need to use IP based virtual hosts.
How to add IP based virtualhost in apache
For IP based virtual host we should need more then one IP. So first we need to add aditional IP (Using IP aliasing), We alredy know we are having one IP 10.0.2.15 which was used for Namebaed virtual host. Follow the below steps to add additional IP to same NIC card. For SSL certificate installation everybody using IP based virtualhost.
# Add Additional IP into System # $ sudo ifconfig eth0:1 10.0.2.16 netmask 255.255.255.0 $ ifconfig eth0 Link encap:Ethernet HWaddr 08:00:27:1e:a2:47 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe1e:a247/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:23723 errors:0 dropped:0 overruns:0 frame:0 TX packets:16342 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:15295059 (15.2 MB) TX bytes:1930608 (1.9 MB) eth0:1 Link encap:Ethernet HWaddr 08:00:27:1e:a2:47 inet addr:10.0.2.16 Bcast:10.0.2.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:2020 errors:0 dropped:0 overruns:0 frame:0 TX packets:2020 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:202350 (202.3 KB) TX bytes:202350 (202.3 KB)
Follow the same steps which we descriped in Name based virtual host. And modify below steps slightly.
- (6a) Change the Virtualhost IP for dev.2daygeek.com to 10.0.2.16 instead of 10.0.2.15
- (11a) Change dev.2daygeek.com IP from 10.0.2.15 to 10.0.2.16
2a) Access newly setup website
Navigate your browser and access the site’s by hitting the links http://10.0.2.15 & http://10.0.2.16
dev.2daygeek.com (10.0.2.16) – Output
We are preparing all articles in-depth to understand by all level/stage Linux administrators. If the article is useful for you, then please spend less than a minute to share your valuable comments in our commenting section.
Please stay tune with us…Good Luck.
8 Comments on “Setup Apache Virtual Hosts In LinuxMint / Ubuntu / Debian”
I am using Linux Mint 18. I setup virtual host as per you given steps for “IP based virtual host”. It’s working fine when I have created but when I restarted my PC, I have to assign the IP to my network again and again. Please help me.
Good tutorial 🙂
Very useful! Thanks men!
Ultra useful! Thanks Men.
Hey, many thanks for writing this short very useful guide. I was wondering for a long time about creating configuration and it brought me many problems, but now I am able to configure it. Once more time thanks.