Setup an nginx-based webserver for multiple websites on Google Cloud Platform with Docker and Let’s Encrypt auto-updates

Steps

  1. Google Cloud Platform setup (VM instance, IP-address, domain)
  2. Setup Docker and docker-compose
  3. Obtain Let’s Encrypt certificate and auto-renewal

Google Cloud Platform setup

Create a VM instance

The first thing you will need is a VM instance in GCP. You can create VM instances very easily under “Computing Engine” > VM instances.

Create a static IP address

For that, we have to go over to “VPC network” > External IP addresses and “Reserve a static address”

Point your domain to the VM instance/IP address

Important note: Depending on where you’ve registered your domain, you additionally need to point the domain to the previously created IP address in your registrars settings. Since your registrar and all the other root-servers in the world don’t know nothing about your intention to host your website on GCP. The propagation of the DNS can take up to 48 hours. As a workaround you may want to edit your hosts-file and point the IP address manually.

Setup Docker

Install Docker and docker-compose

So to setup Docker we have to SSH into our VM instance and install docker there.

Choose a Docker image

When you search for pre-configured web-server images, you will come across many good ones. I like to work with the WebDevOps Docker images, since I find them to be the best configured ones and you can use them in your docker-compose right out of the box without further configuration.

docker run --name webtmp -d webdevops/php-nginx:ubuntu-17.04
docker cp webtmp:/opt/docker/etc/nginx .
docker stop webtmp && docker rm webtmp

Configure docker-compose.yml

We need to create a docker-compose.yml that will start up a Docker container for the nginx web-server and one for the MySQL-server.

Obtain a Let’s Encrypt certificate for your domain and setup auto-renewal cronjob

Obtain certificate

If the DNS propagation isn’t done by now, you have to postpone this step until it’s done.

docker run --rm -v nginx/ssl:/etc/letsencrypt -v html:/webroot certbot/certbot certonly --webroot
[[webroot_map]]
example.com = /webroot/example.com

Setup cronjob for renewal

Finally, we will create a cronjob that runs every day at 01:00AM to renew all the certificates.

crontab -e
0 1 * * * sh /home/YOUR_GCP_USERNAME/cronjob_renew_letsencrypt.sh

Done!

We’re done folks :-) As you can see, It’s that simple to set up a web-server on Google Cloud Platform with Docker.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adis Durakovic

Adis Durakovic

Digital Entrepreneur & Developer. Based in Vienna/Austria.