w3resource

Laravel (5.7) Deployment

Introduction:

The following will be great starting points for making sure your Laravel application is deployed properly and efficiently.

Server Configuration

Deploying your laravel application to a server that is running Nginx, will require you to use the following configuration file as a starting point for configuring your web server. Most likely, this file will need to be customized depending on your server's configuration. Just in case you would like assistance in managing your server, consider using a service such as Laravel Forge

server {
    listen 80;
    server_name example.com;
    root /example.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/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

Let's explain some of the directives used here;

The listen directive in the default configuration file has the default_server option enabled, which specifies that the server block should serve a request if no other server block is suitable.

The root directive specifies where application files are stored. The Laravel application is stored in /var/www/html/quickstart, but only the /public subdirectory should be exposed to the internet; all other application files should not be accessible via the browser at all. To comply with these best practices, we set the webroot to /var/www/html/quickstart/public.

The server_name directive specifies the list of domain names the server block will respond to. We used example.com and www.example.com here, but you should replace those with the domain name you want to use for your website.

Previous: Laravel (5.7) Valet
Next: Laravel (5.7) Request Lifecycle



Share this Tutorial / Exercise on : Facebook and Twitter