If you are using WordPress on AWS LightSail and having difficulty remembering all the commands, here is an easy way to automate many of your day-to-day tasks.
AWS LightSail uses Linux as a base operating system for the WordPress image. So most of the work on the server needs to be done through line commands. If you are familiar with the Linux operating system then you will be comfortable with those commands.
But if you are not from computer background or doesn’t have much experience from Linux, it might be overwhelming to remember all those.
Automate Tasks on AWS LightSail
What we are going to do in this post is to look at some of the common tasks which every webmaster needs to do on AWS LightSail. We will see how we can easily automate and schedule some of the recurring tasks on your AWS LightSail server.
This way once done, you don’t need to remember any commands. You can easily execute these scripts and that work will be done.
This is one of the constant demands on all of my AWS LightSail Tutorials. People want to automate the SSL certificate renewal and forget about it rather than keep doing it every three months.
Create User-scripts Folder
I always prefer a modularised approach and separating out responsibilities. So I prefer creating a separate folder for the user created scripts. This way you know what you have created and can manage it easily.
I always create a folder under /opt/bitnami/ as most of the server stuff is inside that folder but if you prefer any other location, just change the folder location in the below commands.
To create a folder for user created scripts, connect to your AWS instance through shell and enter the below command on the command line.
This command will create a folder names “user-scripts” inside /opt/bitnami/. We will use this folder to store all of our scripts going forward.
If you are not aware about the mkdir line command, here is a good tutorial which can help in learning about it. Though, we already have parent folder available so above command should work fine for our work.
Most of the work on the AWS LightSail server is done through line commands and Shell scripting is a good way to combine multiple line commands and invoke them easily.
There are many different kind of scripting languages but because we are going to use Linux commands in our script, we will use Bash for our work. You need to indicate it in the first line of your script by adding the below line.
Make sure this is the first line of each of your script as it tells the operating system about the content of your file. With this line, the server will understand to execute the following commands with the bash.
If you want to become more comfortable with Shell scripting, here is a good hands-on tutorial. You can create a 1-month free AWS basic server instance and practice shell scripting before doing it on your primary server.
Update AWS LightSail Server Patches
One of the common task which every webmaster needs to do is update server to install newer patches. Currently, there is no way to update the Bitnami image to latest version automatically but you can still update the server to install the newer OS and services patches.
Now let’s create our first shell script “server-upgrade.sh”. You can copy the below code and paste it in a text file, save that text file with the name of “server-upgrade.sh”.
You can use the SFTP to upload this file on your AWS LightSail server. Upload it in the folder “user-scripts” which we have created in the above step.
You need to make sure this script is executable, so you need to change the permissions on the server. Enter the below command on the server shell
This will make sure that you can directly execute this script from the shell. Use the below command to execute this script and upgrade the server.
This will run all three shell commands and fetch the update information, upgrade packages and auto clean the server after that. The only thing is that it might need input at a certain location by asking [Y/n] options. If you want to skip that also, you can edit the upgrade command in the script as mentioned below.
Now, the script will not stop for manual inputs and will install the packages automatically.
Clearing Pagespeed Cache
Another common task is clearing the Pagespeed cache when you have made some changes and have Pagespeed enabled on the server.
So we will create a new script called “clear-pagespeed-cache” by copying the below lines to a text file.
What we are doing in the above two commands are clearing the Pagespeed cache and restart the apache server. You can upload this file in your “user-scripts” folder on your server. Change the permissions with the help of below commands to make it executable.
Renew SSL Certificates
Another good use of scripts are automating the SSL certificate renewal. If you have followed our SSL certificate generation guide for AWS LightSail, you have to renew the certificate every three months.
So here is a script which can help you renew your SSL certificate.
We can save this script as “renew-cert.sh” and upload it into our user scripts folder. Change the permissions with the below commands to make it executable.
If you would have noticed, we have used one of the earlier created scripts “server-upgrade.sh” in this script. This is a benefit of modularizing which gives us easy management of the functions.
In this script, we are upgrading the server, renewing the SSL certification and restarting the apache server.
How To Execute These Scripts
Whenever you want to perform any of the tasks, you can go to the user-scripts folder and execute the required scripts. Just open the shell and enter the below command to go to user-scripts folder.
Once you are in the folder, you can use the “ls” command to list all the available scripts.
Most of these functions need root access while executing so you can use the below command to execute the scripts.
Just replace the script-name with actual script name and it will execute it. So go ahead and automate some of the basic recurring tasks with the help of shell scripts. This will help you not remember all the commands and easily manage the server.
Let me know if you are looking for any other specific work to be automated. I will try to create the scripts and update the post with that automation.