Changing IP in config.php every restart on a local test VM.

Changing IP in config.php every restart on a local test VM.

by Daniel Martin -
Number of replies: 4

For testing Moodle is running on Hyper-V with Default Switch as the network device.  This makes it easy to test because only the host computer has access to the Moodle LAMP server, yet the server can access the internet.

One drawback is the IP address changes every time the server is restarted.  Th Moodle site stops functioning.  Running this script ever time the server is started makes it so the IP is updated in config.php:

sudo chmod -R 777 /var/www/html/moodle

ipaddress=$(hostname -I)

ipaddress=$(echo $ipaddress | sed 's/ *$//g')

cat /var/www/html/moodle/config.php | sed -r 's/(\b[0-9]{1,3}\.){3}[0-9]{1,3}\b'/"$ipaddress"/

sudo chmod -R 0755 /var/www/html/moodle

echo $ipaddress

As many have helped me on this site I thought I would share something too.
Someone who knows Linux more may be able automate this.
Average of ratings: -
In reply to Daniel Martin

Re: Changing IP in config.php every restart on a local test VM.

by Ken Task -
Picture of Particularly helpful Moodlers

Thanks for sharing back for a specific environment.   It will help those that have the same setup.   Uhhhh .... do have something else you might need to investigate ... when the site url changes, the config.php file does need to change, but what of the internal links that are now residing in tables of the DB?

I used to travel to different venues for (BYOB - Build Your Own Box) installing/maintaining moodles and used  a 'travel server' plugged into that venues network pulling a DHCP IP which was unknown.  Had travel server send me an email so I'd know the new IP acquired so I access the travel server via ssh ... changed IP in config.php and did a sed s/r on an sql dump of DB, set up new db with proper character set and collation, then imported the sed edited sql dump.

Changed config.php to point to new DB ... everything then works. smile

'SoS', Ken

Average of ratings: Useful (1)
In reply to Ken Task

Re: Changing IP in config.php every restart on a local test VM.

by Daniel Martin -
Didn't even think of internal links in the DB. I guess I am used to relative links. Thanks for the input
In reply to Daniel Martin

Re: Changing IP in config.php every restart on a local test VM.

by Visvanath Ratnaweera -
Picture of Particularly helpful Moodlers Picture of Translators
Virtualization environments have their own DNS like names. Each box gets a name, a single word. You might be able to use it similar to the FQDN in the Internet. In short, "$CFG->wwwroot='http://VMNAME';" IP can hide behind the VMNAME.
Average of ratings: Useful (1)
In reply to Visvanath Ratnaweera

Re: Changing IP in config.php every restart on a local test VM.

by Daniel Martin -
I will need to look into this for Hyper-V, that would be much easier.