Page 1 of 4

How to Auto Change IP Address in VestaCP

Posted: Wed Oct 08, 2014 8:48 pm
by crypto
In case you change vps's/ips and need to change the Shared Ip for your vestacp server, can be a real pain to do by hand.

Option 1 - Best Way Download and Run Code

Code: Select all

wget http://www.nobill.tv/dl/changeip.tar.gz
tar -zxvf changeip.tar.gz
mv v-change-server-ip /usr/local/vesta/bin/
v-change-server-ip OLDIP NEWIP

Option 2 - Long way Create it yourself

Code: Select all

touch /usr/local/vesta/bin/v-change-server-ip
chmod 0755 /usr/local/vesta/bin/v-change-server-ip

Code: Select all

#!/bin/sh

#script to change ips on a VestaCP server.
#usage:
# $0 <oldip> <newip>

LOG=/var/log/vesta/system.log

MYUID=`/usr/bin/id -u`
if [ "$MYUID" != 0 ]; then
        echo "You require Root Access to run this script";
        exit 0;
fi

if [ $# != 2 ] && [ $# != 3 ]; then
        echo "Usage:";
        echo "$0 <oldip> <newip> [<file>]";
        echo "you gave #$#: $0 $1 $2 $3";
        exit 0;
fi

OLD_IP=$1
NEW_IP=$2

HAVE_HTTPD=1
HAVE_NGINX=1

DATE=`date '+%F %X'`
BIN=`echo $0 | awk -F/ '{print $NF}'`

log()
{
        echo -e "$1";
        echo -e "$1" >> $LOG;
}

swapfile()
{
        if [ ! -e $1 ]; then
                log "Cannot Find $1 to change the IPs. Skipping...";
                return;
        fi

        TEMP="perl -pi -e 's/${OLD_IP}/${NEW_IP}/g' $1"
        eval $TEMP;

        log "$DATE $BIN $1\t: $OLD_IP -> $NEW_IP";
}

if [ $# = 3 ]; then
        swapfile $3;
        exit 0;
fi


IPFILE_OLD=/usr/local/vesta/data/ips/$OLD_IP
IPFILE_NEW=/usr/local/vesta/data/ips/$NEW_IP
if [ ! -e $IPFILE_OLD ]; then
        echo -n "$IPFILE_OLD does not exist.  Do you want to continue anyway? (y/n) : ";
        read YESNO;
        if [ "$YESNO" != "y" ]; then
                exit 0;
        fi
else
        mv -f $IPFILE_OLD $IPFILE_NEW
        log "$DATE $0 $IPFILE_OLD\t: $OLD_IP -> $NEW_IP";
fi

if [ "${HAVE_HTTPD}" -eq 1 ]; then
        if [ -e /etc/httpd/conf.d/${OLD_IP}.conf ]; then
                swapfile /etc/httpd/conf.d/${OLD_IP}.conf
                mv -f /etc/httpd/conf.d/$OLD_IP.conf /etc/httpd/conf.d/${NEW_IP}.conf
        fi
        swapfile /etc/httpd/conf.d/mod_extract_forwarded.conf
fi

if [ "${HAVE_NGINX}" -eq 1 ]; then
        if [ -e /etc/nginx/conf.d/${OLD_IP}.conf ]; then
                swapfile /etc/nginx/conf.d/${OLD_IP}.conf
                mv -f /etc/nginx/conf.d/$OLD_IP.conf /etc/nginx/conf.d/${NEW_IP}.conf
        fi
fi

swapfile /etc/hosts

ULDDU=/usr/local/vesta/data/users

for i in `ls $ULDDU`; do
{

        if [ ! -d $ULDDU/$i ]; then
                continue;
        fi

        swapfile $ULDDU/$i/web.conf
        swapfile $ULDDU/$i/dns.conf
        for j in `ls $ULDDU/$i/dns/*.conf`; do
        {
                swapfile $j
        };
        done;

        if [ "${HAVE_HTTPD}" -eq 1 ]; then
                swapfile /home/$i/conf/web/httpd.conf
        fi
        if [ "${HAVE_NGINX}" -eq 1 ]; then
                swapfile /home/$i/conf/web/nginx.conf
        fi

        for j in `ls /home/$i/conf/dns/*.db`; do
        {
                swapfile $j
        };
        done;

};
done;

#this is needed to update the serial in the db files.
if [ "${HAVE_HTTPD}" -eq 1 ]; then
   service httpd restart
fi
if [ "${HAVE_NGINX}" -eq 1 ]; then
   service nginx restart
fi

echo "*** Done swapping $OLD_IP to $NEW_IP ***";
Enjoy!

Re: How to Auto Change IP Address in VestaCP

Posted: Tue Oct 21, 2014 3:49 pm
by uscreator
Wow this is wonderful script and much needed.
Thank you very much!

P.S on a side note it did not fix my apache2 for some reason.
But everything else seems to be changed to new IP.

Apache is still complaining...

EDIT 1:
ok I just figure it out... old IP configuration file was still in Apache directory and it was trying to start it and it collide with new ip.conf file

But nevertheless it's a very useful and excellent script. Thanks again!

EDIT 2:
file location: /etc/apache2/conf.d/11.0.2.75.conf
After script runs it will create file with a new IP.
You need to go and remove file with old IP

Re: How to Auto Change IP Address in VestaCP

Posted: Wed Oct 22, 2014 8:41 am
by gobinathdpi
Ok any idea or explanation on how to remove that file?

Re: How to Auto Change IP Address in VestaCP

Posted: Thu Oct 23, 2014 2:33 pm
by uscreator
gobinathdpi wrote:Ok any idea or explanation on how to remove that file?
file location: /etc/apache2/conf.d/11.0.2.75.conf
After script runs it will create file with a new IP.conf.
You need to go and remove file with old IP.conf

Re: How to Auto Change IP Address in VestaCP

Posted: Wed Nov 26, 2014 5:08 pm
by crypto
Not everyone has the /etc/apache2/ dir. Most default centos cloud installs are in /etc/httpd/ but may mod to detect what dir.

Glad you find this usefull, I know it helps me with a large number of servers to manage. Hopefully they will include this script by default with a new release of vestacp.

Re: How to Auto Change IP Address in VestaCP

Posted: Mon Dec 29, 2014 7:13 pm
by jhoedram
Hello, I am also having problems with apache

I changed ip with the first tutorial Friend "crypto" and everything works fine except for apache, I thought the problem would be for you comment. However I check my files and ip is average.

What do you think could be the problem with apache?

attached image : Image
The new ip is set in the image (this crossed out), I do not see any trace of the old ip.

I hope you can help me, thanks.

uscreator wrote:Wow this is wonderful script and much needed.
Thank you very much!

P.S on a side note it did not fix my apache2 for some reason.
But everything else seems to be changed to new IP.

Apache is still complaining...

EDIT 1:
ok I just figure it out... old IP configuration file was still in Apache directory and it was trying to start it and it collide with new ip.conf file

But nevertheless it's a very useful and excellent script. Thanks again!

EDIT 2:
file location: /etc/apache2/conf.d/11.0.2.75.conf
After script runs it will create file with a new IP.
You need to go and remove file with old IP

Re: How to Auto Change IP Address in VestaCP

Posted: Mon Dec 29, 2014 9:24 pm
by uscreator
jhoedram wrote:Hello, I am also having problems with apache
Try to run configuration check for apache and see what errors you get

Code: Select all

apachectl configtest

Re: How to Auto Change IP Address in VestaCP

Posted: Tue Dec 30, 2014 9:26 pm
by jhoedram
This is menssage :

[Tue Dec 30 16:23:09 2014] [warn] NameVirtualHost 104.xxx.139.21:8443 has no VirtualHosts
Syntax OK

uscreator wrote:
jhoedram wrote:Hello, I am also having problems with apache
Try to run configuration check for apache and see what errors you get

Code: Select all

apachectl configtest

Re: How to Auto Change IP Address in VestaCP

Posted: Tue Dec 30, 2014 10:05 pm
by uscreator
For fix follow directions → viewtopic.php?f=11&t=4483
jhoedram wrote:This is menssage :
[Tue Dec 30 16:23:09 2014] [warn] NameVirtualHost 104.xxx.139.21:8443 has no VirtualHosts
Syntax OK

Re: How to Auto Change IP Address in VestaCP

Posted: Sat Jan 10, 2015 1:08 am
by xtinagp
This is an old post, but after running the marvelous solution posted by @Crypto I still had some errors on my site.

There are two additional places to change the IP. (Ubuntu 12.04 server)

/home/admin/conf/web/apache2.conf
First line has the virtual host IP

and

/home/admin/conf/web/snginx.conf
Second line lists the IP where it is supposed to listen to.

Hope this helps!