DEV Community

Ching Cheng Kang
Ching Cheng Kang

Posted on

How to Deploy Laravel App to Amazon EC2 Free Tier: A Step-by-Step Guide for Easy Deployment

Before we proceed to deployment, i assumed you already had an AWS account, if not, you can also register one AWS account for free and use the free tier for one year
AWS

Create A Virtual Machine

In the AWS Console, Click EC2 under compute

Click EC2 under compute in AWS Console

Once you're signed in to your AWS account, navigate to the EC2 service by clicking on "Services" at the top of the page and typing "EC2" in the search bar.

Click on "Launch Instance" to create an VM

Click Launch Instance to create an VM
Click on "Launch Instance" to create a new EC2 instance.

Select Ubuntu Server 20.04 (LTS)

Actually, Laravel do not care which distro you are running, so feel free to choose preferred distro of you, but I am gonna just choose Ubuntu here.

Select Ubuntu Server 20.04

Choose Instance type

For composer, it is recommended to have at least 1GB of RAM for composer install

Choose Instance type
Go a head and choose the t2.micro/t3.micro for instance type.

Set up misc information

Configure Instance Detail

Add Storage

You can set up to 30 GB, as that is maximum aws free tier provide

Network Configuration

Configure Security Group

Configure the security group. You will need to allow inbound traffic on port 80 (HTTP), 443 (HTTPS) and port 22 (SSH) to be able to access the Laravel application and connect to the instance using SSH.

Review Setting

Review Instance Setting

Review your settings and launch the instance.

Generate Key Pair

Generate Key Pair or Select Existing Key Pair
When launching the instance, AWS will prompt you to generate a key pair. This is necessary for connecting to the instance using SSH.
Choose "Create a new key pair" and give it a name.
Download the private key file (.pem file) and save it to a secure location.
Note: You will not be able to download the private key file again, so make sure to save it in a safe place.

Setup Instance

Connect to the Instance

For Unix-based systems (Mac, Linux):

Run the following command to change the permissions of the private key file: chmod 400 your_key_pair_name.pem
Run the following command to connect to the instance: ssh -i your_key_pair_name.pem ec2-user@your_public_dns_name
Note: Your public DNS name can be found in the EC2 dashboard under "Instances" -> "Description" -> "Public DNS (IPv4)"

For Windows:

If you're using the default Windows Command Prompt, run the following command to convert the .pem file to a .ppk file: puttygen your_key_pair_name.pem -O private -o your_key_pair_name.ppk. You will need to install putty for this task.
If you're using Git Bash, skip this step as it natively supports .pem files.
Open PuTTY and enter your instance's public IP address under "Host Name (or IP address)".
Navigate to "Connection" -> "SSH" -> "Auth" and browse for the .ppk file you created in the previous step.
Click "Open" to connect to the instance.

Install Required Software

Once connected to the instance, you will need to install the necessary software to run Laravel.
Update the package lists on your instance by running the following command: sudo apt-get update

Install PHP:

Laravel 10 requires PHP 8.1 or newer. In this demo, we will be using PHP 8.2, run the following command to install it along with some common extensions:



sudo apt install openssl php8.2 php8.2-fpm php8.2-cli php8.2-bcmath php8.2-curl php8.2-json php8.2-mbstring php8.2-pdo php8.2-mysql php8.2-tokenizer php8.2-xml php8.2-zip php8.2-gd



Enter fullscreen mode Exit fullscreen mode

Install MySQL:

Most Laravel Application uses a database to store application data. To install MySQL, run the following command:



sudo apt-get install -y mysql-server


Enter fullscreen mode Exit fullscreen mode

After installation, start the Nginx service by running: sudo systemctl start nginx
You can check if Nginx is running by visiting your instance's public IP address in your web browser.

Install Nginx:

Laravel requires a web server to serve the application. We will use Nginx. To install it, run the following command:



sudo apt-get install -y nginx


Enter fullscreen mode Exit fullscreen mode

After installation, start the Nginx service by running: sudo systemctl start nginx
You can check if Nginx is running by visiting your instance's public IP address in your web browser.

Install Node.js:

Node.js is a JavaScript runtime that allows you to run JavaScript on the server-side. To install it, run the following commands:



curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash 
sudo apt-get install -y nodejs


Enter fullscreen mode Exit fullscreen mode

Install Git:

Git is a version control system that allows you to manage your codebase. To install it, run the following command:



sudo apt-get install -y git


Enter fullscreen mode Exit fullscreen mode

Other useful software:
Some additional software that might be useful for your Laravel application:
Composer: A dependency manager for PHP. You can install it by running: sudo apt-get install -y composer
Redis: A fast in-memory key-value store. You can install it by running: sudo apt-get install -y redis-server

That's it! Your instance is now ready to host your Laravel application.

Clone your Laravel Application from Github

I would assume you store your application somewhere on Git Repository Provider such as GitHub or GitLab.

Change into the Laravel application directory:



mkdir /var/www/laravel
cd /var/www/laravel


Enter fullscreen mode Exit fullscreen mode

Install the Laravel dependencies using Composer:



sudo composer install --no-dev


Enter fullscreen mode Exit fullscreen mode

Configure Nginx to Serve Your Laravel Application

By default, Nginx is configured to serve static files from the /var/www/html directory. We will update the Nginx configuration file to serve our Laravel application instead.

Create a new server block for your Laravel application

First, create a new Nginx configuration file for your Laravel application:



sudo nano /etc/nginx/sites-available/laravel


Enter fullscreen mode Exit fullscreen mode

Paste the following configuration into the file:



server {
    listen 80;
    server_name example.com; # Replace with your domain name
    root /var/www/laravel/public;

    index index.php index.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # Replace with your PHP version
    }

    location ~ /\.ht {
        deny all;
    }
}


Enter fullscreen mode Exit fullscreen mode

TODO :

  • Replace example.com with your domain name.
  • Replace fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; with the appropriate PHP version and FPM socket path for your system.

Enable the server block

Next, create a symbolic link to enable the server block:



sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/


Enter fullscreen mode Exit fullscreen mode

Test the Nginx configuration to make sure there are no syntax errors:



sudo nginx -t


Enter fullscreen mode Exit fullscreen mode

If the test is successful, restart the Nginx service to apply the changes:



sudo systemctl restart nginx


Enter fullscreen mode Exit fullscreen mode

Update Laravel configuration

Laravel requires write access to the storage and bootstrap/cache directories. Change the ownership and permissions on these directories:



sudo chown -R www-data:www-data /var/www/laravel/storage /var/www/laravel/bootstrap/cache
sudo chmod -R 775 /var/www/laravel/storage /var/www/laravel/bootstrap/cache


Enter fullscreen mode Exit fullscreen mode

Finally, update the .env file in your Laravel project with your database credentials and other configuration options:



cd /var/www/laravel
cp .env.example .env
nano .env


Enter fullscreen mode Exit fullscreen mode

Save the changes and generate a new application key:



php artisan key:generate


Enter fullscreen mode Exit fullscreen mode

That's it! Your Laravel application should now be accessible at your server's public IP address or domain name.

Top comments (3)

Collapse
 
zaratedev profile image
Jonathan Zarate

Hello, I have this error
sudo apt install php8.2-cli
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package php8.2-cli
E: Couldn't find any package by glob 'php8.2-cli'

Collapse
 
chengkangzai profile image
Ching Cheng Kang

Oops didnt expect to have any reply after a year publish the article.
So the problem with that error is that php8.2 doesnt not exists in ubutnu 20.04 by default and you have to add ondrej/php repository in order for ubuntu to get to know where it is.

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt upgrade
sudo apt install php8.2-cli
Enter fullscreen mode Exit fullscreen mode

And it should work!

Collapse
 
abdul_majid_6dfa7cf7175d2 profile image
ABDUL MAJID