Setting Up Nginx for Laravel: A Comprehensive Guide
Tags: nginx, laravel, webdev, deployment
Published: 2024-02-23 21:48:43

## Introduction

In the ever-evolving world of web development, Laravel has consistently secured its position as one of the most popular PHP frameworks. Its elegant syntax, extensive set of features, and robustness have made it a favorite among developers world over. However, to bring a Laravel application to life, a server is needed. This is where Nginx comes in.

Nginx is a high-performance HTTP server that is known for its stability, rich feature set, simple configuration, and low resource consumption. Nginx can be efficiently configured for Laravel, hence making it a suitable choice for running Laravel applications. This blog post aims to guide you through the process of setting up Nginx for Laravel. It will cover the steps involved in installing Nginx, configuring Laravel, and setting up Nginx to work with Laravel.

## Installing Nginx

Before we can configure Nginx for Laravel, Nginx itself needs to be installed on the server. This guide will focus on installing Nginx on a Ubuntu machine. This can be accomplished by executing the following commands in the terminal:

```bash
sudo apt update
sudo apt install nginx

```

These commands update your package lists for upgrades and new package installations followed by the installation of Nginx. After the installation is complete, it's crucial to verify that Nginx was installed correctly. You can do this by checking the version of Nginx installed on your server. To check the Nginx version, use the command `nginx -v`. If Nginx has been installed successfully, the version number will be displayed.

## Installing PHP

Before Laravel can run on your Nginx server, you need to install PHP and PHP-FPM (FastCGI Process Manager). PHP-FPM is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. Here is how you can install PHP and PHP-FPM on Ubuntu:

Install PHP and PHP-FPM. If you want to install a specific version of PHP, replace 'php' with the version you want to install. For example, if you want to install PHP 7.4, you would use 'php7.4' and 'php7.4-fpm':

```bash
sudo apt install php php-fpm php-mysql
```

After the installation is complete, you can verify that PHP and PHP-FPM have been installed correctly by checking their versions. Use these commands to check the PHP and PHP-FPM versions:

```bash
php -v
```

If PHP and PHP-FPM have been installed successfully, the version numbers will be displayed.

![Php Version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fncimwwca04u9y2gnqo6.png)

## Laravel Configuration

Before we jump into configuring Nginx, it's essential that your Laravel project is set up and ready. If you haven't yet created a Laravel project, you can do so by executing the command `composer create-project --prefer-dist laravel/laravel blog`, where 'blog' is the name of your project.

After creating a Laravel project, it's imperative to set the appropriate permissions for the storage and bootstrap/cache directories to ensure that Laravel functions correctly. Use the following commands to set these permissions:

```bash
sudo chown -R :www-data /path/to/your/laravel/root/directory
sudo chmod -R 775 /path/to/your/laravel/root/directory/storage
sudo chmod -R 775 /path/to/your/laravel/root/directory/bootstrap/cache
```

Remember to replace '/path/to/your/laravel/root/directory' with the actual path to your Laravel project.


## **Configuring Nginx for Laravel**

**Create a New Configuration File**

Navigate to Nginx's **`sites-available`** directory and create a configuration file for your project:

```bash
sudo nano /etc/nginx/sites-available/your_domain.com
```

**Configuration File Content**

Paste the following configuration, adjusting **`your_domain.com`**, the root directory, and the PHP version as necessary:

```bash
server {
    listen 80;
    server_name your_domain.com;
    root /var/www/your_domain.com/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm 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.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

## **Enabling Your Site and Restarting Nginx**

**Enable the Site**

Link your site to Nginx's **`sites-enabled`** directory:

```bash
sudo ln -s /etc/nginx/sites-available/your_domain.com /etc/nginx/sites-enabled/
```

**Test Nginx Configuration**

Ensure there are no syntax errors:

```bash
sudo nginx -t
```

**Restart Nginx**

Apply your changes:

```bash
sudo systemctl restart nginx
```

## **Conclusion**

While setting up Nginx for Laravel might seem like a daunting task initially, especially for beginners, it is not as complicated as it appears. Once you delve into the process and understand each step, it becomes pretty straightforward.

The crux of the setup involves three primary steps: the installation of Nginx, the configuration of Laravel, and the Nginx configuration for Laravel. A smooth and successful setup is often achieved by meticulously following these steps and paying close attention to the configurations.

Once set up, you can enjoy the benefits of a high-performance HTTP server running your Laravel applications. Happy coding, and here's to creating fantastic applications with Laravel and Nginx!