فهرست ها

Deployment

Introduction

When you're ready to deploy your Laravel application to production, there are some important things you can do to make sure your application is running as efficiently as possible. In this document, we'll cover some great starting points for making sure your Laravel application is deployed properly.

Server Requirements

The Laravel framework has a few system requirements. You should ensure that your web server has the following minimum PHP version and extensions:

  • PHP >= 8.3

  • Ctype PHP Extension

  • cURL PHP Extension

  • DOM PHP Extension

  • Fileinfo PHP Extension

  • Filter PHP Extension

  • Hash PHP Extension

  • Mbstring PHP Extension

  • OpenSSL PHP Extension

  • PCRE PHP Extension

  • PDO PHP Extension

  • Session PHP Extension

  • Tokenizer PHP Extension

  • XML PHP Extension

Server Configuration

Nginx

If you are deploying your application to a server that is running Nginx, you may 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. If you would like assistance in managing your server, consider using a fully-managed Laravel platform like Laravel Cloud.

Please ensure, like the configuration below, your web server directs all requests to your application's public/index.php file. You should never attempt to move the index.php file to your project's root, as serving the application from the project root will expose many sensitive configuration files to the public Internet:


server {

    listen 80;

    listen [::]:80;

    server_name example.com;

    root /srv/example.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 ~ ^/index\.php(/|$) {

        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;

        include fastcgi_params;

        fastcgi_hide_header X-Powered-By;

    }

  

    location ~ /\.(?!well-known).* {

        deny all;

    }

}

FrankenPHP

FrankenPHP may also be used to serve your Laravel applications. FrankenPHP is a modern PHP application server written in Go. To serve a Laravel PHP application using FrankenPHP, you may simply invoke its php-server command:


frankenphp php-server -r public/

To take advantage of more powerful features supported by FrankenPHP, such as its Laravel Octane integration, HTTP/3, modern compression, or the ability to package Laravel applications as standalone binaries, please consult FrankenPHP's Laravel documentation.

Directory Permissions

Laravel will need to write to the bootstrap/cache and storage directories, so you should ensure the web server process owner has permission to write to these directories.

Optimization

When deploying your application to production, there are a variety of files that should be cached, including your configuration, events, routes, and views. Laravel provides a single, convenient optimize Artisan command that will cache all of these files. This command should typically be invoked as part of your application's deployment process:


php artisan optimize

The optimize:clear method may be used to remove all of the cache files generated by the optimize command as well as all keys in the default cache driver:


php artisan optimize:clear

In the following documentation, we will discuss each of the granular optimization commands that are executed by the optimize command.

Caching Configuration

When deploying your application to production, you should make sure that you run the config:cache Artisan command during your deployment process:


php artisan config:cache

This command will combine all of Laravel's configuration files into a single, cached file, which greatly reduces the number of trips the framework must make to the filesystem when loading your configuration values.

[!WARNING]

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function for .env variables will return null.

Caching Events

You should cache your application's auto-discovered event to listener mappings during your deployment process. This can be accomplished by invoking the event:cache Artisan command during deployment:


php artisan event:cache

Caching Routes

If you are building a large application with many routes, you should make sure that you are running the route:cache Artisan command during your deployment process:


php artisan route:cache

This command reduces all of your route registrations into a single method call within a cached file, improving the performance of route registration when registering hundreds of routes.

Caching Views

When deploying your application to production, you should make sure that you run the view:cache Artisan command during your deployment process:


php artisan view:cache

This command precompiles all your Blade views so they are not compiled on demand, improving the performance of each request that returns a view.

Reloading Services

[!NOTE]

When deploying to Laravel Cloud, it is not necessary to use the reload command, as gracefully reloading of all services is handled automatically.

After deploying a new version of your application, any long-running services such as queue workers, Laravel Reverb, or Laravel Octane should be reloaded / restarted to use the new code. Laravel provides a single reload Artisan command that will terminate these services:


php artisan reload

If you are not using Laravel Cloud, you should manually configure a process monitor that can detect when your reloadable processes exit and automatically restart them.

Debug Mode

The debug option in your config/app.php configuration file determines how much information about an error is actually displayed to the user. By default, this option is set to respect the value of the APP_DEBUG environment variable, which is stored in your application's .env file.

[!WARNING]

In your production environment, this value should always be false. If the APP_DEBUG variable is set to true in production, you risk exposing sensitive configuration values to your application's end users.

The Health Route

Laravel includes a built-in health check route that can be used to monitor the status of your application. In production, this route may be used to report the status of your application to an uptime monitor, load balancer, or orchestration system such as Kubernetes.

By default, the health check route is served at /up and will return a 200 HTTP response if the application has booted without exceptions. Otherwise, a 500 HTTP response will be returned. You may configure the URI for this route in your application's bootstrap/app file:


->withRouting(

    web: __DIR__.'/../routes/web.php',

    commands: __DIR__.'/../routes/console.php',

    health: '/up', // [tl! remove]

    health: '/status', // [tl! add]

)

When HTTP requests are made to this route, Laravel will also dispatch a Illuminate\Foundation\Events\DiagnosingHealth event, allowing you to perform additional health checks relevant to your application. Within a listener for this event, you may check your application's database or cache status. If you detect a problem with your application, you may simply throw an exception from the listener.

Deploying With Laravel Cloud or Forge

Laravel Cloud

If you would like a fully-managed, auto-scaling deployment platform tuned for Laravel, check out Laravel Cloud. Laravel Cloud is a robust deployment platform for Laravel, offering managed compute, databases, caches, and object storage.

Launch your Laravel application on Cloud and fall in love with the scalable simplicity. Laravel Cloud is fine-tuned by Laravel's creators to work seamlessly with the framework so you can keep writing your Laravel applications exactly like you're used to.

Laravel Forge

If you prefer to manage your own servers but aren't comfortable configuring all of the various services needed to run a robust Laravel application, Laravel Forge is a VPS server management platform for Laravel applications.

Laravel Forge can create servers on various infrastructure providers such as DigitalOcean, Linode, AWS, and more. In addition, Forge installs and manages all of the tools needed to build robust Laravel applications, such as Nginx, MySQL, Redis, Memcached, Beanstalk, and more.

استقرار (Deployment)

مقدمه (Introduction)

وقتی آماده هستید تا اپلیکیشن لاراول خود را در محیط پروداکشن یا همان عملیاتی (Production) مستقر کنید، کارهای مهمی وجود دارد که می‌توانید برای اطمینان از اجرای هرچه کارآمدتر برنامه خود انجام دهید. در این سند، به برخی از نکات شروع عالی خواهیم پرداخت تا مطمئن شویم اپلیکیشن لاراول شما به درستی مستقر (Deployed) شده است.

نیازمندی‌های سرور (Server Requirements)

فریم‌ورک لاراول (Laravel framework) چند نیازمندی سیستمی دارد. شما باید مطمئن شوید که وب‌سرور (Web Server) شما حداقل نسخه PHP و افزونه‌های (Extensions) زیر را دارد:

  • PHP >= 8.3

  • Ctype PHP Extension

  • cURL PHP Extension

  • DOM PHP Extension

  • Fileinfo PHP Extension

  • Filter PHP Extension

  • Hash PHP Extension

  • Mbstring PHP Extension

  • OpenSSL PHP Extension

  • PCRE PHP Extension

  • PDO PHP Extension

  • Session PHP Extension

  • Tokenizer PHP Extension

  • XML PHP Extension

پیکربندی سرور (Server Configuration)

Nginx

اگر اپلیکیشن خود را روی سروری مستقر می‌کنید که Nginx روی آن اجرا می‌شود، می‌توانید از فایل پیکربندی (Configuration File) زیر به عنوان نقطه‌ی شروع برای تنظیم وب‌سرور خود استفاده کنید. به احتمال زیاد، این فایل با توجه به پیکربندی سرور شما نیاز به شخصی‌سازی (Customized) خواهد داشت. اگر برای مدیریت سرور خود به کمک نیاز دارید، استفاده از یک پلتفرم کاملاً مدیریت‌شده (Fully-managed Platform) لاراول مانند Laravel Cloud را مد نظر قرار دهید.

لطفاً مطمئن شوید که وب‌سرور شما، مانند پیکربندی زیر، تمام درخواست‌ها (Requests) را به فایل public/index.php اپلیکیشن شما هدایت می‌کند. شما هرگز نباید فایل index.php را به ریشه یا همان روت (Root) پروژه خود منتقل کنید، زیرا در دسترس قرار دادن برنامه از ریشه پروژه، بسیاری از فایل‌های پیکربندی حساس را در معرض اینترنت عمومی قرار می‌دهد:

Nginx

server {
    listen 80;
    listen [::]:80;
    server_name example.com;
    root /srv/example.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 ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }

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

FrankenPHP

از FrankenPHP نیز می‌توان برای میزبانی و سرو کردن اپلیکیشن‌های لاراول استفاده کرد. FrankenPHP یک اپلیکیشن‌سرور (Application Server) مدرن برای PHP است که به زبان Go نوشته شده است. برای سرو کردن یک اپلیکیشن لاراول با استفاده از FrankenPHP، کافی است دستور (Command) مربوط به php-server آن را فراخوانی کنید:

Shell

frankenphp php-server -r public/

برای بهره‌مندی از ویژگی‌های قدرتمندتر FrankenPHP، مانند ادغام آن با لاراول اکتین (Laravel Octane)، پروتکل HTTP/3، فشرده‌سازی مدرن، یا قابلیت بسته‌بندی اپلیکیشن‌های لاراول به عنوان باینری‌های مستقل (Standalone Binaries)، لطفاً به مستندات لاراول در FrankenPHP مراجعه کنید.

دسترسی‌های دایرکتوری (Directory Permissions)

لاراول باید بتواند در دایرکتوری‌های (Directories) bootstrap/cache و storage بنویسد، بنابراین باید مطمئن شوید که مالک فرآیند وب‌سرور (Web server process owner) مجوز نوشتن (Permission) در این دایرکتوری‌ها را دارد.

بهینه‌سازی (Optimization)

هنگام استقرار اپلیکیشن در محیط پروداکشن (Production)، فایل‌های مختلفی وجود دارند که باید کاش یا همان حافظه پنهان (Cache) شوند؛ از جمله پیکربندی‌ها، رویدادها، مسیرها و ویوها. لاراول یک دستور تک و راحت در Artisan به نام optimize ارائه می‌دهد که تمام این فایل‌ها را کاش می‌کند. این دستور معمولاً باید به عنوان بخشی از فرآیند استقرار (Deployment Process) اپلیکیشن شما اجرا شود:

Shell

php artisan optimize

متد optimize:clear می‌تواند برای حذف تمام فایل‌های کاش تولید شده توسط دستور optimize و همچنین پاک کردن تمام کلیدها (Keys) در درایور کاش پیش‌فرض (Default cache driver) استفاده شود:

Shell

php artisan optimize:clear

در مستندات زیر، به هر یک از دستورات بهینه‌سازی جزئی (Granular Optimization Commands) که توسط دستور optimize اجرا می‌شوند، خواهیم پرداخت.

کاش کردن پیکربندی (Caching Configuration)

هنگام استقرار اپلیکیشن در محیط پروداکشن، باید مطمئن شوید که دستور config:cache در Artisan را در طول فرآیند استقرار خود اجرا می‌کنید:

Shell

php artisan config:cache

این دستور تمام فایل‌های پیکربندی لاراول را در یک فایل کاش‌شده واحد ترکیب می‌کند، که به شدت تعداد دفعاتی را که فریم‌ورک برای بارگذاری مقادیر پیکربندی باید به سیستم فایل (Filesystem) مراجعه کند، کاهش می‌دهد.

[!WARNING]

اگر دستور config:cache را در طول فرآیند استقرار خود اجرا می‌کنید، باید مطمئن شوید که تابع env را فقط از داخل فایل‌های پیکربندی خود صدا می‌زنید. پس از کاش شدن پیکربندی، فایل .env دیگر بارگذاری نخواهد شد و تمام فراخوانی‌های تابع env برای متغیرهای (Variables) محیطی داخل فایل .env مقدار null را برمی‌گردانند.

کاش کردن رویدادها (Caching Events)

شما باید نگاشت‌های رویداد به شنودکننده یا همان لیسنر (Event to Listener Mappings) را که به صورت خودکار شناسایی شده‌اند (Auto-discovered)، در طول فرآیند استقرار خود کاش کنید. این کار با فراخوانی دستور event:cache در Artisan در حین استقرار انجام می‌شود:

Shell

php artisan event:cache

کاش کردن مسیرها (Caching Routes)

اگر در حال ساخت یک اپلیکیشن بزرگ با مسیرهای (Routes) بسیار زیاد هستید، باید مطمئن شوید که دستور route:cache در Artisan را در طول فرآیند استقرار خود اجرا می‌کنید:

Shell

php artisan route:cache

این دستور تمام ثبت مسیرهای شما را به یک فراخوانی متد (Method Call) واحد در یک فایل کاش‌شده کاهش می‌دهد و عملکرد ثبت مسیر را هنگام ثبت صدها مسیر بهبود می‌بخشد.

کاش کردن ویوها (Caching Views)

هنگام استقرار اپلیکیشن در محیط پروداکشن، باید مطمئن شوید که دستور view:cache در Artisan را در طول فرآیند استقرار خود اجرا می‌کنید:

Shell

php artisan view:cache

این دستور تمام ویوهای بلید (Blade Views) شما را از پیش کامپایل (Precompiles) می‌کند تا در لحظه و بنا به درخواست (On Demand) کامپایل نشوند، که این کار عملکرد هر درخواستی که یک ویو را برمی‌گرداند بهبود می‌بخشد.

راه‌اندازی مجدد سرویس‌ها (Reloading Services)

[!NOTE]

هنگام استقرار روی پلتفرم لاراول کلود (Laravel Cloud)، نیازی به استفاده از دستور reload نیست، زیرا راه‌اندازی مجدد ایمن (Graceful Reload) تمام سرویس‌ها به طور خودکار مدیریت می‌شود.

پس از استقرار نسخه جدیدی از اپلیکیشن، هر سرویس طولانی‌مدتی (Long-running Services) مانند ورکرهای صف (Queue Workers)، سرویس لاراول ریورب (Laravel Reverb) یا لاراول اکتین (Laravel Octane) باید مجدداً بارگذاری یا ری‌استارت شوند تا از کد جدید استفاده کنند. لاراول یک دستور واحد به نام reload در Artisan ارائه می‌دهد که این سرویس‌ها را خاتمه (Terminate) می‌دهد:

Shell

php artisan reload

اگر از لاراول کلود (Laravel Cloud) استفاده نمی‌کنید، باید به صورت دستی یک مانیتور فرآیند (Process Monitor) را پیکربندی کنید که بتواند زمان خروج فرآیندهای قابل بارگذاری مجدد (Reloadable Processes) را تشخیص داده و آن‌ها را به طور خودکار ری‌استارت کند.

حالت دیباگ (Debug Mode)

گزینه دیباگ (Debug Option) در فایل پیکربندی config/app.php شما تعیین می‌کند که چه مقدار اطلاعات درباره یک خطا واقعاً به کاربر نمایش داده شود. به طور پیش‌فرض، این گزینه به گونه‌ای تنظیم شده است که از مقدار متغیر محیطی (Environment Variable) APP_DEBUG (که در فایل .env اپلیکیشن شما ذخیره شده است) تبعیت کند.

[!WARNING]

در محیط پروداکشن (تولید)، این مقدار همیشه باید false باشد. اگر متغیر APP_DEBUG در پروداکشن روی true تنظیم شده باشد، این ریسک وجود دارد که مقادیر حساس پیکربندی در معرض دید کاربران نهایی اپلیکیشن شما قرار گیرد.

مسیر سلامت (The Health Route)

لاراول شامل یک مسیر بررسی سلامت (Health Check Route) داخلی است که می‌تواند برای نظارت روی وضعیت اپلیکیشن شما استفاده شود. در محیط پروداکشن، این مسیر ممکن است برای گزارش وضعیت برنامه به یک مانیتور آپتایم (Uptime Monitor)، لود بالانسر (Load Balancer) یا سیستم ارکستراسیون (Orchestration System) مانند کوبرنتیز (Kubernetes) استفاده شود.

به طور پیش‌فرض، مسیر بررسی سلامت در آدرس /up سرویس‌دهی می‌کند و اگر اپلیکیشن بدون هیچ استثنایی (Exceptions) بوت شده باشد، یک پاسخ HTTP با کد 200 برمی‌گرداند. در غیر این صورت، یک پاسخ HTTP با کد 500 برگشت داده خواهد شد. شما می‌توانید URI این مسیر را در فایل bootstrap/app اپلیکیشن خود پیکربندی کنید:

PHP

->withRouting(
    web: __DIR__.'/../routes/web.php',
    commands: __DIR__.'/../routes/console.php',
    health: '/up', // [tl! remove]
    health: '/status', // [tl! add]
)

هنگامی که درخواست‌های HTTP به این مسیر ارسال می‌شوند، لاراول یک رویداد به نام Illuminate\Foundation\Events\DiagnosingHealth را نیز دیسپچ یا همان ارسال (Dispatch) می‌کند که به شما امکان می‌دهد بررسی‌های سلامت بیشتری را متناسب با اپلیکیشن خود انجام دهید. در داخل یک شنودکننده یا همان لیسنر (Listener) برای این رویداد، می‌توانید وضعیت دیتابیس (Database) یا کاش اپلیکیشن خود را بررسی کنید. اگر مشکلی در برنامه خود تشخیص دادید، کافی است از داخل لیسنر یک Exception (خطا) پرتاب کنید.

استقرار با Laravel Cloud یا Forge (Deploying With Laravel Cloud or Forge)

Laravel Cloud

اگر به دنبال یک پلتفرم استقرار کاملاً مدیریت‌شده با قابلیت اسکیل خودکار (Auto-scaling) هستید که برای لاراول بهینه‌سازی شده باشد، Laravel Cloud را بررسی کنید. پلتفرم Laravel Cloud یک سیستم استقرار قدرتمند برای لاراول است که محاسبات ابر مدیریت‌شده (Managed Compute)، دیتابیس‌ها، کاش‌ها و آبجکت استوریج (Object Storage) را ارائه می‌دهد.

اپلیکیشن لاراول خود را روی Cloud راه‌اندازی کنید و عاشق سادگی و مقیاس‌پذیری آن شوید. Laravel Cloud توسط سازندگان لاراول بهینه‌سازی شده تا به‌طور بی‌نقص با این فریم‌ورک کار کند، بنابراین می‌توانید اپلیکیشن‌های لاراول خود را دقیقاً همان‌طور که عادت دارید بنویسید.

Laravel Forge

اگر ترجیح می‌دهید سرورهای خود را خودتان مدیریت کنید اما با پیکربندی تمام سرویس‌های مختلف مورد نیاز برای اجرای یک اپلیکیشن لاراول قدرتمند راحت نیستید، لاراول فورج (Laravel Forge) یک پلتفرم مدیریت سرور VPS برای اپلیکیشن‌های لاراول است.

پلتفرم Laravel Forge می‌تواند روی ارائه‌دهندگان زیرساخت (Infrastructure Providers) مختلف مانند DigitalOcean، Linode، AWS و موارد دیگر سرور ایجاد کند. علاوه بر این، Forge تمام ابزارهای مورد نیاز برای ساخت اپلیکیشن‌های لاراول قدرتمند مانند Nginx، MySQL، Redis، Memcached، Beanstalk و غیره را نصب و مدیریت می‌کند.