Virtual hosting is a method for hosting multiple websites on a single server. There are two types of virtual hosting – Name based virtual hosting and IP based virtual hosting. IP-based virtual hosting is a technique to apply different directives based on the IP address and port a request is received on. You can assign a separate IP for each domain on a single server using IP-based virtual hosting. Mostly, this is used to host different websites on different ports or IP’s.
Here, we are going to host “www.ipvhost1.com” on IP “192.168.1.227”, “www.ipvhost2.com” on IP “192.168.1.228” and “www.portvhost.com” on IP “192.168.1.228” with Port 8080.
Requirements
OS: Ubuntu server 14.04 with Apache installed
IP address1: 192.168.1.227
IP address2: 192.168.1.228
Domain: www.ipvhost1.com
Domain: www.ipvhost2.com
Domain: www.portvhost.com
Create Multiple IP Addresses On Single Network Interface:
To setup IP based virtual hosting, you must have more than one IP address assigned to your Linux machine. Setup multiple IP addresses on a single network interface is called IP aliasing. IP aliasing is very useful if you have only one network interface card.
To do this, you need to edit “/etc/network/interfaces” file.
Add the following lines:
Save and close the file, then restart network service to make these changes take effect.
Setup Multiple Instances of Apache
By default, Apache listen on port 80. For port based virtual hosting, you need to tell Apache to listen IP “192.168.1.227” and “192.168.1.228” on port 80 and IP “192.168.1.228” on port 8080.
To setup multiple port, you need to edit “/etc/apache2/ports.conf”.
Add the following line:
Save and close the file, then restart Apache to make these changes take effect.
Create Virtual Directories
First, you need to make a directory structure which will hold the web pages. This directory is known as DocumentRoot for the domain.
Now, create three directories for websites “www.ipvhost1.com”, “www.ipvhost2.com” and “www.portvhost.com” under Apache default DocumentRoot directory.
Create test webpages for Each Virtual Host
Now, you need to create a “index.html” file for each website that identifies specific IP’s and Port.
Let’s create a index.html file for “www.ipvhost1.com” virtual host.
Add the following content.
Save and close the file when you are finished.
Next, Create a “index.html” file for “www.ipvhost2.com” virtual host.
Add the following content.
Save and close the file when you are finished.
Next, Create a “index.html” file for “www.portvhost.com” virtual host.
Add the following content.
Save and close the file when you are finished.
Setting Up Ownership and Permissions
By default, Apache service runs as a “www-data” user in Ubuntu. You must change the ownership of these three virtual directories to “www-data”, so that Apache can do read and write data.
To do this, run:
Also, you need to make Apache web root (/var/www/html) directory is readable, so that everyone can read files from that directory.
Create Virtual Host files
By default, Apache comes with a default virtual host file called “000-default.conf”. You need to disable this virtual host file first.
To do this, run the following command.
The next step is to create a virtual host configuration file for each website. The name of each configuration file must end with “.conf”.
Let’s create a virtual host file for website “www.ipvhost1.com”.
Add the following content.
Save and close the file.
Next, create a virtual host file for website “www.ipvhost2.com”.
Add the following content.
Save and close the file.
Next, create a virtual host file for website “www.portvhost.com”.
Add the following content.
Save and close the file.
After creating the virtual host files, you need to enable new virtual host.
You can do this by running.
Finally, restart the Apache service.
Testing Virtual Hosts
Now, it’s time to test IP Virtualhost. On a computer, open your web browser and navigate to URL “http://192.168.1.227:80” and “http://192.168.1.228:80”. You should see a sample demo pages for IP based virtual hosting that looks like this:
Similarly, to test Port Virtualhost, open your web browser and navigate to URL “http://192.168.1.228:8080”. You should see a sample demo page for Port based virtual hosting that looks like this:
Conclusion
In this post, I showed the step-by-step procedure to create and enable a IP based and Port based virtual host on Apache web server. You can easily set up many domains on the same server.
The post Setting Up IP and Port Based Virtualhost Apache appeared first on Make Tech Easier.