How to Install PHP in Termux — Run PHP & Build Web Apps on Android (2026)
- Introduction — PHP on Android?
- Why Learn PHP in Termux?
- Requirements Before You Start
- Step 1 — Install and Update Termux
- Step 2 — Install PHP in Termux
- Step 3 — Write and Run Your First PHP Script
- Step 4 — Start a Local PHP Web Server
- Step 5 — Create and Serve HTML Files
- Step 6 — Mix PHP and HTML Together
- Step 7 — Install Extra PHP Modules
- Step 8 — Install Composer and PHP Frameworks
- Troubleshooting Common Errors
- Pro Tips
- FAQ
- Conclusion
// 01 — Introduction: PHP on Your Android Phone?
Here is something most people do not realise: your Android phone is capable of running a complete PHP development environment. Not a stripped-down, limited version — a real, full PHP setup where you can write scripts, run a local web server, process forms, connect to databases, and even install entire frameworks like Laravel. All of this works inside Termux, and none of it requires rooting your device.
PHP is one of the most important languages in web development. It powers over 77% of all websites that use a server-side language — WordPress, Facebook's original backend, Wikipedia, and hundreds of millions of other sites around the world all run on PHP. If you want to get into web development, backend programming, or even just understand how the websites you use every day actually work, PHP is a fantastic starting point. And starting it in Termux on Android means you can learn and build anywhere, at any time, on the device you already carry with you.
This guide was written by Rixon Xavier at HYDRA TERMUX specifically for Android users who want to get into PHP without owning a laptop or paying for cloud hosting. Every single command is explained in plain language. Every step is tested. Whether you have never opened a terminal before or you are a developer looking to set up a quick mobile coding environment, this guide covers everything you need from the very beginning to running real PHP web applications on your phone.
By the time you finish reading and following along, you will have PHP installed, your first scripts running, a local web server serving pages in your mobile browser, PHP modules installed for extended functionality, and Composer set up so you can pull in any PHP package or framework you need. Let's get into it.
// 02 — Why Learn PHP in Termux?
Before diving into the commands, it is worth understanding why this matters and what you actually gain from running PHP inside Termux on Android.
The most obvious benefit is accessibility. Most PHP tutorials assume you are sitting in front of a laptop or desktop computer with XAMPP, WAMP, or some other local server stack installed. If you do not have that setup — if you only have an Android phone — those tutorials leave you out. Termux changes that entirely. Your Android phone becomes a real Linux environment, and PHP in Termux gives you the same capabilities you would have on any Linux development machine, just in your pocket.
There is also the learning angle. Running PHP from a command line in Termux teaches you things that GUI tools like XAMPP hide from you. You learn what a web server actually is, how PHP processes requests, what modules do, how Composer manages dependencies. This deeper understanding makes you a significantly better developer than someone who has only ever clicked buttons in a visual interface.
And practically speaking, PHP in Termux is genuinely useful for real work. You can test scripts on the go, build small tools and automations, prototype web applications, work on freelance projects during a commute, or practice for interviews anywhere. The development server PHP includes runs fine on Android hardware — modern phones are faster than many dedicated servers from ten years ago.
PHP also pairs extremely well with the other tools available in Termux. You can combine it with Git for version control, SQLite or MariaDB for databases, Composer for package management, and even connect it to Python or Node.js scripts if your project needs it. The Termux ecosystem is surprisingly complete for full-stack web development.
// 03 — Requirements Before You Start
Everything you need for this setup is free. Here is the full checklist before you run your first command:
| Requirement | Details |
|---|---|
| Android Version | Android 7.0 (Nougat) or higher |
| Termux App | Download from F-Droid only — NOT Google Play Store |
| Free Storage | At least 300 MB free (500 MB+ if installing Laravel) |
| Internet | Required during installation only |
| Root Access | Not required — works on any stock Android device |
| Experience Level | Beginner friendly — no prior experience needed |
The single most important item on that list is the Termux source. This point comes up in every HYDRA TERMUX guide and it always will — the Google Play Store version of Termux is abandoned and completely outdated. Its package repositories are broken and you will get errors trying to install anything modern. Delete it if you have it and install the correct version from F-Droid instead. The F-Droid version is actively maintained by the Termux team and gets regular updates.
Storage is the other thing to pay attention to. Basic PHP takes under 50 MB. Installing a few modules adds another 50 to 100 MB. If you plan to create a Laravel project, budget around 500 MB extra because Laravel pulls in a lot of dependencies. Check your available storage before starting and free up space if needed — you do not want an installation failing halfway through because the disk filled up.
// 04 — Step 1: Install and Update Termux
Go to f-droid.org in your Android browser and search for Termux. Download and install the APK. Android will ask you to allow installation from unknown sources — go to your settings, enable it for your browser, and proceed. Once installed, open Termux. You will see a black terminal screen with a command prompt. That is your Linux environment up and running on Android.
The very first thing to do before installing anything is update your package list and upgrade all existing packages. Skipping this step causes more failed installations than anything else. Do not skip it.
pkg update && pkg upgrade -y
What this does: pkg update contacts Termux's package servers and downloads the latest list of available software. pkg upgrade -y then upgrades every installed package to its current version. The -y flag answers yes automatically to any confirmation prompts so you do not have to keep pressing Enter.
This process takes anywhere from one to five minutes depending on your connection speed and how many packages need updating. You will see a lot of text scrolling by — that is completely normal. Do not interrupt it. If it asks whether to replace a configuration file, just press Enter to keep the existing one and let it continue. When you see the command prompt appear again at the bottom, the update is finished.
pkg update && pkg upgrade -y every time you open Termux before installing new software. It takes two minutes and prevents most installation errors before they happen.// 05 — Step 2: Install PHP in Termux
With your packages updated, installing PHP itself is a single command. Termux handles all the dependencies automatically — you do not have to manually install anything else for PHP to work.
Run the PHP Installation Command
Install PHP using Termux's package manager:
pkg install php -y
This downloads and installs PHP along with all necessary libraries. The installation typically takes thirty to sixty seconds. When it finishes you will see the command prompt again.
Verify the Installation Worked
Always confirm a successful install by checking the version:
php -v
You should see output that looks something like this:
PHP 8.3.x (cli) (built: Feb 2026)
Copyright (c) The PHP Group
Zend Engine v4.3.x
If you see a version number, PHP is installed and working correctly. The exact version will depend on what Termux's repository has available at the time you install, but as of 2026 it installs PHP 8.2 or 8.3 — both are modern, fully supported versions.
// 06 — Step 3: Write and Run Your First PHP Script
The best way to confirm PHP is working and to get comfortable with the workflow is to write and run an actual script. Termux comes with the nano text editor built in — it is simple, works completely in the terminal, and is perfect for editing code on a phone.
Create a PHP File with Nano
Open nano and create a new file called index.php:
nano index.php
The nano editor opens. Type the following PHP code:
<?php
echo "Hello, Termux!";
echo "\nPHP is running on Android!";
echo "\nCurrent time: " . date("Y-m-d H:i:s");
echo "\nPHP Version: " . PHP_VERSION;
?>
To save the file: press Ctrl + X, then press Y to confirm saving, then press Enter to keep the filename. You are back at the command prompt with your file saved.
Execute the PHP Script
Run your newly created PHP file:
php index.php
You should see output like this in your terminal:
Hello, Termux!
PHP is running on Android!
Current time: 2026-02-28 14:35:22
PHP Version: 8.3.x
There it is. Your first PHP script running on Android. Notice that date() returned the current time from your phone's clock and PHP_VERSION printed the exact version installed. These are real PHP functions working exactly as they would on any Linux server.
This command-line mode — running PHP scripts directly with php filename.php — is called the PHP CLI (Command Line Interface). It is incredibly useful for automation scripts, cron jobs, data processing, command-line tools, and testing code quickly without needing a browser. Many professional developers use it daily.
// 07 — Step 4: Start a Local PHP Web Server
PHP includes a built-in development web server that has been part of PHP since version 5.4. This is one of PHP's most convenient features for developers — you can spin up a web server instantly without installing Apache or Nginx. On Termux, this means you can serve web pages directly from your Android phone and view them in your mobile browser.
Start the PHP Development Server
Launch the built-in server on port 8080:
php -S localhost:8080
You will see this output in Termux:
PHP 8.3.x Development Server (http://localhost:8080) started
Press Ctrl-C to quit.
The server is now running. Open your Android browser — Chrome, Firefox, Brave, whatever you prefer — and go to:
http://localhost:8080
You should see the output of your index.php file rendered in the browser. This is a real web server request — your browser sent an HTTP request to localhost:8080, PHP processed the script, and returned the response. The same thing that happens on every website you visit, running entirely on your phone.
Stop the Server When Done
When you are finished testing, go back to the Termux session running the server and press:
Ctrl + C
This stops the server cleanly. You can restart it any time by running the same php -S localhost:8080 command.
// 08 — Step 5: Create and Serve HTML Files
The PHP built-in server does not just serve PHP files. It serves any file you point it at — HTML, CSS, JavaScript, images. This makes it a complete local development server for web projects of all kinds.
Create an HTML File
Create a new HTML file in the same directory:
nano index.html
Type this HTML inside the editor:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Termux Web Page</title>
<style>
body {
background: #0a0e0f;
color: #00ff88;
font-family: monospace;
text-align: center;
padding: 40px;
}
h1 { font-size: 2rem; margin-bottom: 16px; }
p { color: #6b8a94; }
</style>
</head>
<body>
<h1>Hello from Termux! 🚀</h1>
<p>This HTML page is being served by PHP on Android.</p>
<p>No laptop. No cloud. Just your phone.</p>
</body>
</html>
Save with Ctrl + X → Y → Enter. Start the server if it is not already running, then visit http://localhost:8080/index.html in your browser. You will see your styled HTML page rendered perfectly in your mobile browser — served locally from your phone.
// 09 — Step 6: Mix PHP and HTML Together
The real power of PHP comes from combining it with HTML. PHP files can contain both server-side PHP code and regular HTML markup in the same file. The PHP code runs on the server side and generates HTML output, which gets sent to the browser. This is how virtually every dynamic website works.
nano page.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP + HTML in Termux</title>
<style>
body { background:#0a0e0f; color:#c8d8dc;
font-family:monospace; padding:30px; }
h1 { color:#00ff88; }
.info{ background:#141b1d; border-left:3px solid #00ff88;
padding:14px 18px; border-radius:4px; margin:16px 0; }
</style>
</head>
<body>
<h1>Dynamic PHP Page</h1>
<?php
$author = "Rixon Xavier";
$blog = "HYDRA TERMUX";
$datetime = date("Y-m-d H:i:s");
$version = PHP_VERSION;
$os = php_uname('s');
?>
<div class="info">
<p><strong>Author:</strong> <?php echo $author; ?></p>
<p><strong>Blog:</strong> <?php echo $blog; ?></p>
<p><strong>Server Time:</strong> <?php echo $datetime; ?></p>
<p><strong>PHP Version:</strong> <?php echo $version; ?></p>
<p><strong>Running On:</strong> <?php echo $os; ?></p>
</div>
<p>This content was generated dynamically by PHP — not written statically in HTML.</p>
</body>
</html>
Save the file, start the server, and visit http://localhost:8080/page.php in your browser. You will see a styled page with live data — the current time, your PHP version, and the OS string — all generated by PHP at the moment the page was requested. Every time you reload the page the time updates. That is dynamic, server-side PHP working exactly as intended.
You can now visit all three files through the same server:
http://localhost:8080/index.html ← Static HTML page
http://localhost:8080/index.php ← PHP CLI output in browser
http://localhost:8080/page.php ← Dynamic PHP + HTML page
// 10 — Step 7: Install Extra PHP Modules
The base PHP installation that comes from pkg install php is solid and covers most common use cases. But as your projects grow — especially if you start working with APIs, image processing, databases, or international text — you will need additional PHP modules. Termux makes this easy.
pkg install php-gd php-curl php-mbstring php-xml php-sqlite -y
Here is what each module does and when you actually need it:
| Module | What It Does | When You Need It |
|---|---|---|
| php-gd | Image creation, editing, resizing in PHP | Image upload features, thumbnails, captchas |
| php-curl | Make HTTP requests to external URLs and APIs | REST APIs, webhooks, web scraping |
| php-mbstring | Multi-byte string support for Unicode text | Non-English languages, emoji, special characters |
| php-xml | Parse and generate XML data | RSS feeds, SOAP web services, XML configs |
| php-sqlite | SQLite database support built into PHP | Lightweight database without needing MySQL |
To see a complete list of all PHP modules currently loaded on your installation, run:
php -m
This prints every loaded extension. If a module you need is missing, check whether Termux has a package for it with pkg search php to see the full list of available PHP packages.
php --ini to find the location of your PHP configuration file. You can edit it with nano to adjust settings like memory limits, file upload sizes, and error reporting levels.// 11 — Step 8: Install Composer and PHP Frameworks
Composer is the standard dependency manager for PHP. It is to PHP what npm is to JavaScript or pip is to Python. With Composer you can install any of the hundreds of thousands of PHP packages from Packagist — the main PHP package registry — with a single command. This includes entire frameworks like Laravel, Symfony, and CodeIgniter.
Install Composer in Termux
Termux has Composer in its package repository, so installation is simple:
pkg install composer -y
Verify Composer installed correctly:
composer --version
Composer version 2.x.x
Create a New Laravel Project (Optional)
With Composer installed, you can create a complete Laravel application on your Android phone. Laravel is the most popular PHP framework in the world and is used by companies ranging from small startups to large enterprises.
# Create a new Laravel project named myapp
composer create-project laravel/laravel myapp
# Navigate into the project folder
cd myapp
# Start the Laravel development server
php artisan serve --host=localhost --port=8080
Open http://localhost:8080 in your Android browser and you will see the Laravel welcome page — a complete MVC PHP framework running on your phone. The php artisan serve command is Laravel's wrapper around PHP's built-in server, configured specifically for the Laravel project structure.
Install Individual Composer Packages
You do not have to install a full framework to use Composer. You can add individual packages to any project. For example, to add the popular Guzzle HTTP client to a project:
# Initialize a new project
composer init
# Add a specific package (example: Guzzle HTTP client)
composer require guzzlehttp/guzzle
# Add a dev-only package (example: PHPUnit testing)
composer require --dev phpunit/phpunit
// 12 — Troubleshooting Common Errors
pkg install php fails with "unable to locate package"
You are almost certainly using the outdated Play Store version of Termux. Uninstall it, download the F-Droid version from f-droid.org, install it, run pkg update && pkg upgrade -y, then try again.
PHP server starts but browser says "connection refused"
Make sure you are typing the URL exactly as http://localhost:8080 — not https, not just localhost without the port. Also check that the PHP server is still running in Termux (you should see the "Development Server started" message in that session).
php: command not found after installation
Close Termux completely and reopen it. Sometimes the PATH does not refresh in the same session after installing a new package. Reopening Termux fixes this almost every time.
# If still not found after reopening, reinstall PHP
pkg reinstall php -y
Composer create-project fails or hangs
This is usually a network timeout or storage issue. Check your storage with df -h. Make sure you are on WiFi. If the download keeps timing out, try setting Composer's memory limit and timeout:
COMPOSER_MEMORY_LIMIT=-1 composer create-project laravel/laravel myapp
Port 8080 already in use
Another process is using port 8080. Simply start PHP's server on a different port number:
php -S localhost:9000
// 13 — Pro Tips for PHP in Termux
-t flag to serve files from a specific folder without navigating into it first. For example: php -S localhost:8080 -t /sdcard/myprojectifconfig in Termux. Then start the server with php -S 0.0.0.0:8080. Other devices on your WiFi network can now access your PHP server using your phone's IP address./sdcard/PhpProjects/ so they are accessible from your Android file manager and backed up with your photos. Files stored inside Termux's internal directory are lost if you reinstall Termux.pkg install mariadb -y. This gives you a full LAMP-style stack — Linux, Apache equivalent (PHP server), MySQL, PHP — all running on your Android phone.pkg upgrade php -y regularly to stay on the latest PHP version. Security patches and performance improvements come out frequently and the Termux repository is updated quickly.// FAQ
php -v.pkg install mariadb -y. MariaDB is a drop-in replacement for MySQL and is fully compatible with PHP's PDO and MySQLi extensions. Once both are running you can build complete PHP applications with a proper relational database entirely on your Android phone.ifconfig in Termux. Then start the PHP server with php -S 0.0.0.0:8080 instead of localhost. Any device on the same WiFi network can then access your PHP server at http://YOUR_PHONE_IP:8080. This is great for testing on multiple devices simultaneously.pkg update && pkg upgrade -y first, then install PHP. That sequence works every time on the correct Termux version.php script.php and see the output in the terminal. The PHP built-in web server handles HTTP requests from a browser — it listens on a port, receives browser requests, runs the matching PHP file, and sends back HTML. Both are part of the same PHP installation and are useful for different purposes.// 15 — Conclusion
That is the complete picture — from downloading the right version of Termux all the way to running Laravel on your Android phone. You now have a proper PHP development environment that most people would not believe is running on a mobile device. No root. No laptop. No cloud subscription. Just Termux and a few minutes of setup.
What you do with it from here is entirely up to you. If you are just starting out with PHP, spend time writing scripts and testing things in the CLI mode first — it is the fastest way to learn the language itself without worrying about HTML and browsers. Once you are comfortable with variables, functions, loops, and arrays, move to building simple web pages with the built-in server. Then tackle forms, sessions, and database queries. Then frameworks.
If you are already an experienced developer, you have a surprisingly capable mobile coding environment now. The PHP server is fast enough for real development work on modern Android hardware. Composer gives you the full Packagist ecosystem. Git is available in Termux for version control. You can genuinely do real work from your phone.
For more guides like this one — covering PHP databases, building real apps, combining PHP with Python scripts, and setting up full web stacks in Termux — keep checking hydratermux.blogspot.com. New tutorials from Rixon Xavier are published every week covering everything from beginner commands to advanced Android Linux setups.
php -S localhost:8080, write your scripts with nano, and build something real. Happy coding!
Comments
Post a Comment