To set up IP-based virtual hosting, you need to have more than one IP address assigned to your server. Setting up multiple IP addresses on a single network interface is called “IP aliasing.” It is very useful, particularly if your server only has one network interface card (NIC).
To set up multiple IPs, you need to edit the /etc/network/interfaces file:
—————————-For Virual Machine (CT) —————————————–
……………….
auto venet0:0
iface venet0:0 inet static
address 192.168.x.x <—– first IP
netmask 255.255.255.255
iface venet0:0 inet static
address 192.168.x.x <—– first IP
netmask 255.255.255.255
auto venet0:1
iface venet0:1 inet static
address 192.168.x.y <—– second IP
netmask 255.255.255.255
iface venet0:1 inet static
address 192.168.x.y <—– second IP
netmask 255.255.255.255
Save and close the file when you are finished. Then restart the network service to make these changes take effect.
Set up multiple instances of Apache
By default Apache listens for incoming connections on port 80. For port-based virtual hosting, you need to tell Apache to listen for IP address 192.168.x.x on port 80 and for IP address 192.168.x.y on port 8080.
To set up multiple ports, you need to edit the ports.conf file:
#nano /etc/apache2/ports.conf
#nano /etc/apache2/ports.conf
Add/edit the following lines:
NameVirtualHost 192.168.x.x:80 NameVirtualHost 192.168.x.y:8080 Listen 80 Listen 8080
Save and close the file, then restart Apache to make these changes take effect.
Create the directory structure
First, you need to make a directory structure which will hold the web pages. This directory is known as “document root” for the domain.
#mkdir -p /var/www/cnc.tkj/ip-vhost.cnc.tkj #mkdir -p /var/www/cnc.tkj/port-vhost.cnc.tkj
Create test web pages for each virtual host
Now, you need to create an index.html file for each website which will identify that specific domain.
Let’s create an index.html file for the ip-vhost.cnc.tkj ip virtual host.
#nano /var/www/cnc.tkj/ip-vhost.cnc.tkj/index.html
Add the following content.
<html> <head> <title>ip-vhost.cnc.tkj</title> </head> <body> <h1>The ip-vhost.cnc.tkj virtual host is working!</h1> </body> </html>
Save and close the file when you are finished.
Similarly, create an index.html file for the port-vhost.cnc.tkj virtual host.
#nano /var/www/cnc.tkj/port-vhost.cnc.tkj/index.html
Add the following content.
<html> <head> <title>port-vhost.cnc.tkj</title> </head> <body> <h1>The port-vhost.cnc.tkj virtual host is working!</h1> </body> </html>
Save and close this file as well. Now, you have the pages to test the virtual host configuration.
Set up ownership and permissions
You must change the ownership of these two virtual directories to apache, so that Apache can read and write data.
You can change the ownership with chown command.
#chown -R www-data:www-data /var/www/cnc.tkj/ip-vhost.cnc.tkj #chown -R www-data:www-data /var/www/cnc.tkj/port-vhost.cnc.tkj
You should also make the Apache document root /var/www/cnc.tkj directory world readable, so that everyone can read files from that directory.
#chmod -R 755 /var/www/cnc.tkj
Now your web server has the permissions it needs to serve content.
Create virtual host files
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 ip-vhost.cnc.tkj
#nano /etc/apache2/conf.d/ip-vhost.cnc.tkj.conf
Add the following content.
<VirtualHost 192.168.x.x:80> ServerName ip-vhost.cnc.tkj ServerAlias ip-vhost.cnc.tkj DocumentRoot /var/www/cnc.tkj/ip-vhost.cnc.tkj ErrorLog /var/www/cnc.tkj/ip-vhost.cnc.tkj/error.log CustomLog /var/www/cnc.tkj/ip-vhost.cnc.tkj/access.log combined </VirtualHost>
Save and close the file when you are finished.
Similarly, create a virtual host file for website port-vhost.cnc.tkj
#nano /etc/apache2/conf.d/port-vhost.cnc.tkj.conf
Add the following content.
<VirtualHost 192.168.x.y:8080> ServerName port-vhost.cnc.tkj ServerAlias port-vhost.cnc.tkj DocumentRoot /var/www/cnc.tkj/port-vhost.cnc.tkj ErrorLog /var/www/cnc.tkj/port-vhost.cnc.tkj/error.log CustomLog /var/www/cnc.tkj/port-vhost.cnc.tkj/access.log combined </VirtualHost>
When you are finished, it is a good idea to check the syntax of the configuration. You can check the syntax of files with the following command:
#apachectl configtest
After the syntax check is done, restart Apache to make these changes take effect.
#service apache2 restart
Test the virtual hosts
Now on the desktop Linux computer, open your web browser and go to the URLs http://192.168.x.x:80 and http://192.168.x.y:8080.
0 Comments