Introduction: LAMP Server on Android for Wordpress

WHY?

So why would you want a LAMP server and/or Wordpress on an Android phone or tablet?

I work for a small local charity and our web hosting is currently managed by someone else, but we have to put our own hosting in place later this year, so I wanted to have Wordpress running on something that's "mine". My only home computer is my work laptop, and I really didn't want to muck about with putting a server onto it!

I messed around with an old Raspberry Pi a few days ago (2012 A model?) but the performance was awful, and it eventually bricked the OS due to lack of RAM. I had my old Motorola Moto mobile (G5 or G6 I think) sitting on the shelf, and thought "hey, Android is kinda Linux" and an hour later I had it all running. Cool!

I also noticed that some how-to documents online are missing steps, or don't have all of the configuration for Wordpress, so I copied my .bash_history over and did this Instructable.

Hopefully it's useful to someone!

Supplies

Necessary: An Android phone or tablet running Android 7.0 or later. This does NOT need to be rooted, and we won't be rooting it, so anything will do.

Fairly necessary: Another computer/laptop with an SSH client. Windows, Mac, Linux, Chromebook etc.

(Note that you can do the configuration on the Android device itself, but if so I'd recommend a Bluetooth keyboard. Typing Linux commands on a touchscreen gets tiring fast, and text editing is nigh on impossible.)

Step 1: Setting Up Android

Networking

The first step is to get Android on to your WiFi network, which I won't go into here, but also to ensure it has a static IP address on your WiFi network.

Get it onto WiFi yourself and then set up the static IP:

  1. From your Android home screen drag down from the top and tap the Settings cog.
  2. Tap Network & Internet (At the top of the screen you'll see "Wi-Fi" with the name of your network.)
  3. Tap on the "Wi-Fi/[name of your network]" and you'll be on the screen that gives the list of all remembered networks with your network name and Connected.
  4. Press and hold the name of your network until the pop-up menu appears.
  5. Tap Modify Network
  6. Open up Advanced Options and scroll down to IP Settings and switch it to Static
  7. It should auto-populate with the current IP settings, so just make a note of your IP address and then hit Save.

Job done! That IP address will be the one we use for all the SSH editing, and also for connecting to Wordpress. Keep it safe. In my case it's 192.168.1.186, so wherever you see that through the Instructable you can substitute your own IP address.

Setting up Android to allow apps from Unknown Sources

We're going to be using Termux as our Terminal on Android, and they've stopped publishing on the Play Store, so we need to download the APK and install it, which can only be done with "Install Unknown Apps" switched on. (I'm pretty sure the Play Store version would work OK for something as simple as this, but haven't tested it. Give me a yell if you think it would be useful to append to this!)

This varies a bit from Android version to Android version, so here's a foolproof way of doing it:

  1. From the Android home screen drag down from the top and tap the Settings cog.
  2. In the "Search in Settings" box, type in the word unknown
  3. Tap on the "Install unknown apps" item in the search list.
  4. Tap on the "Install unknown apps" item in the Settings list.
  5. Find your web browser, e.g. Chrome, tape on this and enable "Allow from this source"

This enables Chrome to download Android Package files (apk) and install them. Note that you'll still get a security prompt, and online checking of sources will still take place. Android isn't the wild west.

Step 2: Install Termux and Configure for SSH

Install Termux

In your browser on your phone navigate to

https://f-droid.org/en/packages/com.termux/

Scroll down to the latest version (0.108 in April 2021) and find "Download APK" for that version and tap on it.

Once it's downloaded, tap "Open" and follow the install prompts. It takes a few seconds to install.

Once its installed, launch it by tapping the Termux icon.

We're now in Termux on the phone, and unless you're doing this with a Bluetooth keyboard, we want to spend as little time as possible using the touchscreen keyboard! So let's set it up for SSH access.

Configure SSH

The first thing we need to do is set up a password for the Termux user. Note that this is not root, but is effectively a root alias, so works the same way. You don't need su or sudo to do anything advanced in Termux.

Type in:

passwd

The prompt "New password:" will appear. Type in the password you want to use and then retype it when prompted.

Now we install ssh by typing

pkg install openssh

If prompted "[Y/n]?" during install, tap Enter to allow it to proceed.

Finally, we need to run the ssh daemon... and that means the last command we're typing on the phone!

So type:

sshd

It should appear that nothing has happened. The phone is back to the prompt. But we can now remote into it, use a full-size screen and keyboard and get things set up. Yay! Put the phone to one side now.

Step 3: First SSH Into Termux

I'm using PuTTy under Windows, but using the Windows ssh client, or ssh under Linux or MacOS is equally as easy.

The only thing you need to know is that we're not running on the default SSH port 22, as we're not on a rooted device. The Termux sshd runs on port 8022.

So in PuTTY, type your IP address into the box and 8022 into the port box.

From a command line client, type:

ssh -l root -p 8022 192.168.1.186

substituting the 192.168.1.186 with the IP address you noted down earlier.

On PuTTy when prompt "login as:" type root and in all cases, type in your password.

You should now be at the terminal prompt on your computer.

If you have any problems, go back through the previous steps, check that your IP address is what you think it is, e.g. by typing

ping 192.168.1.186

from your computer. Make sure you have launched sshd too. There's no "startup" or systemd or whatever in Termux, so you need to launch sshd manually every time you restart your phone/device.

Once you're into ssh, you can start installing packages and getting things configured.

Step 4: Installing Packages for LAMP Server

This is really straightforward. Get SSH'd in, and type the following one at a time, waiting for each to finish. If you get a "Do you want to continue[Y/n]?" prompt, just press Enter. The default is Y.

The packages we're installing are: PHP (web scripting language); Apache2 (web server); MariaDB (lightweight database engine for MySQL); php-apache (modules for allowing web hosts to run PHP, which is the language Wordpress uses) and finally wget (a way of downloading files from the Internet without a web browser).

pkg install php
pkg install mariadb
pkg install apache2
pkg install php-apache
pkg install wget

That should be everything you need, so we now need to edit/create some files, set up the database for Wordpress, and we'll be ready to go.

Step 5: Getting to Know the Filesystem Layout in Termux

The first thing to note is the unusual file structure in Termux. As it's running within Android but without any elevation, it cannot use the normal Linux places for settings files, e.g. /etc, /var/www etc.

The root filesystem for Termux is effectively at:

/data/data/termux.com/files

Note that there are ways of getting Termux to see your larger filesystem, e.g. SD cards etc., but we don't need them. Read the Termux manual if you're interested, e.g. for doing backups or transferring files into the Termux environment.

Our user's home folder is at:

/data/data/termux.com/files/home/

Note that this is the folder you will be in when you first ssh into Termux. Worth knowing!

Also note that the system root is effectively at:

/data/data/termux.com/files/usr

e.g the etc folder is:

/data/data/termux.com/files/usr/etc

the bin folder is:

/data/data/termux.com/files/usr/bin

Takes a bit of getting used to, but once you get used to it it's quick to move around. You can poke around /etc and /proc etc. within the Linux filesystem if you want!

Step 6: Setting Up Apache and Creating Php.ini

I am not going to do a huge Linux path/folder command tutorial here. I'll assume we've started in our home folder:

/data/data/termux.com/files/home/

If in doubt type:

cd ~

and that's where you'll be. We're trying to get to etc/apache2, so type:

cd ../usr/etc/apache2

and to edit the configuration file type

nano httpd.conf

Move around the file with the arrow keys, and type to change things. Nano is an old school text editor!

If you get lost, see below so you can quit without changing anything.

Scroll down about a page to the huge list of LoadModule commands. We need to disable one and enable a couple. The # sign disables a line. Find:

#LoadModule mpm_prefork_module libexec/apache2/mod_mpm_prefork.so

and remove the # at the beginning. The following line should be:

LoadModule mpm_worker_module libexec/apache2/mod_mpm_worker.so

Add a # at the beginning of that line.

Close to the bottom of the LoadModule lists, find:

#LoadModule rewrite_module libexec/apache2/mod_rewrite.so

and again, remove the # from the beginning.

We now need to tell apache to use the PHP module, so add this line after the line you just edited:

LoadModule php_module /data/data/com.termux/files/usr/libexec/apache2/libphp.so

Now, scroll down until you find the section header:

DocumentRoot "/data/data/com.termux/files/usr/share/apache2/default-site/htdocs"
<Directory "/data/data/com.termux/files/usr/share/apache2/default-site/htdocs">

A few lines down, you'll see the line:

AllowOverride None

Change this to read:

AllowOverride FileInfo

Immediately below this, you'll see a block:

<IfModule dir_module>
  DirectoryIndex index.html
</IfModule> <br>

Change index.html to read index.php and append the following code immediately below that block:

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch><br>

That should be everything. I've attached a screenshot of the latter part of this for reference. To save the file, type Ctrl-O and press Enter, and it should say "[Wrote XXX lines]" at the bottom of the screen. Type Ctrl-X to exit nano.

NOTE: If you think you may have made some mistakes, and want to exit nano without saving, type Ctrl-X and type "n" when prompted "Save modified buffer?"

That's the hardest text editing done! Let's sort out the php.ini file, which lives in the usr/lib folder.

Type:

cd ../../lib

Let's create the php.ini file and edit it. Type:

touch php.ini
nano php.ini

We just want to add two line to our empty php.in file, so type these as below:

upload_max_filesize = 32M
post_max_size = 32M

and as before press Ctrl-O to save the file and Ctrl-X to exit nano

Step 7: Configuring MySQL

We need to launch the MySQL daemon and configure a user for Wordpress. To launch the daemon type:

mysqld_safe &

You might need to tap Enter to get a command prompt to appear. The daemon is now running, so we can connect to it by typing:

mysql

You should see a prompt:

MariaDB [(none)]>

We need to set up a database for Wordpress and a user for Wordpress to access the database.

To add the database type:

CREATE DATABASE wordpress;

[You should get a reply: Query OK, 1 row affected (0.001 sec) or similar]

To create a user, type:

GRANT ALL PRIVILEGES ON wordpress.* TO "wordpress"@"localhost" IDENTIFIED BY "password";

You can obviously substitute the word "password" for something that's meaningful! As it's only accessible to the device itself (localhost) you should be fine with something fairly insecure, but don't let me dictate how secure you want it!

[You should get a reply: Query OK, 0 rows affected (0.054 sec) or similar]

Type:

quit

That's MySQL configured. You can test it by typing:

mysql -u wordpress -p

and type your password. You should get to the MariaDB prompt. Type quit again.

Step 8: Install Wordpress

This should be the final setup step.

We need to get into the root folder for apache2, download and install Wordpress and then connect to it. Start by getting into the correct folder, usr/share/apache2/default-site/htdocs

Type:

cd ../../share/apache2/default-site/htdocs

We need to empty the folder completely first, so type:

rm -r *

Download Wordpress:

wget https://wordpress.org/latest.tar.gz

Decompress it:

tar -xzf latest.tar.gz

(This takes a few seconds)

Move Wordpress into place:

mv wordpress/* .

and finally clean up the download file and folder:

rm latest.tar.gz
rmdir wordpress/

Step 9: Get Connected!

Right, we've installed everything we need, so let's get connected to Wordpress.

We need to launch the apache web server, so type:

httpd

That should be the system running.

Open a browser, and type this into the address bar:

http://192.168.1.186:8080

(replacing 192.168.1.186 with your own IP address)

Follow the prompts for installing Wordpress. Database name is wordpress, username is wordpress and the password is whatever you set up during the MySQL install step.

Step 10: Final Notes

Startup script

Having a script to launch apache2 and mysql is pretty useful. I have one in my home folder, which is as simple as:

mysqld_safe &
sleep 5
httpd

The script is called startwp. You create this by typing:

cd ~
touch startwp
nano startwp

and type in the commands above, and do Ctrl-O and Ctrl-X to save and exit. Then type:

chmod 755 startwp

So if I reboot my phone, I just launch Termux and type:

./startwp

to kick it off.

Performance

Running the phone on battery means that Android's processor governor will make the processor stay in its lowest power state when the screen is off, which drastically reduces the performance of all the processes. So leave it plugged in, or set the screen timeout to something high, or both.

In general performance is awesome. I found it to be on a par with the shared hosting I'm using for the published site, and certainly good enough for any restyling, testing etc.

Moving a site to/from the phone.

I found that migrating quite a large site from an online host was as simple as using the Wordpress Export function on the host site, and then on the Android host I installed all of the major plugins first (For me this was WooCommerce, Contact Form 7, Events Manager, Kadence Blocks and Kadence free theme) before doing the Wordpress Import. Using the "import media too" worked fine. An 850MB site took around 20 minutes to fully import to my phone's Wordpress.

To get a site off the phone you'll probably need a proper plugin, e.g. Duplicator, as the remote host won't be able to "see" your phone to get to your media gallery.