Skip to content

Instantly share code, notes, and snippets.

@ErxrilOwl
Last active May 31, 2024 15:46
Show Gist options
  • Save ErxrilOwl/ecd9f5eac27697ad5bb91ca1a1759f32 to your computer and use it in GitHub Desktop.
Save ErxrilOwl/ecd9f5eac27697ad5bb91ca1a1759f32 to your computer and use it in GitHub Desktop.
Deploy Laravel on AWS EC2 Ubuntu (nginx)

IF LINUX SERVER, CONFIGURE APACHE2/NGINX via APT COMMAND

LEMP INSTALLATION RESOURCE - https://www.howtoforge.com/tutorial/ubuntu-laravel-php-nginx/

RPM (RED HAT, FEDORA, CentOS), yum COMMAND

yum install php

debian (UBUNTU,DEBIAN), sudo COMMAND

sudo apt-get install php

CONNECT TO VM via SSH command

ssh VM_USERNAME@PUBLIC_IP_ADDRESS -p 22
ssh hs-server@104.43.9.220 -p 22

UPDATE UBUNTU

sudo apt-get update

INSTALL NGINX

sudo apt install nginx -y (-y is autoconfirm command)

INSTALL PHP SERVICES REQUIRED BY LARAVEL FRAMEWORK

sudo apt install php7.2 php7.2-curl php7.2-common php7.2-cli php7.2-mysql php7.2-mbstring php7.2-fpm php7.2-xml php7.2-zip php7.2-gd -y

ADD NGINX TO SERVICE/STARTUP AND PHP-FPM

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start php7.2-fpm
sudo systemctl enable php7.2-fpm

RESTART/STOP/START NGINX/PHP SERVICE COMMAND

sudo service nginx start/stop/restart
sudo service php7.2-fpm start/stop/restart

DEFAULT PHP DIRECTORY

/etc/php_VERSION
/etc/php7.2 or /etc/php7.1
NANO EDITOR COMMANDS
CTRL + W - SEARCH WORD
CTRL + O - SAVE FILE
CTRL + X - EXIT EDITOR

INSTALL GIT AND COMPOSER

sudo apt install git composer -y

DEFAULT WWWROOT

cd /var/www

MAKE DIRECTORY PERMITTED TO 777

sudo chmod 777 -R FOLDER_NAME/*
sudo chmod 777 -R example/*

COPY AND PASTE COMMAND (RECREATE .env file)

sudo cp .env.example .env

CREATE sites-available file for your laravel project via /etc/nginx/sites-available COPY THIS sites-available laravel configured file

sudo nano example.com
server {
         listen 80;
         listen [::]:80 ipv6only=on;
 
         # Log files for Debugging
         access_log /var/log/nginx/laravel-access.log;
         error_log /var/log/nginx/laravel-error.log;
 
         # Webroot Directory for Laravel project
         root /var/www/example/public;
         index index.php index.html index.htm;
 
         # Your Domain Name
         server_name example.com;
 
         location / {
                 try_files $uri $uri/ /index.php?$query_string;
         }
 
         # PHP-FPM Configuration Nginx
         location ~ \.php$ {
                 try_files $uri =404;
                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
                 fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                 fastcgi_index index.php;
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
         }
 }

ADD your server_name to your domain manager

eg. add example.com to sample.com DNS Management

CREATE symbolink in sites-enabled

sudo ln -s ../sites-available/{link} link
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment