DEV Community

Cover image for How To Deploy Laravel Project on EC2 AWS .
Junaid Javed
Junaid Javed

Posted on

How To Deploy Laravel Project on EC2 AWS .

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

Update your Libraries

$ sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

Install Apache2

$ sudo apt-get install apache2
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Configuring MySQL

$ sudo mysql_secure_installation
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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';
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Install Git

$ sudo apt-get install git-core
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Adding .env File to your Laravel project

$ sudo vi .env
Enter fullscreen mode Exit fullscreen mode

Installing Libraries In Project

$ sudo composer install
Enter fullscreen mode Exit fullscreen mode

Read Write Permissions for your Project

sudo chmod 777 -R storage/
sudo chmod 777 -R bootstrap/
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image description

Restart your Apache

$ sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)