这篇博客运行在回收的谷歌Pixel 5上。
This blog is running on a recycled Google Pixel 5 (2024)

原始链接: https://blog.ctms.me/posts/2024-08-29-running-this-blog-on-a-pixel-5/

受限于在微控制器和路由器等极简硬件上运行网站的项目启发,作者成功地将他们的博客直接托管在一台谷歌Pixel 5安卓手机上。该项目源于对高效、回收利用硬件的渴望,以及对“永久计算”的迷恋。 尽管Pixel 5的运营商锁阻止了自定义ROM的安装,作者利用Termux终端模拟器及其现成的软件包——包括Hugo(博客的静态站点生成器)和`dufs`(用于文件上传)来运行网站。一个关键要求是有线互联网连接,通过手机的USB-OTG支持和一个扩展坞实现。 该设置由太阳能电池板和Jackery移动电源供电,符合离网理念。博客运行稳定且快速,与传统服务器无异。自动备份通过`rsync`同时备份到桌面电脑和NAS,并提供基于Git的备份选项。作者详细介绍了必要的Termux安装和配置,强调了这个过程出乎意料地简单。

相关文章

原文

If you glance over this blog, you will see that I am an avid Android fan. After setting up numerous Linux proot desktops on phones, I wanted to see if I use a phone as a server and run my blog from an Android phone. Since you are reading this, I was successful.

I was inspired my a few Mastodon posts earlier this week to give it a go. First, I stumbled on a post from @kaimac who is running a site from an ESP32 microcontroller. In the comments of that post, I saw a mention to compost.party created by user @computersandblues that runs completely on an Android device and a solar panel. Last, @stevelord who is essentially running a homelab on a TP-Link router with OpenWRT installed.

I think a lot about power consumption of my homelab and I also love using old hardware for random projects to give them new life. I was truly inspired by the above works, so I got right down to business.

The hardware

I looked through the devices I had laying around and I chose a Google Pixel 5 my brother-in-law gave me after he upgraded. The Pixel 5 is carrier locked to Verizon, which is notorious for making it impossible to also unlock the bootloader and install custom ROMs. At first I wanted a device that I could install PostmarketOS to run a proper Linux server. In the end, I’m glad I didn’t go that route.

Another reason I chose the Pixel 5 is because it supports USB-OTG and can use docks with hard-wired internet. I didn’t want to run the site on wifi and having an ethernet connection was mandatory.

Last, it is the most current phone I have. This device is open to the internet, so I wanted to make sure it is an updated as possible.

Solar powered blog!

This summer I’ve been testing using a 100w solar panel I got from Harbor Freight Tools so I can learn more about how it all works before diving into larger projects. I have that panel connected to a Jackery 160w power station to keep it charged up and we use it to charge our mobile devices. I got the Jackery last year as a power bank I use while on jobsites.

Since I already have this set up, I am now using it to power this blog. I’m happy with this setup as I’ve been getting more into permacomputing. Having a website that is fully offgrid using recycled parts is exciting!

What I used to create the site (Termux is the GOAT)

While considering what projects I could do with this phone, I was thinking I was going to install a proot desktop and then run from within a Linux environment. Before I started I decided to check out a few packages that are in Termux (the flat out amazing terminal emulator) to see how far I could push it.

I checked for some basics and read about setting up an ssh connection. Then I randomly searched for Hugo, which is what my blog was already built on. Sure enough, it is right there in the Termux repos! Turns out, it has been in there for a long time. I see a lot of posts from 2018 with people using it.

How has it been going

Great! Site is fast and reliable. I ran into a few hiccups on the first day or so, which were mostly around the version of Hugo on my server and the newer version I am using on the phone. The other is related to my solar setup and keeping an eye on the battery levels.

To be honest, I don’t think anyone can tell it is running on an Android phone instead of a x86 Linux box or a hyperscaler VPS.

At the moment I have no plans to change this setup and will leave it as-is until some issue arises. But, there’s really not much to report other than it works fantastic.

Below are my longform notes on how I set it up. But, the short version is it was way simpler than I thought it would be. You can get up and running with a Hugo site by just installing git, screen, your favorite text editor, and hugo straight from the repos.

Not included in this post is how I add new posts to the phone. I can use scp to send a files, but I prefer to use dufs that is a static file server in that can be accessed in the browser. Using dufs I can upload files and make quick edits straight in the browser from any device. Surprise! dufs is also in the Termux repos and is so easy to get up and running. Again, message me if you’d like to see a write-up about it.


Installs

Of course I need some basic utilities. These are the utilities I need to have at a minimum when working with a Linux system:

  • rsync
  • openssh
  • git
  • wget
  • curl
  • fish shell
  • cronie
  • termux-services
  • iperf3
  • speedtest-go
  • screen
  • helix
  • hugo

Restart Termux and use sv-enable to run certain items as services. I do this for sshd and cronie. It looks like this:

$ sv-enable sshd
$ sv-enable cronie

After running sv-enable, restart Termux.

openssh

I could build all of this straight from the phone using either the touchscreen keyboard or connecting a standard keyboard and mouse either with a USB-C dock or bluetooth. But, I want to manage this like all of my other servers, which is to ssh into the device and work from my desk.

There is an official guide for setting up an ssh server. All I will add here is some pointers I learned along the way.

  1. Adding an ssh key is simple and should be one of the first steps done. After generating the key and importing with ssh-copy-id from the desktop, edit the sshd file in $PREFIX/etc/ssh/sshd_config.
  2. Termux generates its own username and cannot be changed. Run whoami to see what it is.
  3. It is the same for the ssh port. As far as I can tell you cannot change the port, which is automatically set to 8022.

Running the site

There are lots of guides out there on how to setup a hugo site. I have an existing site that I migrated from a VM to this phone, so my notes do not include how to get a hugo site running. I also do not need to do any port forwarding as I already have a reverse proxy that I just changed where it points for my blog.

I would like to hear feedback if there is a need to add those notes here. Message me on Mastodon or by email using the links at the bottom of this post.

Below are notes on how I use the package cronie to start the blog using screen and the automatically reload the blog occasionally. cronie is for setting up cron tasks. Once installed and enabled, run crontab -e like usual to setup tasks.

This is how I do it.

First, set a fish alias for the command to reload the blog:

alias blog_run='cd /data/data/com.termux/files/home/<website_root_dir> && /data/data/com.termux/files/usr/bin/hugo serve --bind=0.0.0.0 --baseURL=https://blog.ctms.me --appendPort=false --environment=production --disableFastRender --cacheDir /data/data/com.termux/files/home/<website_root_dir>/cache'

funcsave blog_run

Now, create a script and place in ~/scripts that closes a previous instance of screen, clears the cache, and then starts a new screen session titled “hugo” and execute the alias:

#! /bin/bash
screen -X -S "hugo" quit
rm -rf /data/data/com.termux/files/home/<website_root_dir/cache/<site_name_dir>/filecache/getresource/
screen -S hugo -d -m fish -c 'blog_run; exec fish'

Last, set it to run with cron

*/5 * * * * cd /data/data/com.termux/files/home/scripts && sh blog_reload.sh

Backing up

Since Termux supports ssh connections, I can use it on remote machines to pull the files from the phone using rsync.

First, need to install rsync on the phone with pkg install rsync.

Desktop backup

Now we can run it from my desktop to pull the files:

rsync -aP pixel:~/<website_root_dir> /local/dir/pixel_blog/

On my desktop, I have this for cron

@reboot sleep 30 && rsync -aP pixel:~/<website_root_dir> /local/dir/pixel_blog/ >> $HOME/logs/pixel-hugo-backup.log 2>&1

nas backup

This is the same configuration. The only difference is the backup location and the cron timing.

rsync -aP pixel:~/<website_root_dir> /local/dir/pixel_blog

The automation:

5 6 * * * rsync -aP pixel:~/<website_root_dir> /local/dir/pixel_blog >> $HOME/logs/pixel-hugo-backup.log 2>&1

git backup

I have a local self-hosted git instance I push backups to, but you can totally set it up to send them to Github or whatever forge you use. No instructions here because there are plenty of guides out there on how to set this up.

- - - - -

Thank you for reading! If you would like to comment on this post you can start a conversation on the Fediverse. Message me on Mastodon at @[email protected]. Or, you may email me at [email protected]. This is an intentionally masked email address that will be forwarded to the correct inbox.

联系我们 contact @ memedata.com