How to Transform an Old Android Phone into a Home Server

Author: huaxiaorong

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

1. Tool Preparation

  • Android Phone: Used for transformation into a server
  • Other Devices: Used for testing and viewing server access pages, which can be other phones, computers, or platforms.
  • Bluetooth Keyboard: Convenient for connecting to the Android phone for command processing

Due to Python’s convenient and rich toolkits, the main idea is to use Termux to enable the phone to run Python scripts, leveraging Python’s server capabilities, and implementing user login, access, upload, and download functions through a web interface.

2. Environment Preparation

1. Install Termux

1) Install Termux via GitHub or the APKFab app store.

2) Update and install basic software packages

pkg update && pkg upgrade -y
pkg install wget curl nano -y

2. Install Apache Support

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

1) Install Apache

pkg install apache2 -y

2) Start Apache

apachectl start

By default, Apache runs on port 8080. You can access it in a browser at <span>http://127.0.0.1:8080</span>, and if you see the default “Test Page” of Apache, it indicates that the server has started successfully.

Here, ‘<span>127.0.0.1</span>‘ 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

To access Apache in the local area network, the device must be connected to the same WiFi as other devices.

  • Use ifconfig in Termux to view the device’s local IP address (usually under wlan0).
  • Access <span>http://DeviceIP:8080</span> on devices within the same local area network to see the default Apache page.

3. Configure Apache’s Web Root Directory

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

1. Modify Web Root Directory (Optional)

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

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 <span>DocumentRoot</span> and <span><Directory></span> to your desired file path (e.g., <span>~/my_website</span>).

2. Create and Edit a Test Page

Create a test page <span>index.html</span> 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, and you should see <span>Hello, Termux Apache Server!</span> on the page.

4. Install PHP Support

Apache can support PHP through the <span>mod_php</span> module for dynamic content.

1. Install PHP

pkg install php php-apache

2. Configure Apache to Support PHP

After installation, edit the Apache configuration file <span>httpd.conf</span> to enable PHP support.

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

Find the following lines in the file and uncomment or add them:

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

3. Test if PHP is Working

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

<?php
phpinfo();
?>

Restart the Apache server:

apachectl restart

Then access <span>http://DeviceIP:8080/index.php</span> in the browser to see the PHP configuration information page (phpinfo), indicating that PHP is configured successfully.

5. Install and Configure MySQL (MariaDB)

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

1. Install MariaDB

pkg install mariadb -y

2. Initialize the 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 &&

3. Set the Root User Password

mysql -u root

After entering MySQL, set the root password:

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

4. 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";
?>

6. Access Apache Server in the Local Area Network

When accessing Apache from other devices, you can reach the web server on Termux via <span>http://DeviceIP:8080</span>. Ensure all devices are connected to the same WiFi network, and that the firewall and network permissions on the Termux device are configured correctly.

7. Install phpMyAdmin

phpMyAdmin can conveniently manage MySQL databases.

1. Download and Extract 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

2. Configure phpMyAdmin

Edit the <span>phpmyadmin/config.inc.php</span> file and set the following:

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

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

Open the <span>httpd.conf</span> file:

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

Find the following section and ensure that <span>DirectoryIndex</span> includes <span>index.php</span>:

DirectoryIndex index.php index.html

Add the following at the end of the file 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 type
AddType application/x-httpd-php .php

Save and close the file.

4. Create a Test File to Confirm PHP is Working

Create a file <span>info.php</span> in the Apache root directory (default is <span>/data/data/com.termux/files/usr/share/apache2/default-site/htdocs</span>) to test PHP:

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

5. Start or Restart Apache

Restart the Apache server to apply the configuration:

apachectl restart

6. Access phpMyAdmin

Access <span>http://DeviceIP:8080/phpmyadmin</span> in the browser, and log in with the MySQL username and password to manage the database. The device IP can be viewed using ifconfig.

Reprint Statement: All reprinted articles must indicate the original source or reprint source (in cases where the reprint does not indicate the original source), and if there is any infringement, please contact for removal.

Leave a Comment