Skip to content
🚀 Limited-time offer $130 for Lifetime Pro access. Buy once and use forever

Status List

Uptime & Status Pages

NGINX + Laravel Setup Guide

nginx + laravel

NGINX makes it easy to run Laravel on a production web server. The setup has a few steps, but it’s pretty straight forward. Let’s get your app off into production!

The Laravel config is like any other PHP web application. We need a web server (NGINX) and a php runtime (PHP-FPM).

Note: you will still need to setup your database and other services after this guide.

Contents

Install Packages

First, let’s install NGINX and PHP-FPM. Pull out your favorite package manager and run the install command. Here’s an example using yum:

				
					yum install -y nginx php-fpm php
php -v
				
			

If you want a particular version of php, you may need to modify your install command to include the version (e.g. yum install -y php8.1)

We also need to install some php extras for Laravel. We need the following extra packages: 

  • Ctype
  • CURL
  • DOM
  • FileInfo
  • Filter
  • Hash
  • Mbstring
  • OpenSSL
  • PCRE
  • PDO
  • Session
  • Tokenizer
  • XML

Here’s a command to install those extras:

				
					yum install -y php-ctype php-curl php-dom php-fileinfo php-filter php-hash php-mbstring php-openssl php-pcre php-pdo php-session php-tokenizer php-xml
				
			

Setup PHP-FPM

Let’s get PHP-FPM setup and ready to go. By default PHP-FPM will create a www worker pool. Let’s check that pool is configured correctly. Open the the configuration file at /etc/php-fpm.d/www.conf. Verify the listen property is set to listen = /run/php-fpm/www.sock. If you have another unix socket path here, that’s ok – just take note of it.

Let’s start up the PHP-FPM service. Run the command service php-fpm start. You should see the service and worker pool running. You may also want to set the service to auto-start on boot. You can use the following command to enable auto-start: systemctl enable php-fpm.

Ready to know about downtime before your customers?
 
Status List delivers uptime monitoring and professional hosted status pages for sites of all shapes and sizes.

Trusted by 1000+ companies

Setup Laravel

Laravel needs a little tweaking for the production environment. I’m going to assume you’ve cloned your application files and setup your .env file already. Let’s run a few optimizations for Laravel. 

First, we’ll optimize the autoloader. This will improve response time by optimizing the lookup map for production use. Run the following in your application folder:

				
					composer install --optimize-autoloader --no-dev
				
			

Second, we’ll optimize your configuration files. This will combine all your configurations into a single file for Laravel to load. You’ll need to run this command after each update deployment. Run the following in your application folder:

				
					php artisan config:cache
				
			

Finally, if you have lots of routes or views, you’ll want to run the following optimizations: 

				
					php artisan route:cache
php artisan view:cache
				
			

Setup NGINX

NGINX needs a little bit of direction to connect PHP-FPM and your Laravel site. For more configuration details, you may want to check out our NGINX Configuration Guide.

Create a new site for NGINX by making a new config file at /etc/nginx/site-available/mysite.com.conf. Create a server block that listens on port 80. Add a location block for .php that directs requests to our PHP-FPM unix socket. See below for a full example:

				
					server {
    listen 80;
    listen [::]:80;
    server_name mysite.com;
    root /opt/mysite.com/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:/run/php-fpm/www.sock; # if you have a different php-fpm socket path, enter it here
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}
				
			

Test NGINX Config

Check that your configuration is ok using the nginx -t command. If there are any syntax errors, please fix them.

Putting it all together

Let’s check if everything is working. Start the NGINX service (service nginx start). Open up your browser and access your site. You should see your Laravel home page loaded up.

Don’t forget to setup your database and other services if you need those.

Next Steps