@@ -22,13 +22,12 @@ with Apache or Nginx.
22
22
another location (e.g. ``public_html/ ``) make sure you
23
23
:ref: `override the location of the public/ directory <override-web-dir >`.
24
24
25
- Apache with PHP-FPM
25
+ Configuring PHP-FPM
26
26
-------------------
27
27
28
- To make use of PHP-FPM with Apache, you first have to ensure that you have
29
- the FastCGI process manager ``php-fpm `` binary and Apache's FastCGI module
30
- installed (for example, on a Debian based system you have to install the
31
- ``libapache2-mod-fastcgi `` and ``php7.4-fpm `` packages).
28
+ All configuration examples below use the PHP FastCGI process manager
29
+ (PHP-FPM). Ensure that you have installed PHP-FPM (for example, on a Debian
30
+ based system you have to install the ``php-fpm `` package).
32
31
33
32
PHP-FPM uses so-called *pools * to handle incoming FastCGI requests. You can
34
33
configure an arbitrary number of pools in the FPM configuration. In a pool
@@ -37,6 +36,8 @@ listen on. Each pool can also be run under a different UID and GID:
37
36
38
37
.. code-block :: ini
39
38
39
+ ; /etc/php/7.4/fpm/pool.d/www.conf
40
+
40
41
; a pool called www
41
42
[www]
42
43
user = www-data
@@ -45,43 +46,37 @@ listen on. Each pool can also be run under a different UID and GID:
45
46
; use a unix domain socket
46
47
listen = /var/run/php/php7.4-fpm.sock
47
48
48
- ; or listen on a TCP socket
49
- listen = 127.0.0.1:9000
49
+ ; or listen on a TCP connection
50
+ ; listen = 127.0.0.1:9000
50
51
51
- Using mod_proxy_fcgi with Apache 2.4
52
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52
+ Apache
53
+ ------
53
54
54
- If you are running Apache 2.4, you can use ``mod_proxy_fcgi `` to pass incoming
55
- requests to PHP-FPM. Configure PHP-FPM to listen on a TCP or Unix socket, enable
56
- ``mod_proxy `` and ``mod_proxy_fcgi `` in your Apache configuration, and use the
57
- ``SetHandler `` directive to pass requests for PHP files to PHP FPM:
55
+ If you are running Apache 2.4+, you can use ``mod_proxy_fcgi `` to pass
56
+ incoming requests to PHP-FPM. Install the Apache2 FastCGI mod
57
+ (``libapache2-mod-fastcgi `` on Debian), enable ``mod_proxy `` and
58
+ ``mod_proxy_fcgi `` in your Apache configuration, and use the ``SetHandler ``
59
+ directive to pass requests for PHP files to PHP FPM:
58
60
59
61
.. code-block :: apache
60
62
63
+ # /etc/apache2/conf.d/example.com.conf
61
64
<VirtualHost *:80>
62
- ServerName domain.tld
63
- ServerAlias www.domain.tld
65
+ ServerName example.com
66
+ ServerAlias www.example.com
64
67
65
68
# Uncomment the following line to force Apache to pass the Authorization
66
69
# header to PHP: required for "basic_auth" under PHP-FPM and FastCGI
67
70
#
68
71
# SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
69
72
70
- # For Apache 2.4.9 or higher
71
- # Using SetHandler avoids issues with using ProxyPassMatch in combination
72
- # with mod_rewrite or mod_autoindex
73
73
<FilesMatch \.php$>
74
- SetHandler proxy:fcgi://127.0.0.1:9000
75
- # for Unix sockets, Apache 2.4.10 or higher
76
- # SetHandler proxy:unix:/path/to/fpm.sock|fcgi://dummy
77
- </FilesMatch>
78
-
79
- # If you use Apache version below 2.4.9 you must consider update or use this instead
80
- # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/project/public/$1
74
+ # when using PHP-FPM as a unix socket
75
+ SetHandler proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://dummy
81
76
82
- # If you run your Symfony application on a subpath of your document root, the
83
- # regular expression must be changed accordingly:
84
- # ProxyPassMatch ^/path-to-app/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/project/public/$1
77
+ # when PHP-FPM is configured to use TCP
78
+ # SetHandler proxy:fcgi://127.0.0.1:9000
79
+ </FilesMatch>
85
80
86
81
DocumentRoot /var/www/project/public
87
82
<Directory /var/www/project/public>
@@ -107,8 +102,9 @@ The **minimum configuration** to get your application running under Nginx is:
107
102
108
103
.. code-block :: nginx
109
104
105
+ # /etc/nginx/conf.d/example.com.conf
110
106
server {
111
- server_name domain.tld www.domain.tld ;
107
+ server_name example.com www.example.com ;
112
108
root /var/www/project/public;
113
109
114
110
location / {
@@ -124,7 +120,12 @@ The **minimum configuration** to get your application running under Nginx is:
124
120
# }
125
121
126
122
location ~ ^/index\.php(/|$) {
123
+ # when using PHP-FPM as a unix socket
127
124
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
125
+
126
+ # when PHP-FPM is configured to use TCP
127
+ # fastcgi_pass 127.0.0.1:9000;
128
+
128
129
fastcgi_split_path_info ^(.+\.php)(/.*)$;
129
130
include fastcgi_params;
130
131
@@ -146,7 +147,7 @@ The **minimum configuration** to get your application running under Nginx is:
146
147
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
147
148
fastcgi_param DOCUMENT_ROOT $realpath_root;
148
149
# Prevents URIs that include the front controller. This will 404:
149
- # http://domain.tld /index.php/some-path
150
+ # http://example.com /index.php/some-path
150
151
# Remove the internal directive to allow URIs like this
151
152
internal;
152
153
}
@@ -166,11 +167,6 @@ The **minimum configuration** to get your application running under Nginx is:
166
167
If you use NGINX Unit, check out the official article about
167
168
`How to run Symfony applications using NGINX Unit `_.
168
169
169
- .. note ::
170
-
171
- Depending on your PHP-FPM config, the ``fastcgi_pass `` can also be
172
- ``fastcgi_pass 127.0.0.1:9000 ``.
173
-
174
170
.. tip ::
175
171
176
172
This executes **only ** ``index.php `` in the public directory. All other files
@@ -186,7 +182,46 @@ The **minimum configuration** to get your application running under Nginx is:
186
182
187
183
For advanced Nginx configuration options, read the official `Nginx documentation `_.
188
184
189
- .. _`Apache documentation` : https://httpd.apache.org/docs/
190
- .. _`FastCgiExternalServer` : https://docs.oracle.com/cd/B31017_01/web.1013/q20204/mod_fastcgi.html#FastCgiExternalServer
185
+ Caddy
186
+ -----
187
+
188
+ When using Caddy on the server, you can use a configuration like this:
189
+
190
+ .. code-block :: raw
191
+
192
+ # /etc/caddy/Caddyfile
193
+ example.com, www.example.com {
194
+ root * /var/www/project/public
195
+
196
+ # serve files directly if they can be found (e.g. CSS or JS files in public/)
197
+ encode zstd gzip
198
+ file_server
199
+
200
+
201
+ # otherwise, use PHP-FPM (replace "unix//var/..." with "127.0.0.1:9000" when using TCP)
202
+ php_fastcgi unix//var/run/php/php7.4-fpm.sock {
203
+ # optionally set the value of the environment variables used in the application
204
+ # env APP_ENV "prod"
205
+ # env APP_SECRET "<app-secret-id>"
206
+ # env DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name"
207
+
208
+ # Configure the FastCGI to resolve any symlinks in the root path.
209
+ # This ensures that OpCache is using the destination filenames,
210
+ # instead of the symlinks, to cache opcodes and php files see
211
+ # https://caddy.community/t/root-symlink-folder-updates-and-caddy-reload-not-working/10557
212
+ resolve_root_symlink
213
+ }
214
+
215
+ # return 404 for all other php files not matching the front controller
216
+ # this prevents access to other php files you don't want to be accessible.
217
+ @phpFile {
218
+ path *.php*
219
+ }
220
+ error @phpFile "Not found" 404
221
+ }
222
+
223
+ See the `official Caddy documentation `_ for more examples, such as using
224
+ Caddy in a container infrastructure.
225
+
191
226
.. _`Nginx documentation` : https://www.nginx.com/resources/wiki/start/topics/recipes/symfony/
192
227
.. _`How to run Symfony applications using NGINX Unit` : https://unit.nginx.org/howto/symfony/
0 commit comments