Migrations that go badly usually go badly because the cutover was also the first attempt. Someone copies the files late at night, imports the database, flips DNS, and only then finds out that the PHP version is different and the uploads directory has the wrong owner. The fix is dull but it works: do nearly all the work in advance, rehearse it, and keep the switch itself to a short window with a written list of steps.
The examples assume nginx or Apache, PHP, MySQL or MariaDB, and a directory of user uploads. The old server is 203.0.113.10 and the new one is 198.51.100.20.
Lower the DNS TTL first#
This is the one step you can’t rush. Ask the authoritative nameserver directly, so you see the configured TTL rather than a cached countdown:
dig +noall +answer example.com A @ns1.your-dns-provider.net
dig +noall +answer www.example.com A @ns1.your-dns-provider.netThe second column is the TTL in seconds. If it says 86400, a resolver that just cached your record can keep handing out the old address for a full day after you change it. Drop it to 300, then wait at least as long as the old TTL before you cut over.
Check AAAA records too. A site that moved its A record but left the AAAA on the old server is half migrated, and you won’t notice if your own connection is IPv4 only.
Write down what the old server does#
The web server and database are obvious. What gets forgotten is the rest:
- Cron jobs for every user, files in
/etc/cron.d, and systemd timers. - Outgoing mail: the MTA config, DKIM keys, and the SPF record that lists the old IP.
- Firewall rules on the box, plus allowlists elsewhere that contain the old IP, like a remote API, a payment provider’s webhook settings or the backup server.
- PHP extensions, FPM pool settings and
php.inioverrides. - IP addresses and absolute paths hardcoded in application config.
A few commands cover most of it:
for u in $(cut -d: -f1 /etc/passwd); do
crontab -l -u "$u" 2>/dev/null | sed "s/^/$u: /"
done
ls -l /etc/cron.d
systemctl list-timers --all
php -v; php -m
apt-mark showmanual > /root/manual-packages.txt
nft list ruleset 2>/dev/null || iptables-save
grep -rl '203.0.113.10' /etc /var/www 2>/dev/nullBuild the new box and copy the files#
Match the PHP version unless the migration is also an upgrade, and if it is, test the upgrade separately. Changing server and runtime on the same evening doubles the list of things that might be wrong.
Create the system users with the same names as on the old box. When rsync runs as root on the receiving side it maps ownership by user and group name, so files owned by deploy stay owned by deploy even if the numeric UID differs. Then do the first full copy, as root on the old server:
rsync -aHAX --info=progress2 -e ssh \
--rsync-path="sudo rsync" \
/var/www/ admin@198.51.100.20:/var/www/-H, -A and -X keep hard links, ACLs and extended attributes. --rsync-path="sudo rsync" runs the remote end as root so it can set ownership, which means admin needs sudo for rsync without a password prompt on the new box. Mind the trailing slash: /var/www/ copies the directory’s contents, /var/www copies the directory itself.
This copy can take as long as it likes, since the final pass only sends what changed.
Copy the vhosts and PHP pool files too, but read them rather than dropping the old /etc on a fresh system.
Rehearse the database move#
Do a full dump and import now, and time it. That number tells you how long the write freeze will be on the night.
time mysqldump --single-transaction --routines --triggers --events \
--databases appdb | gzip | ssh admin@198.51.100.20 'gunzip | sudo mysql'In bash, time covers the whole pipeline. A --databases dump doesn’t include users, so recreate the application account on the new server. SHOW GRANTS FOR 'app'@'localhost'; on the old one gives you the statements to adapt. Moving between MySQL and MariaDB, or across major versions, is where collation and SQL mode surprises show up, and this is the time to find them.
If the import takes hours, a dump with a write freeze isn’t realistic. Set up the new server as a replica of the old one instead. Seed it with a physical copy (mariabackup for MariaDB, Percona XtraBackup for MySQL) or a dump taken with --master-data, which recent MySQL calls --source-data, so the replica knows where to start in the binary log. On the night you freeze writes, wait until the replica has caught up, stop replication and point the application at it. The freeze then lasts seconds, not the length of a dump. It needs binary logging on the old server and port 3306 open between the two hosts only.
Test through the hosts file#
Point the domain at the new server on your own machine only. On Linux and macOS that’s /etc/hosts, on Windows C:\Windows\System32\drivers\etc\hosts:
198.51.100.20 example.com www.example.comBrowse the site, log in, upload a file, check the error log. For scripted checks, curl does the same without the hosts file:
curl -sI --resolve example.com:443:198.51.100.20 https://example.com/Keep cron disabled on the new box until the cutover. It has a copy of the production database, so a scheduled job there will send the same reminder emails as the old server.
TLS before the switch#
The HTTP-01 challenge goes to whatever address DNS returns, which is still the old server, so a normal certbot run on the new box fails. There are two ways around it.
The simplest is to copy the existing certificates. -a matters here, because the files in live/ are symlinks into archive/:
rsync -a -e ssh --rsync-path="sudo rsync" \
/etc/letsencrypt/ admin@198.51.100.20:/etc/letsencrypt/The copied renewal configs still name the old authenticator, so make sure the nginx or Apache plugin is installed on the new box, or that the webroot path exists.
The other way is DNS-01, which proves control of the domain through a TXT record and doesn’t care where the A record points. Debian and Ubuntu package certbot plugins for several DNS providers:
certbot certonly --dns-cloudflare \
--dns-cloudflare-credentials /root/.secrets/cloudflare.ini \
-d example.com -d www.example.comAfter the cutover, run certbot renew --dry-run on the new server and stop renewal on the old one with systemctl disable --now certbot.timer. Snap installs use snap.certbot.renew.timer instead.
The cutover#
With the rehearsal done, the night itself is a list:
- Confirm the low TTL has been live for longer than the old TTL.
- Stop cron on the old server with
systemctl stop cron, plus any application timers. - Put the site into maintenance mode on the old server, either the application’s own mode or an nginx rule that returns 503.
- Run
SET GLOBAL read_only = ON;on the old database. It blocks writes from ordinary accounts, though admin accounts with SUPER level privileges can still write. - Run the final rsync with
--deleteadded, so files removed on the old side go away on the new side too. - Do the final dump and import, or let the replica catch up and stop replication. Compare row counts on a few busy tables.
- Test the new server through the hosts file one more time.
- Change the A and AAAA records.
- Enable cron on the new server, update SPF with the new IP, and send a test email.
- Watch the access and error logs on both machines.
For a while after the DNS change, some clients will still hit the old IP. Instead of a maintenance page, make the old server proxy to the new one:
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
location / {
proxy_pass https://198.51.100.20;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
proxy_ssl_name $host;
}
}The new server will see those requests coming from 203.0.113.10, so if the application cares about client IPs, add the old address to set_real_ip_from there for the duration. A tail -f on the old access log shows the stragglers dying off.
Keep the old server for a while#
Leave it running for a week or two, with the database read-only and untouched. It’s your fallback. Before you delete it, check the mail queue is empty with mailq, take one last backup, and remove the old IP from SPF and any allowlists.
Have a rollback plan before you start#
Rollback gets harder at each step, so write down what it means at each stage. Before the DNS change it’s easy: set read_only = OFF, leave maintenance mode, start cron again. After the DNS change, the new server starts collecting orders, comments and uploads that the old one doesn’t have. Pointing DNS back at that point either throws those writes away or turns into a second migration in the opposite direction.
Pick a cutoff in advance, for example the first hour after the switch, and agree that problems found before it mean rolling back, while problems found after it get fixed on the new server. Put the rollback commands in the same document as the cutover steps, so nobody has to work them out on the night.