Every once in a while a hosting provider has to change an IP address for a single user or even an entire server on the fly. If you’ll try do this the usual way, by changing the IP address in WHM you can expect downtime ranging from several minutes to several hours until the DNS cache is cleared.
While cPanel does not officially allow accounts to have more than one IP address, there is no such restriction for Apache. So the solution is to override the default cPanel configurations with your own.
In the first example I will show you how to do this with a single site and in the second one I will focus on the entire server.
- Change a single site to work from multiple IP addresses:
In this example we want to alter the domain example.com which runs on the IP
address: 185.20.1.1 to also run on the IP address: 185.20.2.2 – assuming that both IP addresses are already configured on the server.
Apache’s configuration file is automatically generated at on of the following locations.
On EASY APACHE 4 based servers:
# /etc/apache2/conf/httpd.conf
On EASY APACHE 3 based servers:
# /usr/local/apache/conf/httpd.conf
And a large capital letters warning us:
DO NOT EDIT. AUTOMATICALLY GENERATED. USE INCLUDE FILES IF YOU NEED TO MAKE A CHANGE
While there is no use editing the file, we can pull all the VHOST info from it.
It will look like this:
<VirtualHost 185.20.1.1:80 > ServerName example.com ServerAlias mail.example.com www.example.com DocumentRoot /home/example/public_html ServerAdmin webmaster@example.com UseCanonicalName Off ... more settings here ... </VirtualHost>
If you are using an SSL certificate there is another block for the SSL virutal host:
<VirtualHost 185.20.1.1:443 > ServerName example.com ServerAlias mail.example.com www.example.com DocumentRoot /home/example/public_html ServerAdmin webmaster@example.com UseCanonicalName Off ... more settings here ... </VirtualHost>
We need to copy both of these blocks to the file:
/usr/local/apache/conf/includes/pre_virtualhost_global.conf
for EasyApache3
OR
/etc/apache2/conf.d/includes/pre_virtualhost_global.conf
for EasyApache4
Afterwards edit the opening line on each VHOST block so it includes both of the first and the second IP addresses like:
<VirtualHost 185.20.1.1:80 185.20.2.2:80> ...vhost settings... </VirtualHost> <VirtualHost 185.20.1.1:443 185.20.2.2:443> ...vhost settings... </VirtualHost>
Save and run:
/scripts/rebuildhttpdconf
make sure you get no errors and then restart Apache:
/scripts/restartsrv_httpd
That’s it, you’re good to go 😉
2. Change a server to load from multiple IP addresses:
Instead of copying an entire VHOST, as in the previous example, we will edit the default template that cPanel uses to build VirtualHosts.
Step 1: cd /var/cpanel/templates/apache2_4/
Step 2:
cp vhost.default vhost.local cp ssl_vhost.default ssl_vhost.local
Step 3: Edit each file:
In the vhost.local file change the first line from:
<VirtualHost[% FOREACH ipblock IN vhost.ips %] [% ipblock.ip %]:[% ipblock.port %][% END %]>
To:
<VirtualHost 185.20.1.1:80 185.20.2.2:80 >
In ssl_vhost.local file change the first line from:
<VirtualHost[% FOREACH ipblock IN vhost.ips %] [% ipblock.ip %]:[% ipblock.port %][% END %]>
To:
<VirtualHost 185.20.1.1:443 185.20.2.2:443 >
Step 4:
/scripts/rebuildhttpdconf /scripts/restartsrv_httpd
By completing all the above steps, all of the accounts on the server will run from both IPs addresses.
If you have accounts that uses a private IP address, you can use a combination of both methods. Change the template to run from the two main addresses as in method 2 and then copy the the virtual host settings as in method 1, but remember to change the IP address to the site’s private IP address.
Tags: apache, cPanel
Thanks for sharing this.
Since I have many IP’s to use, I tried adding to vhost.default and to ssl_vhost.default.
After rebuilding the httpd.conf and restarting apache, my website would redirect to /cgi-sys/defaultwebpage.cgi
Would you know how to get things working with *:80 and *:443?
Cheers!
Hi Khan, please note you are not supposed to edit the vhost.default and ssl_vhost.default files themselves, rather you are supposed to copy them to:
vhost.local
ssl_vhost.local
and then edit those files.
Hello Oleg,
Yes, I actually copied vhost.default and ssl_vhost.default to vhost.default.bk and ssl_vhost.default.bk.
Whenever I added a specific server IP to and , the website would work fine.
However, when I change the serverp:80 and serverip:443 to *:80 and *:443, the website would redirect to /cgi-sys/defaultwebpage.cgi
I would like the VirtualHost to be like and so websites could run on all IP’s. Would you know how this can be done? ?
Thank you
It seems my last comment was modified by your firewalls. I have shared my comment in the screenshot here https://prnt.sc/pp3tfj