How to Build and Host a Website Using PHP in Termux — Local Dev on Android
- Introduction
- What Is PHP and Why Use It in Termux?
- Installing PHP in Termux — Step by Step
- Building Your First PHP Website Locally
- Adding a Database with MariaDB
- Hosting Your PHP Site Over Your Local Network
- Common Errors and Fixes
- Pro Tips for PHP Development in Termux
- PHP vs Node.js in Termux — Quick Comparison
- Frequently Asked Questions
- Conclusion
// 01 — Introduction: PHP Web Development on Android with Termux
Did you know you can build and host a real PHP website right from your Android phone using Termux — no root required? If you've been waiting to get into web development but don't have access to a PC, your Android device is more powerful than you think. With Termux and PHP, you can spin up a fully functional local web server, write dynamic pages, connect to a real database, and even share your site across your Wi-Fi network — all from the palm of your hand.
PHP in Termux is a surprisingly clean experience in 2026. The Termux package manager gives you access to PHP 8.x, MariaDB, and everything you need to build a production-style local development environment. Whether you're a student learning backend development, a freelancer prototyping a client site, or just someone curious about how web servers work, this guide walks you through every step from scratch.
In this tutorial by Rixon Xavier, you'll learn how to install PHP in Termux, set up the built-in PHP development server, create dynamic web pages, connect to a MariaDB database, and share your locally hosted PHP website with other devices on the same network. By the end, you'll have a working local dev environment running entirely on Android — no laptop, no root, no cloud hosting needed.
This guide is written for beginners, so every command is explained clearly. If you've never touched PHP or Termux before, don't worry — you'll be up and running faster than you expect. Let's build something real.
// 02 — What Is PHP and Why Use It in Termux?
PHP (Hypertext Preprocessor) is one of the most widely used server-side scripting languages on the internet. Platforms like WordPress, Laravel, Joomla, and countless custom applications are built on PHP. It runs on the server and generates dynamic HTML that gets sent to the user's browser. Unlike static HTML, PHP can pull data from a database, process form inputs, handle user sessions, and respond differently based on conditions — making it the backbone of interactive web applications.
So why run PHP in Termux specifically? Because Termux turns your Android phone into a real Linux environment. It gives you a package manager (pkg), a full Bash shell, and access to a massive library of Linux tools — including PHP. When you install PHP in Termux, you get the exact same interpreter that runs on a Linux web server. There's no simulation going on — it's the real thing.
Use Cases for PHP in Termux
There are more reasons to run PHP locally on Android than you might think:
- Learning web development without needing a computer
- Prototyping web apps quickly while away from your desk
- Testing PHP scripts before deploying to a live server
- Running local tools like custom dashboards or admin panels
- Understanding how web servers work at a fundamental level
PHP also ships with a built-in development web server — meaning you don't need Apache or Nginx to get started. One command starts a server that listens on a port of your choice. This makes PHP in Termux incredibly lightweight compared to a full LAMP stack.
PHP Version in Termux (2026)
As of 2026, Termux ships PHP 8.x via its package repositories. PHP 8 brought major performance improvements over PHP 7, including JIT compilation, union types, named arguments, and match expressions. You're not working with an outdated version — this is modern PHP.
One thing to understand: PHP in Termux runs in the Termux Linux user environment, not as root. This is actually good practice — running web servers without root privileges is safer and mirrors how production Linux servers are typically configured. Everything in this guide works without root access.
// 03 — Installing PHP in Termux — Step by Step
Getting PHP installed in Termux is straightforward. We'll also install a text editor and set up a project directory so you're ready to start writing code immediately after installation. Follow each step carefully.
Step 1 — Update Your Termux Packages
Before installing anything new, always update your package lists and upgrade existing packages. This ensures you get the latest available version of PHP and avoids dependency conflicts.
Update and Upgrade Packages
Run this command to sync your package index and upgrade installed packages:
pkg update && pkg upgrade -y
This may take a minute or two depending on your internet speed. Let it complete fully before moving on.
Step 2 — Install PHP
Install the PHP Package
Install PHP using Termux's pkg manager:
pkg install php -y
Once installed, verify the installation by checking the PHP version:
php --version
You should see output similar to this:
PHP 8.3.x (cli) (built: ...)
Copyright (c) The PHP Group
Zend Engine v4.3.x
If you see version information, PHP is installed and working correctly.
Step 3 — Install a Text Editor
Install nano (Beginner-Friendly Editor)
You'll need a text editor to write your PHP files. nano is the easiest option for beginners:
pkg install nano -y
micro (pkg install micro) which has syntax highlighting and is still beginner-friendly.Step 4 — Create Your Project Directory
Set Up Your Project Folder
Create a dedicated directory for your PHP project:
mkdir -p ~/mywebsite
cd ~/mywebsite
You're now inside your project folder. All your PHP files will live here. Think of this folder as your "web root" — the same concept as the public_html or www folder on a real hosting server.
// 04 — Building Your First PHP Website Locally
With PHP installed and your project directory ready, it's time to write actual PHP code and see it running in a browser. This section covers creating a dynamic homepage, understanding PHP syntax basics, and starting the built-in PHP development server.
Creating Your First PHP File
Let's create a proper dynamic homepage using PHP. This file will demonstrate core PHP features: variables, echo statements, date functions, and embedded HTML.
nano index.php
Type or paste the following PHP code into the editor:
<?php
$site_name = "My Termux Website";
$year = date("Y");
$current_time = date("H:i:s");
$message = "Welcome! This site is running PHP on Android via Termux.";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $site_name; ?></title>
<style>
body { font-family: monospace; background: #0d1117; color: #c9d1d9; padding: 40px; }
h1 { color: #58a6ff; }
.card { background: #161b22; padding: 20px; border-radius: 8px; border: 1px solid #30363d; margin: 20px 0; }
.green { color: #3fb950; }
</style>
</head>
<body>
<h1><?php echo $site_name; ?></h1>
<div class="card">
<p><?php echo $message; ?></p>
<p>Current Server Time: <span class="green"><?php echo $current_time; ?></span></p>
<p>© <?php echo $year; ?> — Powered by PHP <?php echo phpversion(); ?></p>
</div>
</body>
</html>
Save and exit nano by pressing Ctrl+X, then Y, then Enter.
Starting the PHP Built-In Web Server
PHP includes a built-in web server designed for local development. It's not meant for production use, but it's perfect for learning and testing. Start it with this command from inside your project directory:
php -S 0.0.0.0:8080
Breaking down this command:
php— calls the PHP interpreter-S— tells PHP to start its built-in server0.0.0.0— binds to all network interfaces (so other devices can connect)8080— the port number to listen on
You'll see output like this:
PHP 8.3.x Development Server (http://0.0.0.0:8080) started
Now open a browser on your phone and go to:
http://localhost:8080
You should see your PHP-powered homepage with your site name, current server time, and PHP version displayed. That's a live PHP website running on your Android phone — no cloud, no hosting fees, no root.
Creating Additional Pages
Let's add an About page to practice routing. Open a new terminal session (swipe right in Termux to open a new session) and navigate to your project:
cd ~/mywebsite
nano about.php
<?php
$page_title = "About This Project";
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<h1><?php echo $page_title; ?></h1>
<p>This project is a PHP website built entirely on Android using Termux.</p>
<p>PHP Version: <?php echo phpversion(); ?></p>
<a href="/">Back to Home</a>
</body>
</html>
Visit http://localhost:8080/about.php in your browser to see the new page. PHP's built-in server automatically serves any .php file in your project directory.
// 05 — Adding a Database with MariaDB
A PHP website becomes truly powerful when it's connected to a database. MariaDB is the database system available in Termux — it's open-source, MySQL-compatible, and runs great without root. In this section, you'll install MariaDB, create a database, and connect it to your PHP site.
Installing MariaDB in Termux
Install MariaDB
Install the MariaDB package along with the PHP MySQL extension:
pkg install mariadb -y
Initialize the Database
Before you can start MariaDB, you need to initialize it:
mysql_install_db
This sets up the system tables MariaDB needs to operate. It only needs to be run once.
Start the MariaDB Server
Start MariaDB in the background:
mysqld_safe -u $(whoami) &
Wait a few seconds, then press Enter. MariaDB is now running in the background.
Log In to MariaDB
Connect to the database server:
mysql -u root
Creating a Database and Table
Inside the MariaDB shell, run these SQL commands to create a database for your PHP site:
CREATE DATABASE mywebsite;
USE mywebsite;
CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
message TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO messages (name, message) VALUES ('Alice', 'Hello from the database!');
INSERT INTO messages (name, message) VALUES ('Bob', 'PHP in Termux is awesome!');
exit;
Connecting PHP to MariaDB
Now create a PHP page that reads from the database and displays the results:
nano ~/mywebsite/messages.php
<?php
$host = "localhost";
$dbname = "mywebsite";
$user = "root";
$pass = "";
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->query("SELECT * FROM messages ORDER BY created_at DESC");
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head><title>Messages</title></head>
<body style="font-family:monospace; background:#0d1117; color:#c9d1d9; padding:40px;">
<h1 style="color:#58a6ff;">Messages from Database</h1>
<?php foreach ($messages as $row): ?>
<div style="background:#161b22; padding:15px; margin:10px 0; border-radius:6px; border:1px solid #30363d;">
<strong style="color:#3fb950;"><?php echo htmlspecialchars($row['name']); ?></strong>
<p><?php echo htmlspecialchars($row['message']); ?></p>
<small style="color:#8b949e;"><?php echo $row['created_at']; ?></small>
</div>
<?php endforeach; ?>
</body>
</html>
Visit http://localhost:8080/messages.php in your browser. You should see both database records displayed on screen — PHP pulling live data from MariaDB and rendering it as HTML. This is the foundation of every dynamic website on the internet.
root with no password is fine for local development only. Never deploy a database to the internet without setting a strong password and creating a dedicated user with limited privileges.// 06 — Hosting Your PHP Site Over Your Local Network
One of the best features of running PHP in Termux is the ability to share your locally hosted PHP website with other devices on the same Wi-Fi network. This means you can view your site on a laptop, tablet, or another phone — great for testing responsive layouts or showing work-in-progress to someone nearby.
Finding Your Android Device's IP Address
First, you need to find your phone's local IP address on the network. Run this command in Termux:
ip addr show wlan0
Look for a line that says inet followed by an address like 192.168.1.x or 10.0.0.x. That's your local IP. Alternatively:
ifconfig 2>/dev/null | grep "inet " | grep -v 127.0.0.1
Starting the Server on All Interfaces
Make sure you started the PHP server with 0.0.0.0 as the host (not localhost or 127.0.0.1). The 0.0.0.0 binding is what allows other devices to connect:
cd ~/mywebsite
php -S 0.0.0.0:8080
Accessing the Site From Another Device
On any other device connected to the same Wi-Fi network, open a browser and navigate to:
http://YOUR_PHONE_IP:8080
Replace YOUR_PHONE_IP with the actual IP address you found (example: http://192.168.1.45:8080). Your PHP website should load on the other device just like it does on your phone.
Keeping the Server Running in the Background
To keep the PHP server alive while you use other apps, you can use Termux's wake lock feature. Swipe down on the Termux notification and tap Acquire wakelock. This prevents Android from killing the Termux session when your screen turns off.
For a more permanent solution, run the server with nohup so it continues even if the terminal session is interrupted:
nohup php -S 0.0.0.0:8080 > server.log 2>&1 &
The server output will be saved to server.log in your project directory. To stop the server later, find its process ID and kill it:
kill $(lsof -ti:8080)
// 07 — Common Errors and Fixes
Error: "Address already in use"
This means something is already using port 8080. Either kill the existing server using the command above, or switch to a different port like 8081:
php -S 0.0.0.0:8081
Error: "Could not connect to MariaDB"
MariaDB may not be running. Start it again:
mysqld_safe -u $(whoami) &
Wait 5 seconds and try your PHP page again.
Error: "php: command not found"
PHP isn't installed or the PATH isn't set. Re-install:
pkg install php -y
Browser Shows Raw PHP Code Instead of Rendered Page
You may have accidentally opened the file directly instead of through the server. Always access pages via http://localhost:8080, not by opening the file in a file manager.
Error: "PDO driver not found" or MySQLi Not Available
Some PHP extensions may need to be enabled. Check which extensions are loaded:
php -m | grep -i mysql
If nothing returns, try installing the PHP extension package:
pkg install php-apache -y
// 08 — Pro Tips for PHP Development in Termux
router.php file and start the server with php -S 0.0.0.0:8080 router.php. The router file handles all requests and lets you build custom URL structures.pkg install composer. This opens up thousands of PHP libraries including Laravel, Slim, and Guzzle.tail -f server.log in another to watch requests and errors live — just like a real server admin.ini_set('display_errors', 1); error_reporting(E_ALL); — it shows errors in the browser instead of blank pages.pkg install git and initialize a repo in your project folder. This lets you track changes and push your code to GitHub — great practice for any web developer.// 09 — PHP vs Node.js in Termux — Quick Comparison
| Feature | PHP in Termux | Node.js in Termux |
|---|---|---|
| Installation | pkg install php | pkg install nodejs |
| Built-in Web Server | ✅ Yes (php -S) | ❌ No (needs Express) |
| Database Support | MariaDB, SQLite | MariaDB, SQLite, MongoDB |
| Learning Curve | Beginner Friendly | Moderate |
| Performance | Good (PHP 8 JIT) | Excellent (async I/O) |
| Use Case | Websites, CMS, APIs | APIs, Real-time Apps |
| Composer / npm | Composer available | npm included |
| Root Required | ❌ No | ❌ No |
Both PHP and Node.js are excellent choices for local web development in Termux. PHP wins for beginners because it has a built-in server and its syntax closely mirrors what you'll find in WordPress and traditional hosting environments. Node.js wins for building real-time apps and APIs. For learning web fundamentals, start with PHP.
// FAQ — PHP in Termux
php --version to see the exact version on your device. PHP 8 includes significant performance improvements and modern language features.nohup method described in this guide and enable the Termux wakelock from the notification. Even then, Android may eventually stop the process to save battery.cloudflared or ngrok. Stay tuned to hydratermux.blogspot.com for a future tutorial on this exact topic.pkg install composer. Once installed, you can use it to install PHP libraries and frameworks exactly like you would on a regular Linux machine. This opens up access to tools like Laravel, Symfony, Guzzle, and thousands of others via Packagist.// 10 — Conclusion: Your Android Phone Is a Web Dev Machine
You've just built and hosted a real PHP website on an Android phone using Termux — and you did it without root, without a laptop, and without spending a single rupee on hosting. That's the power of PHP in Termux. What started as a few pkg commands turned into a live, database-connected web application accessible to every device on your network.
The skills you practiced in this guide are real, professional-grade skills. Writing PHP, querying a MariaDB database with PDO, managing a web server, debugging output — these are the exact things backend developers do every day. Your Android phone, running Termux, is a legitimate development environment. Don't underestimate it.
From here, the next steps are wide open: install Composer and explore PHP frameworks like Slim or Laravel, build a contact form that saves submissions to the database, add user authentication with sessions, or try exposing your local site to the internet using a tunnel service. Every one of those topics is covered or will be covered right here on this blog.
If this guide helped you get PHP running in Termux, share it with someone else who's trying to learn web development on Android. Drop your questions in the comments — every question helps make the next tutorial better. Keep building, keep experimenting, and remember: the best development environment is the one you actually have in your hands.
→ Suggested next read: How to Install and Use MariaDB in Termux — Full Database Guide (coming soon to HYDRA TERMUX)

Comments
Post a Comment