How to Install Laravel on Ubuntu 22.04 with Nginx - vetechno
5/5 - (3 votes)

Last Updated on November 27, 2022 by Vikash Ekka

How to Install Laravel on Ubuntu 22.04 with Nginx - vetechno
How to Install Laravel on Ubuntu 22.04 with Nginx – vetechno

In this tutorial, we are going to install and configure latest Laravel framework on Ubuntu 22.04 with Nginx.

Before we start installing latest Laravel , make sure that you have already installed LEMP stack (Linux, Nginx, MySQL, and PHP). If not done yet, then you can follow the link to install and configure the LAMP stack on your Ubuntu system Link

What is Laravel ?

Laravel is an open-source PHP web application framework which is developed for faster implementation and development of web applications along with many built-in features and many libraries. It provides a set of tools and resources to build modern PHP applications.

The framework is created by Taylor Otwell and its source code is hosted on GitHub.

Prerequisites

  1. You must have sudo / root privileges to access Ubuntu Operating System
  2. Internet connection
  3. Operating System :- Ubuntu 22.04, 20.04, 18.04 LTS.

Follow the below Simple Steps to install and Configure Laravel on Ubuntu 22.04 LTS.

First Update System Packages:

To install any package on Ubuntu operating system need to update the repository’s package list by using the following command:

sudo apt update

Install PHP 8.1 on Ubuntu 22.04 LTS.

Before we move forward please make sure you have installed the PHP dependency packages. Here I’m installing PHP 8.1 for this tutorial.

sudo apt install php8.1

Install PHP 8.1 modules or extensions.

Below I have mentioned all PHP 8.1 module that are generally needed for executing PHP code on the web server.

apt-get install php8.1-common php8.1-mysql php8.1-xml php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-tidy php8.1-yaml php8.1-bcmath -y

Check PHP version and modules by following below command. php -m will list all PHP modules and extensions.

php -v
php -m

Install PHP composer on Ubuntu 22.04 LTS

To install Laravel, PHP composer is one of the dependencies which is required to install to run the Laravel framework properly in Ubuntu System. It is a library manager for PHP projects.

Here we will install PHP Composer using CURL command in your terminal . If you have not curl installed then follow the below command.

sudo apt install curl

Installing of PHP composer

sudo curl -sS https://getcomposer.org/installer | sudo php
sudo mv composer.phar /usr/local/bin/composer
sudo ln -s /usr/local/bin/composer /usr/bin/composer
install composer on ubuntu 22.05 – vetechno

Create a Database for Laravel on Ubuntu 22.04

We need to create a MySQL database for Laravel to connect with database, it stores the data generated by your Laravel app project.
Follow the below step by step commands for create new MySql Database

Create New Laravel Database

CREATE DATABASE laravel_db;

Create separate User for Laravel

CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'Password@123';

Grant full Privileges on Laravel Database

GRANT ALL ON laravel_db.* to 'laravel_user'@'localhost';

Flush Privileges and exit from the Mysql Database server

FLUSH PRIVILEGES;
quit;
Create a Database for Laravel on Ubuntu

Download and Configure the Laravel framework project on Ubuntu 22.04 LTS

Now go to /var/www/html/ directory, and download Laravel framework from composer using the below command.
it will create a folder and install the all dependency package to to run Laravel

sudo composer create-project --prefer-dist laravel/laravel vetechno-project

Note:- vetechno-project is my project name

Configure the Laravel database connection

Create a copy of .env.example to .env

sudo cp .env.example .env

Generate an encryption key for your app:

sudo php artisan key:generate
php artisan key generate

Open .env file and update the MySQL credentials that we have created earlier.

sudo vim .env
Laravel database connection

Save the file using:wq! , and hit the Enter key to exit.

Change the ownership of the Laravel directory

Now we need to give the web server user write access to the storage and cache folders, and set the correct permissions with the commands below:

sudo chown -R :www-data /var/www/html/vetechno-project/storage/
sudo chown -R :www-data /var/www/html/vetechno-project/bootstrap/cache/
sudo chmod -R 0775 /var/www/html/vetechno-project/storage/

Configure Niginx to Work with Laravel

We have installed the laravel on local server, now its time to configure Nginx to server the laravel

sudo apt install nginx

Go to /etc/nginx/sites-available/ and create a new .conf file for laravel

sudo vim /etc/nginx/sites-available/laravel.conf

Paste the following lines of code, save the file and close it.

server {
    listen 80;
    server_name vetechno-project;
    root /var/www/html/vetechno-project/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
    try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
             fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
             fastcgi_index index.php;
             fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
             include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
    deny all;
    }
}

Save the file using:wq! , and hit the Enter key to exit.

To activate laravel conf, create symbolic link by the follow command.

sudo ln -s /etc/nginx/sites-available/laravel.conf /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default

Next check the Nginx configuration syntax by using below command

nginx -t

If everything is working fine then restart the Nginx Server.

sudo systemctl restart nginx

Accessing Laravel Application from a Web Browser

Enter the server IP or domain address to access the Laravel Application in a Browser

http://localhost/
Accessing Laravel Application

Conclusion

Congratulation, you have successfully Installed and Configured the Laravel Framework project with Nginx on Ubuntu System.

Please me know in the comment box if you are facing any issue. We will happy to assist you.

Hope you enjoy it.

By Vikash Ekka

Hi All, My name is Vikash Ekka from India. I’m the founder and tech editor of https://www.vetechno.in. I have completed my Graduation in BCA. I love to write technical articles like Windows, Linux & MAC Tutorials, Tips, Tricks, How To fix, Tutorials About Ethical Hacking & Cyber Security Guide, and Software Review. Currently, I have been working as an IT professional since 2018.

Leave a Reply

Your email address will not be published. Required fields are marked *