Tired of typing long PHP development server commands in Termux? Discover the xphp-server script that automates the process and gives you instant PHP hosting on your mobile device.
At its heart, xphp-server is a wrapper around PHP’s built-in development server, but with some convenient enhancements
specifically for Termux. It’s designed to get a server running with zero friction.
Here is the code:
#!/bin/bash
host="0.0.0.0"
port=8080
if [ $# -eq 1 ]; then
args="-t $1"
fi
termux-open http://$host:$port
php -S 0.0.0.0:$port $args
Let’s break down this script to see the magic inside.
host="0.0.0.0" and port=8080
: The script pre-configuresif [ $# -eq 1 ]; then ... fi
: This is the clever part. It-t /path/to/your/directory
. Thetermux-open http://$host:$port
: This is the user-friendly touch. It uses Termux’s native termux-open command tophp -S 0.0.0.0:$port $args
: This is the final command thatUsing the script is incredibly simple.
Navigate to your project folder containing an index.php or other files and simply run:
xphp-server
Your browser will pop open, and you’ll see your project live!
Let’s say you are in your home directory, but your project is located in storage/shared/www/my-site
. You can serve it without navigating there first:
xphp-server storage/shared/www/my-site
The server will start, using my-site as the root, and your browser will open to the correct address.
Ensure you have a bin directory in your home folder. If not, create it:
mkdir -p ~/bin
Add ~/bin
to your PATH Variable, so any
executable script you put here becomes a system-wide command.
export PATH="$PATH:~/bin" >> ~/.bashrc
source ~/.bashrc
Create the script file:
touch ~/bin/xphp-server
Open the file in your favorite editor (like nano or vim) and paste the script code from above.
Make the script executable:
chmod +x ~/bin/xphp-server
That’s it! You can now run xphp-server
from anywhere in your Termux session.