Easymux
Termux – Android – Linux – Windows

Termux Guide: How To Create xphp-server For PHP Development?

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.

What Is xphp-server?

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

How It Works?

Let’s break down this script to see the magic inside.

  1. host="0.0.0.0" and port=8080: The script pre-configures
    the server to listen on 0.0.0.0, meaning it will be
    accessible from any device on the same network (not just on
    your phone via localhost). It uses port 8080, which is
    unlikely to conflict with other common applications.
  2. if [ $# -eq 1 ]; then ... fi: This is the clever part. It
    checks if you provided exactly one argument to the script.
    1. If you did provide an argument (like a directory path), it
      sets a variable args to be -t /path/to/your/directory. The
      -t flag tells the PHP server to use that directory as the
      document root.
    2. If you didn’t provide an argument, the args variable
      remains empty, and the PHP server will use the current
      working directory as the root.
  3. termux-open http://$host:$port: This is the user-friendly touch. It uses Termux’s native termux-open command to
    automatically open the server’s URL in your phone’s default web browser. No more manually typing IP addresses!
  4. php -S 0.0.0.0:$port $args: This is the final command that
    starts the server. It uses the host, port, and optional document root path we defined earlier.

How to Use It

Using the script is incredibly simple.

Scenario 1: Serve the current directory

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!

Scenario 2: Serve a different directory

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.

How to Install It

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.