Skip to content

christianmu/version_switcher

Repository files navigation

version_switcher

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.

Why

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

Features

  • 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

Screenshot

Dashboard Screenshot

Requirements

  • Linux with Apache2
  • multiple PHP versions installed as Apache modules, for example:
    • libapache2-mod-php8.1
    • libapache2-mod-php8.2
    • libapache2-mod-php8.3
    • libapache2-mod-php8.4
  • wp-cli
  • sudo access configured for www-data

Installation

Copy the project

sudo mkdir -p /var/www/html/tools
sudo cp -r version_switcher /var/www/html/tools/

Set file permissions

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.html

Create the root script for PHP switching

File: /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.sh

Add the sudoers rule

File: /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 -c

Usage

Open the tool in your browser:

http://localhost/tools/version_switcher/

or:

http://<server>/tools/version_switcher/

Switch WordPress version

  • enter the path to the WordPress installation
  • choose the target version and locale
  • start the switch

A backup is created automatically before the switch.

Switch PHP version

  • 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.

Verification

Test directly in the terminal:

sudo /usr/local/bin/switch-apache-php.sh php8.4
sudo /usr/local/bin/switch-apache-php.sh php8.3

Test as www-data:

sudo -u www-data sudo -n /usr/local/bin/switch-apache-php.sh php8.4

Check active Apache PHP modules:

apache2ctl -M | grep php
a2query -m php8.3
a2query -m php8.4

Troubleshooting

wp: command not found

wp-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/wp

sudo: a password is required

The sudoers rule is missing or invalid.

sudo visudo -c
ls -l /etc/sudoers.d/version-switcher

PHP version does not change

apache2ctl -M | grep php
a2query -m php8.3
a2query -m php8.4
sudo apache2ctl configtest

Backups

Before switching a WordPress version, the tool creates backups of:

  • the database as .sql
  • wp-config.php
  • wp-content

Backups are stored as timestamped .tar.gz archives in the target WordPress installation directory.

Security

  • 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

Notes

This tool is built for Apache with mod_php. If your setup uses PHP-FPM, PHP switching needs a different implementation.

Additional Screenshots

WordPress switching

WordPress switching

PHP selection

PHP selection

PHP output

PHP output

About

Lightweight web tool for switching WordPress and PHP versions on Apache-based local development setups.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors