Post

Run a service on an old smartphone

Turn an old Android smartphone into a self-hosted server with Termux and Ubuntu

We live in the middle of a hardware crisis. Due to the AI arms race, corporations are buying any newly produced hardware, driving up the prices of PC components and making them basically unbuyable for regular person.

Luckily, there is another category of computers that remains cheap precisely because most people consider them e-waste: smartphones running outdated versions of Android.

And I mean it.

I was inspired by this post and started searching local marketplaces for the same Samsung Galaxy S10+ phones. They have an 8-core CPU and 8 GB of RAM, which is enough to run quite a few lightweight services.

I found a lot of good offers, since I don’t mind scratched bodies or broken screens. Ultimately, I bought two 128 GB phones for USD 55 in total and one 512 GB phone for another USD 55. That gives me 24 CPU cores, 24 GB of RAM, and 768 GB of storage for USD 110.

I think that’s a pretty good deal.

In this post, I will show how to run a service on one of these phones. I need Forgejo to host my personal Git repositories, but you can run many other services in exactly the same way - even something like a Jellyfin media server.

The setup is surprisingly straightforward. If you are already comfortable working in a Linux terminal, you should have no problem following along.

Overview

Here are the main steps:

  1. Install F-Droid.
  2. Install Termux.
  3. Install Termux:Boot.
  4. Configure battery settings.
  5. Install Ubuntu using proot-distro.
  6. Install Forgejo.
  7. Configure Forgejo.
  8. Start Forgejo manually for the first time.
  9. Complete the Forgejo web setup.
  10. Verify the Forgejo installation.
  11. Configure automatic startup with Termux:Boot.

Install F-Droid

F-Droid will be used to install Termux and its companion applications.

  1. Download the F-Droid APK from the official F-Droid website.
  2. Open the downloaded APK on the phone.
  3. Android may ask for permission to install applications from this source.
  4. Allow the browser or file manager to install unknown applications.
  5. Complete the F-Droid installation.
  6. Open F-Droid and allow it to update its repositories.

Install Termux

Termux provides the Linux-like environment used to install and run the server software on Android.

Install Termux from F-Droid.

Do not use the old Termux version from Google Play. Although an experimental Google Play version is available, the Termux maintainers currently recommend the F-Droid or GitHub builds for most users.

After installation, open Termux and update its packages:

1
2
pkg update
pkg upgrade -y

Install the packages needed for the server setup:

1
pkg install -y proot-distro openssh curl wget

Install Termux:Boot

Termux:Boot automatically starts the required Termux scripts and services when the phone boots.

Install Termux:Boot from F-Droid.

After installation, open the Termux:Boot application once. This initializes its integration with Termux.

Termux:Boot automatically executes scripts located in:

1
~/.termux/boot/

We will place our startup script there so that Forgejo launches automatically when the phone boots.

Configure battery settings

Sometimes Android’s background-process restrictions can prevent Termux:Boot from starting services after a reboot.

Open:

Settings → Apps → Termux → Battery

Set battery usage to:

Unrestricted

Then do the same for Termux:Boot.

Also check:

Settings → Battery → Background usage limits

Make sure that neither Termux nor Termux:Boot is listed under:

  • Sleeping apps
  • Deep sleeping apps

These settings are required for reliable automatic startup.

Install Ubuntu using proot-distro

proot-distro allows us to run an Ubuntu userspace on Android without rooting the phone.

Install Ubuntu from Termux:

1
proot-distro install ubuntu

Enter the Ubuntu environment:

1
proot-distro login ubuntu

Update the Ubuntu installation:

1
2
apt update
apt upgrade -y

Install the packages required by Forgejo:

1
apt install -y git openssh-server wget curl ca-certificates

At this point you have the Ubuntu environment ready for homelabbing. If you’re interested, here are instructions how to configure and run Forgejo. Or you can just jump straight to final thoughts.

Install Forgejo

Create a dedicated git user:

1
useradd --system --create-home --home-dir /home/git --shell /bin/bash git

Create the Forgejo installation directory:

1
mkdir -p /opt/forgejo

Download the ARM64 Forgejo binary appropriate for the phone’s architecture:

1
2
wget -O /opt/forgejo/forgejo \
  https://code.forgejo.org/forgejo/forgejo/releases/download/v15.0.8/forgejo-15.0.8-linux-arm64

Make it executable and verify that it works:

1
2
chmod +x /opt/forgejo/forgejo
/opt/forgejo/forgejo --version

Create the Forgejo data directories:

1
2
3
mkdir -p /var/lib/forgejo/custom
mkdir -p /var/lib/forgejo/data
mkdir -p /var/lib/forgejo/log

Set their ownership:

1
chown -R git:git /var/lib/forgejo

Create the configuration directory:

1
mkdir -p /etc/forgejo

Set its ownership and permissions:

1
2
chown root:git /etc/forgejo
chmod 770 /etc/forgejo

Configure Forgejo

Create:

1
nano /etc/forgejo/app.ini

For this setup, the basic configuration looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
APP_NAME = Forgejo
RUN_USER = git
RUN_MODE = prod
WORK_PATH = /var/lib/forgejo

[server]
HTTP_ADDR = 0.0.0.0
HTTP_PORT = 3000

[database]
DB_TYPE = sqlite3
PATH = /var/lib/forgejo/data/forgejo.db

[repository]
ROOT = /var/lib/forgejo/data/git/repositories

[log]
MODE = console

During the initial web setup, Forgejo needs to be able to update its configuration file. Set the ownership and permissions accordingly:

1
2
chown git:git /etc/forgejo/app.ini
chmod 660 /etc/forgejo/app.ini

Start Forgejo manually for the first time

Switch to the git account:

1
su - git

Start Forgejo:

1
/opt/forgejo/forgejo web --work-path /var/lib/forgejo --config /etc/forgejo/app.ini

Forgejo should eventually report something similar to:

1
Listen: http://0.0.0.0:3000

Complete the Forgejo web setup

Find the phone’s Wi-Fi IP address.

From Termux, run:

1
ifconfig

Look for the wlan0 interface.

For example:

1
2
wlan0:
    inet 192.168.0.105

From another computer on the same network, open:

1
http://PHONE_IP:3000

For example:

1
http://192.168.0.105:3000

Complete the Forgejo installation page.

Use:

  • Database: SQLite3
  • Database path: /var/lib/forgejo/data/forgejo.db
  • HTTP listen port: 3000

Create the Forgejo administrator account.

After completing the installation, verify that you can log in to Forgejo and create a repository.

Once the web setup is complete, Forgejo no longer needs write access to its configuration file. Lock down the configuration directory:

1
2
3
4
chown root:git /etc/forgejo
chown root:git /etc/forgejo/app.ini
chmod 750 /etc/forgejo
chmod 640 /etc/forgejo/app.ini

Verify the Forgejo installation

Run Forgejo in background

1
2
3
4
nohup /opt/forgejo/forgejo web \
  --work-path /var/lib/forgejo \
  --config /etc/forgejo/app.ini \
  > /var/log/forgejo.log 2>&1 &

Forgejo should listen on all network interfaces:

1
0.0.0.0:3000

Test it from the phone:

1
curl http://127.0.0.1:3000

You can also test it using the phone’s LAN address:

1
curl http://PHONE_IP:3000

A successful request should return Forgejo’s HTML.

From another computer on the same LAN, opening:

1
http://PHONE_IP:3000

should display the Forgejo web interface.

Configure automatic startup with Termux:Boot

Create the Termux:Boot directory:

1
mkdir -p ~/.termux/boot

Create startup script at:

1
~/.termux/boot/start-homelab.sh

Here is how my script looks like:

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

# Keep the phone awake
termux-wake-lock

# Give Android/Termux a little time to finish booting
sleep 10

# Start Termux SSH server if it isn't already running
if ! pgrep -x ssh >/dev/null; then
  sshd
fi

# Start Forgejo inside Ubuntu
# Keep the PRoot process alive for as long as Forgejo is running
if ! pgrep -f "forgejo web --config /etc/forgejo/app.ini" >/dev/null; then
  nohup proot-distro login ubuntu -- \
    su - git -c "/opt/forgejo/forgejo web --work-path /var/lib/forgejo --config /etc/forgejo/app.ini" \
    >> "$HOME/.forgejo.log" 2>&1 &
fi

Make sure it is executable:

1
chmod +x ~/.termux/boot/start-homelab.sh

At this point, reboot the phone and verify that Forgejo becomes available without manually opening Termux.

Final thoughts

And that’s it.

An old Android phone that would otherwise be thrown away is now running a self-hosted Git service and can start it automatically whenever the phone boots.

The same approach can be used for many other lightweight services, making old smartphones surprisingly capable little homelab machines.

If you enjoy this post, there are high chances that you will like the next one as well as I will take this idea a step further: flash a proper Linux distribution onto a phone and run services in containers.

This post is licensed under CC BY 4.0 by the author.

Trending Tags