跳到內容

設定網頁伺服器

編輯此頁面

開發 Symfony 應用程式的首選方式是使用 Symfony Local Web Server

然而,在生產環境中執行應用程式時,您需要使用功能完整的網頁伺服器。本文說明如何將 Symfony 與 Apache、Nginx 或 Caddy 搭配使用。

公開目錄是您應用程式所有公開和靜態檔案的所在地,包括圖片、樣式表和 JavaScript 檔案。前端控制器 (index.php) 也位於此處。

公開目錄在設定您的網頁伺服器時作為文件根目錄。在以下範例中,public/ 目錄將作為文件根目錄。此目錄為 /var/www/project/public/

如果您的託管服務提供商要求您將 public/ 目錄變更為其他位置 (例如 public_html/),請務必覆寫 public/ 目錄的位置

設定 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 應用程式的官方文章。

提示

這只會執行公開目錄中的 index.php。所有其他以 ".php" 結尾的檔案都將被拒絕。

如果您的公開目錄中有其他需要執行的 PHP 檔案,請務必將它們包含在上面的 location 區塊中。

警告

在您部署到生產環境後,請確保您無法存取 index.php 腳本 (即 http://example.com/index.php)。

如需進階 Nginx 設定選項,請閱讀官方 Nginx 文件

Apache

如果您執行的是 Apache 2.4+,您可以使用 mod_proxy_fcgi 將傳入的請求傳遞給 PHP-FPM。安裝 Apache2 FastCGI 模組 (libapache2-mod-fastcgi on Debian),在您的 Apache 設定中啟用 mod_proxymod_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 文件

這項作品,包括程式碼範例,均根據 Creative Commons BY-SA 3.0 授權條款授權。
目錄
    版本