Deploy Laravel App on AWS
How To Install and Configure Laravel with Nginx on Ubuntu 24.04 (LEMP) with PHP 8.2
Introduction
Laravel is a popular open-source PHP framework known for its elegant syntax and comprehensive tools for building modern web applications. Its robust ecosystem and built-in features have made it a favorite among developers, leading to its rapid adoption in recent years.
In this guide, you'll learn how to install and configure a new Laravel application on an Ubuntu 24.04 LTS server. We'll use Composer to download and manage the framework dependencies and Nginx to serve the application.
Prerequisites
To follow this guide, you will need to have an AWS EC2 instance up and running. This guide does not cover the setup of AWS or how to connect to your instance, as we will start from the point where you already have an Ubuntu instance on AWS. Let's get started.
1. Prepare for installing the required PHP and Nginx
In AWS EC2 instances, all root privileges are delegated to the default user. Therefore, you need to use the sudo
command before executing any commands that require superuser privileges. This tells the server that you are authorized to run these commands. For example, to update the package list, you would run sudo apt update
.
2. Install Nginx
Install Nginx using the following command:
Now to verify that nginx works. We will need to visit the IP address of the server in the browser.
You will see a page like this:
3. Install PHP 8.2 and PHP-FPM
Since PHP 8.2 is relatively new, you might need to add a third-party repository to get the latest version. Use the following commands to add the repository and install PHP 8.2 along with PHP-FPM.
4. Install PHP-FPM
Okay, let's check whether our PHP version has worked or not.
Awesome! It's worked. Before installing Laravel, you need to install several PHP modules required by the framework. These extensions provide additional support for handling character encoding, XML processing, etc. Here is the server requirement.
5. Installing the Composer
We’ll use Composer to install Laravel and its dependencies. You can install Composer by following the digital ocean guide on How to Install Composer on Ubuntu 22.04. That's the same installation on Ubuntu 24.04. You can feel free to start with Step 2! I already covered the php additional dependencies.
6. Installing the Node.js
Download your desired Node.js version PPA installation script. For example, run the following command to use the Node.js version 22.x
.
Run the Node.js setup script
Install Node.js using the following command.
View the installed Node.js and NPM version on your server.
7. Installing Laravel App
So right now we have Nginx, PHP, PHP-FPM, and Composer installed. Let's start cloning the Laravel repo from GitHub. To do that, we need to generate an SSH key pair on the server, copy the public key, and save it as a deployment key on GitHub.
Okay. Let's print that key.
ssh-rsa AAAAB3NzaC1yc2*****ubuntu@ip-172-31-20
And paste that key into your repo Settings>Deploy keys.
That's it. And we need to check /www
the directory which is owned by the root user or not. Let's change our directory ~
home to /var
using:
List down all of the directory and file permissions:
That's not good /www
the directory is owned by the root user which means if we want to write any files in this directory we will have to use sudo
. Okay, let's change the owner to Ubuntu. We need to change the owner chown
belong to the user ubuntu
and the group ubuntu
for /www
. Here is the command:
Why www directory? The www
directory in Nginx is commonly used as the document root or web root directory where your website's files (such as HTML, CSS, JavaScript, images, etc.) are stored. When Nginx is configured to serve a website, it looks in this directory to find the files to serve to users. If you want to learn more about nginx, php-fpm like that check it out here.
And let's check again.
Cool now. Right? Next, let's clone our Laravel app from GitHub. Change directory to /www
and clone it.
Great. Now cd
to weshop
folder and let's install the composer. Notice that we are handling the production version of our app.
We're running composer install --ignore-platform-reqs --no-dev
will install only the necessary production dependencies, ignoring any platform requirements, and excluding the development dependencies.
8. Asset Bundling (Vite)
Laravel is now default support with Vite by providing an official plugin and Blade directive to load your assets for development and production. So, we need to install node modules on our server.
9. Configuring Laravel
We’ll now edit the .env
file to customize the configuration options for the current application environment. At this time, we don't have .env
file. Just an example file in here. Let's copy that environment file.
Let's generate the key.
We don’t need to set up all of them now. Just cover some values.
Fantastic, we're almost there! All of the Laravel configurations are now complete. But, we need to tell the Nginx "This is our laravel application! I want to show it on the web server". Let's go back to the terminal. Before we configure nginx to display our Laravel application, we need to change back the ownership of the /www
directory to a user and a group called www-data.
Perfect.
10. Setting up Nginx
We still need to configure Nginx to serve the content. To do this, we’ll create a new virtual host configuration file at /etc/nginx/sites-available
:
The following configuration file contains the recommended settings for Laravel applications on Nginx. So, let's create a new one called weshop
.
And paste that code.
The server_name is left blank, we will use this attribute to match requests coming from a specific domain to a specific nginx block in the future.
We need to activate the weshop
new virtual host configuration file into the /etc/nginx/sites-enabled/
The final thing you need to know is that nginx will only read the above blocks that are enabled. To enabled use this command:
In the Nginx configuration file's 'sites-available' directory, the default site configuration is still present. Nginx cannot operate with multiple default sites, so we need to remove the existing default site configuration.
And site-enabled directory. Change directory to /etc/nginx/sites-enabled
.
To apply the changes, reload Nginx with:
Now go to your browser and access the application using the server’s domain name or IP address.
Boom. you will see your Laravel app.
Conclusion
In this tutorial, you’ve set up a new Laravel application on top of a LEMP stack (Linux, Nginx, MySQL and PHP), running on an Ubuntu 24.04 server with php 8.2. However, I didn't cover MySQL, you can use any other AWS RDS Postgres, SQLite etc.
If you have any error, you can contact me sbdev.business@gmail.com. Follow me and googling with keywords Shaung Bhone
. Thank you.
Last updated