Skip to content

Commit 33ea1fa

Browse files
committed
Clean-up Apache and Nginx parts
1 parent 035af33 commit 33ea1fa

File tree

1 file changed

+72
-37
lines changed

1 file changed

+72
-37
lines changed

setup/web_server_configuration.rst

Lines changed: 72 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ with Apache or Nginx.
2222
another location (e.g. ``public_html/``) make sure you
2323
:ref:`override the location of the public/ directory <override-web-dir>`.
2424

25-
Apache with PHP-FPM
25+
Configuring PHP-FPM
2626
-------------------
2727

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).
3231

3332
PHP-FPM uses so-called *pools* to handle incoming FastCGI requests. You can
3433
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:
3736

3837
.. code-block:: ini
3938
39+
; /etc/php/7.4/fpm/pool.d/www.conf
40+
4041
; a pool called www
4142
[www]
4243
user = www-data
@@ -45,43 +46,37 @@ listen on. Each pool can also be run under a different UID and GID:
4546
; use a unix domain socket
4647
listen = /var/run/php/php7.4-fpm.sock
4748
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
5051
51-
Using mod_proxy_fcgi with Apache 2.4
52-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52+
Apache
53+
------
5354

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:
5860

5961
.. code-block:: apache
6062
63+
# /etc/apache2/conf.d/example.com.conf
6164
<VirtualHost *:80>
62-
ServerName domain.tld
63-
ServerAlias www.domain.tld
65+
ServerName example.com
66+
ServerAlias www.example.com
6467
6568
# Uncomment the following line to force Apache to pass the Authorization
6669
# header to PHP: required for "basic_auth" under PHP-FPM and FastCGI
6770
#
6871
# SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
6972
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
7373
<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
8176
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>
8580
8681
DocumentRoot /var/www/project/public
8782
<Directory /var/www/project/public>
@@ -107,8 +102,9 @@ The **minimum configuration** to get your application running under Nginx is:
107102

108103
.. code-block:: nginx
109104
105+
# /etc/nginx/conf.d/example.com.conf
110106
server {
111-
server_name domain.tld www.domain.tld;
107+
server_name example.com www.example.com;
112108
root /var/www/project/public;
113109
114110
location / {
@@ -124,7 +120,12 @@ The **minimum configuration** to get your application running under Nginx is:
124120
# }
125121
126122
location ~ ^/index\.php(/|$) {
123+
# when using PHP-FPM as a unix socket
127124
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+
128129
fastcgi_split_path_info ^(.+\.php)(/.*)$;
129130
include fastcgi_params;
130131
@@ -146,7 +147,7 @@ The **minimum configuration** to get your application running under Nginx is:
146147
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
147148
fastcgi_param DOCUMENT_ROOT $realpath_root;
148149
# 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
150151
# Remove the internal directive to allow URIs like this
151152
internal;
152153
}
@@ -166,11 +167,6 @@ The **minimum configuration** to get your application running under Nginx is:
166167
If you use NGINX Unit, check out the official article about
167168
`How to run Symfony applications using NGINX Unit`_.
168169

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-
174170
.. tip::
175171

176172
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:
186182

187183
For advanced Nginx configuration options, read the official `Nginx documentation`_.
188184

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+
191226
.. _`Nginx documentation`: https://www.nginx.com/resources/wiki/start/topics/recipes/symfony/
192227
.. _`How to run Symfony applications using NGINX Unit`: https://unit.nginx.org/howto/symfony/

0 commit comments

Comments
 (0)