A small web-based tool for switching WordPress versions and PHP versions on an Apache server running Linux.
Designed for local development environments or controlled admin setups using Apache and
mod_php.
version_switcher is useful when you need to work with multiple WordPress and PHP versions on the same machine.
Typical use cases:
- local WordPress development
- testing different PHP versions
- checking compatibility of older WordPress installs
- switching environments quickly without running Apache commands manually
- switch WordPress versions from a web interface
- choose the locale for the WordPress download
- automatic backup before switching WordPress
- switch Apache PHP versions via
libapache2-mod-php - clear browser output for status and errors
- Linux with Apache2
- multiple PHP versions installed as Apache modules, for example:
libapache2-mod-php8.1libapache2-mod-php8.2libapache2-mod-php8.3libapache2-mod-php8.4
wp-clisudoaccess configured forwww-data
sudo mkdir -p /var/www/html/tools
sudo cp -r version_switcher /var/www/html/tools/sudo chmod +x /var/www/html/tools/version_switcher/wp_switcher.sh
sudo chmod 644 /var/www/html/tools/version_switcher/execute-script.php
sudo chmod 644 /var/www/html/tools/version_switcher/php_switcher.php
sudo chmod 644 /var/www/html/tools/version_switcher/index.htmlFile: /usr/local/bin/switch-apache-php.sh
#!/usr/bin/env bash
set -euo pipefail
REQUESTED_VERSION="${1:-}"
ALLOWED_VERSIONS=(
"php5.6"
"php7.4"
"php8.1"
"php8.2"
"php8.3"
"php8.4"
)
is_allowed=false
for version in "${ALLOWED_VERSIONS[@]}"; do
if [[ "$REQUESTED_VERSION" == "$version" ]]; then
is_allowed=true
break
fi
done
if [[ "$is_allowed" != true ]]; then
echo "Invalid PHP version: ${REQUESTED_VERSION:-<empty>}"
exit 2
fi
echo "Switching to $REQUESTED_VERSION"
for version in "${ALLOWED_VERSIONS[@]}"; do
if a2query -m "$version" 2>/dev/null | grep -q 'enabled'; then
echo "Disabling $version"
a2dismod -q "$version"
else
echo "$version is already disabled"
fi
done
echo "Enabling $REQUESTED_VERSION"
a2enmod -q "$REQUESTED_VERSION"
echo "Testing Apache configuration"
apache2ctl configtest
echo "Reloading Apache"
apachectl graceful
echo "Active module:"
a2query -m "$REQUESTED_VERSION" || true
echo "Done: $REQUESTED_VERSION"Then run:
sudo chown root:root /usr/local/bin/switch-apache-php.sh
sudo chmod 755 /usr/local/bin/switch-apache-php.shFile: /etc/sudoers.d/version-switcher
www-data ALL=(root) NOPASSWD: /usr/local/bin/switch-apache-php.sh *
Then set the correct permissions:
sudo chown root:root /etc/sudoers.d/version-switcher
sudo chmod 0440 /etc/sudoers.d/version-switcher
sudo visudo -cOpen the tool in your browser:
http://localhost/tools/version_switcher/
or:
http://<server>/tools/version_switcher/
- enter the path to the WordPress installation
- choose the target version and locale
- start the switch
A backup is created automatically before the switch.
- choose the target PHP version
- start the switch
The actual switch is handled by the root script, which updates the Apache module and reloads Apache.
Test directly in the terminal:
sudo /usr/local/bin/switch-apache-php.sh php8.4
sudo /usr/local/bin/switch-apache-php.sh php8.3Test as www-data:
sudo -u www-data sudo -n /usr/local/bin/switch-apache-php.sh php8.4Check active Apache PHP modules:
apache2ctl -M | grep php
a2query -m php8.3
a2query -m php8.4wp-cli is missing or not available in PATH.
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wpThe sudoers rule is missing or invalid.
sudo visudo -c
ls -l /etc/sudoers.d/version-switcherapache2ctl -M | grep php
a2query -m php8.3
a2query -m php8.4
sudo apache2ctl configtestBefore switching a WordPress version, the tool creates backups of:
- the database as
.sql wp-config.phpwp-content
Backups are stored as timestamped .tar.gz archives in the target WordPress installation directory.
- do not expose this tool publicly without additional protection
- intended for local or administrative use
- protect access if used outside a local environment
- use HTTPS when accessed over a network
This tool is built for Apache with mod_php. If your setup uses PHP-FPM, PHP switching needs a different implementation.



