Code Monkey home page Code Monkey logo

deploy-laravel-ec2's Introduction

Larevel on AWS EC2

Guide to Deploy Laravel Starter on AWS EC2

In this section i will show how to deploy Laravel Starter on AWS EC2

Laravel Starter (based on Laravel 10.x) from Nasir Khan Saikat

For this project i used Sample App Laravel Starter from Nasir Khan Saikat to Deploy on AWS EC2 you can find this project on https://github.com/nasirkhan/laravel-starter

Deployment Step

Create Instance on AWS EC2

  1. Go to your AWS Console, then open the EC2 Instance.
  2. Create new Instance, you can find on Launch Instance Button.
  3. On this section i use Free Tier Spec:
  • Operating System : Ubuntu 22.04 LTS
  • Instance Type : t2.micro
  • Storage : 8 GB
  • Port Allow Needed : 22 (SSH), 80(HTTP), 433(HTTPS), 8000(Optional Laravel Port)
  1. Create or use existing keypair for SSH Remote.
  2. For the Network Setting, you can use default VPC or your own VPC.
  3. Create Security Group, for allow Port 22, 80, 8000, and 433. On this section i use security group was i already created, i give name WebServer
  4. To shorten the time, on this section i use metadata was i created on this Repository.
  5. Click Advanced Details, go to Metadata Section and drop it metadata-laravel config into it.
  6. Review Instance on Summary, made sure all the spec and config already correct. Choose Launch Instance.
  7. After instance succesfully created, check on your instance EC2 dashboard. Make sure instance running and status check passed.

Metadata Explanation

#!/bin/bash
apt update -y && apt upgrade -y
timedatectl set-timezone Asia/Jakarta
apt install -y nginx
apt install -y git
apt install -y php-cli php-xml php-curl php-zip php-mbstring php-dom php8.1-mysql php8.1-fpm php-bcmath
apt install -y mariadb-server
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
apt install -y nodejs
systemctl start nginx
service php8.1-fpm start
  1. Update and Upgrade package linux
  2. Set Timezone to your Region (You can replace with your region)
  3. Install Nginx
  4. Install Git
  5. Install PHP 8.1 and other extension
  6. Install MariaDB
  7. Set Package Repo for Install NodeJS Version 16.x
  8. Start Nginx Service
  9. Start PHP 8.1 FPM Service

Verify Package

  1. Connect to instance using SSH
ssh -i (YOUR_KEY_PAIR) ubuntu@PUBLIC_IP_AWS_EC2
  1. If you rejected to connect instance using SSH, try to sudo chmod 400 (YOUR_KEY_PAIR). And then reconnect
  2. After you successfully connect instance using SSH, made sure all package successfully installed.
  3. For verify package you can use (PACKAGE_NAME --version) example nginx --version
  4. Check Nginx already running using sudo systemctl status nginx
  5. Try to Access Website via Public IP Instance AWS EC2.
  6. If success, it will return like this.

Nginx Test

Installation Laravel

  1. Go to /var/www/html
  2. Remove default index sudo rm index.nginx-debian.html
  3. Git clone Laravel Starter using git clone https://github.com/nasirkhan/laravel-starter.git
  4. After done, open laravel-starter folder
  5. Install Composer package
sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
  1. Run sudo composer install
  2. Duplicate .env.example to .env using
sudo cp .env.exampe .env
  1. Set permission setting for mysql & Root Password using mysql_secure_installation
  2. Login mysql using mysql -u root -p
  3. Enter root password
  4. Run this MYSQL Script
CREATE DATABASE laravel_starter;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'Laravel123!!!';
GRANT ALL PRIVILEGES ON laravel_starter.* TO 'laravel_user'@'localhost' IDENTIFIED BY 'Laravel123!!!';
FLUSH PRIVILEGES;
  1. Test login mysql using laravel user sudo msyql -u laravel_user -p
  2. Try to use DB laravel_starter and show tables
USE DATABASE laravel_starter;
SHOW TABLES;
  1. Change .env config for DB Connection
  2. Run sudo nano .env
  3. Edit following this
APP_URL=YOUR_IP_PUBLIC_AWS
DB_USER=laravel_user
DB_PASSWORD=Laravel123!!!
  1. Run sudo php artisan migrate --seed
  2. Run sudo php artisan key:generate
  3. Set Required File Permission
sudo chown -R www-data:www-data /var/www/html/laravel-starter/
sudo chmod -R 755 /var/www/html/laravel-starter
  1. Change Path Folder to /etc/nginx/sites-available
  2. Create new file config nginx sudo nano laravel-server
  3. Copy configuration on file sites-enabled-laravel on this repo and then paste it
  4. Save and Exit
  5. Create link to sites-enabled using sudo ln /etc/nginx/sites-available/laravel-server /etc/nginx/sites-enabled
  6. Check configuration file nginx nginx -t
  7. Unlink default nginx configuration unlink default
  8. Reload nginx sudo systemctl reload nginx
  9. After successfully, let's try open Laravel Starter via Public IP was already set (via HTTP Protocol)

Open Laravel Starter using IP via http

Setting up SSL using Lets Encrypt on Nginx

deploy-laravel-ec2's People

Contributors

syahidhzblh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.