Running WordPress on a Android phone

In this article, we will show how to run WordPress in an android phone.

Prerequisites

  • Termux
  • MariaDB/MySQL
  • PHP
  • Linux text editor such vi or nano

Termux

Termux is terminal emulator and Linux environment apps which runs on Android phone. It combines powerful terminal emulation with an extensive Linux package collection.

Download it from Play Store and install in your phone.

Update and install packages

Use the following commands to update packages and install php, MySQL, etc:

  • $ termux-setup-storage
  • $ apt update
  • $ apt upgrade
  • $ pkg install php mariadb wget nano composer -y

Other settings

Create a database for WordPress. You also need to download latest version of WordPress. Use the following commands:

  • $ wget https://wordpress.org/latest.tar.gz
  • $ tar -xvf latest.tar.gz
  • $ cd wordpress
  • $ mysql
    • CREATE DATABASE wpdb;
    • GRANT ALL PRIVILEGES ON wpdb.* TO “wp-user”@”%” IDENTIFIED BY “strongpasswd”;
    • FLUSH PRIVILEGES;
    • exit
  • $ cp wp-config-sample.php wp-config.php
  • $ nano wp-config.php and set values for the following variables:
    • DB_USER
    • DB_PASSWORD
    • DB_NAME
    • DB_HOST i.e. define( ‘DB_HOST’, ‘127.0.0.1’ );

From command line, run PHP built in webserver:

  • $ php -S 0.0.0.0:8081

Workaround to few issues

WordPress only supports ports – 80, 443 and 8080. However running on a different port might gives “A valid URL was not provided” error message during upgrading or installing plugins. You might have to edit wp-config.php file and add the following codes at the end of file.

function allow_unsafe_urls ( $args ) {
	$args['reject_unsafe_urls'] = false;
	return $args;
}
add_filter( 'http_request_args', 'allow_unsafe_urls' );

Database connection failure. Use the following code snippet to test database connection.

<?php
define('WEB_ID','aa01');
require('wp-config.php');
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// failed
// die with an error
if (!$link) {
    // send HTTP/500 status code first
    header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
    echo "<html><head><title>Failed at " .WEB_ID. "</title></head><body><pre>";
    echo "Web server: ". WEB_ID .  PHP_EOL;
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}
// succeeded and show message
echo "<html><head><title>Success at " .WEB_ID. "</title></head><body><pre>";
echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>