Skip to content

Symfony 5.2 and Apache 2.4

Certainly the most convenient way to develop a Symfony 5.x app is to use the symfony server. Eventually however, you may have to deploy the application. For a number of years nginx with php-fpm was the preference for many sysadmins seeking to wring the maximum performance out of a webserver that also has to run php scripts, but there are now simple ways of configuring apache to use php-fpm while achieving comparable performance to nginx.

For example a vhost setting like this one is possible:

<VirtualHost *:80>
    SetEnv ENVIRONMENT "dev"
    <FilesMatch \.php$>
        SetHandler proxy:fcgi://php:9000
        # for Unix sockets, Apache 2.4.10 or higher
    # Proxy .php requests to port 9000 of the php-fpm container
    DocumentRoot /usr/local/apache2/cms/public
    ServerName cms.mydev.local
    ServerAdmin admin@mydev.local

    <Directory /usr/local/apache2/cms/public>
        DirectoryIndex index.php index.html
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    # Send apache logs to stdout and stderr
    CustomLog /proc/self/fd/1 common
    ErrorLog /proc/self/fd/2

This particular configuration is for a Docker container setup, where php-fpm is being provided from the official php container, and since the container is named "php" it can be referenced in the "SetHandler proxy:fcgi://php:9000". Hopefully it should be clear that Apache will send requests for .php files to php-fpm at port 9000.

Should you try and run your new symfony app, under apache, you'll immediately notice that it doesn't work properly, and routing and the debug bar amongst other things, will be broken.

The reason for this, is that many virtual directories and routes to virtual files won't be found and will return as 404 errors.

Clearly some rewrite magic is needed here, but what exactly?

Rather than try and figure this out yourself, Symfony helpfully provides a recipe that installs an .htaccess file to make your Symfony 5.2+ application work. Just use composer to add it to your project:

composer require symfony/apache-pack

This will add an .htaccess file to your project public directory with the needed rewrites to get your Symfony application routing successfully.

Of course there is an overhead for directory level rewriting, and the Symfony project folks helpfully suggest that for production you move the .htaccess rewrite rules directly into your vhost configuration.

Defined tags for this entry: , , , , , ,


No Trackbacks


Display comments as Linear | Threaded

No comments

Add Comment

Pavatar, Gravatar, Favatar, MyBlogLog, Pavatar author images supported.
BBCode format allowed
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
Form options