Table of Contents

Introduction

Are you struggling to find an easy tutorial on how to deploy Laravel application on AWS EC2? Then here is a blog for you to lessen your hustle! Follow this step-by-step guide to deploy your application on Amazon EC2.

Tutorial Takeaway

You’d be learning about the following topics for this tutorial.

  • Working with Nginx
  • MySQL: Installation and Configuration
  • Laravel: Basic Set-Up and Project Creation
  • Deploy Laravel Application on AWS EC2

Prerequisite

Make sure you have the following resources to use.

  • RDS
  • VPC
  • EC2 Instance: Visit create EC2 instance if have not yet created.

Technical Stack: Deploy Laravel Application on AWS EC2

To host our Laravel app on Amazon EC2 we will be using the following technical stack.

  • Ubuntu
  • Nginx
  • PHP7.2-FPM

Login to EC2 Instance

Use the following command for logging in to your EC2 instance.

Copy Text
$ ssh -i “pemfilename” username@ipaddress

Unleash the power of AWS and elevate your Laravel app to new heights!
Hire AWS Developers who can craft robust infrastructure that fuels your success and witness your digital ambitions come to life.

Install Nginx

First of all, run the command to update the libraries with the latest packages.

Copy Text
$ sudo apt-get update

Get superuser permissions.

Copy Text
$ sudo su

Using the previous variable, add the repository. Then, press enter to confirm.

Copy Text
$ add-apt-repository ppa:nginx/$nginx
 
$ sudo apt install nginx

Once done with the installation, check NGINX version.

Copy Text
$ nginx -V

Install and Configure MySQL

There are two ways to install a MySQL database.

  • Using AWS EC2 as Localhost
  • Using AWS RDS DB Instance

Using AWS EC2 as Localhost

Install MySQL in Ubuntu by running the below command.

Copy Text
sudo apt-get update
sudo apt install mysql-server
sudo mysql_secure_installation

Now, open the MySQL prompt.

Copy Text
sudo mysql

Once we are done with the installation process use the below command to configure the root account with an authentication password.

Copy Text
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_your_password';

Flush the privileges (changes)

Copy Text
FLUSH PRIVILEGES;

Using AWS RDS DB Instance

For creating a MySQL database instance, go to the link below, there is a step-by-step guide: Create a MySQL DB instance and connect to a database using AWS RDS.

Since we are done with configuring our database let’s move towards the Laravel project setup. If you are familiar with the initial setup of laravel then feel free to skip the next section and jump to the deployment section.

Develop and Deploy with Bacancy!
Looking for top-notch developers with extraordinary problem-solving skills to accomplish your requirements? Contact us now and hire DevOps developer.

Laravel Project Set-Up

Install PHP with CLI

Copy Text
sudo apt update
sudo apt install php-cli unzip
cd ~

Download the composer globally using the command

Copy Text
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php

HASH=`curl -sS https://composer.github.io/installer.sig`

Verify the hash and installation script

Copy Text
echo $HASH        

Verify the installation script for the composer.     
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH')
{ 
echo 'Installer verified'; 
}
else 
{ 
echo 'Installer corrupt'; unlink('composer-setup.php'); 
} 
echo PHP_EOL;"

Install the composer in /usr/local/bin directory

Copy Text
sudo php /tmp/composer-setup.php
--install-dir=/usr/local/bin --filename=composer

Check composer version

Copy Text
composer -v		

Create the Laravel App and Deploy your Application

Create the Laravel project

Copy Text
composer create-project laravel/laravel demo
cd demo

Install composer in the preferable directory

Copy Text
composer install

Copy .env file from .env.example and add the following changes. In case you’re using RDS then you need to configure it with RDS creds.

Copy Text
cp .env.example .env
DB_DATABASE=dabasename
DB_USERNAME=root
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=3306

Migrate the database

Copy Text
php artisan migrate

Run the server

Copy Text
php artisan serve

Now move your Laravel demo code to /var/www/html.

Copy Text
cd /demo /var/www/html

For further information

Laravel Performance Optimization

Configure Virtual Host on Nginx

Moving towards the final step- change the Nginx configuration file as per your requirements to deploy the application.

Copy Text
sudo nano /etc/nginx/sites-available/default

After changing the default virtual host file, your file would look like something like this.

Copy Text
server {
      listen 80;
      server_name localhost;
      root /var/www/html/demo/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/php7.2-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            include fastcgi_params;
        }

       location ~ /\.(?!well-known).* {
          deny all;
       }
  }

Save the file and run the following commands to verify the Nginx syntax and restart Nginx for loading your changes.

Copy Text
sudo nginx -t
sudo service nginx restart 

Visit http://EC2_PUBLIC_IPADDRESS

Conclusion

So, this was about how to deploy laravel application on AWS EC2. What are you waiting for? Go try these steps and host your application as well! For more such cloud tutorials, feel free to visit the Cloud tutorials page and start learning! Write us back your suggestions and feedback; we’d love to hear from you!

Need Help to Laravel Application on AWS EC2?

Experience high-quality service which empowers developers and hire hand-picked dedicated developers to keep your app up-to-date.

GET IN TOUCH NOW

Build Your Agile Team

Hire Skilled Developer From Us

[email protected]

Your Success Is Guaranteed !

We accelerate the release of digital product and guaranteed their success

We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.

How Can We Help You?