Deploy a LAMP Web App on Amazon Lightsail
TUTORIAL
Module 1: Create Your Lightsail PHP Application
In this module, you will configure a LAMP stack using the AWS CLI
Overview
What you will accomplish
In this module, you will:
- Create a Lightsail instance using the AWS CLI.
- Deploy your PHP application using the user data of the instance.
Minimum time to complete
10 minutes
Module prerequisites
- AWS Account with administrator-level access**
- Recommended browser: The latest version of Chrome or Firefox
[**]Accounts created within the past 24 hours might not yet have access to the services required for this tutorial.
Implementation
How to create a Lightsail instance
aws lightsail create-instances \
--instance-names <name_of_your_instance> \
--availability-zone <availability_zone> \
--blueprint-id <blueprint_id> \
--bundle-id <bundle_id> \
--key-pair-name <key_pair_name> \
--user-data <user-data>
The command expects the following parameters:
- an instance name
- an Availability Zone, where you want to deploy your instance
- the ID of a blueprint
- the ID of a bundle
- an SSH key pair to access your instance
- and user data, which will be executed at the start of your instance.
Note: A bundle refers to a preconfigured set of resources that determine the amount of memory, compute power, and storage that is available for an Amazon Lightsail instance. A blueprint, on the other hand, refers to the virtual machine image of the instance. This machine image includes the operation system and commonly used software applications that are configured on the machine.
Defining the user data
In our case, the user data will be used to deploy our LAMP stack. The following script will remove the default website of the blueprint, clone the sample app to replace it, set the appropriate file permissions, configure the auto-generated database password in the sample app's config file, and run the init.sql script to create the database and populate it with the initial values.
# remove default website
#-----------------------
cd /opt/bitnami/apache2/htdocs rm -rf *
# clone github repo
#------------------
/usr/bin/git clone -b loft https://github.com/mikegcoleman/todo-php.git .
# set write permissons on the settings file
#-----------------------------------
chown bitnami:daemon ./*
chmod 666 connectvalues.php
# inject database password into configuration file
#-------------------------------------------------
sed -i.bak "s/<password>/$(cat /home/bitnami/bitnami_application_password)/;" /opt/bitnami/apache2/htdocs/connectvalues.php
# create database
#----------------
cat /home/bitnami/htdocs/data/init.sql | /opt/bitnami/mariadb/bin/mysql -u root -p$(cat /home/bitnami/bitnami_application_password)
Creating an SSH key pair
To create an SSH key pair, you can use the AWS CLI. The following command will create an SSH key for you and save the public key in lightsailguide.pub and the private key in lightsailguide.
aws lightsail create-key-pair --key-pair-name LightsailGuide > ssh_key_response.json
cat ssh_key_response.json | jq -r '.publicKeyBase64' > lightsailguide.pub
cat ssh_key_response.json | jq -r '.privateKeyBase64' > lightsailguide
chmod 400 lightsailguide.pub lightsailguide
Creating the Lightsail instance
Now that we prepared everything, we can use the AWS CLI to create the instance. For this guide, we will be using the Ireland (eu-west-1) Region, and the LAMP blueprint with the blueprintId of lamp_7. If you want to see a full list of available blueprints, you can run the following command:
aws lightsail get-blueprints
aws lightsail get-bundles
# Create the Lightsail instance:
aws lightsail create-instances \
--instance-names "LightsailLampExample" \
--availability-zone eu-west-1a \
--blueprint-id lamp_7 \
--bundle-id micro_2_0 \
--key-pair-name LightsailGuide \
--user-data '# remove default website
#-----------------------
cd /opt/bitnami/apache2/htdocs rm -rf *
# clone github repo
#------------------
/usr/bin/git clone -b loft https://github.com/mikegcoleman/todo-php.git .
# set write permissons on the settings file
#-----------------------------------
chown bitnami:daemon ./*
chmod 666 connectvalues.php
# inject database password into configuration file
#-------------------------------------------------
sed -i.bak "s/<password>/$(cat /home/bitnami/bitnami_application_password)/;" /opt/bitnami/apache2/htdocs/connectvalues.php
# create database
#----------------
cat /home/bitnami/htdocs/data/init.sql | /opt/bitnami/mariadb/bin/mysql -u root -p$(cat /home/bitnami/bitnami_application_password)'
{
"operations": [
{
"id": "a49e1398-fb81-455a-8a50-3159c9bd9966",
"resourceName": "LightsailLampExample",
"resourceType": "Instance",
"createdAt": "2021-09-21T16:38:40.566000+02:00",
"location": {
"availabilityZone": "eu-west-1a",
"regionName": "eu-west-1"
},
"isTerminal": false,
"operationType": "CreateInstance",
"status": "Started",
"statusChangedAt": "2021-09-21T16:38:40.566000+02:00"
}
]
}
aws lightsail get-instance-state --instance-name LightsailLampExample
{
"state": {
"code": 16,
"name": "running"
}
}
aws lightsail get-instance --instance-name LightsailLampExample | jq -r .instance.publicIpAddress
Conclusion
In this first module, we learned how to create our infrastructure using the AWS CLI, and deploy a sample application. In the next module, we will learn how to clean up the resources used in this guide.
Up Next: Clean Up Resources