Blog Post Image
16 August, 2023

Laravel from scratch on was EC2

Icon

Vimuth Somarathna

Icon

10 minutes

1.   Add Laravel on EC2 server
 

1.1 Create an EC2 server and add dependencies
 

First, we need to create an ec2 server with AWS Linux 2. Then flow these steps.



This tutorial talks about adding simple PHP project

https://tecadmin.net/install-apache24-php7-on-amazon-linux/

This tutorial is about adding Laravel project

https://hbayraktar.medium.com/how-to-install-laravel-5-on-amazon-linux-2-303ef2d38fa4

 

·      Initial Setup

sudo yum update

 
·      Install Apache 2.4

sudo yum install httpd httpd-tools mod_ssl

You need to enable Apache

sudo systemctl enable httpd 

sudo systemctl start httpd

 

Apache has been installed on your Amazon Linux system. Access to the server in a web browser using an IP address, This will show you the default Apache page.

 

·      Need to enable PHP 8.1

sudo amazon-linux-extras enable php8.1

 

Very important to know that this does not install PHP. This tells server that when download download php8.1

 

·      Now we install PHP with extensions and git

sudo yum clean metadata

sudo yum install php php-common php-pear 

sudo yum install php-{cgi,curl,mbstring,gd,mysqlnd,gettext,json,xml,fpm,intl,zip}  

sudo yum install git

 

·      Give necessary permissions

sudo chown -R ec2-user:apache /var/wwwsudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;find /var/www -type f -exec sudo chmod 0664 {} \;
 

 
·      Now PHP is installed so we should test it

sudo vi /var/www/html/info.php

And inside add <?php phpinfo(); ?>

Now inside http://server-ip/info.php we have, (need to run sudo systemctl restart httpd this to work)

 

·      Now add Composer

sudo curl -sS https://getcomposer.org/installer | sudo php
sudo mv composer.phar /usr/local/bin/composer
sudo ln -s /usr/local/bin/composer /usr/bin/composer
 
 
·      Create a git repo in GitHub and clone it to your local machine

git clone https://github.com/vimuths123/laratest.git .
 

·      Install Laravel there

composer create-project laravel/Laravel .
 

If you see this error copy install to another location and copy content form it.

 

·      Now you have git in local. We need to commit push and clone it inside server.

cd /var/www/html/laratest
 
If there is no such repository create like this

mkdir /var/www/html/laratest
 

and git clone it

 
·       Install Laravel dependences

 
composer install
cp .env.example .env
php artisan key:generate
 
 

1.2
 Create virtual host and add domains
 
·      Now we need to create a virtual host

First, we need to go to /etc/httpd/conf/httpd.conf file and comment out this line.

sudo vim /etc/httpd/conf/httpd.conf
 

DocumentRoot "/var/www/html"
 
The reason is we need to open /var/www/html/project1/public directory when server runs. Index.php file of Laravel is there.

Then we create the virtual host

sudo vim /etc/httpd/conf.d/vhost.conf
 

Remember there is no vhost.conf file. It just get created now

 

Check there is DocumentRoot added.

<VirtualHost *:80>    DocumentRoot /var/www/html/laratest/public     <Directory /var/www/html/laratest/public>        Options Indexes FollowSymLinks MultiViews        AllowOverride All        Order allow,deny        Allow from all        Require all granted    </Directory></VirtualHost>
 
 
·      Now we have to add domain.

Here we are adding a subdomain

Configure DNS Settings:

Create an "A" (Address) record for the subdomain

 
 

Now change the virtual host:

<VirtualHost *:80>    ServerName subdomain.domain.com    DocumentRoot /var/www/html/laratest/public     <Directory /var/www/html/laratest/public>        Options Indexes FollowSymLinks MultiViews        AllowOverride All        Order allow,deny        Allow from all        Require all granted    </Directory></VirtualHost>
 
 

1.3
Add another project
 

I have copied the content from folder laratest to laratest2. But permissions changed when do that

cd /var/www/htmlcp -r laracast laracast2 

 
Need to give these permissions again. Otherwise will not be able to run commands like composer install

sudo chown -R ec2-user:apache /var/www
sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
 
 
 
Now you have to get second project live. Believe me it’s really easy

 

 

And create another vhost entry below

<VirtualHost *:80>    ServerName subdomain2.domain.com    DocumentRoot /var/www/html/laratest2/public     <Directory /var/www/html/laratest2/public>        Options Indexes FollowSymLinks MultiViews        AllowOverride All        Order allow,deny        Allow from all        Require all granted    </Directory></VirtualHost> 

 

2.   Add GitHub Actions Laravel on EC2 server

 

Here is a tutorial to do this: https://medium.com/@bjnandi/how-to-deploy-laravel-application-to-ec2-instance-using-github-action-automatically-ssh-deploy-e198b0a3136d

 

GitHub action let us automatically deploy our changes from GitHub to the location of our EC2 server. Let’s check how to do it.

Actually, this task is simpler than it seems. But we have to careful on few little things.

 


2.1
Creating your first workflow
 

Create this file, .github/workflows/deploy-to-ec2.yml

 

name: Push-to-EC2

# Trigger deployment only on push to master branch
on:
  push:
    branches:
      - main

jobs:
  deploy:
    name: Deploy to EC2 on master branch push
    runs-on: ubuntu-latest

    steps:
      - name: Checkout the files
        uses: actions/checkout@v2

      - name: Deploy to EC2
        uses: easingthemes/ssh-deploy@main
        env:
          SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
          REMOTE_HOST: ${{ secrets.EC2_HOST }}
          REMOTE_USER: ${{ secrets.EC2_USERNAME }}
          TARGET: ${{ secrets.TARGET_DIR }}

 

Then you have to commit this file to git.

After that you have to find Actions secrets and variables in your repository settings

 

 

Now you had to add four secrets.

·      EC2_HOST : Public IP Address of the instance, it will look something like this 3.83.255.7

 

·      EC2_USERNAME : Will be the username of the EC2 instance, usually ubuntu

 

·      SSH_PRIVATE_KEY: This will be your .pem file which you will use to login to the instance.

 

You have to open your .pem file from notepad and copy content into this variable. Make sure to get .pem because .ppk content will not work.

Apart from that you need to allow SSH port to everywhere on your vpc group

 

·      TARGET_DIR: This is where you want to deploy your code. for Nginx I use it will look something like this html/project_dir (this directory after of /var/www)

In my projects I added to /var/www/html/laratest so this is my TARGET_DIR

 

Apart from this in actions section you can check it’s deployed or not.

 

And if you need to add another deploy you can do like this

·      Add a new file on develop branch .github/workflows/deploy-to-ec2-develop.yml
·      Add branch
 
on:
  push:
    branches:
      - develop
 
·      Add new directory
 
TARGET: ${{ secrets.TARGET_DIR_DEV }}

            This TARGET_DIR_DEV can be /var/www/html/laratest2

 

 

3.   Add SSL with lets encrypt

Believe me this is really easy than it looks. Let’s continue.

 

·      Install Certbot:

sudo amazon-linux-extras install epelsudo yum install certbot python2-certbot-apache

 

·      Obtain SSL Certificates:

sudo certbot –apache

Run the Certbot command to obtain SSL certificates for your virtual hosts:

 

 

·      Automate Certificate Renewal:

Certbot will automatically add a cron job to renew your certificates before they expire. You can test the renewal process with the following command:

sudo certbot renew --dry-run

 

For this you don’t even have to restart apache. But for below code to work you need apache restart

You don’t need to worry on redirect from http to https. Certbot does it for you

 
<VirtualHost *:80>    ServerName laratest.vimuthonline.live    DocumentRoot /var/www/html/project1/public     <Directory /var/www/html/project1/public>        Options Indexes FollowSymLinks MultiViews        AllowOverride All        Order allow,deny        Allow from all        Require all granted    </Directory>    RewriteEngine on    RewriteCond %{SERVER_NAME} =laratest.vimuthonline.live    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]</VirtualHost> 
 

4.   Laravel coding