<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: BRIAN MWOHI</title>
    <description>The latest articles on DEV Community by BRIAN MWOHI (@bmwohi254).</description>
    <link>https://dev.to/bmwohi254</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4258554%2F4cbfdde0-7aa4-4669-b2df-b075299c1bba.jpg</url>
      <title>DEV Community: BRIAN MWOHI</title>
      <link>https://dev.to/bmwohi254</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bmwohi254"/>
    <language>en</language>
    <item>
      <title>How to Automate Encrypted Off-Site Linux Server Backups with Rclone and Cron</title>
      <dc:creator>BRIAN MWOHI</dc:creator>
      <pubDate>Sun, 22 Nov 2026 10:08:02 +0000</pubDate>
      <link>https://dev.to/bmwohi254/how-to-automate-encrypted-off-site-linux-server-backups-with-rclone-and-cron-4fa0</link>
      <guid>https://dev.to/bmwohi254/how-to-automate-encrypted-off-site-linux-server-backups-with-rclone-and-cron-4fa0</guid>
      <description>&lt;p&gt;published: false&lt;br&gt;
description: Build a zero-trust automated backup pipeline on Ubuntu 24.04 using Bash, Rclone client-side encryption, S3-compatible object storage, and Cron.&lt;/p&gt;
&lt;h2&gt;
  
  
  tags: linux, devops, security, bash
&lt;/h2&gt;

&lt;p&gt;Relying solely on local disk snapshots or storing backups on the same virtual private server (VPS) leaves your infrastructure exposed to single points of failure. If the underlying host disk corrupts, an accidental &lt;code&gt;rm -rf&lt;/code&gt; wipes a directory, or an attacker compromises the machine, local backups vanish right alongside your production data.&lt;/p&gt;

&lt;p&gt;At the same time, uploading raw &lt;code&gt;.tar.gz&lt;/code&gt; archives to cloud object storage introduces a privacy risk: configuration files, &lt;code&gt;.env&lt;/code&gt; secrets, and database dumps sit unencrypted on a third-party provider's servers.&lt;/p&gt;

&lt;p&gt;In this tutorial, you will build an automated, encrypted off-site backup pipeline on &lt;strong&gt;Ubuntu 24.04 LTS&lt;/strong&gt;. You will write a defensive Bash script that archives critical server directories, encrypts both file contents and filenames locally using &lt;strong&gt;Rclone's &lt;code&gt;crypt&lt;/code&gt; backend&lt;/strong&gt;, uploads the archive to any S3-compatible bucket, enforces a 14-day retention policy, and runs automatically every night via &lt;strong&gt;Cron&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Backup Pipeline Overview
&lt;/h3&gt;

&lt;p&gt;Every scheduled run follows this zero-trust workflow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Local Directories (/etc/nginx, /var/www)
        │
        ▼
  Tar/Gzip Archive (/tmp/backup-YYYY-MM-DD.tar.gz)
        │
        ▼
  Rclone Crypt (Client-Side AES Encryption)
        │
        ▼
  S3-Compatible Object Storage (Scrambled Filenames &amp;amp; Data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To follow along, you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An &lt;strong&gt;Ubuntu 24.04 LTS&lt;/strong&gt; server with a non-root user that has &lt;code&gt;sudo&lt;/code&gt; privileges.&lt;/li&gt;
&lt;li&gt;An S3-compatible object storage bucket (such as AWS S3, Backblaze B2, DigitalOcean Spaces, or Cloudflare R2) along with an &lt;strong&gt;Access Key ID&lt;/strong&gt;, &lt;strong&gt;Secret Access Key&lt;/strong&gt;, and &lt;strong&gt;Endpoint URL&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Basic familiarity with the Linux command line and Bash scripting.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 1 — Install Rclone and Configure Your Base Cloud Remote
&lt;/h2&gt;

&lt;p&gt;Ubuntu's default &lt;code&gt;apt&lt;/code&gt; repositories often ship older versions of Rclone. Install the latest maintained release directly using Rclone's official installation script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -fsSL https://rclone.org &lt;span class="o"&gt;|&lt;/span&gt; sudo bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify the installation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rclone version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rclone v1.68.1
- os/version: ubuntu 24.04 (64 bit)
- os/kernel: 6.8.0-45-generic (x86_64)
- os/type: linux
- os/arch: amd64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because your backup script will run as &lt;code&gt;root&lt;/code&gt; via Cron so it can read restricted system directories like &lt;code&gt;/etc/nginx&lt;/code&gt; and &lt;code&gt;/etc/letsencrypt&lt;/code&gt;, store your Rclone configuration under &lt;code&gt;/root/.config/rclone/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Create the configuration directory and open &lt;code&gt;rclone.conf&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo mkdir -p /root/.config/rclone
sudo nano /root/.config/rclone/rclone.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add a base remote named &lt;code&gt;[cloud-storage]&lt;/code&gt; using your S3-compatible provider credentials (the example below uses Backblaze B2 / generic S3 formatting):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="k"&gt;[cloud-storage]&lt;/span&gt;
&lt;span class="na"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s3&lt;/span&gt;
&lt;span class="na"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Other&lt;/span&gt;
&lt;span class="na"&gt;access_key_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;YOUR_ACCESS_KEY_ID&lt;/span&gt;
&lt;span class="na"&gt;secret_access_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;YOUR_SECRET_ACCESS_KEY&lt;/span&gt;
&lt;span class="na"&gt;endpoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//backblazeb2.com&lt;/span&gt;
&lt;span class="na"&gt;acl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;private&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you use AWS S3, set &lt;code&gt;provider = AWS&lt;/code&gt;, specify &lt;code&gt;region = us-east-1&lt;/code&gt;, and omit the &lt;code&gt;endpoint&lt;/code&gt; line.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lock down the file permissions so non-root users on the server cannot read your cloud credentials:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo chmod 600 /root/.config/rclone/rclone.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test the connection by listing your bucket (replace &lt;code&gt;my-server-backups-bucket&lt;/code&gt; with your actual bucket name):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo rclone lsd cloud-storage:my-server-backups-bucket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the command exits cleanly without errors, Rclone can authenticate with your storage bucket.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2 — Layer Client-Side Encryption with Rclone &lt;code&gt;crypt&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now wrap your &lt;code&gt;[cloud-storage]&lt;/code&gt; remote inside an Rclone &lt;code&gt;crypt&lt;/code&gt; remote. When you copy files to a &lt;code&gt;crypt&lt;/code&gt; remote, Rclone encrypts both the file payload and the file name on your server &lt;em&gt;before&lt;/em&gt; transmitting a single byte over the network.&lt;/p&gt;

&lt;p&gt;First, generate two strong, random passwords to serve as your encryption key and salt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;openssl rand -base64 32
openssl rand -base64 32
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rclone requires passwords inside &lt;code&gt;rclone.conf&lt;/code&gt; to be obscured using &lt;code&gt;rclone obscure&lt;/code&gt;. Run the command below twice—once for your primary password and once for your salt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo rclone obscure &lt;span class="s2"&gt;"PASTE_FIRST_RANDOM_STRING_HERE"&lt;/span&gt;
sudo rclone obscure &lt;span class="s2"&gt;"PASTE_SECOND_RANDOM_STRING_HERE"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open &lt;code&gt;/root/.config/rclone/rclone.conf&lt;/code&gt; again and append the &lt;code&gt;[secure-backup]&lt;/code&gt; section at the bottom:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="k"&gt;[secure-backup]&lt;/span&gt;
&lt;span class="na"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;crypt&lt;/span&gt;
&lt;span class="na"&gt;remote&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cloud&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;backups&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;encrypted&lt;/span&gt;
&lt;span class="na"&gt;filename_encryption&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;standard&lt;/span&gt;
&lt;span class="na"&gt;directory_name_encryption&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="na"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;YOUR_FIRST_OBSCURED_PASSWORD&lt;/span&gt;
&lt;span class="na"&gt;password2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;YOUR_SECOND_OBSCURED_PASSWORD&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Verify Client-Side Encryption
&lt;/h3&gt;

&lt;p&gt;Create a small test file, upload it through &lt;code&gt;secure-backup:&lt;/code&gt;, and inspect how it appears in the underlying &lt;code&gt;cloud-storage:&lt;/code&gt; bucket:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"sensitive production secret"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; sudo tee /tmp/secret-test.txt &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null
sudo rclone copy /tmp/secret-test.txt secure-backup:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare what the decrypted view looks like versus what your cloud provider actually stores:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# View through the crypt remote (decrypted locally on the fly)&lt;/span&gt;
sudo rclone ls secure-backup:

&lt;span class="c"&gt;# View the raw bucket contents (what your cloud provider sees)&lt;/span&gt;
sudo rclone ls cloud-storage:my-server-backups-bucket/encrypted
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       28 secret-test.txt
       76 8f9k2m4p7q1v9x3z5b8n0c4w2y6r1t5l
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your cloud provider only sees a scrambled filename (&lt;code&gt;8f9k2m4p7q...&lt;/code&gt;) and encrypted bytes. Clean up the test file before moving on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo rm -f /tmp/secret-test.txt
sudo rclone deletefile secure-backup:secret-test.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 3 — Write the Automated Backup and Retention Script
&lt;/h2&gt;

&lt;p&gt;Next, create a standalone Bash script that packages your target directories, uploads the compressed tarball to &lt;code&gt;secure-backup:&lt;/code&gt;, deletes the local temporary archive, and prunes remote backups older than 14 days.&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;/usr/local/bin/server-backup.sh&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo nano /usr/local/bin/server-backup.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paste the following script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -euo pipefail

&lt;span class="c"&gt;# Configuration&lt;/span&gt;
&lt;span class="nv"&gt;TIMESTAMP&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;date +&lt;span class="s2"&gt;"%Y-%m-%d_%H-%M-%S"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;HOSTNAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;hostname -s&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;BACKUP_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"${HOSTNAME}-backup-${TIMESTAMP}.tar.gz"&lt;/span&gt;
&lt;span class="nv"&gt;TMP_DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"/tmp/server-backups"&lt;/span&gt;
&lt;span class="nv"&gt;ARCHIVE_PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"${TMP_DIR}/${BACKUP_NAME}"&lt;/span&gt;
&lt;span class="nv"&gt;REMOTE_DEST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"secure-backup:"&lt;/span&gt;
&lt;span class="nv"&gt;RETENTION_DAYS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"14d"&lt;/span&gt;

&lt;span class="c"&gt;# Directories to back up (customize for your environment)&lt;/span&gt;
&lt;span class="nv"&gt;SOURCE_DIRS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="s2"&gt;"/etc/nginx"&lt;/span&gt;
  &lt;span class="s2"&gt;"/etc/letsencrypt"&lt;/span&gt;
  &lt;span class="s2"&gt;"/var/www"&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Ensure temporary directory exists and clean up on exit&lt;/span&gt;
mkdir -p &lt;span class="s2"&gt;"${TMP_DIR}"&lt;/span&gt;
&lt;span class="nb"&gt;trap&lt;/span&gt; &lt;span class="s1"&gt;'rm -rf "${TMP_DIR}"'&lt;/span&gt; EXIT

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"[$(date -Iseconds)] Starting backup: ${BACKUP_NAME}"&lt;/span&gt;

&lt;span class="c"&gt;# Create compressed archive (ignore missing optional directories gracefully if needed)&lt;/span&gt;
tar -czf &lt;span class="s2"&gt;"${ARCHIVE_PATH}"&lt;/span&gt; &lt;span class="s2"&gt;"${SOURCE_DIRS[@]}"&lt;/span&gt;

&lt;span class="c"&gt;# Upload encrypted archive to object storage&lt;/span&gt;
/usr/bin/rclone copy &lt;span class="s2"&gt;"${ARCHIVE_PATH}"&lt;/span&gt; &lt;span class="s2"&gt;"${REMOTE_DEST}"&lt;/span&gt; --config /root/.config/rclone/rclone.conf

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"[$(date -Iseconds)] Upload complete. Pruning backups older than ${RETENTION_DAYS}..."&lt;/span&gt;

&lt;span class="c"&gt;# Delete remote backups older than 14 days&lt;/span&gt;
/usr/bin/rclone delete &lt;span class="s2"&gt;"${REMOTE_DEST}"&lt;/span&gt; --min-age &lt;span class="s2"&gt;"${RETENTION_DAYS}"&lt;/span&gt; --config /root/.config/rclone/rclone.conf

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"[$(date -Iseconds)] Backup finished successfully."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why This Script Uses &lt;code&gt;set -euo pipefail&lt;/code&gt; and &lt;code&gt;trap&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;set -euo pipefail&lt;/code&gt; forces the script to abort immediately if &lt;code&gt;tar&lt;/code&gt; fails or &lt;code&gt;rclone&lt;/code&gt; encounters a network error, preventing broken or empty archives from being treated as successful runs.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;trap 'rm -rf "${TMP_DIR}"' EXIT&lt;/code&gt; guarantees the local &lt;code&gt;.tar.gz&lt;/code&gt; file in &lt;code&gt;/tmp&lt;/code&gt; is deleted whether the script succeeds or fails, preventing your VPS disk from filling up over time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make the script executable only by &lt;code&gt;root&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo chmod 700 /usr/local/bin/server-backup.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run a manual test execution now and confirm the encrypted archive lands in your remote bucket:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo /usr/local/bin/server-backup.sh
sudo rclone ls secure-backup:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2026-11-22T10:15:02+00:00] Starting backup: ubuntu-prod-backup-2026-11-22_10-15-02.tar.gz
[2026-11-22T10:15:04+00:00] Upload complete. Pruning backups older than 14d...
[2026-11-22T10:15:05+00:00] Backup finished successfully.
    14820 ubuntu-prod-backup-2026-11-22_10-15-02.tar.gz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 4 — Schedule Daily Backups with Cron
&lt;/h2&gt;

&lt;p&gt;With the script verified, schedule it to run automatically every night at &lt;strong&gt;02:00 AM&lt;/strong&gt; using root's crontab.&lt;/p&gt;

&lt;p&gt;Open root's crontab editor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo crontab -e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following line at the bottom of the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 2 * * * /usr/local/bin/server-backup.sh &amp;gt;&amp;gt; /var/log/server-backup.log 2&amp;gt;&amp;amp;1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This schedules the script for minute &lt;code&gt;0&lt;/code&gt;, hour &lt;code&gt;2&lt;/code&gt; every day, and appends both standard output (&lt;code&gt;stdout&lt;/code&gt;) and errors (&lt;code&gt;stderr&lt;/code&gt;) to &lt;code&gt;/var/log/server-backup.log&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To prevent &lt;code&gt;/var/log/server-backup.log&lt;/code&gt; from growing indefinitely over months of operation, create a simple &lt;strong&gt;logrotate&lt;/strong&gt; rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo tee /etc/logrotate.d/server-backup &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s1"&gt;'EOF'&lt;/span&gt;
/var/log/server-backup.log &lt;span class="o"&gt;{&lt;/span&gt;
    weekly
    rotate 4
    compress
    missingok
    notifempty
&lt;span class="o"&gt;}&lt;/span&gt;
EOF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 5 — Test a Full Disaster Recovery Restore
&lt;/h2&gt;

&lt;p&gt;A backup strategy is only as reliable as your last restore test. Verify that you can download and decrypt an archive back onto a clean directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mkdir -p /tmp/restore-test
sudo rclone copy secure-backup: /tmp/restore-test/ --max-age 1h --config /root/.config/rclone/rclone.conf
ls -lh /tmp/restore-test/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Extract the downloaded archive into &lt;code&gt;/tmp/restore-test/extracted&lt;/code&gt; and inspect the restored directory tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mkdir -p /tmp/restore-test/extracted
tar -xzf /tmp/restore-test/*.tar.gz -C /tmp/restore-test/extracted
ls -la /tmp/restore-test/extracted/etc/nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>automation</category>
      <category>devops</category>
      <category>linux</category>
      <category>security</category>
    </item>
    <item>
      <title>How to Deploy a Dockerized Web App Behind an Nginx Reverse Proxy with Free SSL on Ubuntu</title>
      <dc:creator>BRIAN MWOHI</dc:creator>
      <pubDate>Sun, 22 Nov 2026 10:01:00 +0000</pubDate>
      <link>https://dev.to/bmwohi254/how-to-deploy-a-dockerized-web-app-behind-an-nginx-reverse-proxy-with-free-ssl-on-ubuntu-2j14</link>
      <guid>https://dev.to/bmwohi254/how-to-deploy-a-dockerized-web-app-behind-an-nginx-reverse-proxy-with-free-ssl-on-ubuntu-2j14</guid>
      <description>&lt;p&gt;published: false&lt;br&gt;
description: A step-by-step guide to running a Docker container bound to localhost, routing traffic through Nginx, and automating HTTPS with Let's Encrypt on Ubuntu 24.04.&lt;/p&gt;
&lt;h2&gt;
  
  
  tags: devops, docker, nginx, linux
&lt;/h2&gt;

&lt;p&gt;Running a containerized web application directly on a public port like &lt;code&gt;:3000&lt;/code&gt; or &lt;code&gt;:8080&lt;/code&gt; works during local development, but it creates serious security and reliability gaps in production. Exposing a container port directly to the internet leaves your traffic unencrypted over plain HTTP, makes managing multiple services on a single server difficult, and bypasses reverse-proxy protections like rate limiting and clean header forwarding.&lt;/p&gt;

&lt;p&gt;In this guide, you will deploy a containerized web service on an &lt;strong&gt;Ubuntu 24.04 LTS&lt;/strong&gt; server using a production-ready architecture. Instead of exposing the container to the public internet, you will bind it strictly to &lt;code&gt;127.0.0.1&lt;/code&gt;, route incoming requests through &lt;strong&gt;Nginx&lt;/strong&gt;, and secure your domain with an auto-renewing &lt;strong&gt;Let's Encrypt&lt;/strong&gt; SSL certificate.&lt;/p&gt;
&lt;h3&gt;
  
  
  Architecture Overview
&lt;/h3&gt;

&lt;p&gt;By the end of this tutorial, all external traffic will follow this request flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Internet (HTTPS :443) ──▶ UFW Firewall ──▶ Nginx Reverse Proxy ──▶ Docker Container (127.0.0.1:8080)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before you begin, make sure you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An &lt;strong&gt;Ubuntu 24.04 LTS&lt;/strong&gt; cloud server (VPS) with at least 1 GB of RAM and a non-root user with &lt;code&gt;sudo&lt;/code&gt; privileges.&lt;/li&gt;
&lt;li&gt;A registered domain or subdomain (such as &lt;code&gt;://yourdomain.com&lt;/code&gt;) with a DNS &lt;strong&gt;A record&lt;/strong&gt; pointing to your server's public IPv4 address.&lt;/li&gt;
&lt;li&gt;SSH access to your server.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Replace &lt;code&gt;://yourdomain.com&lt;/code&gt; with your actual domain name throughout every command and configuration file in this tutorial.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 1 — Configure the Firewall and Install Dependencies
&lt;/h2&gt;

&lt;p&gt;Start by updating your server's package index and installing the prerequisite packages needed to add Docker's official repository alongside Nginx.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo apt update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; sudo apt upgrade -y
sudo apt install -y ca-certificates curl gnupg nginx ufw
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, add Docker's official GPG key and repository so you install the maintained Docker Engine rather than an outdated distribution package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://docker.com &lt;span class="o"&gt;|&lt;/span&gt; sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

&lt;span class="nb"&gt;echo&lt;/span&gt; \
  &lt;span class="s2"&gt;"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://docker.com \
  $(. /etc/os-release &amp;amp;&amp;amp; echo "&lt;/span&gt;&lt;span class="nv"&gt;$VERSION_CODENAME&lt;/span&gt;&lt;span class="s2"&gt;") stable"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; \
  sudo tee /etc/apt/sources.list.d/docker.list &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null

sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add your non-root user to the &lt;code&gt;docker&lt;/code&gt; group so you can manage containers without prefixing every command with &lt;code&gt;sudo&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo usermod -aG docker &lt;span class="nv"&gt;$USER&lt;/span&gt;
newgrp docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now configure &lt;strong&gt;UFW (Uncomplicated Firewall)&lt;/strong&gt; to allow SSH connections and standard web traffic (&lt;code&gt;80&lt;/code&gt; and &lt;code&gt;443&lt;/code&gt; via the &lt;code&gt;Nginx Full&lt;/code&gt; profile) while blocking all other incoming ports:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo ufw allow OpenSSH
sudo ufw allow &lt;span class="s1"&gt;'Nginx Full'&lt;/span&gt;
sudo ufw --force &lt;span class="nb"&gt;enable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify both your firewall rules and Docker installation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo ufw status
docker --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see output confirming that ports &lt;code&gt;22&lt;/code&gt;, &lt;code&gt;80&lt;/code&gt;, and &lt;code&gt;443&lt;/code&gt; are open and Docker is installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

Docker version 27.3.1, build ce12230
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 2 — Run the Sample Container Bound to Localhost
&lt;/h2&gt;

&lt;p&gt;When developers run &lt;code&gt;docker run -p 8080:80 ...&lt;/code&gt;, Docker binds port &lt;code&gt;8080&lt;/code&gt; to &lt;code&gt;0.0.0.0&lt;/code&gt; (every network interface on the host) and automatically modifies &lt;code&gt;iptables&lt;/code&gt; rules—often bypassing UFW completely. That means anyone who knows your server's IP address could visit &lt;code&gt;http://YOUR_SERVER_IP:8080&lt;/code&gt; and bypass Nginx and HTTPS altogether.&lt;/p&gt;

&lt;p&gt;To prevent this, explicitly bind the host port to &lt;code&gt;127.0.0.1&lt;/code&gt; so only processes running on the same server (like Nginx) can reach the container.&lt;/p&gt;

&lt;p&gt;Launch a lightweight demo web container (&lt;code&gt;nginxdemos/hello&lt;/code&gt;) bound strictly to localhost:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run -d \
  --name web-app \
  --restart unless-stopped \
  -p 127.0.0.1:8080:80 \
  nginxdemos/hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what each flag accomplishes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-d&lt;/code&gt; runs the container in detached mode in the background.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--name web-app&lt;/code&gt; assigns a predictable name for logs and restarts.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--restart unless-stopped&lt;/code&gt; ensures the container starts automatically if the server reboots.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-p 127.0.0.1:8080:80&lt;/code&gt; maps container port &lt;code&gt;80&lt;/code&gt; exclusively to port &lt;code&gt;8080&lt;/code&gt; on the host's loopback interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Confirm the container is running and responds locally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker ps
curl -I http://127.0.0.1:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP/1.1 200 OK
Server: nginx/1.25.3
Content-Type: text/html
Connection: keep-alive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 3 — Configure Nginx as a Reverse Proxy
&lt;/h2&gt;

&lt;p&gt;Now configure Nginx to accept public requests for &lt;code&gt;://yourdomain.com&lt;/code&gt; and forward them internally to &lt;code&gt;http://127.0.0.1:8080&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Create a dedicated server block configuration file in &lt;code&gt;/etc/nginx/sites-available/&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/://yourdomain.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paste the following configuration, replacing &lt;code&gt;://yourdomain.com&lt;/code&gt; with your actual domain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;listen&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;server_name&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//yourdomain.com;&lt;/span&gt;

    &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;proxy_pass&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//127.0.0.1:8080;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_http_version&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="nc"&gt;Host&lt;/span&gt; $&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Real&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="no"&gt;IP&lt;/span&gt; $&lt;span class="n"&gt;remote_addr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Forwarded&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;For&lt;/span&gt; $&lt;span class="n"&gt;proxy_add_x_forwarded_for&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Forwarded&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Proto&lt;/span&gt; $&lt;span class="n"&gt;scheme&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="nc"&gt;Upgrade&lt;/span&gt; $&lt;span class="n"&gt;http_upgrade&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;proxy_set_header&lt;/span&gt; &lt;span class="nc"&gt;Connection&lt;/span&gt; &lt;span class="s2"&gt;"upgrade"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why These Proxy Headers Matter
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Host $host&lt;/code&gt; preserves the original domain requested by the client rather than passing &lt;code&gt;127.0.0.1:8080&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;X-Real-IP&lt;/code&gt; and &lt;code&gt;X-Forwarded-For&lt;/code&gt; pass the visitor's real IP address to your application logs instead of showing every request as coming from &lt;code&gt;127.0.0.1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;X-Forwarded-Proto&lt;/code&gt; lets your backend app know whether the visitor connected over &lt;code&gt;http&lt;/code&gt; or &lt;code&gt;https&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Save and close the file (&lt;code&gt;Ctrl + O&lt;/code&gt;, &lt;code&gt;Enter&lt;/code&gt;, &lt;code&gt;Ctrl + X&lt;/code&gt; in &lt;code&gt;nano&lt;/code&gt;). Next, enable the site by creating a symbolic link in &lt;code&gt;sites-enabled&lt;/code&gt;, remove the default welcome site if you no longer need it, and test your Nginx syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/://yourdomain.com /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the configuration test succeeds, reload Nginx to apply the changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo systemctl reload nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify that your domain now serves the containerized application over HTTP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -I http://://yourdomain.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 4 — Secure the Domain with Let's Encrypt SSL
&lt;/h2&gt;

&lt;p&gt;With HTTP routing confirmed, use &lt;strong&gt;Certbot&lt;/strong&gt; to issue a free TLS certificate from Let's Encrypt and automatically configure Nginx to redirect all HTTP traffic to HTTPS.&lt;/p&gt;

&lt;p&gt;Install Certbot and its Nginx plugin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo apt install -y certbot python3-certbot-nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run Certbot for your domain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo certbot --nginx -d ://yourdomain.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Certbot will prompt you for:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An email address for urgent renewal and security notices.&lt;/li&gt;
&lt;li&gt;Agreement to the Let's Encrypt Terms of Service.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once validated, Certbot modifies &lt;code&gt;/etc/nginx/sites-available/://yourdomain.com&lt;/code&gt; to attach your SSL certificate and enable automatic HTTP-to-HTTPS redirection.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verify HTTPS and Auto-Renewal
&lt;/h3&gt;

&lt;p&gt;First, verify that HTTP requests now return a &lt;code&gt;301 Moved Permanently&lt;/code&gt; redirect to HTTPS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -I http://://yourdomain.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HTTP/1.1 301 Moved Permanently
Server: nginx/1.24.0 (Ubuntu)
Location: https://://yourdomain.com/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, confirm that Certbot's scheduled systemd timer will renew your certificate automatically before its 90-day expiration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo certbot renew --dry-run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see &lt;code&gt;Congratulations, all simulated renewals succeeded&lt;/code&gt;, your TLS automation is ready for production.&lt;/p&gt;




&lt;h2&gt;
  
  
  Troubleshooting Common Errors
&lt;/h2&gt;

&lt;p&gt;Even with clean configs, two issues commonly trip up fresh deployments:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;code&gt;502 Bad Gateway&lt;/code&gt; in the Browser
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;502 Bad Gateway&lt;/code&gt; means Nginx is reachable, but it cannot connect to your upstream container on &lt;code&gt;127.0.0.1:8080&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Check container status:&lt;/strong&gt; Run &lt;code&gt;docker ps -a&lt;/code&gt; to see if the container exited with an error. Inspect logs with &lt;code&gt;docker logs web-app&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify port mapping:&lt;/strong&gt; Ensure the host port in &lt;code&gt;docker run -p 127.0.0.1:8080:80&lt;/code&gt; matches the &lt;code&gt;proxy_pass http://127.0.0.1:8080;&lt;/code&gt; directive in your Nginx config.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Certbot Fails Domain Validation
&lt;/h3&gt;

&lt;p&gt;Let's Encrypt must connect to your server over port &lt;code&gt;80&lt;/code&gt; to verify domain ownership.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Check DNS propagation:&lt;/strong&gt; Run &lt;code&gt;dig +short ://yourdomain.com&lt;/code&gt; and verify it returns your server's exact public IPv4 address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check cloud security groups:&lt;/strong&gt; In addition to UFW inside Ubuntu, confirm that your cloud provider's external firewall or Security Group (AWS, DigitalOcean, Vultr, etc.) allows inbound TCP traffic on ports &lt;code&gt;80&lt;/code&gt; and &lt;code&gt;443&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion &amp;amp; Next Steps
&lt;/h2&gt;

&lt;p&gt;You now have a containerized web application running behind an Nginx reverse proxy on Ubuntu 24.04, isolated from direct public port access and encrypted with automated Let's Encrypt SSL certificates.&lt;/p&gt;

&lt;p&gt;From here, you can expand this setup by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Replacing the single &lt;code&gt;docker run&lt;/code&gt; command with a &lt;code&gt;docker-compose.yml&lt;/code&gt; file to manage multi-container stacks (such as a backend API plus Redis or PostgreSQL).&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>devops</category>
      <category>docker</category>
      <category>linux</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
