60%
翻译进度
5
分块数量
1
参与人数

部署

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。


Deployment#

简介#

当你准备将 Laravel 应用程序部署到生产环境时,有一些重要的步骤可以确保你的应用程序运行得尽可能高效。在本文档中,我们将涵盖一些确保 Laravel 应用程序正确部署的良好起点。

服务器要求#

Laravel 框架有一些建议的系统要求。你应确保你的 Web 服务器至少具备以下 PHP 版本和扩展:

  • PHP >= 8.2
  • 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 PHP 扩展

服务器配置#

Nginx#

如果你正在将应用程序部署到运行 Nginx 的服务器上,你可以使用以下配置文件作为配置 Web 服务器的起点。根据你的服务器配置,此文件很可能需要进行定制。如果你需要协助管理服务器,可以考虑使用 Laravel 官方提供的服务器管理和部署服务,如 Laravel Cloud.

bai615 翻译于 1 周前

请确保,像下面的配置一样,你的 Web 服务器将所有请求指向应用程序的 public/index.php 文件。绝不能把 index.php 文件移动到项目根目录,因为从项目根目录提供应用程序将会使许多敏感的配置文件暴露在公共互联网上:

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.2-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 也可以用来服务 Laravel 应用程序。FrankenPHP 是用 Go 编写的现代 PHP 应用服务器。要使用 FrankenPHP 为 Laravel PHP 应用提供服务,您可以简单地调用其 php-server 命令:

frankenphp php-server -r public/

要利用 FrankenPHP 支持的更强大功能,例如其 Laravel Octane 集成、HTTP/3、现代压缩技术或将 Laravel 应用打包为独立的二进制文件,请参阅 FrankenPHP 的 Laravel 文档.

D 目录权限#

Laravel 需要写入 bootstrap/cachestorage 目录,因此你应确保 Web 服务器进程所有者有权限写入这些目录。

bai615 翻译于 1 周前

优化#

在将应用程序部署到生产环境时,有多种文件应该被缓存,包括配置、事件、路由和视图。Laravel 提供了一个方便的单一 optimize Artisan 命令,可以缓存所有这些文件。这个命令通常应该作为应用程序部署过程的一部分被调用:

php artisan optimize

optimize:clear 方法可以用来删除由 optimize 命令生成的所有缓存文件:

php artisan optimize:clear

在接下来的文档中,我们将讨论由 optimize 命令执行的每个细粒度优化命令。

缓存配置#

在将应用程序部署到生产环境时,应确保在部署过程中运行 config:cache Artisan 命令:

php artisan config:cache

这个命令会将 Laravel 的所有配置文件合并成一个缓存文件,这大大减少了框架在加载配置值时对文件系统进行的访问次数。

[! 注意]
如果您在部署过程中执行 config:cache 命令,您应确保只在配置文件中调用 env 函数。一旦配置被缓存,.env 文件将不会被加载,对 env 变量的所有调用 .env 函数将返回 null

缓存事件#

在部署过程中,应该缓存应用程序的自动发现的事件到监听器映射。这可以通过在部署期间调用 event:cache Artisan 命令来完成:

php artisan event:cache
bai615 翻译于 1 周前

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.

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.

本文章首发在 LearnKu.com 网站上。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
贡献者:1
讨论数量: 0
发起讨论 查看所有版本


暂无话题~