Transform Your Old Android Phone into a Home Server

huaxiaorong

Reading time required

8

minutes

Speed read only takes 3 minutes

Link: https://juejin.cn/post/7440377607000326207

Tools Preparation

  1. Android phone: used for transformation as a server
  2. Other devices: used for testing to see server access webpage, can be other phones, computers, platforms.
  3. Bluetooth keyboard: convenient to connect to the Android phone for command processing

Due to Python’s convenient and rich tool packages, the main idea is to use Termux to enable the phone to run Python scripts, leveraging Python program server capabilities, and implementing user login, access, upload, and download functions through the interface (webpage).

Environment Preparation

Install Termux

  1. Install Termux through GitHub or APKFab app store.
  2. Update and install basic software packages
pkg update && pkg upgrade -y
pkg install wget curl nano -y

Install Apache Support

Apache is a powerful web server suitable for handling dynamic and static content.

  1. Install Apache

pkg install apache2 -y

  1. Start Apache

apachectl start By default, Apache runs on port 8080.

You can access http://127.0.0.1:8080 in the browser, and if you see Apache’s default “test page”, it means the server started successfully. Where ‘127.0.0.1’ is the default server address.

3. Stop and restart Apache

# Stop Apache
apachectl stop
# Restart Apache
apachectl restart
4. Access Apache in the local area network Accessing Apache in the local area network requires the device and other devices to be connected to the same WiFi.

Use ifconfig in Termux to check the device’s local area network IP address (usually under wlan0).

Access http://deviceIP:8080 on devices in the same local area network to see the Apache default page.

Configure Apache’s Web Root Directory

The default web root directory of Apache is located at ~/../usr/share/apache2/default-site/htdocs. You can place web content (such as HTML and PHP files) in this directory.

Modify Web Root Directory (Optional)

If you want to use a custom directory as the web root directory, you can modify the Apache configuration file ~/../usr/etc/apache2/httpd.conf.

Find the following configuration section:

DocumentRoot "/data/data/com.termux/files/usr/share/apache2/default-site/htdocs"
<Directory "/data/data/com.termux/files/usr/share/apache2/default-site/htdocs">

Change DocumentRoot and <Directory> directory to the file path you want (e.g., ~/my_website).

Create and Edit Test Page

Create a test page index.html in the web root directory to test the server:

echo "Hello, Termux Apache Server!" > ~/../usr/share/apache2/default-site/htdocs/index.html

Refresh the browser to see “Hello, Termux Apache Server!” on the page.

Install PHP Support

Apache can support PHP through the mod_php module to support dynamic content.

Install PHP

pkg install php php-apache

Configure Apache to Support PHP

After installation, edit the Apache configuration file httpd.conf to enable PHP support.

nano ~/../usr/etc/apache2/httpd.conf

Find the following line in the file, uncomment it or add it:

LoadModule php_module /data/data/com.termux/files/usr/libexec/apache2/libphp.so
AddType application/x-httpd-php .php

Test if PHP is Effective

Create a PHP file index.php in the web root directory with the following content:

<?php
phpinfo();
?>

Restart the Apache server:

apachectl restart

Then access http://deviceIP:8080/index.php in the browser to see the PHP configuration information page (phpinfo), indicating that PHP configuration is successful.

Install and Configure MySQL (MariaDB)

For web applications with databases, you can use MariaDB (a branch of MySQL).

Install MariaDB

pkg install mariadb -y

Initialize Database and Start MariaDB

When using MariaDB for the first time, you need to initialize the database files:

mysql_install_db
mysqld_safe --datadir=$PREFIX/var/lib/mysql &&

Set root User Password

mysql -u root

After entering MySQL, set the root password:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;
EXIT;

Test MySQL Connection

Write the following code in a PHP file to test the MySQL connection:

<?php
$servername = "localhost";
$username = "root";
$password = "new_password";

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connection successful";
?>

Access Apache Server in Local Area Network

When accessing Apache from other devices, you can access the web server on Termux via http://deviceIP:8080. Make sure all devices are connected to the same WiFi network and that the firewall and network permissions on the Termux device are configured correctly.

Install phpMyAdmin

phpMyAdmin allows for easy management of MySQL databases.

Download and Unzip phpMyAdmin

cd ~/../usr/share/apache2/default-site/htdocs
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
tar -xzvf phpMyAdmin-latest-all-languages.tar.gz
mv phpMyAdmin-*-all-languages phpmyadmin

Configure phpMyAdmin

Edit the phpmyadmin/config.inc.php file and set the following content:

$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'new_password';

Configure Apache to Parse PHP

In Termux, the Apache configuration file is usually located at:

/data/data/com.termux/files/usr/etc/apache2/httpd.conf

Modify the Apache configuration file

  1. Open the httpd.conf file:
nano /data/data/com.termux/files/usr/etc/apache2/httpd.conf
  1. Find the following section and ensure that DirectoryIndex includes index.php:
DirectoryIndex index.php index.html
  1. At the end of the file, add the following content to ensure Apache can correctly parse PHP files:
# Load PHP module
LoadModule php_module /data/data/com.termux/files/usr/libexec/apache2/libphp.so
# Add PHP file types
AddType application/x-httpd-php .php
  1. Save and close the file.

Create a Test File to Confirm PHP is Working

Create an info.php file in Apache’s root directory (default is /data/data/com.termux/files/usr/share/apache2/default-site/htdocs) to test PHP:

echo "<?php phpinfo(); ?>" > /data/data/com.termux/files/usr/share/apache2/default-site/htdocs/info.php

Start or Restart Apache

Restart the Apache server to apply the configuration:

apachectl restart

Access phpMyAdmin

Access http://deviceIP:8080/phpmyadmin in the browser, log in with MySQL username and password to manage the database. The device IP can be checked via ifconfig.

Transform Your Old Android Phone into a Home Server

Give a look you are the best

Transform Your Old Android Phone into a Home Server

Leave a Comment

×