配置 Web 服务器
开发 Symfony 应用程序的首选方式是使用 Symfony 本地 Web 服务器。
但是,在生产环境中运行应用程序时,您需要使用功能完善的 Web 服务器。本文介绍了如何将 Symfony 与 Apache、Nginx 或 Caddy 一起使用。
配置 PHP-FPM
以下所有配置示例均使用 PHP FastCGI 进程管理器 (PHP-FPM)。确保您已安装 PHP-FPM(例如,在基于 Debian 的系统上,您必须安装 php-fpm
包)。
PHP-FPM 使用所谓的池来处理传入的 FastCGI 请求。您可以在 FPM 配置中配置任意数量的池。在池中,您可以配置 TCP 套接字(IP 和端口)或 Unix 域套接字来监听。每个池也可以在不同的 UID 和 GID 下运行
1 2 3 4 5 6 7 8 9 10 11 12
; /etc/php/8.3/fpm/pool.d/www.conf
; a pool called www
[www]
user = www-data
group = www-data
; use a unix domain socket
listen = /var/run/php/php8.3-fpm.sock
; or listen on a TCP connection
; listen = 127.0.0.1:9000
Nginx
使您的应用程序在 Nginx 下运行的最低配置是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
# /etc/nginx/conf.d/example.com.conf
server {
server_name example.com www.example.com;
root /var/www/project/public;
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
# optionally disable falling back to PHP script for the asset directories;
# nginx will return a 404 error when files are not found instead of passing the
# request to Symfony (improves performance but Symfony's 404 page is not displayed)
# location /bundles {
# try_files $uri =404;
# }
location ~ ^/index\.php(/|$) {
# when using PHP-FPM as a unix socket
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
# when PHP-FPM is configured to use TCP
# fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# optionally set the value of the environment variables used in the application
# fastcgi_param APP_ENV prod;
# fastcgi_param APP_SECRET <app-secret-id>;
# fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name";
# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
# FPM.
# Otherwise, PHP's OPcache may not properly detect changes to
# your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
# for more information).
# Caveat: When PHP-FPM is hosted on a different machine from nginx
# $realpath_root may not resolve as you expect! In this case try using
# $document_root instead.
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
# Prevents URIs that include the front controller. This will 404:
# http://example.com/index.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
提示
如果您使用 NGINX Unit,请查看有关如何使用 NGINX Unit 运行 Symfony 应用程序的官方文章。
提示
这仅执行 public 目录中的 index.php
。所有其他以“.php”结尾的文件都将被拒绝。
如果您的 public 目录中有其他需要执行的 PHP 文件,请务必将它们包含在上面的 location
块中。
警告
部署到生产环境后,请确保您无法访问 index.php
脚本(即 http://example.com/index.php
)。
有关高级 Nginx 配置选项,请阅读官方 Nginx 文档。
Apache
如果您运行的是 Apache 2.4+,则可以使用 mod_proxy_fcgi
将传入的请求传递给 PHP-FPM。安装 Apache2 FastCGI 模块(Debian 上为 libapache2-mod-fastcgi
),在您的 Apache 配置中启用 mod_proxy
和 mod_proxy_fcgi
,并使用 SetHandler
指令将 PHP 文件的请求传递给 PHP FPM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
# /etc/apache2/conf.d/example.com.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
# Uncomment the following line to force Apache to pass the Authorization
# header to PHP: required for "basic_auth" under PHP-FPM and FastCGI
#
# SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
<FilesMatch \.php$>
# when using PHP-FPM as a unix socket
SetHandler proxy:unix:/var/run/php/php8.3-fpm.sock|fcgi://dummy
# when PHP-FPM is configured to use TCP
# SetHandler proxy:fcgi://127.0.0.1:9000
</FilesMatch>
DocumentRoot /var/www/project/public
<Directory /var/www/project/public>
AllowOverride None
Require all granted
FallbackResource /index.php
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/project>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
注意
如果您正在使用 Apache 进行一些快速测试,您也可以运行 composer require symfony/apache-pack
。此软件包在 public/
目录中创建一个 .htaccess
文件,其中包含为 Symfony 应用程序提供服务所需的必要重写规则。但是,在生产环境中,建议将这些规则移动到主 Apache 配置文件中(如上所示),以提高性能。
Caddy
在服务器上使用 Caddy 时,您可以使用如下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# /etc/caddy/Caddyfile
example.com, www.example.com {
root * /var/www/project/public
# serve files directly if they can be found (e.g. CSS or JS files in public/)
encode zstd gzip
file_server
# otherwise, use PHP-FPM (replace "unix//var/..." with "127.0.0.1:9000" when using TCP)
php_fastcgi unix//var/run/php/php8.3-fpm.sock {
# only fall back to root index.php aka front controller.
try_files {path} index.php
# optionally set the value of the environment variables used in the application
# env APP_ENV "prod"
# env APP_SECRET "<app-secret-id>"
# env DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name"
# Configure the FastCGI to resolve any symlinks in the root path.
# This ensures that OpCache is using the destination filenames,
# instead of the symlinks, to cache opcodes and php files see
# https://caddy.community/t/root-symlink-folder-updates-and-caddy-reload-not-working/10557
resolve_root_symlink
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
@phpFile {
path *.php*
}
error @phpFile "Not found" 404
}
有关更多示例,例如在容器基础设施中使用 Caddy,请参阅官方 Caddy 文档。