PHP in Termux isn’t a hack—it’s what happens when FOSS meets hustle.
I was stuck in traffic one rainy Tuesday with nothing but my phone and an idea I couldn’t shake. No laptop. No dev environment. Just raw brainpower and a decent data plan. That’s when I remembered Termux—a terminal emulator I’d installed out of curiosity. Within minutes, I had PHP in Termux running and was coding right from the back of a jeepney.
This isn’t just a party trick—it’s FOSS power in your pocket. Whether you’re a student, freelancer, or digital nomad, you can turn downtime into dev time with just a few commands.
In this post, I’ll show you exactly how to get started with PHP in Termux so you can break free from your desk and code on your own terms—the open source way. Ready to go mobile? Read on.
Download my FREE Termux Cheat Sheet Now!
- Why Use PHP in Termux?
- Understanding Termux: What Sets It Apart from Linux Terminals?
- Installation of PHP in Termux: A Step-by-Step Primer
- PHP for CLI vs. Web Development
- Composer: Managing Dependencies the FOSS Way
- Troubleshooting Common PHP in Termux Issues
- Security Note: PHP in Termux for Development Only
- Go Mobile, Stay Open
Why Use PHP in Termux?
PHP is a popular server-side scripting language widely used for web development. By installing PHP in Termux, you can develop and test your PHP scripts directly on your Android device. This can be particularly useful for developers who want to code on the move or learn PHP without needing a full-blown server setup.
- Portability: Your entire development environment fits in your pocket, allowing you to write and test PHP code on the move.
- Complete Linux Experience: Termux provides a full Linux environment, meaning you have access to essential development tools like Git, MySQL, and Apache, all from your mobile device.
- Lightweight: Running PHP in Termux is resource-efficient, making it ideal for Android devices with limited processing power.
- Flexibility: Whether you’re developing small scripts, dynamic websites, or complex PHP applications, Termux provides a versatile platform to handle it all.
Use Cases for PHP in Termux:
- Mobile Web Development: With PHP in Termux, you can create and test PHP-based web applications on your Android device, turning your phone into a local development server.
- PHP Scripting: Automate tasks and run PHP scripts directly from your mobile terminal, giving you the flexibility to work outside of a traditional desktop setup.
- Learning and Experimentation: If you’re learning PHP, Termux provides a quick and easy way to practice coding on the go without needing access to a full computer.
- Server Management: Manage and develop websites on the go, using tools like PHP and MySQL in Termux to handle your backend development needs.
With PHP in Termux, you’re not just limited to writing code—you can run full-fledged PHP environments, install popular PHP frameworks, and even connect to external servers via SSH. This guide will help you get started by showing you how to install and configure PHP, giving you a fully functioning PHP environment on your Android device.
· · ─ ·𖥸· ─ · ·
Understanding Termux: What Sets It Apart from Linux Terminals?
Termux is different from a traditional Linux environment in that it’s lightweight and doesn’t come with a full desktop environment, making it perfect for mobile development. You install packages like you would on a standard Linux terminal, but instead of the usual /usr/bin
directory, Termux stores installed packages under $PREFIX
, which is typically /data/data/com.termux/files/usr/
. This is important to remember when accessing or managing your files in Termux, as file paths may differ from what you’re used to on a regular Linux machine.
Installation of PHP in Termux: A Step-by-Step Primer
To get started with PHP development on your Android device using Termux, you’ll need to install a few essential tools. This primer walks you through the steps to set up PHP and Composer, enabling you to run and manage PHP scripts directly from your mobile terminal. Whether you’re building small automation tasks or testing web applications locally, this guide will equip you with everything you need to begin coding in PHP right on your phone or tablet. Let’s dive into the installation procedure.
Prerequisites
Before you start, ensure that you have the following:
- An Android device with Termux installed.
- A stable internet connection.
- Basic knowledge of using the terminal.
Step 1: Update and Upgrade Termux Packages
First, it’s always a good idea to update and upgrade the packages in Termux to ensure you have the latest versions. Open Termux and enter the following command:
pkg update && pkg upgrade
This command updates the package list and upgrades all installed packages to their latest versions.
Step 2: Install PHP in Termux
Now, let’s install PHP in Termux. Type the following command in the Termux terminal:
pkg install php
Termux will fetch and install the PHP package along with its dependencies. Once the installation is complete, you can verify the installation by checking the PHP version:
php -v
This should display the installed PHP version, confirming that PHP is now ready to use.
Step 3: Configure PHP for Development
With PHP installed, you might want to configure it to suit your development needs. This involves setting up some essential configurations.
a. Creating a PHP Development Directory
It’s a good practice to organize your PHP scripts in a dedicated directory. Let’s create one:
mkdir ~/php-projects
cd ~/php-projects
You can now store all your PHP projects in this directory.
b. Configuring PHP INI Settings
PHP’s behavior can be customized using the php.ini
file. Termux’s PHP installation comes with a default php.ini
configuration file, but you can create a custom one for your projects.
Also: PHP Configuration Tips and Tricks
First, locate the php.ini
file:
php --ini
To create a custom php.ini
, you can copy the default configuration:
cp $PREFIX/etc/php.ini ~/php-projects/php.ini
Edit the file using any text editor available in Termux, such as nano
:
nano ~/php-projects/php.ini
Make the necessary adjustments based on your development requirements, such as enabling error reporting or adjusting memory limits.
Step 4: Running PHP Scripts in Termux
You can now run PHP scripts directly from Termux. Navigate to your PHP project directory and create a simple PHP script:
echo "<?php echo 'Hello, PHP in Termux!'; ?>" > hello.php
Run the script using the following command:
php hello.php
If everything is set up correctly, you should see the output:
Hello, PHP in Termux!
Step 5: Setting Up a Local PHP Server (Optional)
Termux allows you to run a local PHP server, making it easier to test web applications. To start a PHP server, use the built-in command:
php -S localhost:8000
This command starts a local server at http://localhost:8000
. You can access this URL from your mobile browser to test your PHP applications.
· · ─ ·𖥸· ─ · ·
PHP for CLI vs. Web Development
PHP can be used for more than just web development—it’s also great for writing command-line scripts. There’s a significant difference between using PHP in the Command Line Interface (CLI) and running it as a web server.
CLI PHP
You write PHP scripts that are executed directly from the command line. This is useful for small automation tasks, creating cron jobs, or processing data offline. For example, you could create a simple PHP script to back up files:
pkg install curl php
curl -sS https://getcomposer.org/installer | php
mv composer.phar $PREFIX/bin/composer
Run it with: bashCopyEdit
php backup.php
Web PHP
Running PHP via php -S
starts a built-in server to host web pages. This is perfect for testing simple dynamic websites directly from your device. For example, to serve a PHP script locally:
php -S localhost:8000
You can now open a browser and visit http://localhost:8000
to view your PHP scripts in action.
· · ─ ·𖥸· ─ · ·
Composer: Managing Dependencies the FOSS Way
Composer is an essential tool for modern PHP development. It allows you to manage dependencies, meaning you can easily download and update libraries for your project. This is incredibly useful because it eliminates the need to manually download or include PHP libraries in your projects. For example, you can use Composer to install libraries like Laravel, Symfony, or Guzzle for API requests.
To install Composer on Termux, use the following steps:
pkg install curl php
curl -sS https://getcomposer.org/installer | php
mv composer.phar $PREFIX/bin/composer
Once installed, you can check if it works by running:
composer --version
Now, you can use Composer to install libraries by navigating to your project folder and running:
composer install
This automatically fetches the libraries specified in your composer.json
file.
· · ─ ·𖥸· ─ · ·
Troubleshooting Common PHP in Termux Issues
Even though PHP in Termux is pretty straightforward, you might run into some common issues:
Permission Denied Errors
You might get errors like “permission denied” when trying to execute PHP scripts. This usually happens if the script or directory doesn’t have the right permissions. Use chmod
to adjust file permissions, like so:
chmod +x script.php
This grants execution permissions to the PHP script.
Missing Extensions
Termux’s PHP installation doesn’t come with every extension by default. For example, if you need php-curl
for making HTTP requests, you can install it with:
pkg install php-curl
Out of Memory Errors
PHP in Termux can sometimes run into memory limits, especially if you’re working on large projects or handling lots of data. To solve this, you can break your tasks into smaller scripts or adjust Termux’s memory limits by using ulimit
.
ulimit
· · ─ ·𖥸· ─ · ·
Security Note: PHP in Termux for Development Only
Running php -S
is ideal for development, but exposing it to the internet can be risky. Termux’s built-in PHP server is meant for local use and is not optimized for security or performance. If you try to expose it to the internet (even via Wi-Fi), anyone on the same network can access your scripts unless you implement additional security layers like firewalls or basic authentication.
For example, while you can run a PHP server like this:
php -S 0.0.0.0:8000
This would make your server accessible to anyone on the same network. Instead, to avoid unnecessary exposure, use localhost
or 127.0.0.1
when testing locally:
php -S localhost:8000
Only expose your PHP server to the public once you’re fully aware of the security considerations and have implemented necessary protections.
· · ─ ·𖥸· ─ · ·
Go Mobile, Stay Open
Coding doesn’t have to be tied to a desk or an expensive machine. With PHP in Termux, you’re embracing the freedom and flexibility that FOSS tools provide—building, testing, and deploying from anywhere, anytime.
Whether you’re optimizing your workflows or just experimenting, this setup proves you don’t need proprietary bloat to be productive.
· · ─ ·𖥸· ─ · ·
If this guide helped unlock a new way of working, subscribe to DevDigest—my free weekly newsletter where I share more open-source how-tos, clever Termux tricks, and dev tools that empower you to do more with less.
Leave a Reply