laravel_getting started_deployment_部署

本文指导如何在生产环境中高效部署Laravel应用,涉及服务器需求、Nginx配置、缓存策略、调试模式以及使用LaravelForge和Vapor进行轻松部署。
摘要由CSDN通过智能技术生成

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.

当您准备将 Laravel 应用程序部署到生产环境时,有一些重要的工作可以确保您的应用程序尽可能高效地运行。在本文档中,我们将介绍一些确保您的 Laravel 应用程序正确部署的重要起点。

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.2
  • 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

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扩展

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 first-party Laravel server management and deployment service such as Laravel Forge.

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:

如果您将您的应用程序部署到运行 Nginx 的服务器上,您可以使用以下配置文件作为配置您的 Web 服务器的起点。很可能,根据您服务器的配置,该文件需要进行自定义修改。如果您需要协助管理服务器,请考虑使用第一方 Laravel 服务器管理和部署服务,例如 Laravel Forge

请确保像下面的配置一样,您的 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 ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

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:

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

php artisan optimize

The optimize:clear method may be used to remove all of the cache files generated by the optimize command:

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

php artisan optimize:clear

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

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

Caching Configuration

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

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

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.

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.

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

如果在部署过程中执行config:cache命令,请确保您在配置文件中只使用env函数。一旦配置被缓存,.env文件将不会被加载,所有对.env变量的env函数调用将返回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:

您应该在部署过程中缓存应用程序自动发现的事件与监听器的映射关系。这可以通过在部署过程中调用event:cache Artisan命令来实现:

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:

如果您正在构建一个具有许多路由的大型应用程序,您应确保在部署过程中运行 route:cache Artisan 命令:

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:

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

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.

这个命令预编译所有的Blade视图,因此它们不再按需编译,提高了每个返回视图的请求的性能。

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.

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.

在您的config/app.php配置文件中的调试选项确定实际向用户显示有关错误信息的程度。默认情况下,此选项设置为遵循存储在应用程序的.env文件中的APP_DEBUG环境变量的值。

在生产环境中,这个值应该始终设置为 false。如果APP_DEBUG变量在生产环境中设置为 true,则有泄露敏感配置值给应用程序最终用户的风险。

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:

Laravel 包含一个内置的健康检查路由,用于监视应用程序的状态。在生产环境中,可以使用此路由将应用程序的状态报告给运行时间监视器、负载均衡器或诸如 Kubernetes 等编排系统。

默认情况下,健康检查路由位于 /up,如果应用程序在启动时没有异常,将返回一个 200 HTTP 响应。否则,将返回一个 500 HTTP 响应。您可以在应用程序的 bootstrap/app 文件中配置此路由的URI:

->withRouting(
    web: __DIR__.'/../routes/web.php',
    commands: __DIR__.'/../routes/console.php',
    health: '/up', 
    health: '/status', 
)

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.

当向此路由发出 HTTP 请求时,Laravel 还将会触发一个 Illuminate\Foundation\Events\DiagnosingHealth 事件,使您能够执行与您的应用程序相关的额外健康检查。在这个事件的 监听器 中,您可以检查应用程序的数据库或缓存状态。如果检测到应用程序存在问题,您可以简单地从监听器中抛出异常。

Easy Deployment With Forge / Vapor

Laravel Forge

If you aren’t quite ready to manage your own server configuration or aren’t comfortable configuring all of the various services needed to run a robust Laravel application, Laravel Forge is a wonderful alternative.

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.

Want a full guide to deploying with Laravel Forge? Check out the Laravel Bootcamp and the Forge video series available on Laracasts.

如果您还不准备好管理自己的服务器配置,或者对配置运行强大的 Laravel 应用程序所需的各种服务不够了解,Laravel Forge 是一个不错的选择。

Laravel Forge 可以在各种基础设施提供商上创建服务器,如 DigitalOcean、Linode、AWS 等。此外,Forge 还安装和管理构建强大 Laravel 应用程序所需的所有工具,如 Nginx、MySQL、Redis、Memcached、Beanstalk 等。

想要了解使用 Laravel Forge 进行部署的完整指南吗?查看 Laravel Bootcamp 和 Laracasts 上提供的 Forge 视频系列

Laravel Vapor

If you would like a totally serverless, auto-scaling deployment platform tuned for Laravel, check out Laravel Vapor. Laravel Vapor is a serverless deployment platform for Laravel, powered by AWS. Launch your Laravel infrastructure on Vapor and fall in love with the scalable simplicity of serverless. Laravel Vapor 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 调优的部署平台,请查看 Laravel Vapor。Laravel Vapor 是 Laravel 的无服务器部署平台,由 AWS 提供支持。在 Vapor 上启动您的 Laravel 基础架构,并坠入无服务器的可扩展简单之爱。Laravel Vapor 被 Laravel 的创作者们调优,与框架无缝协作,让您可以继续按照自己习惯的方式编写 Laravel 应用程序。

您好!要在Kubernetes上部署snmp_exporter,您可以按照以下步骤进行操作: 1. 首先,创建一个部署文件(deployment.yaml),内容如下: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: snmp-exporter spec: replicas: 1 selector: matchLabels: app: snmp-exporter template: metadata: labels: app: snmp-exporter spec: containers: - name: snmp-exporter image: prom/snmp-exporter ports: - containerPort: 9116 args: - "--config.file=/etc/snmp_exporter/snmp.yml" volumeMounts: - name: config-volume mountPath: /etc/snmp_exporter volumes: - name: config-volume configMap: name: snmp-config ``` 2. 创建一个配置文件(configmap.yaml),内容如下: ```yaml apiVersion: v1 kind: ConfigMap metadata: name: snmp-config data: snmp.yml: | version: 2c communities: - community_string: public target_oids: - .1.3.6.1.2.1.2.2.1.10 ``` 此配置文件示例设置了SNMP版本为2c,使用公共社区字符串(public)并监视了接口的入站字节数。您可以根据需要进行修改。 3. 使用kubectl命令进行部署: ```shell kubectl apply -f deployment.yaml kubectl apply -f configmap.yaml ``` 这将创建一个名为"snmp-exporter"的部署,并创建一个名为"snmp-config"的配置映射。 4. 部署完成后,您可以通过访问snmp-exporter服务的IP地址和端口(默认为9116)来访问snmp_exporter的指标数据。 ```shell kubectl get services ``` 您应该能够看到"snmp-exporter"服务的外部IP地址。使用该IP地址和端口号,您可以使用Prometheus或其他监控工具来获取和可视化snmp_exporter的指标数据。 希望这对您有所帮助!如有任何问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值