There is a learning curve when you are a N00b at blogging. In other posts we learned how SEO and Analytics are useful and help the blogger. What if you thought of a new blog and want to quickly deploy a new blog site with LEMP and WordPress? Please someone Automate My Life!
Automate the tutorials for a server install at Digital Ocean whereby the person installing only has to know their IP and what they want their new user to be called.
Digital Ocean is a website where you can purchase a Virtual Private Server (VPS) for as little as $5 a month. They call their VPS a “droplet” and it’s very easy to create one.
Important: For this tutorial, we used a Ubuntu 16.04 (Linux) server install and we are also assuming you have a Linux local computer to make the OpenSSH connection. The code below will not work on Windows. Additionally, ONLY EXECUTE THIS CODE THROUGH TERMINAL (i.e. $python ~/path/autoLEMP.py). If you do not do this, you will miss some of the terminal editing when you run makeLemp() later.
First, let’s get a quick win by testing our Python code to see if this is even possible. We will be using Python’s standard Subprocess package.
Now we want to create a function that uses subprocess.Popen() to access a terminal instance on your local machine. Here we are just interested in listing what files are on your ~/Desktop.
You can test this function and whether or not it works by running runCommand(‘cd ~/Desktop; ls; cd‘)
Now that we can open a folder locally, our next obstacle
(and a painful one if you have never done this before like me) is to SSH through your local machine to your DO droplet and run commands on the remote server. Lucky for you that is what AML is all about.
When I spun up a new droplet for this tutorial, I quickly found that “ssh -t someUser@someIP” is a big deal to put in front of any bash command you are sending to a server. I would guess that each command creates it’s own SSH connection to your remote server and that is why it was important to write this function. The “-t” flag ensures that something called “tty” is executed in terminal, which allows you to enter your password when you use the sudo method. That is why we now have two separate run commands, runLocalCommand() and runServerCommand().
We now have everything we need to write a process expanding on the two command functions. All we do is copy/paste the individual bash commands from the Digital Ocean tutorials for initial server setup, LEMP install, WordPress install and LetsEncrypt into their own functions as you see fit. As you make the separate functions, you can then make one “main loop” function that executes the step-wise process.
In the above code, each major step of the DO tutorials have been documented in their own functions and are called by the main function makeLEMP(). Since you have executed this Python code from your terminal, whenever you need to enter your user’s password just copy/paste it into the terminal. The only time you will have to type will be when you modify the configuration files (which can be found in the DO tutorials).
Full AML Code:
Key Points: Global variables at the bottom for “newUser” and “myIP”. The function runOnce() should only be run one time. If your code exits to terminal for some reason, you will try to recreate a Superuser each time.