Update MediaWiki settings with dynamic IP address 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
That's nutty, trying to address a server by a changing IP address. Reconfiguring the wiki to adjust to it is even crazier.

The standard practice for adapting to dynamic IP addresses is to use a dynamic DNS service. Then, everything else works normally.


Pick a hostname, like afuna.noip.com. Run a DDNS client to register your hostname whenever you acquire a DHCP lease, and re-register that hostname periodically thereafter.
Use it to form the URL of your site, e.g. https://afuna.noip.com/.
Configure your MediaWiki setting using that URL once, and you won't need that hacky script anymore.