Main menu
WalkswithMeApache Linux Commands System Tips VestaCpHow to Change IP Address in Vesta Control Panel

How to Change IP Address in Vesta Control Panel

How to change IP  address in Vesta Control Panel or VestaCP , Yes there may be situations like this recently my provder asked me to change the IP address of the server as part of some hardware upgrade , So  I don’t have any other choice to avoid this.

There is an old script available to do the same in VestaCP forum but that alone won’t be enough to achieve the same , that’s why I’m writing this article for the future references.

first of all make sure you have root or sudo access to the server then follow the steps.

Create a file called v-change-server-ip


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

Make sure the file has proper permissions.


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

 

Now paste the below content into the file


#!/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 ***";

 

After the script run still the websites are not working as expected, so I inspected the details and found there are few more areas we need to change the IP address.

Make sure the new IP file is created in the below path.


/etc/apache2/conf.d/NEW_IP.conf

If the file not created with new IP address you have to create the file with new IP the content can be copy paste from the old file and chnage the old IP to new IP address.

If you are using the Apache and Nginx together then you have to make sure the change the files in the following path too.


/home/USER/conf/web/SITE_NAME.apache2.conf
//If you're using lets encrypt.
/home/USER/conf/web/SITE_NAME.apache2.ssl.conf
//If you're using Nginx
/home/USER/conf/web/SITE_NAME.nginx.conf
//if you are using nginx
/home/USER/conf/web/SITE_NAME.nginx.ssl.conf

make sure all these files contain OLD IP address changed to new IP.

If you’re using manually change the VIM or Vi to find and replace the OLD IP command will be as follows.


:%s/OLD_IP_ADDRESS/NEW_IP_ADDRESS/g

Also check the below path have new IP address file created , if not create one with new IP and remove the old IP file.


/usr/local/vesta/data/ips/NEW_IP_ADDRESS

 

All these places if you’re done then you can restart the Apache and Nginx , I recommand a complete restart of the server too. Then you’re good to go all set.

Hopefully this will help some in the future to chnage the IP address of your VestaCP server.

Happy Reading 🙂 🙂 🙂 .

 

 

FacebookTwitterGoogle+RSS