How to Deploy Laravel Application on AWS EC2?

Page 1

How to Deploy Laravel Application on AWS EC2?

www.bacancytechnology.com


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. $ ssh -i “pemfilename” username@ipaddress

Provide us with your requirements; We will hustle to lessen your hustles! Bacancy provides the Best AWS Architects to Manage AWS Cloud Services for your project! Get in touch with us today to hire AWS experts without wasting your time!


Install Nginx


First of all, run the command to update the libraries with the latest packages. $ sudo apt-get update Get superuser permissions. $ sudo su Using the previous variable, add the repository. Then, press enter to confirm. $ add-apt-repository ppa:nginx/$nginx $ sudo apt install nginx


Once done with the installation,

check NGINX version. $ 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.

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


Now, open the MySQL prompt.

sudo mysql

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

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


Flush the privileges (changes)

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 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

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


Download the composer globally using the command curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php HASH=`curl -sS https://composer.github.io/install er.sig`


Verify the hash and installation script 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 sudo php /tmp/composersetup.php --install-dir=/usr/local/bin -filename=composer

Check composer version composer -v


Create the Laravel App and Deploy your Application


Create the Laravel project

composer create-project laravel/laravel demo cd demo

Install composer in the preferable directory

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.

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


Migrate the database

php artisan migrate

Run the server

php artisan serve


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

cd /demo /var/www/html

Configure Virtual Host on Nginx Moving towards the final stepchange the Nginx configuration file as per your requirements to deploy the application. sudo nano /etc/nginx/sitesavailable/default


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

server { listen 80; server_name localhost; root /var/www/html/demo/public; add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-TypeOptions "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.

sudo nginx -t sudo service nginx restart


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!


Thank You!

www.bacancytechnology.com


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.