ubuntips

Upgrade Ubuntu first, and survive a dead mirror

Draft posted on · 5 min read

On this page
Draft commented on Dec 2, 2026

My setup script compiles nginx, PHP and Valkey from source on Ubuntu and tunes them to the hardware. Three small releases went out this week (1.22.42 to 1.22.44). Each one came from something I watched happen on a real server.

The builds now start from an up-to-date system#

Before running --optimize, I used to type the same line by hand on every server:

apt update -y && apt upgrade -y && apt dist-upgrade -y && apt autoremove --purge -y && apt autoclean -y && apt clean -y && systemctl daemon-reload

Now the script runs it itself, as the first step of a new install and of every --optimize, before any source build. A compiler, linker or library from Ubuntu’s own updates is then in place before nginx or PHP is built against it. One function covers both cases, and they differ only in what happens around the upgrade.

Nothing gets restarted behind your back. On Ubuntu 24.04 and later, a plain apt upgrade lets needrestart restart every service that uses an upgraded library. On a live web server that means nginx, PHP-FPM, Valkey and MariaDB, one after another, in whatever order it picks. The script suspends needrestart for the upgrade. The report then lists the services still on an old library and says Reboot needed when the kernel or core libraries changed. You choose when to restart, with --restart-all or a reboot. A fresh install is different: nothing is serving yet, so it restarts what needs it right away, as before.

A broken upgrade doesn’t take the run down. If dpkg fails mid-upgrade, the step repairs it once (dpkg --configure -a, then apt-get -f install --no-remove) and tries again. If it still fails, --optimize marks the step FAILED in its report, carries on tuning everything else and exits non-zero. A local change to a package’s config file is always kept (--force-confold), so the run never stops at a conffile prompt. --optimize --dry-run shows how many packages are due, and SETUP_OPTIMIZE_APT=0 skips the upgrade for one run.

The install that failed on a mirror#

I tested that release with a fresh install on my test server, and it failed halfway. The hosting provider’s Ubuntu mirror was answering 503 for automake and libtool, two packages the PHP build needs. My script was fine; the provider’s copy of Ubuntu was down. On a server you’re setting up at 2 a.m., that’s the whole evening gone.

My first thought was to mirror Ubuntu’s packages myself. That’s the wrong fix: a copy of the archive is a lot of storage to keep in sync, and it would be one more mirror that can go down. apt already has the right tool, the mirror+file: method. A sources entry points at a list of mirrors in priority order, and apt moves to the next mirror for each file that fails.

So the package step now begins by rewriting the Ubuntu entries in ubuntu.sources (or the older sources.list) to use two such lists:

# /etc/apt/setup-mirrors-archive.list
http://<the mirror this server already used>/ubuntu/   priority:1
http://archive.ubuntu.com/ubuntu/                       priority:2
http://mirrors.edge.kernel.org/ubuntu/                  priority:3

The security list works the same way and starts with the server’s own security mirror. The mirror the server already used stays first, so on a normal day nothing changes and the fast local mirror does the work. On a bad day, apt falls back to Canonical’s main archive, then to kernel.org’s copy.

This is safe because the trust is in the signatures, not the mirror. Every index is signed by Ubuntu’s archive key, and apt checks it whichever mirror served the file. A few more guard rails:

  • Only Ubuntu’s own entries are rewritten. Third-party sources (MariaDB, Elasticsearch, Node.js) are left untouched.
  • A rewrite apt can’t parse is undone straight away. The script checks the result with apt-cache policy and puts the original files back if apt rejects it.
  • --purge basic restores the original files byte for byte. It also removes the two lists.
  • x86_64 only. ARM servers use ports.ubuntu.com, which has its own mirrors.

The proof was a rerun of the failed test: the same server, with its own mirror still answering 503. The install finished, and the apt log showed automake and libtool downloaded from the fallback.

Checking every live server before deleting code#

The script carried code to cope with older releases: it accepted CPU fingerprints recorded by 1.22.0-1.22.4 builds, and it could restore /etc/security/limits.conf under a narrow condition. I wanted it gone, but deleting it blind is how a live server gets a surprise rebuild. So first I ran a read-only check across all 21 live servers:

  • CPU fingerprints: all 50 build.info files (nginx, PHP, Valkey) record the current fingerprint and match their CPU exactly, so nothing needs the old acceptance.
  • limits.conf: no server’s file matches the template exactly, which is the only case where the restore would run, so it had never done anything.
  • Memory: no server had logged a single corrected memory error (ECC), while I was there anyway.

Both pieces were removed. That cleared the way for a wider sweep. One reviewer listed what looked unused, and a second one tried to prove each item was still needed. About 11,000 lines left the two repositories: dead variables, roughly 45 checks that could never fail, nginx templates the script never renders, old audit reports and finished benchmark tools. Nothing rebuilds and nothing behaves differently; git history keeps all of it.

A 20-second walk through attachments#

Every --optimize checks each site for SQLite use, so the PHP build keeps the SQLite modules only where something needs them. On a big XenForo forum the check ran out of its 20-second budget on every run. It was walking data/ and internal_data/, where XenForo keeps every attachment, avatar and thumbnail. Those trees hold no code and no database, so a XenForo root (and wp-content/uploads for WordPress) now skips them. On a test site with 603,001 files, the scan dropped from 2-2.8 seconds to 0.3, and it no longer grows with the attachments.

1.22.42 then went to all 21 live servers with --optimize. Every run succeeded, no service restarted, every page probe passed, and XenForo’s error logs stayed empty.

The test server went back to its recorded clean state afterwards: the same 701 packages, now with twelve newer versions from Ubuntu’s updates, which is the new step doing its job.

Back to top ↑