To install Laravel with Nginx on Ubuntu, prepare PHP and its required extensions, install Composer, create a Laravel project, and configure Nginx to serve the project’s public directory through PHP-FPM. The steps below use Ubuntu’s package repositories and a standard Nginx server block; check your Ubuntu and PHP versions before using version-specific package names.
How the Laravel and Nginx stack works
Nginx accepts web requests and serves static files, such as images and CSS. For PHP requests, it passes work to PHP-FPM, a separate service that runs PHP code. Laravel’s front controller, public/index.php, receives application requests and routes them through the framework.
Composer installs Laravel and manages the project’s PHP dependencies. A typical request path is: browser → Nginx → PHP-FPM → Laravel. The web server should expose only Laravel’s public directory—not the project root, which contains configuration and application files.
1. Update Ubuntu and install the prerequisites
Connect to the server using an account with sudo privileges. Update package information and install Nginx, PHP-FPM, PHP’s command-line interface, and common Laravel extensions:
sudo apt update
sudo apt upgrade
sudo apt install nginx php-fpm php-cli php-mbstring php-xml php-curl php-zip php-bcmath php-sqlite3 unzip git
Laravel’s exact PHP requirements vary by framework release, and applications may require additional extensions or a database driver. Consult the documentation for the Laravel version and project you plan to run before settling on a PHP version or extension list. If you use MySQL, for example, install the matching PHP MySQL extension; SQLite and PostgreSQL applications need their respective drivers.
Check what Ubuntu installed:
php -v
php -m
systemctl status nginx
On Ubuntu, the PHP-FPM service name includes its version, such as php8.3-fpm. Find the installed service rather than copying a version from an unrelated guide:
systemctl list-units --type=service 'php*-fpm.service'
2. Install Composer and create the Laravel project
Composer is Laravel’s dependency manager. Prefer the official Composer installation instructions for the current installer and its integrity-check procedure. Avoid treating a downloaded installer as trustworthy without verifying it. If Ubuntu’s repository provides Composer, you can install it with:
sudo apt install composer
composer --version
Create the project in a directory intended for web applications. The following commands create a project named example-app under /var/www:
cd /var/www
sudo composer create-project laravel/laravel example-app
cd /var/www/example-app
Running Composer as root can create files that are awkward to manage later. For a long-lived deployment, create or select a dedicated deployment account, give it ownership of the project, and run Composer as that account. For example, if the deployment account is deploy, set ownership accordingly before installing the project. Do not make the whole application writable by the web-server account just to get past a permissions error.
Laravel needs a configured environment file. A new project commonly includes .env.example; if the installer has not created .env, copy the example and generate an application key:
cp .env.example .env
php artisan key:generate
Set environment values—especially the application environment, database connection, and credentials—before deployment. Keep .env private and out of version control. Never reuse a production application key in unrelated applications; changing an established application’s key can invalidate encrypted data or sessions.
3. Set Laravel’s file permissions
Laravel must be able to write to storage and bootstrap/cache. The deployment user should own the application files, and the PHP-FPM process needs write access to those two directories. A common arrangement uses a shared group for the deployment user and the PHP-FPM worker, but the correct group depends on your server configuration.
Inspect the PHP-FPM pool configuration to identify the worker user and group, then grant only the required access. Avoid broad permissions such as chmod -R 777: they allow every local user to modify files and can turn a minor issue into a security problem. Confirm that secrets and source files are not writable by unrelated system users.
4. Configure Nginx for Laravel
Create a server block, replacing example.com and the PHP-FPM socket with values for your server. Check the socket path under /run/php/; it typically includes the installed PHP version.
sudo nano /etc/nginx/sites-available/example-app
Use a configuration like this:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example-app/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
The try_files rule sends application routes that are not real files to Laravel’s front controller. The PHP location passes PHP scripts to PHP-FPM, while the document root limits public access to the intended directory. Change php8.3-fpm.sock to the socket actually present on your machine. If Nginx and PHP-FPM are configured to communicate over TCP instead, use the configured address and port rather than a socket path.
Enable the site, test the syntax, and reload Nginx only after the test succeeds:
sudo ln -s /etc/nginx/sites-available/example-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
If the default Nginx site claims the same hostname or catches requests unexpectedly, review the enabled server blocks and disable the default site if appropriate. DNS for the domain must point to the server before visitors can reach it by name.
5. Verify the application and prepare for production
Visit the domain or server address after confirming Nginx and the PHP-FPM service are running. If Laravel displays an error, inspect its log at storage/logs/laravel.log, and check Nginx and PHP-FPM logs as well. Once configuration is correct, production deployments commonly use:
php artisan config:cache
php artisan route:cache
php artisan view:cache
Run these commands as the deployment user and repeat the appropriate cache-clearing or rebuilding steps after configuration changes. Do not enable Laravel debug output on a public production site: detailed exception pages can expose sensitive information. Configure HTTPS with a trusted certificate, keep Ubuntu and application dependencies updated, and restrict SSH access using your organization’s normal server practices.
Common problems and fixes
- 502 Bad Gateway: Nginx cannot reach PHP-FPM. Confirm that the FPM service is active and that
fastcgi_passmatches its actual socket or address. - 404 on Laravel routes: Check that the Nginx
try_filesrule falls back to/index.phpand that the document root ends in/public. - 403 Forbidden: Verify directory traversal permissions and that Nginx can read the public files. Check the Nginx error log for the exact path or permission failure.
- Laravel cannot write logs or cache: Grant the PHP-FPM user or shared group access to
storageandbootstrap/cache, not the entire project indiscriminately. - Composer reports a missing extension: Install the extension matching the active PHP version, then restart PHP-FPM if needed. Check both CLI and FPM configurations, which can differ.
Frequently Asked Questions
Which directory should Nginx use for Laravel?
Use the project’s public directory, such as /var/www/example-app/public. Pointing Nginx at the project root can expose files that should remain private.
Do I need PHP-FPM to run Laravel with Nginx?
For the usual Nginx and PHP setup, yes: Nginx forwards PHP requests to a PHP processor such as PHP-FPM. Nginx alone does not execute Laravel’s PHP code.
Why does Laravel work from the terminal but not in a browser?
The command-line PHP version or extensions may differ from PHP-FPM’s, or Nginx may point to the wrong socket. Check the FPM service, socket, installed extensions, and server logs.
Can I install Laravel with SQLite instead of MySQL?
Yes, if the Laravel version and application support SQLite. Install the matching PHP SQLite extension, configure the database connection in .env, and ensure the database file and its directory have appropriate permissions.
Is the default setup ready for production?
It is a starting point, not a complete production security plan. Configure HTTPS, production environment values, restricted file permissions, backups, updates, and any required database and queue services before exposing a real application.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix Error 0x80072f76 0x20016 in Windows
- How to Backup and Restore Drivers in Windows 11
- Fix Windows Couldn’t Complete the Requested Changes
- Move to iOS Stuck? Fixes That Actually Work
- USB Bluetooth Dongle: What It Is and How It Works
- ADB Device Not Found Error on Windows: Fixed