A complete web server stack running on Android that combines Nginx with PHP to serve dynamic websites directly from your mobile device.
Nginx (pronounced “engine-x”) is an open-source web server that also functions as a reverse proxy, load balancer, and HTTP cache. Originally created by Igor Sysoev in 2004, Nginx has grown to become one of the most popular web servers in the world, powering over 40% of the top 10,000 websites.
Before we dive into the technical details, let’s explore why you might want to run a web server on your Android device:
First, launch Termux and ensure your package repositories are up to date:
pkg update && pkg upgrade
This command updates the package lists and upgrades installed packages to their latest versions. Regular updates are crucial for security and stability.
Install Nginx, PHP, and PHP-FPM with this single command:
pkg install nginx php php-fpm
These packages include:
PHP-FPM (FastCGI Process Manager) is crucial for processing PHP files. Let’s configure it properly:
nano $PREFIX/etc/php-fpm.d/www.conf
Look for these configuration lines and modify them as follows:
;user = nobody
;group = nobody
listen = /data/data/com.termux/files/usr/var/run/php-fpm.sock
;listen.owner = nobody
;listen.group = nobody
listen.mode = 0660
These changes ensure proper communication between Nginx and PHP-FPM through a socket file with correct permissions.
Now, let’s set up Nginx to serve our web files and process PHP requests:
nano $PREFIX/etc/nginx/nginx.conf
Replace the entire file content with this optimized configuration:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
root /data/data/com.termux/files/usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/data/data/com.termux/files/usr/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
This configuration:
Let’s create the web directory and some test files to verify our setup:
# Create the web directory if it doesn't exist
mkdir -p $PREFIX/share/nginx/html
# Create a PHP info file to verify PHP is working
echo "<?php phpinfo(); ?>" > $PREFIX/share/nginx/html/info.php
# Create a simple HTML homepage
cat > $PREFIX/share/nginx/html/index.html << EOF
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Termux Server</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1 { color: #333; }
</style>
</head>
<body>
<h1>Nginx is working on Termux!</h1>
<p>Your web server is successfully running on Android.</p>
<p><a href="info.php">Check PHP information</a></p>
</body>
</html>
EOF
Now let’s start both PHP-FPM and Nginx:
# Start PHP-FPM in the background
php-fpm
# Start Nginx
nginx
Open your Android browser and navigate to:
If you can see these pages, congratulations! You’ve successfully set up a web server on your Android device.
To make it easier to start your web server, create a startup script:
nano ~/start-web.sh
#!/bin/bash
echo "Starting PHP-FPM..."
php-fpm
echo "Starting Nginx..."
nginx
echo "Web server started on port 8080"
echo "Access your server at: http://localhost:8080"
chmod +x ~/start-web.sh
Now you can start your server with just:
~/start-web.sh
Depending on your project needs, you might want to install additional PHP extensions:
pkg install php-mysqli php-curl php-gd php-mbstring php-xml
These extensions add support for:
· MySQL databases
· HTTP requests
· Image manipulation
· Multibyte string handling
· XML processing
You can modify PHP settings by editing the php.ini file:
nano $PREFIX/etc/php.ini
Common settings to adjust include:
· memory_limit = 128M – Increase memory allocation
· upload_max_filesize = 32M – Allow larger file uploads
· post_max_size = 32M – Increase maximum POST size
· display_errors = On – Show errors during development
If either service fails to start, check the error logs:
# Check Nginx errors
tail -f $PREFIX/var/log/nginx/error.log
# Check PHP-FPM errors
tail -f $PREFIX/var/log/php-fpm.log
If port 8080 is already in use, you can change it in the Nginx configuration:
nano $PREFIX/etc/nginx/nginx.conf
Change the listen directive to a different port (e.g., 8081).
If you encounter permission issues, ensure the web directory has correct permissions:
chmod 755 $PREFIX/share/nginx/html
chmod 644 $PREFIX/share/nginx/html/*
While your Termux web server is primarily for development, it’s good practice to follow basic security:
Now that you have a working web server, here’s what you can do:
Setting up Nginx and PHP in Termux transforms your Android device into a portable web development environment. While it’s not meant for production hosting, it provides an excellent platform for learning, testing, and development on the go.
The process might seem technical at first, but following these steps will give you a fully functional web stack that rivals what you’d have on a desktop computer. The best part is that it fits in your pocket and goes wherever you do!
Have you set up a web server on your Android device? What projects are you planning to build? Share your experiences in the comments below!
Happy coding on the go! 🚀