Deploy Wordpress, MySQL, Nginx Containers with HTTPS, SSL on GCP

leangaurav
6 min readDec 12, 2019

Before we start setup part let me tell you about the things that made me struggle the most:

  • HTTP to HTTPS redirection
  • Installing SSL Certificate: So that browser doesn’t block your website
  • Apache vs Nginx: Which web server to use
  • Lots of commands: And they need to be in some order

One solution to all of the above problems was Docker. What we can do is use pre-built docker images where we just need to set some values and the job is done. It’s better if you read about docker and try to understand it. But for most of our job we will need to run some basic commands.

Docker hub already has images for letsencrypt(for SSL), wordpress, Nginx, MySQL. So we don’t need to install these our self. Now it’s time to get your hands dirty. So lets get started.

Better type the command first in some plain text editor (notepad, notepad++, sublime etc.) and then paste. Reason is: first all domain names should be correct, so verify you type everything what matches with cloudflare DNS settings; second we will need same command again if it works. This reduces chance of typo errors(most difficult ones to fix).

Step-1: Open SSH window/Terminal

Go back to GCP and head on to VM Instances page. There you will find your VM which you created earlier. There will be a button next to your VM details saying SSH. Click on that and wait for it to open a window.

Your new window will be a black terminal with a prompt ending with $.

Step-2 : Install Docker and Docker-Compose

You can follow instructions from official website for both docker and docker-compose or follow these commands one by one.

sudo apt updatesudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-commoncurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"sudo apt-get updatesudo apt-get install docker-ce docker-ce-cli containerd.io

Press y whenever asked for yes/no. Above commands will install docker.

Now we need to install docker-compose.

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composesudo chmod +x /usr/local/bin/docker-composesudo groupadd dockersudo usermod -aG docker $USERsudo chmod 666 /var/run/docker.sock

To verify docker-compose got installed. Type docker-compose on the prompt and hit enter. You will get some messages telling how to use docker-compose.

Step-3: Clone git repository

Go to below github repository:

Click on Clone or download button and then click on the copy button.

Now go back to terminal and type there: git clone followed by that paste the url which you just copied.

Or you can simply copy paste this command 😛

git clone https://github.com/leangaurav/wordpress_docker.git

This will download the repository in your current location. You can check that by running ls command on terminal. This will show you a name of folder:

wordpress_docker

Step-4: Deploy docker image

Now go to folder you just downloaded.

cd wordpress_docker

If you do ll command (it’s LL in lower case) in worpress_docker folder, you will see a bunch of files.

Of these, we are interested in setup.sh. First thing we need to do is change its permission to executable.(Most likely it will be having permission but still) Run this:

chmod 700 setup.sh

Now the next command is the most important one. Don’t run it directly.

./setup.sh -t -d=yourdomainname

replace yourdomainname with your actual domain name.
Example: if you bought a domain with name ‘example.com’ then it should look like this:

./setup.sh -t -d=example.com

This will generate a bunch of new files in the folder, which you can again check with ll command.

For people who want to use wordpress-multisite with sub-domain option, I already have code for that, but the usage will be updated in some time. Or you can check the readme.md in github repository.

Step-5: Strike Iron

Now everything is ready. We need to run one last command to see if all of this works. Run:

docker-compose up

You will see a bunch of things running automatically and lots of things getting printed. Wait till you see “Server Ready” message (not more than 2–4 minutes).

Now type the IP address of your VM(one that we reserved as a static IP address on GCP) in browser(prefer firefox)and hit enter. (Also try to open website with your domain name)

If your browser shows security risk, then you need to accept risk and continue for now (try doing it in firefox).

It should take you to the wordpress setup page (don’t go ahead with the setup right now):

Congratulations !! your setup works 😃

But this is not your last thing. What we did right now was to deploy a test website with a staging/testing SSL certificate.

Why? If somehow the setup doesn’t work, then you will have to repeat this multiple which may request for a certificate again and again. There is a limit to number of certificate you can request to letsencrypt in a week. But rate limits are higher for staging certificates.

The setup command we ran earlier had -t flag with it.

./setup.sh -t -d=yourdomainname

Now what you need to press Ctrl+c and stop the running docker server in your terminal. Then run the same setup command again but after removing -t this time.

./setup.sh -d=yourdomainname

Then again run docker-compose up but again there is a slight change.

docker-compose up -d

Notice the -d at end. What this does is that it will run docker in detached mode i.e. the server will not keep waiting on the prompt and the command would immediately finish. So that once your setup works you can close the terminal window without killing your docker server.

Once command finishes, wait for a minute or two and then again go to the IP address. If you see the wordpress setup page again then you just need to test it via your domain name instead of Ip address. If that also works, then we are finally done 😄

Now all you need to do is setup wordpress and build you website. (Setting up wordpress and customization has its own learning curve).

--

--

leangaurav

Engineer | Trainer | writes about Practical Software Engineering | Find me on linkedin.com/in/leangaurav | Discuss anything topmate.io/leangaurav