|
| 1 | +--- |
| 2 | +title: How to enable SSL with Let's Encrypt on a single ClickHouse server |
| 3 | +date: 2024-12-11 |
| 4 | +--- |
| 5 | + |
| 6 | +The following steps can be used to enable SSL for a single ClickHouse Server using [Let's Encrypt](https://letsencrypt.org/), a free, automated, and open Certificate Authority (CA) designed to make it easy for anyone to secure their websites with HTTPS. By automating the certificate issuance and renewal process, Let's Encrypt ensures websites remain secure without requiring manual intervention. |
| 7 | + |
| 8 | +**We assume ClickHouse has been installed at the standard package locations in the following guide. We use the domain `product-test-server.clickhouse.com` for all examples. Substitute your domain accordingly.** |
| 9 | + |
| 10 | + |
| 11 | +1. Verify you have a DNS `A` or `AAAA` record pointing to your server. This can be achieved using the Linux tool `dig.` For example, the response for `product-test-server.clickhouse.com` if using the Cloudflare DNS server `1.1.1.1`: |
| 12 | + |
| 13 | +```bash |
| 14 | +dig @1.1.1.1 product-test-server.clickhouse.com |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +``` |
| 19 | + |
| 20 | +Notice the section, |
| 21 | + |
| 22 | +```bash |
| 23 | + |
| 24 | + |
| 25 | +``` |
| 26 | + |
| 27 | +Confirming the presence of an A record. |
| 28 | + |
| 29 | +2. Open port 80 on your server. This port will be used for automatic certificate renewal using the ACME protocol with certbot. For AWS, this can be achieved by [modifying the instance's associated Security Group](https://repost.aws/knowledge-center/connect-http-https-ec2). |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +3. Install [`certbot`](https://certbot.eff.org/instructions) e.g. using `apt` |
| 34 | + |
| 35 | +```bash |
| 36 | +sudo apt install certbot |
| 37 | +``` |
| 38 | + |
| 39 | +4. Obtain an SSL certificate |
| 40 | + |
| 41 | +```bash |
| 42 | +sudo certbot certonly |
| 43 | +``` |
| 44 | + |
| 45 | +:::note |
| 46 | +If you don't have a web server running on your server, use (1) so Certbot can use a standalone temporary web server. |
| 47 | +::: |
| 48 | + |
| 49 | +Enter the full domain name of your server e.g. `product-test-server.clickhouse.com` when requested. |
| 50 | + |
| 51 | +::note |
| 52 | +Let's Encrypt has a policy of not issuing certificates for certain types of domains, such as public cloud provider-generated domains (e.g., AWS *.compute.amazonaws.com domains). These domains are considered shared infrastructure and are blocked for security and abuse prevention reasons. |
| 53 | +::: |
| 54 | + |
| 55 | +5. Copy certificates to the ClickHouse directory. |
| 56 | + |
| 57 | +```bash |
| 58 | +echo '* * * * * root cp -u /etc/letsencrypt/live/product-test-server.clickhouse.com/*.pem /etc/clickhouse-server/ && chown clickhouse:clickhouse /etc/clickhouse-server/*.pem && chmod 400 /etc/clickhouse-server/*.pem' | sudo tee /etc/cron.d/copy-certificates |
| 59 | +``` |
| 60 | + |
| 61 | +This command sets up a cron job to automate the management of Let's Encrypt SSL certificates for a ClickHouse server. It runs every minute as the root user, copying the .pem files from the Let's Encrypt directory to the ClickHouse server's configuration directory, but only if the files have been updated. After copying, the script adjusts the ownership of the files to the clickhouse user and group, ensuring the server has the required access. It also sets secure read-only permissions (`chmod 400`) on the copied files to maintain strict file security. This ensures that the ClickHouse server always has access to the latest SSL certificates without requiring manual intervention, maintaining security and minimizing operational overhead. |
| 62 | + |
| 63 | +6. Configure the use of these certificates in clickhouse-server. |
| 64 | + |
| 65 | +```bash |
| 66 | +echo" |
| 67 | +https_port: 8443 |
| 68 | +openSSL: |
| 69 | + server: |
| 70 | + certificateFile: '/etc/clickhouse-server/fullchain.pem' |
| 71 | + privateKeyFile: '/etc/clickhouse-server/privkey.pem' |
| 72 | + disableProtocols: 'sslv2,sslv3,tlsv1,tlsv1_1' |
| 73 | +"| sudo tee /etc/clickhouse-server/config.d/ssl.yaml |
| 74 | +``` |
| 75 | + |
| 76 | +7. Restart ClickHouse Server |
| 77 | + |
| 78 | +```bash |
| 79 | +sudo clickhouse restart |
| 80 | +``` |
| 81 | + |
| 82 | +8. Validate ClickHouse can communicate over SSL |
| 83 | + |
| 84 | +```bash |
| 85 | +curl https://product-test-server.clickhouse.com:8443/ |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +``` |
0 commit comments