Making a web server from an old Android smartphone, without root

The main idea was to check whether it was possible to make a “full-fledged” server with nginx, php-fpm, MariaDB from an old Android device and run WordPress on it. It turns out that it is possible and with good results.

Device: Xiaomi Mi 4c 2015, unlocked bootloader, TWRP, Android 10 (Havoc OS 3.0), Gapps were not installed to save resources, no root.

First of all, let’s go here https://f-droid.org/en/packages/com.termux/ and download termux. Termux is a terminal emulator with a minimal Linux environment. The device requires Android 7 or higher.

Further, for convenience, we will work via SSH. Installing OpenSSH

pkg install openssh

After installation, the program will prompt you to set a password. Launch OpenSSH

sshd

On my computer, I prefer to use PuTTY as a client and WinSCP for SFTP. If you know the ip of your phone, connect via PuTTY via port 8022. If you don’t know, enter it in Termux ip a

PuTTY

PuTTY

Now it will be more convenient for us to enter commands. Install Nginx.

pkg install nginx

At this stage, you will already be able to host static HTML pages on your device. To start the server, enter:

nginx

Now by entering in the browser the IP address of your device indicating the port 8080 (For example 192.168.1.17:8080) you will see the Nginx welcome page. Website files are located on your device at /data/data/com.termux/files/usr/share/nginx/html
To access, connect via WinSCP or FileZilla using 8022 port

Install php-fpm

pkg install php-fpm

At this stage you need to configure nginx to work with php-fpm, without this nothing will work. Opening nginx.conf , which is located at /data/data/com.termux/files/usr/etc/nginx , go down to line 45 and bring it to this form:

    index  index.html index.htm index.php;

Next, go down to line 65 and bring the next 7 lines to the following form:

 location ~ \.php$ {
            root           /data/data/com.termux/files/usr/share/nginx/html;
            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;
             include        fastcgi_params;
        }

We have “included” the section responsible for working with phpindicated for FastCGI way to php-fpm socket and specified correct path to scripts. Below will be my full config file in case I missed something.

My nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /data/data/com.termux/files/usr/share/nginx/html;
            index  index.html index.htm index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /data/data/com.termux/files/usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /data/data/com.termux/files/usr/share/nginx/html;
            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;
             include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

Let’s launch php-fpm and restart nginxif there are errors you will see them in the console

nginx -s reload
php-fpm

Let’s check if it’s working php. Create a file in the folder with the site info.php and write the code below into it and save it.

<?php
phpinfo();
?>

Now if php earned when typing in the browser http://yourIP:8080/info.php we will see the following

info.php

info.php

All that remains is to install the SQL database MariaDB. Enter:

pkg install mariadb

Here everything is installed straight away, by default the database is immediately created test which we will use for our WordPress. Launch MariaDB with the following command:

mysqld

If everything went well, we will see the following message in the terminal:

Version: '10.11.4-MariaDB'  socket: '/data/data/com.termux/files/usr/var/run/mysqld.sock'  port: 3306  MariaDB Server

Now we delete everything from the folder /data/data/com.termux/files/usr/share/nginx/html and unpack the archive from WordPress. Let’s go to the address http://yourIP:8080/ , there we will be asked to enter data to connect to the database:

Database name: test
Username: whatever, as long as it’s important Latin letters
Password: leave it blank
Database server name: 0.0.0.0

All this can be changed in the wp-config.php file manually

/** Имя базы данных для WordPress */
define( 'DB_NAME', 'test' );

/** Имя пользователя базы данных */
define( 'DB_USER', 'goodboy' );

/** Пароль к базе данных */
define( 'DB_PASSWORD', '' );

/** Имя сервера базы данных */
define( 'DB_HOST', '0.0.0.0' );

After this there will be several more pages with settings and you will be taken to the main page of your site

What’s the result? As a result, we received a web server that can be used for training, development and other experiments. It can even be used for hosting if you have an IP address allocated by your provider. It works quite quickly, there is a good reserve of processor power, but the RAM is limited.

This article describes only the most basic setup. When you exit termux, all services and programs launched through it continue to work. After rebooting the device, they need to be started again. Here are some more useful terminal commands:

ss -tl     (Открытые порты)
pkg list-installed (Список установленных пакетов)
top (Монитор процессов)
free -m (Информация о свободной оперативной памяти)
ps aux (Список процессов с pid)
kill (Убить процесс по pid)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *