Home » AWS lightsail with Apache Server

AWS lightsail with Apache Server

How to create AWS Lightsail Instance with Apache server installed

AWS Lightsail is a great option for running a wide variety of websites and apps. From WordPress to other CMS like Ghost, joomla, Magenta, Joomla, Node.js and WordPress multisite, you have options to install and run any of them on a windows or linux server using an Amazon Lightsail instance.

Amazon Lightsail is fast and it is easy to install a server and start your site on it. This tutorial includes steps to install and run an Apache web server on an Amazon lightsail instance. There are two ways to create an instance. You can create it through your AWS management console or via AWS CLI and install the Apache webserver. However, instead of doing it in two steps, you can do it in one, meaning you can create the instance and simultaneously install and start Apache webserver on it.  Let’s first see how to do it through the AWS management console.

Create a lightsail instance with Apache server (Via AWS console)

Login to your AWS management console and search for Lightsail. Once on the Lightsail dashboard, you will find the button to create instances on the right. Click on the create instance button to create an instance.

In the first step, select the instance location or the zone where you want your instance to be. If the default option is fine, move on to the next step.

In the next step, select linux for your instance image and then select a blueprint for your instance. You have two options here which include Apps + OS and OS only. Click on OS only. The default option in this section is Amazon Linux 2. Select Ubuntu 22.04 for your instance.

Right below it, you will find the option to add a launch script and create key pair or use the default one. First, let’s add the launch script to your instance which will launch the Apache webserver on your instance, simultaneously as it is being deployed.

Click on add launch script to expand the launch script box. Inside the box, add the following lines:

$ sudo apt update -y

$ sudo apt-get install -y apache2

$ sudo systemctl start apache2

$ sudo systemctl enable apache2

These four lines of code will install and enable the Apache webserver on your instance.

In the next step, you can change the default ssh keypair, which you will need to connect with your instance from your preferred client.

Click on change SSH keypair and then on Create new. Give your newkeypair a name and once the keypair is created you can download it to your computer. Later, you will be able to connect with your instance through ssh or sftp using the kepair. If you need to upload website files to the instance using cyberduck, you can use the keypair (.pem file) to establish sftp connection.

Select an instance size for your instance. You have lightsail instances starting from $3.5/month to $160 per month. Amazon has newly added $80 and $160/month instance sizes to lightsail. The $10 plan on Amazon lightsail gives you 2GB RAM, 1vCPU, 60gb ssd storage and 3tb data transfer. Select the instance size that seems appropriate and then give your instance an easy to remember name like: my_instance.

You can also add tags to your instance and then move on to create the instance.

Click on create instance at the bottom and wait till the instance has been created and deployed.

Now, do nothing. Just copy the ip of your instance once its state has changed to running from pending. (Deployment does not last more than a few minutes. So, you will soon have a lightsail instance with Apache webserver running on it. Before connecting to your lightsail instance via ssh, you can verify if Apache has been properly installed on your instance.)

To verify that the webserver is properly installed and is in running state, just copy the ip of your instance and open it in a browser. If you see the Apache2 Default page with Ubuntu, it means you have successfully created a lightsail instance and installed Apache webserver on it.

Now, you can connect to you instance via SSH. The document root file is /var/www/html. If you open the index.html file inside this folder, you will see the html and css for the Apache home page there.

However, we will try to run our own static website on Apache server. To do that, we just need to create a vhost file for our website and add the necessary directives.

Create a different document root for your website. For example, if you have the website example.com, you can create a document root for it at /var/www/example.

$ sudo mkdir /var/www/example

Add an index.html file to this folder.

$ sudo nano /var/www/example/index.html

<!doctype html>
 <html>
   <head>
  
   </head>
   <body> <h2>Welcome to my blog!</h2>
     <p>This is my new website. Contact us to get a website like this – <strong>mail.example.com</strong>. Write to us to learn more about making wonderful sites.</p>
   </body>
 </html>

Now, go to the following file and change the document root from /var/www/html to /var/www/example (the new folder you created).

$ sudo nano /etc/apache2/sites-available/000-default.conf

The 000-default.conf file contains the default configurations for your website. Inside it, you will find:

DocumentRoot /var/www/html

Change it to /var/www/example or whatever you named the new folder.

Reload apache:

$sudo systemctl reload apache2

Now, go to the instance ip and reload it.

You will see the contents of the index.html file you just added to the new document root directory on the page.

You have successfully installed Apache webserver on your Lightsail instance and  hosted a static website on it.  The default virtual host file for your website is located at : /etc/apache2/sites-available/000-default.conf

and the Apache configuration files can be found at :

/etc/apache2/apache2.conf

You can add your own vhost file to the server in the sites-available folder.

Suppose you added a new virtual host file

$ /etc/apache2/sites-available/newwebsite.conf

You will need to ensite the new domain using :

$sudo a2ensite newwebsite.conf

Following that, remove the default vhost configuration using a2dissite :

$sudo a2dissite 000-default.conf

The new virtual host is active and you can upload your website files to the document root you created. Add ServerName and ServerAlias inside the new virtual host file. You can now add a custom domain and then install SSL after you have made the essential DNS changes (added A records).

Create Amazon lightsail instance with Apache server using the AWS CLI

The above part described how to install Apache webserver using the AWS management console. However, if you have the AWS CLI installed on your computer, you can easily create instances with Apache webserver using the CLI.

To create an aws lightsail instance, you will need to run the following command:

 $ aws lightsail create-instances

To create an instance you will need to specify a few more things including a name for the specific instance and then the blueprint id (APP+Os or OS only), and the bundle id.

Here are the two commands that come to your rescue:

$ aws lightsail get-blueprints

$ aws lightsail get-bundles

By running the first command, you will get a list of blueprints from which you can find the right blueprint id to create your lightsail instance. The second command helps you find the right instance size and bundle id for that size.

The output of the first command will look like this:

$ aws lightsail get-blueprints

{

    “blueprints”: [

{

            “blueprintId”: “ubuntu_22_04”,

            “name”: “Ubuntu”,

            “group”: “ubuntu_22”,

            “type”: “os”,

            “description”: “Ubuntu 22.04 – Jammy. Ubuntu Server delivers services reliably, predictably and economically. It is the perfect base on which to build your instances. Ubuntu is free, and you have the option to get support and Landscape from Canonical.”,

            “isActive”: true,

            “minPower”: 0,

            “version”: “22.04 LTS”,

            “versionCode”: “1”,

            “productUrl”: “https://aws.amazon.com/marketplace/pp/prodview-f2if34z3a4e3i”,

            “licenseUrl”: “https://aws.amazon.com/marketplace/pp/prodview-f2if34z3a4e3i#pdp-usage”,

            “platform”: “LINUX_UNIX”

        },

{

            “blueprintId”: “ubuntu_20_04”,

            “name”: “Ubuntu”,

            “group”: “ubuntu_20”,

            “type”: “os”,

            “description”: “Ubuntu 20.04 LTS – Focal. Lean, fast and powerful, Ubuntu Server delivers services reliably, predictably and economically. It is the perfect base on which to build your instances. Ubuntu is free and will always be, and you have the option to get support and Landscape from Canonical.”,

            “isActive”: true,

            “minPower”: 0,

            “version”: “20.04 LTS”,

            “versionCode”: “1”,

            “productUrl”: “https://aws.amazon.com/marketplace/pp/B087QQNGF1”,

            “licenseUrl”: “https://d7umqicpi7263.cloudfront.net/eula/product/aced0818-eef1-427a-9e04-8ba38bada306/0f54e60e-b0e5-43dc-9e02-c7a340498b1b.txt”,

            “platform”: “LINUX_UNIX”

        },

        {

            “blueprintId”: “ubuntu_18_04”,

            “name”: “Ubuntu”,

            “group”: “ubuntu_18”,

            “type”: “os”,

            “description”: “Ubuntu 18.04 LTS – Bionic. Lean, fast and powerful, Ubuntu Server delivers services reliably, predictably and economically. It is the perfect base on which to build your instances. Ubuntu is free and will always be, and you have the option to get support and Landscape from Canonical.”,

            “isActive”: true,

            “minPower”: 0,

            “version”: “18.04 LTS”,

            “versionCode”: “1”,

            “productUrl”: “https://aws.amazon.com/marketplace/pp/B07CQ33QKV”,

The output above includes the blueprint ids for Ubuntu 18.04 (Bionic), Ubuntu 20.04 (focal) and Ubuntu 22.04 (Jammy). We are going to use the one for Ubuntu 22.04 for our instance creation. The above details also include a description of the blueprint as well as version number, code and product url so you can learn more about them.

Now, we need to pick the right instance size or the bundle id for our instance.

 To do that, we will run the command:

$ aws lightsail get-bundles

Once, you run this command, you will get an output like the following:

$ aws lightsail get-bundles

{

    "bundles": [

        {

            "price": 3.5,

            "cpuCount": 1,

            "diskSizeInGb": 20,

            "bundleId": "nano_2_0",

            "instanceType": "nano",

            "isActive": true,

            "name": "Nano",

            "power": 300,

            "ramSizeInGb": 0.5,

            "transferPerMonthInGb": 1024,

            "supportedPlatforms": [

                "LINUX_UNIX"

            ]

        },

        {

            "price": 5.0,

            "cpuCount": 1,

            "diskSizeInGb": 40,

            "bundleId": "micro_2_0",

            "instanceType": "micro",

            "isActive": true,

            "name": "Micro",

            "power": 500,

            "ramSizeInGb": 1.0,

            "transferPerMonthInGb": 2048,

            "supportedPlatforms": [

                "LINUX_UNIX"

            ]

        },

        {

            "price": 10.0,

            "cpuCount": 1,

            "diskSizeInGb": 60,

            "bundleId": "small_2_0",

            "instanceType": "small",

            "isActive": true,

            "name": "Small",

            "power": 1000,

            "ramSizeInGb": 2.0,

            "transferPerMonthInGb": 3072,

            "supportedPlatforms": [

                "LINUX_UNIX"

            ]

-- More  --

As you can see, the output above shows the details of the various instance sizes available in Amazon lightsail and the monthly prices in dollars. The details include the RAM size, disk size, cpu count, and data transfer per month in gb. The smallest instance is priced at $3.5 and includes 0.5 gb RAM as well as 20 gb disk space. Its bundle id is nano_2_0.

To create an instance in aws lightsail, you can run the following command:

$ aws lightsail create-instances \

    --instance-names <name for your instance like Instance-1> \

    --availability-zone us-west-2a \

    --blueprint-id <blueprint_id likeubuntu_22_04> \

    --bundle-id <nano_2_0 or micro_2_0 or small_2_0>

Just replace the blueprint-id, bundle-id and instance name as well as availability zone according to your need.

In our case, the command will look like this with the smallest instance size and Ubuntu22.04 OS.

$ aws lightsail create-instances --instance-names myinstance-1 --availability-zone us-east-1a --blueprint-id ubuntu_22_04 --bundle-id nano_2_0

However, this is not the complete command, since we installed the Apache server in the previous part (using AWS console) when we launched the instance. We will need to add those commands again so that, Apache is installed as we launch the instance.

We can achieve this in the following manner. First create a file named ud.txt. You can name it anything. From the windows powershell run:

$ vim ud.txt

Press I to enter the insert mode and then paste the following into the file:

#!/bin/bash

sudo apt update -y

sudo apt-get install -y apache2

sudo systemctl start apache2

sudo systemctl enable apache2

Hit escape and then type :wq to exit. We have the script required to install Apache on our lightsail instance ready.

Now, our command to launch the instance looks like the following:-

$ aws lightsail create-instances --instance-names myinstance-1 --availability-zone us-east-1a --blueprint-id ubuntu_22_04 --bundle-id nano_2_0 --user-data file:///ud.txt 

In the above command, the name of the instance is myinstance-1. The availability zone is us-east-1a or Virginia. The blueprint id is Ubuntu 22.04 and the bundle id is nano_2_0 or the size priced at $3.5 per month.

Now, when you run the above command, you will see an output like the following:

{                                                                                                                                        “operations”: [                                                                                                                          {                                                                                                                                        “id”: “099ce253-5842-43ac-b925-fe187f8f5674”,                                                                   “resourceName”: “myinstance-1”,                                                                                       “resourceType”: “Instance”,                                                                                                  “createdAt”: “2023-04-08T18:31:19.564000+05:30”,                                                            “location”: {                                                                                                                            “availabilityZone”: “us-east-1a”,                                                                                                    “regionName”: “us-east-1”                                                                                                        },                                                                                                                                   “isTerminal”: false,                                                                                                                 “operationType”: “CreateInstance”,                                                                                       “status”: “Started”,                                                                                                                 “statusChangedAt”: “2023-04-08T18:31:19.564000+05:30”                                                                            }                                                                                                                                ]                                                                                                                                }  

Now, wait for a few minutes to let the instance be deployed and started. The process will be over in a few minutes. If you check right away in your console, you will see it in pending state, which changes to running a few minutes later.

Well, you created an instance with Apache webserver installed on it and you would like to verify that it has been properly installed. You can verify it by checking the ip of the instance or the status of the Apache server through ssh.

Again, just copy the ip of the newly created instance and paste it in a browser. You will see the default Apache Ubuntu welcome page. If you can see the default Apache 2 page saying “It Works”, you have done everything correctly. So, you have created a lightsail instance of desired size with Ubuntu and Apache webserver running on it using a simple AWS CLI command.

If you can see the default Apache welcome page (saying “It Works”), it means you have installed the Apache server successfully with the script inside ud.txt file. You have already started and enabled the Apache webserver when you ran the script included with your command. To check the status of Apache webserver, run:

$sudo systemctl status apache2

To reload Apache webserver after making any configuration changes:

$sudo systemctl reload apache2

That’s all. Thanks for reading.