--- title: "How to Encrypt Data in Transit on Ubuntu 24.04 with TLS" description: "Encrypt data in transit on Ubuntu 24.04: HTTPS for Nginx with Let's Encrypt, TLS-only MySQL connections and hardened SSH ciphers, then verify each one works." category: "Security Compliance" language: en source: https://cubepath.com/docs/security-compliance/data-encryption-in-transit updated: 2026-09-25 --- # How to Encrypt Data in Transit on Ubuntu 24.04 with TLS Encryption in transit protects data while it travels between clients and your server, so nobody on the network path can read or modify it. Most compliance frameworks (PCI DSS, HIPAA, GDPR's "appropriate technical measures") expect it for any traffic that carries credentials or personal data. In this tutorial you will secure the three channels a typical Ubuntu 24.04 server exposes: HTTPS on Nginx with a Let's Encrypt certificate, TLS-only connections to MySQL, and SSH restricted to modern ciphers. After each change you will verify from the outside that the encryption is actually in place. ## Prerequisites To follow this tutorial you need: - A server running Ubuntu 24.04 LTS, for example a CubePath VPS, with a non-root user that has `sudo` privileges. - A registered domain name with an `A` record pointing to the server's public IP. This guide uses `your_domain` as a placeholder. - Nginx installed and serving a site for `your_domain` on port 80 (`sudo apt install nginx`). - MySQL 8.0 installed from the Ubuntu repositories (`sudo apt install mysql-server`) and an application user, called `app_user` here, if you want to follow Step 4. - UFW allowing SSH, HTTP and HTTPS: ```bash sudo ufw allow OpenSSH sudo ufw allow 'Nginx Full' sudo ufw enable ``` ## Step 1 - Obtaining a TLS certificate for Nginx Let's Encrypt issues free, browser-trusted certificates, and Certbot's Nginx plugin requests the certificate and edits your server block for you. Install Certbot and the plugin from the Ubuntu repositories: ```bash sudo apt update sudo apt install certbot python3-certbot-nginx ``` Request a certificate for your domain. The `--redirect` flag makes Certbot add a permanent redirect from HTTP to HTTPS, so no request is ever served in plain text: ```bash sudo certbot --nginx --redirect -d your_domain -d www.your_domain ``` Certbot asks for an email address for expiry notices and for acceptance of the terms of service. When it finishes you see a message like this: ```output Successfully received certificate. Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem Key is saved at: /etc/letsencrypt/live/your_domain/privkey.pem ... Successfully deployed certificate for your_domain to /etc/nginx/sites-enabled/your_domain ``` Certificates are valid for 90 days. The `certbot` package installs a systemd timer that renews them automatically. Confirm the timer is active and that a renewal would succeed: ```bash systemctl list-timers certbot.timer sudo certbot renew --dry-run ``` The dry run should end with `Congratulations, all simulated renewals succeeded`. ## Step 2 - Hardening the Nginx TLS configuration Certbot's server block includes `/etc/letsencrypt/options-ssl-nginx.conf`, which already limits the protocols to TLS 1.2 and TLS 1.3 and sets a reasonable cipher list. What it does not add is HTTP Strict Transport Security (HSTS), which tells browsers to never try plain HTTP for your domain again, even if a user types `http://`. Open the server block that Certbot modified: ```bash sudo nano /etc/nginx/sites-available/your_domain ``` Inside the `server` block that contains `listen 443 ssl`, add the HSTS header below the Certbot lines: ```nginx server { server_name your_domain www.your_domain; root /var/www/your_domain; listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot add_header Strict-Transport-Security "max-age=31536000" always; location / { try_files $uri $uri/ =404; } } ``` > Warning: Once a browser has seen the HSTS header it refuses plain HTTP for your domain for the whole `max-age` period. Only add it when HTTPS works for every subdomain you serve, and do not add `includeSubDomains` unless that is true for all of them. Test the configuration and reload Nginx: ```bash sudo nginx -t sudo systemctl reload nginx ``` `nginx -t` should report `syntax is ok` and `test is successful`. ## Step 3 - Verifying HTTPS from the outside Run these checks from your local machine, not from the server, so they follow the same path as a real visitor. First confirm that plain HTTP redirects to HTTPS: ```bash curl -sI http://your_domain | head -n 3 ``` ```output HTTP/1.1 301 Moved Permanently Server: nginx/1.24.0 (Ubuntu) Date: ... ``` Then check that the HTTPS response carries the HSTS header: ```bash curl -sI https://your_domain | grep -i strict-transport ``` ```output strict-transport-security: max-age=31536000 ``` Check which protocol and cipher are negotiated: ```bash openssl s_client -connect your_domain:443 -servername your_domain -brief Important: Keep your current SSH session open and test a new connection from a second terminal before you log out. If the new connection fails, you can still fix the file from the open session. From your local machine, confirm which algorithms were negotiated: ```bash ssh -v your_user@your_server_ip exit 2>&1 | grep -E 'kex: algorithm|kex: server->client' ``` ```output debug1: kex: algorithm: sntrup761x25519-sha512@openssh.com debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: compression: none ``` ## Troubleshooting - **Certbot fails with `Timeout during connect` or `Connection refused`:** Let's Encrypt must reach port 80 on your server. Check that the `A` record points to the right IP (`dig +short your_domain`) and that `sudo ufw status` shows `Nginx Full` allowed. - **MySQL client error `SSL connection error: SSL is required but the server doesn't support it`:** the server is not loading its certificates. Check the files in `/var/lib/mysql/*.pem` are owned by `mysql` and review `sudo journalctl -u mysql`. - **Old SSH clients fail with `no matching cipher found` or `no matching key exchange method found`:** the client is too old for the restricted list. Update the client rather than re-enabling legacy algorithms on the server. ## Conclusion Your web traffic now uses TLS 1.2 or 1.3 with automatic certificate renewal and HSTS, MySQL rejects any TCP connection without TLS, and SSH negotiates only modern algorithms. Each setting was verified from the client side, which is the evidence an auditor will ask for. As next steps, move traffic between your own servers onto a private network or a WireGuard tunnel, enable TLS on any other service that listens on a public port (mail, Redis, message brokers), and add certificate expiry to your monitoring.