How To Deploy Laravel Project on EC2 AWS .

Rahul Gupta
Nerd For Tech
Published in
3 min readJun 27, 2020

In this Article , We will Learn How to deploy a laravel project on AWS EC2 (Elastic Cloud Computing ) Instance .

Prerequisites

Create a Ubuntu Server On AWS EC2 Instance.
If not created yet ? Learn How to Create a Ubuntu 20.04 Server on AWS EC2 (Elastic Cloud Computing)

Steps To deploy Laravel Project on EC2

Connect to EC2 instance using your private key ( Key Pair ).
Learn How to Connect to your EC2 Instance .

Update your Libraries

$ sudo apt-get update

Install Apache2

$ sudo apt-get install apache2

Now copy the IPv4 Public IP Address of your EC2 instance and hit on the browser. If you see a screen something like below , you are good to go. Apache server is working!

Install PHP and useful packages

$ sudo apt-get install php$ php --version$ sudo apt-get install php7.4-cli php7.4-common php7.4-curl php7.4-gd php7.4-json php7.4-mbstring php7.4-intl php7.4-mysql php7.4-xml php7.4-zip

Usually, when a user requests a directory from the web server, Apache will first look for a file named index.html. If you want to change Apache to serve php files rather than others, move index.php to first position in the dir.conf file as shown below.

$ sudo vi /etc/apache2/mods-enabled/dir.conf

Install MySQL Server

$ sudo apt-get install mysql-server

This will install mysql server but this will not prompt anything to configure mysql .

Configuring MySQL

$ sudo mysql_secure_installation

This way you can make some changes to your MySQL installation’s security options.

Note that even though you’ve set a password for the root MySQL user, this user is not configured to authenticate with a password when connecting to the MySQL shell because it uses auth_socket by default .

In order to use a password to connect to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password by using following commands.

$ sudo mysql

To configure the root account to authenticate with a password, run the following ALTER USER command. Note that this command will change the root password you set in mysql_secure_installation .

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpassword';

Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:

FLUSH PRIVILEGES;

Now You are done with setting up your mysql server on your EC2. you can use your credentials in your laravel project.

Install Composer

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"$ php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"$ php composer-setup.php$ php composer-setup.php --install-dir=bin --filename=composer$ php -r "unlink('composer-setup.php');"$ composer --version

Install Git

$ sudo apt-get install git-core

Clone Your Repository

Change to /var/www/html directory and clone your Laravel Project there using following commands.

$ cd /var/www/html$ sudo git clone https://github.com/username/reponame.git$ cd reponame

Adding .env File to your Laravel project

$ sudo vi .env

Installing Libraries In Project

$ sudo composer install

Read Write Permissions for your Project

sudo chmod 777 -R storage/
sudo chmod 777 -R bootstrap/

Apache Configuration

By Default Apache open /var/www/html/index.html Now we have to open our Laravel project when we go to the public IP of the EC2.
Go to /etc/apache2/sites-available/000-default.conf and change the DocumentRoot as shown below.

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

Restart your Apache

$ sudo systemctl restart apache2

Now you have successfully deployed your laravel Project on AWS ,
Which you can easily access through your public IPV4 IP Address.

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Rahul Gupta
Rahul Gupta

Written by Rahul Gupta

Master of Computer Application | JNU

Responses (6)