Written by: Sanjeev

Useful Scripts To Automate Your Work On AWS LightSail WordPress Install

400 Shares More 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 …

Elegant Themes

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.

Automate General Tasks On AWS LightSail
Photo Source: Unsplash

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.

mkdir /opt/bitnami/user-scripts

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.


Suggested Read: [Review] Designrr Ebook Creator For Professionally Designed Ebooks

Shell Scripting

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.

#!/bin/bash

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”.

#!/bin/bash
apt update
apt upgrade
apt autoclean

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

chmod +x /opt/bitnami/user-scripts/server-upgrade.sh

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.

sudo ./opt/bitnami/user-scripts/server-upgrade.sh

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.

apt upgrade -y

Now, the script will not stop for manual inputs and will install the packages automatically.


Suggested Read: Social Warfare Vs Monarch – Which Is The Best Social Share Plugin For WordPress

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.

#!/bin/bash
touch /opt/bitnami/apache2/var/cache/mod_pagespeed/cache.flush
/opt/bitnami/ctlscript.sh restart apache

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.

chmod +x /opt/bitnami/user-scripts/clear-pagespeed-cache.sh

Suggested Read: Best WordPress Themes Made Exclusively For Page Builders

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.

#!/bin/bash
./server-upgrade.sh
/opt/bitnami/letsencrypt/certbot-auto renew
/opt/bitnami/ctlscript.sh restart apache

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.

chmod +x /opt/bitnami/user-scripts/renew-cert.sh

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.

cd /opt/bitnami/user-scripts/

Once you are in the folder, you can use the “ls” command to list all the available scripts.

AWS LightSail Server User Scripts Forder Screenshot

Most of these functions need root access while executing so you can use the below command to execute the scripts.

sudo ./script-name.sh

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.

Full Disclosure: This post may contain affiliate links, meaning that if you click on one of the links and purchase an item, we may receive a commission (at no additional cost to you). We only hyperlink the products which we feel adds value to our audience. Financial compensation does not play a role for those products.

Photo of author

About Sanjeev

A passionate blogger and technology enthusiast with more than 20 years of experience in enterprise software development. Over 12 Years of experience in successfully building blogs from scratch.

MetaBox

  • How to use sftp to transfer scripts as we are using pem key in putty for connection and we dont know password

    • Hi Fahad,

      You need to use the key-based SFTP connection only. Most of the FTP software like Filezilla provides it. If the software you are using needs a ppk key rather than pem key, you can use the puttygen software to convert your key.

      – Sanjeev

  • Subscribe to Exclusive Tips & Tricks

    MetaBlogue

    MetaBlogue is an online publication which covers WordPress Tips, Blog Management, & Blogging Tools or Services reviews.

    >
    400 Shares
    Share via
    Copy link