Skip to content

Instantly share code, notes, and snippets.

@rezamarzban
Last active September 25, 2025 09:30
Show Gist options
  • Select an option

  • Save rezamarzban/b67cd07f3cbd2c224c28bf181840fbfa to your computer and use it in GitHub Desktop.

Select an option

Save rezamarzban/b67cd07f3cbd2c224c28bf181840fbfa to your computer and use it in GitHub Desktop.

To run Nginx and PHP in Termux, you can install both from the Termux package repository and configure them to work together using PHP-FPM. Here is a summary of the key steps:

Installing Nginx in Termux

  • Install Nginx with the command: pkg install nginx
  • Nginx files are located in $PREFIX/share/nginx/ or /data/data/com.termux/files/usr/share/nginx/
  • Start Nginx server by running: nginx
  • Default web files go to $PREFIX/share/nginx/html
  • You can access the server locally at localhost:8080

Installing PHP in Termux

  • Install PHP by running: pkg install php
  • Confirm installation with php --version
  • PHP-FPM (FastCGI Process Manager) is included to handle PHP requests efficiently

Configuring Nginx with PHP-FPM

  • Configure Nginx to pass PHP files to PHP-FPM via a Unix socket.
  • Modify the Nginx config file ($PREFIX/etc/nginx/nginx.conf) to include a location block for PHP processing:
server {
    listen 8080;
    server_name localhost;
    root /data/data/com.termux/files/usr/share/nginx/html;
    index index.php index.html;

    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;
    }
}
  • Create a PHP test file info.php in the web root with <?php phpinfo(); ?> to verify PHP is working.

Running the Server

  • Start PHP-FPM with: php-fpm
  • Start or reload Nginx with: nginx or nginx -s reload
  • Access your site at localhost:8080 and test PHP script at localhost:8080/info.php

This setup allows you to host dynamic PHP-based websites directly from your Android device using Termux with Nginx as the web server and PHP-FPM to process PHP scripts.[1][2][3][4]

If desired, port forwarding tools like Piggy or Ngrok can make your server accessible over the internet. Would you like detailed commands or a script to automate this setup?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

#!/data/data/com.termux/files/usr/bin/bash
# Update and upgrade Termux packages
pkg update -y && pkg upgrade -y
# Install necessary packages
pkg install nginx php php-fpm nano -y # nano for editing if needed, but we'll automate configs
# Configure PHP-FPM
PHP_FPM_CONF="$PREFIX/etc/php-fpm.d/www.conf"
sed -i 's/listen = 127.0.0.1:9000/listen = \/data\/data\/com.termux\/files\/usr\/var\/run\/php-fpm.sock/g' $PHP_FPM_CONF
sed -i 's/^listen.owner = nobody/;listen.owner = nobody/g' $PHP_FPM_CONF
sed -i 's/^listen.group = nobody/;listen.group = nobody/g' $PHP_FPM_CONF
sed -i 's/;listen.mode = 0660/listen.mode = 0666/g' $PHP_FPM_CONF
# Configure Nginx
NGINX_CONF="$PREFIX/etc/nginx/nginx.conf"
cat > $NGINX_CONF << EOF
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;
}
}
}
EOF
# Set up web directory and test files
WEB_DIR="$PREFIX/share/nginx/html"
mkdir -p $WEB_DIR
echo "<?php phpinfo(); ?>" > $WEB_DIR/info.php
cat > $WEB_DIR/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
# Start services
pkill php-fpm # Stop if already running
pkill nginx
php-fpm &
nginx
echo "Setup complete! Access the server at http://localhost:8080"
echo "To stop: pkill php-fpm && nginx -s stop"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment