រំលងទៅមាតិកា

កម្ពុជាត្រូវការសន្តិភាព / Cambodia needs peace

4 នាទីអាន 11Laravel
Laravel

Deploying Laravel with PHP 8.4 and Nginx on Ubuntu: A Production-Ready Guide

Learn how to configure a high-performance environment for Laravel using PHP 8.4 and Nginx on Ubuntu. This guide covers everything from PPA installation to advanced Nginx virtual host configuration.

Introduction

While Laravel's built-in development server (php artisan serve) is convenient for local coding, it is not designed for production or staging environments. For high-performance, concurrent handling, and robust security, a combination of Nginx and PHP-FPM is the industry standard. With the release of PHP 8.4, developers can now leverage new language features like Property Hooks and Asymmetric Visibility while maintaining the lightning-fast execution Laravel is known for.

This guide provides a comprehensive, step-by-step walkthrough for setting up Laravel on an Ubuntu server using Nginx and PHP 8.4, specifically focusing on virtual host configuration to bypass the need for the artisan development server.

Prerequisites

Before starting, ensure you have:

  • An Ubuntu 22.04 or 24.04 server instance.
  • A user with sudo privileges.
  • Basic familiarity with the command line.
  • A domain name or IP address pointing to your server.

Step 1: Update the System and Add Repositories

First, ensure your package list is up to date. Since PHP 8.4 is the latest version, we need to add the ondrej/php PPA (Personal Package Archive) to fetch the most recent binaries.

sudo apt update && sudo apt upgrade -y
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

Step 2: Install PHP 8.4 and Essential Extensions

Laravel requires several PHP extensions to handle encryption, database connections, and string manipulation. We will install PHP 8.4-FPM (FastCGI Process Manager) which is necessary for Nginx to communicate with PHP.

sudo apt install -y php8.4-fpm php8.4-mysql php8.4-mbstring php8.4-xml php8.4-curl php8.4-zip php8.4-bcmath php8.4-intl php8.4-cli php8.4-gd

After installation, verify the version:

php -v

Step 3: Install Nginx

Nginx acts as our web server and reverse proxy. Install it using the following command:

sudo apt install -y nginx

Ensure the Nginx service is running and enabled to start on boot:

sudo systemctl enable nginx
sudo systemctl start nginx

Step 4: Install Composer and Laravel

Composer is the dependency manager for PHP. We will install it globally and then create a new Laravel project in the /var/www directory.

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

cd /var/www
sudo composer create-project laravel/laravel my-laravel-app

Step 5: Configure Permissions

For Nginx to serve the Laravel application, the web server user (www-data) must own the storage and bootstrap cache directories.

sudo chown -R www-data:www-data /var/www/my-laravel-app/storage
sudo chown -R www-data:www-data /var/www/my-laravel-app/bootstrap/cache
sudo chmod -R 775 /var/www/my-laravel-app/storage

Step 6: Configure the Nginx Virtual Host

Instead of running php artisan serve, we will create an Nginx server block. This configuration tells Nginx how to handle incoming requests and pass them to PHP 8.4-FPM.

Create a new configuration file:

sudo nano /etc/nginx/sites-available/my-laravel-app

Paste the following configuration, replacing example.com with your domain or IP address:

server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    root /var/www/my-laravel-app/public;

    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 ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

Key Configuration Details:

  • root: Points to the /public directory of Laravel, not the root folder.
  • try_files: Ensures that SEO-friendly URLs are routed through index.php.
  • fastcgi_pass: Points to the PHP 8.4 socket file.

Step 7: Enable the Configuration and Restart Nginx

Enable the site by creating a symbolic link to the sites-enabled directory and test the configuration for syntax errors.

sudo ln -s /etc/nginx/sites-available/my-laravel-app /etc/nginx/sites-enabled/
sudo nginx -t

If the test is successful, restart Nginx and PHP-FPM:

sudo systemctl restart nginx
sudo systemctl restart php8.4-fpm

Conclusion

You have successfully deployed a Laravel application on Ubuntu using PHP 8.4 and Nginx. By configuring a virtual host, you have created a environment that is significantly more stable and scalable than the default development server. Your application is now reachable via your domain name, with PHP-FPM efficiently handling processes in the background.

For a production environment, your next steps should include setting up an SSL certificate using Let's Encrypt and configuring a database like MySQL or PostgreSQL.

ចែករំលែកអត្ថបទនេះ

XLinkedIn

© 2026 Hel Mab. រក្សាសិទ្ធិគ្រប់យ៉ាង.

ភ្នំពេញ កម្ពុជា/បង្កើតដោយ Nuxt