I remember the first time I tried to run Laravel on a server. I had just finished a small school project and thought, “Let me put this online.” It sounded simple. But once I saw things like Nginx, PHP-FPM, and document root, I froze. It felt like walking into a room where everyone was speaking a language I didn’t know.
If you’re feeling the same way now, don’t worry. You’re not alone. In this guide, I’ll help you set up Laravel using Nginx and PHP-FPM step-by-step. I’ll also explain any confusing terms as we go. You don’t need to be a server pro to follow this.
Let’s get started.
What is Laravel, Nginx, and PHP-FPM?
Before we dive into setting things up, let’s go over what these words mean. It’s good to understand what you’re working with.
- Laravel is a tool that helps you build websites and web apps using PHP. It’s like a box of tools with many helpful features.
- Nginx (pronounced “engine-x”) is a web server. Think of it as a doorman that handles requests from people who want to see your website.
- PHP-FPM is short for “FastCGI Process Manager.” That’s a mouthful, right? Simply put, it helps your PHP code run faster and smoother, especially when many people visit your site at once.
These three work together like a team:
- Laravel is your app.
- Nginx is the front gate.
- PHP-FPM is the worker who builds each webpage behind the scenes.
Why Use Nginx and PHP-FPM?
You might be wondering: Why not just use Apache?
That’s a fair question. Apache is a popular web server, and it works well. But Nginx is known to be lighter and faster, especially when your site gets busy. Think of Apache as a big truck—strong but heavy. Nginx is more like a scooter—quick and simple.
Some reasons people (including me) choose Nginx:
- It uses less memory.
- It handles many users at once better.
- It’s easier to configure for some setups.
When you pair it with PHP-FPM, your Laravel app can run more efficiently.
Step-by-Step: Running Laravel on Nginx
Let’s now get our hands dirty. Below are the steps I use to get Laravel up and running with Nginx and PHP-FPM.
1. Set Up the Server
You’ll need a server with a Linux operating system. Most people use Ubuntu or Debian. If you’re working on your own computer, you can also install these with tools like VirtualBox or WSL (Windows Subsystem for Linux).
First, update your system:
sudo apt update && sudo apt upgrade -y
Then install Nginx:
sudo apt install nginx -y
Make sure it’s running:
sudo systemctl start nginx
And set it to start on boot:
sudo systemctl enable nginx
2. Install PHP and PHP-FPM
Laravel needs PHP 8.1 or higher. Let’s install PHP and the PHP-FPM package, plus some PHP extensions Laravel needs:
sudo apt install php8.1 php8.1-fpm php8.1-mbstring php8.1-xml php8.1-curl php8.1-mysql php8.1-bcmath php8.1-cli unzip curl git -y
Check that PHP is working:
php -v
3. Install Composer
Composer is a tool that helps you manage PHP packages. Laravel uses it.
Install Composer with this command:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Test it:
composer
4. Install Laravel
Now let’s get Laravel. You can install a fresh Laravel project like this:
cd /var/www
sudo composer create-project laravel/laravel laravel-app
Then change the ownership of the folder so Nginx can access it:
sudo chown -R www-data:www-data /var/www/laravel-app
5. Configure Nginx for Laravel
Here’s where things start to click.
Laravel’s public files (like index.php
) are inside the public
folder. We want Nginx to point there.
First, create a new Nginx config file:
sudo nano /etc/nginx/sites-available/laravel
Paste this inside:
server {
listen 80;
server_name yourdomain.com; # or your server IP
root /var/www/laravel-app/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Then activate the config:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
Restart Nginx:
sudo systemctl restart nginx
How to Test If It Works
Go to your browser and type your server’s IP or domain. You should see Laravel’s welcome page.
If not, check:
- Is the Laravel folder owned by
www-data
? - Did you point Nginx to the
/public
folder? - Are Nginx and PHP-FPM running?
Use these commands to check:
sudo systemctl status nginx
sudo systemctl status php8.1-fpm
Common Mistakes (I’ve Made Them Too)
Don’t worry if things don’t work the first time. Here are a few mistakes I’ve made:
- Wrong folder permissions. If Nginx can’t read your files, nothing shows up.
- Missing PHP extensions. Laravel needs some specific PHP tools, like mbstring and curl.
- Forgetting to restart Nginx after changing the config.
Always check your logs. These are your best friends when things break:
- Nginx logs:
/var/log/nginx/error.log
- Laravel logs:
storage/logs/laravel.log
Tips for a Better Setup
Here are some ideas to make your Laravel + Nginx setup even better:
Security
- Use HTTPS. Tools like Let’s Encrypt make it free and easy.
- Never expose
.env
files. They hold secrets.
Performance
- Turn on OPcache for faster PHP.
- Use Laravel’s cache and queue features.
Clean Code
- Keep your Laravel project updated.
- Remove unused packages with
composer remove
.
When to Use Apache Instead?
Sometimes Apache is a better fit. For example:
- If you’re using shared hosting, Apache is usually pre-installed.
- Some older Laravel packages assume
.htaccess
, which Apache uses but Nginx doesn’t.
In those cases, Apache might be easier. But for most modern apps and custom servers, Nginx + PHP-FPM is a great combo.
Final Thoughts
Setting up Laravel with Nginx and PHP-FPM sounds tricky at first. But once you go through it step by step, it starts to make sense. I’ve made mistakes along the way, but that’s part of learning. You don’t need to be perfect.
Just remember:
- Nginx handles requests.
- PHP-FPM runs your Laravel code.
- Laravel lives in
/var/www/yourapp/public
.
Take your time. Test often. Break things and fix them.
Let me ask you—have you set up Laravel on a server before? If so, how did it go? If not, what’s stopping you?
Let me know if you’d like a follow-up article on deploying Laravel with HTTPS or using a database like MySQL or PostgreSQL.