Bobcares

Install and Configure Laravel with Nginx on Ubuntu 24.04

PDF Header PDF Footer

Learn how to install and configure Laravel with Nginx on Ubuntu 24.04. Our Laravel Support team is here to answer queries and concerns.

Install and Configure Laravel with Nginx on Ubuntu 24.04Install and Configure Laravel with Nginx on Ubuntu 24.04

Laravel is a popular open-source PHP framework designed to simplify the development of modern, robust web applications.

Today, we will explore how to install and configure Laravel with Nginx on Ubuntu 24.04.

Why Choose Laravel?

Laravel offers a range of built-in tools that make development faster and more maintainable. Here are some of its key features:

  • It helps organize code by separating business logic, presentation, and data.
  • Laravel provides a clean, expressive syntax for defining application routes.
  • A lightweight engine that allows reusable components and dynamic content.
  • Interacts with the database using PHP syntax instead of raw SQL.

Benefits of Laravel

  • Simplifies common tasks like routing, caching, and sessions.
  • Ideal for both small websites and large-scale enterprise applications.
  • Offers countless packages, tutorials, and active forums.
  • Guards against SQL injection, XSS, and other common threats.
  • Easily scales to handle growing traffic and feature requirements.

If you’re deploying Laravel on a cloud provider like DigitalOcean, this DigitalOcean LEMP Laravel guide can help you handle cloud-specific tweaks.

Prerequisites

Before getting started, we need:

  • An Ubuntu 24.04 server with root or `sudo` privileges
  • A working LEMP stack (Linux, Nginx, MySQL, PHP)
  • A domain or subdomain pointing to your server

Step-by-Step Guide

  1. We begin by updating the system with this command:
    sudo apt update && sudo apt upgrade -yCopy Code
  2. Laravel requires PHP 8.3 and several extensions. So, run this command:
    sudo 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 -yCopy Code
  3. Next, we need to install Composer. It is a PHP dependency manager that Laravel depends on.
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
    composer --version
    Copy Code
  4. Then, it is time to create a new Laravel project. So, go to the web root:
    cd /var/www/html
    composer create-project --prefer-dist laravel/laravel myproject
    Copy Code

    Replace `myproject` with the project name. If you encounter a “vendor folder missing” error after creating your Laravel app, check out this troubleshooting guide to resolve it.

  5. Now, ensure Nginx can access and write to the necessary directories:
    
    cd /var/www/html/myproject
    sudo chown -R www-data:www-data .
    sudo chmod -R 775 storage bootstrap/cache
    Copy Code
  6. Next, create a new Nginx configuration:
    sudo nano /etc/nginx/sites-available/myprojectCopy Code

    Then, paste the following, updating `mydomain.com` and project paths as needed:

    
    server {
    listen 80;
    server_name mydomain.com;
    root /var/www/html/myproject/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 ~ ^/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;
    }
    }
    Copy Code
  7. Now, enable the site and restart Nginx:
    
    sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl restart nginx
    Copy Code
  8. Then, edit the Laravel environment file:
    nano /var/www/html/myproject/.envCopy Code
  9. Next, update the database credentials:
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=yourdatabase
    DB_USERNAME=yourusername
    DB_PASSWORD=yourpassword
    Copy Code

    Facing issues with proc_open() in Laravel on some servers? Here’s a helpful fix: proc_open Laravel error explained.

  10. Next, generate the application key:
    php artisan key:generateCopy Code
  11. Now, we need to create a MySQL database. So, log in to MySQL:
    sudo mysql -u root -pCopy Code
  12. Then run:
    CREATE DATABASE yourdatabase;
    CREATE USER 'yourusername'@'localhost' IDENTIFIED BY 'yourpassword';
    GRANT ALL PRIVILEGES ON yourdatabase.* TO 'yourusername'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
    Copy Code
  13. Go to the Laravel directory:
    cd /var/www/html/myprojectCopy Code
  14. Then, run these commands:
    
    php artisan migrate
    php artisan config:clear
    php artisan cache:clear
    php artisan route:clear
    php artisan serve
    Copy Code
  15. Now, we can visit our domain in a web browser:
    http://mydomain.comCopy Code

    We will see the Laravel welcome page.

Bonus Tips

  • Secure the server with a firewall and SSL (e.g., Let’s Encrypt)
  • Set up Laravel queues, jobs, and scheduled tasks for advanced functionality.
  • Optimize performance with caching and config optimization for production.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

With the above steps, we can install and configure Laravel with Nginx on Ubuntu 24.04 with ease. This setup provides a solid foundation for building powerful and scalable web applications.

In brief, our Support Experts demonstrated how to install Laravel with Nginx on Ubuntu 24.04.

0 Comments

Submit a Comment

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

Get featured on the Bobcares blog and share your expertise with a global tech audience.

WRITE FOR US
server management

Spend time on your business, not on your servers.

TALK TO US

Or click here to learn more.

Speed issues driving customers away?
We’ve got your back!