Comprehensive Guide to Deploying a Laravel App on AWS EC2 Instance Running Ubuntu 22.04 with Nginx Server

Are you ready to take your Laravel application live on the web? In this step-by-step guide, we’ll walk through the process of deploying your Laravel app on an AWS EC2 instance running Ubuntu 22.04 with an Nginx server. Follow these organized steps to seamlessly deploy your app and get it up and running in no time.

Step 1: Creating an EC2 Instance on AWS Console

First things first, head over to the AWS console and create your EC2 instance. Ensure that you choose Ubuntu 22.04 as your operating system.

Step 2: Connecting with Putty via SSH Key

After creating your EC2 instance, connect to it using Putty and your SSH key. This will give you remote access to your EC2 instance.

Step 3: Installing Nginx Server

Once connected, update your package list and install Nginx by running the following commands:

sudo apt update
sudo apt install nginx
systemctl status nginx
sudo systemctl restart nginx
sudo nginx -t 
 
Step 4: Installing PHP with Required Packages

Update your package list again and install PHP along with necessary packages. Also, install Composer, the PHP package manager:

sudo apt update
sudo apt install --no-install-recommends php8.1
sudo apt-get install y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1 mbstring php8.1-curl php8.1-xml php8.1-bcmath php8.1-fpm
 
Install Composer(PHP Package Manager)
 
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
 
HASH=`curl -sS https://composer.github.io/installer.sig`
echo $HASH
 
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
 
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer
 
Step 5: Installing MySQL Database

Update your package list and install MySQL server. Then, secure your MySQL installation:

sudo apt update
sudo apt install mysql-server
sudo mysql
 
In MySQK Prompt, run:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your password';

 

exit

Step 6: Installing PHPMyAdmin and Configuring with Nginx Server

Update your package list and install PHPMyAdmin. Then, configure it with Nginx server:

sudo apt update
sudo apt install phpmyadmin
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
 
 
Check Permissions
sudo chown -R www-data:www-data /var/www/html/phpmyadmin
sudo chmod -R 755 /var/www/html/phpmyadmin
sudo chown -R www-data:www-data /var/www/html
 
Add Nginx Configuration
sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm

 

 

Step 7: Deploying Your Laravel App

Now, it’s time to deploy your Laravel app:

  1. Create a folder in /var/www/html.
  2. Upload your project code to that folder using Git or SFTP.
  3. Create a Symbolic link in /etc/nginx/sites-available/ folder.

Additional Tips

  • To unzip a zip file, install zip and unzip packages: sudo apt install zip unzip.
  • Set appropriate permissions for Laravel:
sudo chmod 777 -R /var/www/html
sudo chown -R www-data storage
sudo chown -R www-data storage/framework
sudo chmod g+w -R storage
sudo chmod g+w -R storage/framework
sudo chmod g+w -R storage/framework/sessions/
sudo chmod g+w -R storage/logs/

 

Scroll to Top