Easymux
Termux – Android – Linux – Windows

How To Install And Setup NGINX With PHP In Termux

A complete web server stack running on Android that combines Nginx with PHP to serve dynamic websites directly from your mobile device.

What is Nginx?

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.

Why Set Up a Web Server on Android?

Before we dive into the technical details, let’s explore why you might want to run a web server on your Android device:

  • · Mobile Development Environment: Code and test websites anywhere
  • · Educational Purposes: Learn web development without a computer
  • · Portable Testing: Test websites across different networks
  • · Personal Projects: Host small applications for personal use
  • · Offline Development: Work on web projects without internet access

Prerequisites

  • · Android device (version 5.0 or higher)
  • · Termux app (available from F-Droid or Github)
  • · Stable internet connection for downloading packages
  • · At least 500MB of free storage

Step-by-Step Installation Guide

Update and Upgrade Termux

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 Required Package

Install Nginx, PHP, and PHP-FPM with this single command:

pkg install nginx php php-fpm

These packages include:

  • · nginx: A high-performance web server
  • · php: The PHP scripting language
  • · php-fpm: PHP FastCGI Process Manager for handling PHP requests

Configure PHP-FPM

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.

Configure Nginx

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:

  • · Sets Nginx to listen on port 8080 (standard HTTP ports require root access)
  • · Defines the web root directory
  • · Configures proper handling of PHP files through the PHP-FPM socket

Create Web Directory and Test Files

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

Start the Services

Now let’s start both PHP-FPM and Nginx:

# Start PHP-FPM in the background
php-fpm

# Start Nginx
nginx

Test Your Web Server

Open your Android browser and navigate to:

  • · http://localhost:8080 – to see the HTML welcome page
  • · http://localhost:8080/info.php – to view PHP configuration details

If you can see these pages, congratulations! You’ve successfully set up a web server on your Android device.

Advanced Configuration

Creating a Startup Script

To make it easier to start your web server, create a startup script:

nano ~/start-web.sh

Add the following content:

#!/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"

Make the script executable:

chmod +x ~/start-web.sh

Now you can start your server with just:

~/start-web.sh

Installing Additional PHP Extensions

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

Customizing PHP Settings

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

Troubleshooting Common Issues

Service Won’t Start

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

Port Already in Use

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).

Permission Issues

If you encounter permission issues, ensure the web directory has correct permissions:

chmod 755 $PREFIX/share/nginx/html
chmod 644 $PREFIX/share/nginx/html/*

Security Considerations

While your Termux web server is primarily for development, it’s good practice to follow basic security:

  1. Don’t expose to public networks – Keep your server accessible only locally
  2. Use strong passwords – If you add authentication, use secure credentials
  3. Regular updates – Keep Termux and all packages updated
  4. Remove test files – Delete info.php after testing your setup

Practical Applications

Now that you have a working web server, here’s what you can do:

  1. Develop WordPress sites – Install WordPress for mobile development
  2. Test web applications – Try out frameworks like Laravel or CodeIgniter
  3. Learn PHP programming – Practice coding anywhere
  4. Host a personal blog – Use simple CMS systems like Grav or Kirby
  5. Create API endpoints – Develop and test APIs directly from your phone

Conclusion

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! 🚀


Categories Termux