I'm running a MediaWiki instance from a home server where the public IP address is not guaranteed to remain static. The following script is meant to run at boot time to check if the IP address has been changed an if so to update the relevant line in the LocalSettings.php file.
Here's the script:
#!/bin/bash
echo -n 'Retrieving public IP address... '
current_ip=` dig +short myip.opendns.com @resolver1.opendns.com `
if [ "$current_ip" == "" ]; then
echo 'no response. No action taken.'; exit 1;
# check for a well formed IP adress: 000.000.000.000
elif [[ $current_ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo $current_ip
else
echo 'invalid IP address detected. No action taken'; exit 2;
fi
old_ip=`cat /var/lib/mediawiki/LocalSettings.php | grep '\$wgServer' | cut -d '"' -f2 | cut -d\/ -f3`
if [ "$old_ip" == "$current_ip" ]; then
echo 'LocalSettings.php is already up-to-date. No action taken.'
else
echo -n "Replacing $old_ip with $current_ip in LocalSettings.php... "
sed -i.bak "s|\$wgServer =.*|\$wgServer = \"http://${current_ip}\";|" /var/lib/mediawiki/LocalSettings.php
echo 'done.'
fi