Need Assistance?

In only two hours, with an average response time of 15 minutes, our expert will have your problem sorted out.

Server Trouble?

For a single, all-inclusive fee, we guarantee the continuous reliability, safety, and blazing speed of your servers.

How to Install and Configure Laravel with Nginx on Ubuntu 24.04

Laravel is a popular PHP framework for building modern web applications. This guide will walk you through the process of installing and configuring Laravel with Nginx on Ubuntu 24.04, ensuring everything is set up correctly for a smooth development experience.

Prerequisites

Before you begin, ensure you have the following:

  • Ubuntu 24 server with root or sudo privileges.
  • A working LEMP stack installed.
  • Basic knowledge of the terminal and server management.
  • A domain or subdomain set up to point to your server.

Step 1: Update Your System

sudo apt update && sudo apt upgrade

Step 2: Install Required Dependencies

Laravel requires several PHP extensions and software packages. Here we’re installing latest PHP version(8.3). To install them, use the following commands:

apt install php8.3 php8.3-fpm php8.3-cli php8.3-common php8.3-curl php8.3-mbstring php8.3-mysql php8.3-xml php8.3-zip

Step 3: Install Composer

Composer is a dependency manager for PHP, and Laravel depends on it to manage libraries and packages. To install Composer globally, use the following commands:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer --version

You should see the Composer version printed.

Step 4: Install Laravel

Now that Composer is installed, you can use it to create a new Laravel project. Navigate to the directory where you want your project to be located and run:

cd /var/www/html
composer create-project --prefer-dist laravel/laravel myproject

Replace myproject with your desired project name or you can use . to install it on the current directory.

Step 5: Set Permissions for Laravel

After creating the Laravel project, you need to set the correct permissions so that NGINX can read and write files. Run the following commands:

cd myproject 
sudo chown -R www-data:www-data /var/www/html/myproject 
sudo chmod -R 775 /var/www/html/myproject/storage /var/www/html/myproject/bootstrap/cache

Step 6: Configure NGINX for Laravel

Create a new NGINX configuration file for your Laravel project:

sudo nano /etc/nginx/sites-available/myproject

Add the following to the configuration file:

server {
    listen 80;
    server_name mydomain.com;
    root /var/www/html/myproject/public; #replace with your document root
 
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
 
    index 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 ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }

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

Create a symbolic link to enable this site:

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/

Test the NGINX configuration and restart:

sudo nginx -t
sudo systemctl restart nginx

Step 7: Configure the Laravel EnvironmentLaravel uses a .env file for environment settings, such as database credentials.
Open the .env file in your Laravel project:

vim  /var/www/html/myproject/.env

Adjust the following lines with your actual database credentials:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=yourdatabase 
DB_USERNAME=yourusername 
DB_PASSWORD=yourpassword

You can generate the application key by running the following command:

php artisan key:generate

Step 8: Set Up the Database

Log into MySQL and create a database for Laravel:

CREATE DATABASE yourdatabase; 
CREATE USER 'yourusername'@'localhost' IDENTIFIED BY 'yourpassword'; 
GRANT ALL PRIVILEGES ON yourdatabase.* TO 'yourusername'@'localhost'; 
FLUSH PRIVILEGES; 
EXIT;

Replace database name, username and password with your preferred details given in the .env file.

Step 9: Migrations, Clearing Caches, and Running the Application. 

After the database setup, navigate to the Laravel document directory:

cd  /var/www/html/myproject

Run the following commands for successful setup and running of your Laravel project.

php artisan migrate
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan serve

Step 10: Access Your Laravel Application

Finally, navigate to your server’s IP address or domain in a browser, and you should see the default Laravel welcome page.

http://mydomain.com

You’ve successfully installed and configured Laravel with Nginx on Ubuntu 24.04! You can now begin developing your Laravel application. Remember to secure your server, set up SSL for production, and optimize your app for better performance.

If you need further assistance with setting up Laravel with Nginx on Ubuntu 24.04, feel free to contact our support team. We’re here to help with any installation or configuration queries.

Liked!! Share the post.

Get Support right now!

Start server management with our 24x7 monitoring and active support team

Let us know your requirement.

Can't get what you are looking for?

Get Support Right Away!

Thank You

We have received your query and will get back to you soon.