部署
簡介
準備好將 Laravel 專案部署到正式環境時,還有一些影響網站效能的重點要注意。在本文中,我們會討論一些正確部署 Laravel 專案的要點。
伺服器需求
Laravel Framework 有一些系統需求。請確保網頁伺服器有達到下列最小 PHP 版本需求與擴充套件需求:
- PHP >= 8.2
- Ctype PHP 擴充套件
- cURL PHP 擴充套件
- DOM PHP Extension
- Fileinfo PHP 擴充套件
- Filter PHP 擴充套件
- Hash PHP 擴充套件
- Mbstring PHP 擴充套件
- OpenSSL PHP 擴充套件
- PCRE PHP Extension
- PDO PHP 擴充套件
- Session PHP 擴充套件
- Tokenizer PHP 擴充套件
- XML PHP 擴充套件
伺服器設定
Nginx
若將專案部署到執行 Nginx 的伺服器上,則應使用下列設定檔來開始設定網頁伺服器。當然,還需要根據伺服器來調整該檔案中的設定。若需要協助管理伺服器,請參考使用 Laravel 官方的伺服器管理與部署服務,如 Laravel Forge。
像下列設定檔一樣,請確保網頁伺服器有將所有連入網站的請求重新導向到 public/index.php
檔案上。請絕對不要嘗試將 index.php
檔案移到專案根目錄上,因為以專案根目錄來開放網站可能導致一些機敏設定檔被暴露到公開的網際網路上:
1server {2 listen 80;3 listen [::]:80;4 server_name example.com;5 root /srv/example.com/public;67 add_header X-Frame-Options "SAMEORIGIN";8 add_header X-Content-Type-Options "nosniff";910 index index.php;1112 charset utf-8;1314 location / {15 try_files $uri $uri/ /index.php?$query_string;16 }1718 location = /favicon.ico { access_log off; log_not_found off; }19 location = /robots.txt { access_log off; log_not_found off; }2021 error_page 404 /index.php;2223 location ~ \.php$ {24 fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;25 fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;26 include fastcgi_params;27 }2829 location ~ /\.(?!well-known).* {30 deny all;31 }32}
1server {2 listen 80;3 listen [::]:80;4 server_name example.com;5 root /srv/example.com/public;67 add_header X-Frame-Options "SAMEORIGIN";8 add_header X-Content-Type-Options "nosniff";910 index index.php;1112 charset utf-8;1314 location / {15 try_files $uri $uri/ /index.php?$query_string;16 }1718 location = /favicon.ico { access_log off; log_not_found off; }19 location = /robots.txt { access_log off; log_not_found off; }2021 error_page 404 /index.php;2223 location ~ \.php$ {24 fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;25 fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;26 include fastcgi_params;27 }2829 location ~ /\.(?!well-known).* {30 deny all;31 }32}
最佳化
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:
1php artisan optimize
1php artisan optimize
The optimize:clear
method may be used to remove all of the cache files generated by the optimize
command:
1php artisan optimize:clear
1php artisan optimize:clear
In the following documentation, we will discuss each of the granular optimization commands that are executed by the optimize
command.
快取設定檔
在將專案部署到正式環境時,請確保部署流程中有執行 config:cache
Artisan 指令:
1php artisan config:cache
1php artisan config:cache
該指令會將所有的 Laravel 設定檔合併為單一、經過快取的檔案。使用快取檔通常可以減少一些框架在載入設定值時讀取檔案系統的次數。
若在部署流程中執行了 config:cache
指令,應確保只有在設定檔中呼叫 env
函式。設定檔被快取後,就不會再載入 .env
檔了。所有 env
函式查詢 .env
變數的呼叫都會回傳 null
。
快取 Event
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:
1php artisan event:cache
1php artisan event:cache
快取 Route
若正在建立有許多路由的大型專案,請確保在部署過程中有執行 route:cache
Artisan 指令:
1php artisan route:cache
1php artisan route:cache
該指令可將所有的路由註冊減少為快取檔案內的單一方法呼叫,在註冊上百個路由時,可藉此提升路由註冊的效能。
快取 View
在將專案部署到正式環境時,請確保有在部署流程內執行 view:cache
Artisan 指令:
1php artisan view:cache
1php artisan view:cache
該指令會預先編譯所有的 Blade View,這樣一來這些 View 就不會只在有需要的時候才進行編譯,可藉此提升每個有回傳 View 的請求效能。
偵錯模式
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.
在正式環境上,這個值一定要是 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:
1->withRouting(2 web: __DIR__.'/../routes/web.php',3 commands: __DIR__.'/../routes/console.php',4 health: '/up',5 health: '/status',6)
1->withRouting(2 web: __DIR__.'/../routes/web.php',3 commands: __DIR__.'/../routes/console.php',4 health: '/up',5 health: '/status',6)
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.
使用 Forge 或 Vapor 來輕鬆部署
Laravel Forge
若你還未準備好自行管理伺服器設定,或不擅長設定各種執行大型 Laravel 專案所需要的設定,則 Laravel Forge 是一個不錯的選擇。
Laravel Forge 可以在如 DigitalOcean, Linode, AWS… 等各種基礎建設提供商上建立伺服器。此外,Forge 還可以負責安裝並管理各種執行大型 Laravel 專案所需的工具,如 Nginx, MySQL, Redis, Memcached, Beanstalk… 等。
需要一篇使用 Laravel Forge 來部署網站的完整教學嗎?請參考 Laravel Bootcamp (英語) 以及 Laracasts 上的 Forge 影片系列 (英語)。
Laravel Vapor
若想試試完全為 Laravel 最佳化、Auto-Scaling 的 Serverless 部署平台,請參考看看 Laravel Vapor。Laravel Vapor 是一個為 Laravel 設計的 Serverless 部署平台,由 AWS 驅動。使用 Vapor 來發佈你的 Laravel 基礎建設,你會愛上 Serverless 這種能簡單擴充的架構。Laravel Vapor 已由 Laravel 的作者們精心最佳化,以讓 Vapor 能完美配合 Laravel 使用,並讓你能像往常在 Laravel 專案開發就好。