Deploy Laravel Application to Amazon Lightsail
Tags: aws, cloud, lightsail, laravel
Published: 2022-05-19 15:06:43

Beginners friendly step-by-step guide to deploy any application to Amazon Lightsail. This article demos a Laravel app but the idea is the same for all kind of applications.

## Introduction

Amazon Lightsail is a fixed monthly priced VPS service offered by AWS. Once you install the OS of your choice, you can run almost any application. In this application, I'm showing how to deploy one of my favorite backend frameworks called Laravel. Which most of you might hear of. Let's deploy it.

## I don't like/know Laravel

It's totally fine if you don't like or know Laravel. Laravel is just the framework I choose to demo but it can be any app. It can be Node.js, Python, Django, Fast API, Ruby on Rails, or any other app. The idea is the same, but some of the steps in deployment will be different.

## Prerequisites

You need a Laravel app to deploy. Don't worry if you don't have one, I have to best [Laravel (v9) app](https://github.com/HarunRay/laravel-to-lightsail) you can find in the internet (pun intended).

Let's deploy our Laravel app. 📦👉☁️

## Create Amazon Lightsail Instance

1. Go to Lightsail homepage. Click on the **Create instance** button under **Instances** tab.
![CleanShot 2022-05-18 at 19.44.36@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652881692076/hLgNOWXsk.png)

2. You will land on the Create instance page. We will use Ubuntu for this demo but you are open to choosing the OS of your choice. 
![CleanShot 2022-05-18 at 19.51.37@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652882301357/kHB-fJG9L.png)
(A) Change the AWS Region and Availablity Zone (AZ) of your choice if not the correct one selected. (B) Select **Linux/Unix** platform. (C) Select **Ubuntu (20.04 LTS)** as OS. The blueprint should be **OS Only**. (D) Choose a plan. (E) Give it a name, and (F) Hit the **Create instance** button from the bottom of the page.

3. Your instance is in the **Pending** state. Will be **Running** in a few minutes. 

## Assign a static IP

Read [**how to assign a Static IP to an Amazon Lightsail Instance**](https://blog.harun.dev/deploy-wordpress-app-to-amazon-lightsail#heading-assign-a-static-ip-optional).

## Connect to the instance using SSH

> ℹ️ I described in detail [How to connect to an Amazon Lightsail instance using SSH](https://blog.harun.dev/deploy-wordpress-app-to-amazon-lightsail#heading-connect-to-the-instance-using-ssh). 

1. Go to the instance page by clicking on the name. And go to the **Connect** tab. We need these 3 things: (A) Public IP, (B) SSH Username, and (C) Download the SSH Key. 
![CleanShot 2022-05-18 at 20.21.12@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652883714529/oW0f4b2AO.png)

2. Change permission of Key file: 
```bash
# Change "path/to/keyfile.pem" with your key file name
chmod 400 path/to/keyfile.pem
``` 

3. Connect to the SSH
```bash
#format 
ssh -i "path/to/keyfile.pem" <username>@<ip_address>
# Should look like this with your own value
# ssh -i "LightsailDefaultKey-us-east-1.pem" ubuntu@52.201.59.133
```

4. If you are asked to add IP to known hosts, type `yes` and you should be logged in. 

## Installing Necessary Scripts

Our server is created and it's empty. You can install any script you want, depending on what you need. In this demo, we are going to use Laravel. You can follow along even if you have a different app/stack.

1. Update package information
```bash
sudo apt-get update
```

2. Some packages
```bash
sudo apt install curl git unzip -y
```

3. Install PHP 
I love the latest PHP 8.1. It's not officially available in Ubuntu 20.04 yet. We will enable the PHP Repository to install PHP 8.1.
```bash
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt install php8.1-fpm php8.1-cli php8.1-mysql php8.1-curl php-xml php-mbstring -y
```
Check php version by using `php --version` command. You should see something like this: 
![CleanShot 2022-05-18 at 21.02.08@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652886148738/mv58j3RPp.png)

4. Install Composer
```bash
# Download the bin file
curl -sS https://getcomposer.org/installer | php
# Move it to correct path
sudo mv composer.phar /usr/local/bin/composer
# Make it executeable
sudo chmod +x /usr/local/bin/composer
```

5. Install Nginx & configure nginx
```bash
sudo apt install nginx -y
```

6. Clone your application repo
```bash
# go to www directory
cd /var/www/html
# clone the git repo
sudo git clone https://github.com/HarunRay/laravel-to-lightsail.git
# go to application directory
cd laravel-to-lightsail
# copy .env.example file
cp .env.example .env
# Install dependencies
sudo composer install
```

7. Create a nginx config file
```bash
sudo vim /etc/nginx/sites-available/laravel
```
Paste this code. Change `52.201.59.133` to your IP address and save.
```nginx
server {
    listen 80;
    listen [::]:80;
    server_name 52.201.59.133;
    root /var/www/html/laravel-to-lightsail/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```
Now, run this:
```bash
# link nginx config 
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo systemctl start nginx
sudo systemctl enable nginx
```

8. Set Laravel App Permission
```bash
# Go to your laravel app directory
cd /var/www/html/laravel-to-lightsail/
# Add files & folder www-data user & group
sudo chown -R www-data:www-data .
# Add your ubuntu to group
sudo usermod -a -G www-data ubuntu
# Set file(s) permission
sudo find . -type f -exec chmod 644 {} \;
# Set folder(s) permission
sudo find . -type d -exec chmod 755 {} \;
# Set cache directory permission
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
```

9. Generate app key
```bash
sudo php artisan key:generate
```

10. Update the `.env` file
Open file
```bash
sudo vim /var/www/html/laravel-to-lightsail/.env
```
Now, change these values;
```env
APP_ENV=production
# Change IP to yours
APP_URL=http://52.201.59.133
``` 

Now go to your public IP address. Mine is `http://52.201.59.133`. You should see this welcome page. 
![CleanShot 2022-05-19 at 20.50.16@2x.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652971832893/zcqFK0B3U.png)

Congratulations, your Laravel app launched on Amazon Lightsail instance. 🚀

## Conclusion
In the next few articles, we are going to configure the database, load balancer & Redis (Elasticache). Follow me and subscribe to [my newsletter](https://blog.harun.dev).