deploy


Nginx

  • Improvements
  • Autoloader improvements
    • Configuration loading improvements
    Route loading improvements
  • Laravel
  • application to a production environment, please make sure to pay attention to a few important points to ensure that your application runs as efficiently as possible. In this article we will cover a few key points to ensure your
  • Laravel application is deployed properly.

server configuration

Nginx

If you want to deploy your application to the Nginx server, you may use the following configuration file as a starting point to configure your

Web
server . Most likely, this file will need some custom modifications based on your server configuration. If you need help managing your server, consider using 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;   
         }
     }

Improvements

##Autoloader improvements

When you are ready to deploy your application to production, make sure you Optimized the autoloading mapping of your Composer class, so that Composer can quickly find the correct loading file to load a given class:

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

Tips: In addition to optimizing the autoloader, You should also make sure to include the

composer.lock file in your project repository. When you have the composer.lock file in your project code, you can install the dependencies needed in the project faster.

Optimize configuration loading

When you deploy your application to production, you should ensure Run the

config:cache Artisan command during your deployment:

php artisan config:cache

This command merges all Laravel configuration files into a cache file, this time greatly reducing the time the framework loads configuration values. The number of times the file system must be accessed.

{note} If you execute the

config:cache command during your deployment, you should ensure that you only call the env function from your configuration file . Once the configuration is cached, the .env file will not be loaded and all calls to the env function will return null.

Optimize route loading

If you want to build a large application with many routes, you should Make sure to run the

route:cache Artisan command during your deployment:

php artisan route:cache

This command will reduce all route registrations to a single method call in a cache file, thus registering hundreds of Improved the performance of route registration when routing.

{note} Because this feature uses PHP serialization, you can only cache application routes that specifically use controller-based routing. PHP cannot serialize closure routes.

Deploying with Forge

If you are not ready to manage your own server configuration, or not If you are familiar with configuring the various services required to run powerful Laravel applications, Laravel Forge is a good choice.

Laravel Forge can create servers on various infrastructure providers (such as DigitalOcean, Linode, AWS, etc.). Additionally, Forge installs and manages all the tools needed to build powerful Laravel applications, such as: Nginx, MySQL, Redis, Memcached, Beanstalk, and more.

This article was first published on the
LearnKu.com website.