In this section i will show how to deploy Laravel Starter on AWS EC2
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
- Go to your AWS Console, then open the EC2 Instance.
- Create new Instance, you can find on Launch Instance Button.
- 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)
- Create or use existing keypair for SSH Remote.
- For the Network Setting, you can use default VPC or your own VPC.
- 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
- To shorten the time, on this section i use metadata was i created on this Repository.
- Click Advanced Details, go to Metadata Section and drop it metadata-laravel config into it.
- Review Instance on Summary, made sure all the spec and config already correct. Choose Launch Instance.
- After instance succesfully created, check on your instance EC2 dashboard. Make sure instance running and status check passed.
#!/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
- Update and Upgrade package linux
- Set Timezone to your Region (You can replace with your region)
- Install Nginx
- Install Git
- Install PHP 8.1 and other extension
- Install MariaDB
- Set Package Repo for Install NodeJS Version 16.x
- Start Nginx Service
- Start PHP 8.1 FPM Service
- Connect to instance using SSH
ssh -i (YOUR_KEY_PAIR) ubuntu@PUBLIC_IP_AWS_EC2
- If you rejected to connect instance using SSH, try to
sudo chmod 400 (YOUR_KEY_PAIR)
. And then reconnect - After you successfully connect instance using SSH, made sure all package successfully installed.
- For verify package you can use
(PACKAGE_NAME --version)
examplenginx --version
- Check Nginx already running using
sudo systemctl status nginx
- Try to Access Website via Public IP Instance AWS EC2.
- If success, it will return like this.
- Go to
/var/www/html
- Remove default index
sudo rm index.nginx-debian.html
- Git clone Laravel Starter using
git clone https://github.com/nasirkhan/laravel-starter.git
- After done, open laravel-starter folder
- Install Composer package
sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
- Run
sudo composer install
- Duplicate
.env.example
to.env
using
sudo cp .env.exampe .env
- Set permission setting for mysql & Root Password using
mysql_secure_installation
- Login mysql using
mysql -u root -p
- Enter root password
- 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;
- Test login mysql using laravel user
sudo msyql -u laravel_user -p
- Try to use DB laravel_starter and show tables
USE DATABASE laravel_starter;
SHOW TABLES;
- Change .env config for DB Connection
- Run
sudo nano .env
- Edit following this
APP_URL=YOUR_IP_PUBLIC_AWS
DB_USER=laravel_user
DB_PASSWORD=Laravel123!!!
- Run
sudo php artisan migrate --seed
- Run
sudo php artisan key:generate
- 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
- Change Path Folder to
/etc/nginx/sites-available
- Create new file config nginx
sudo nano laravel-server
- Copy configuration on file sites-enabled-laravel on this repo and then paste it
- Save and Exit
- Create link to sites-enabled using
sudo ln /etc/nginx/sites-available/laravel-server /etc/nginx/sites-enabled
- Check configuration file nginx
nginx -t
- Unlink default nginx configuration
unlink default
- Reload nginx
sudo systemctl reload nginx
- After successfully, let's try open Laravel Starter via Public IP was already set (via HTTP Protocol)