Even though it’s by Symfony, the local webserver works with any type of
project - including Drupal 8 (and 9) and Drupal 7.
Getting started
Here are the basic commands to start and stop the server:
# Alias for server:start, starts the server
symfony serve
# Run the server in daemon mode (in the background)
symfony serve -d
# Display the status of the server
symfony server:status
# Stop the server
symfony server:stop
If your Drupal files are within a web or docroot directory, it will
automatically be used as the document root for the server, so files are served
from there if you run the serve command.
If you use a different subdirectory name - one that isn't loaded automatically -
you can use the --document-root option:
symfony serve --document-root www
Different PHP Versions
One of the most useful features of the Symfony server is that it
supports multiple versions of PHP
if you have them installed, and a different version can be selected per
directory.
This is done by adding a .php-version file to the root of the project that
contains the PHP version to use. For example:
echo "7.3" > .php-version
Next time the server is started, this file will be read and the correct version
of PHP will be used.
If you’re using macOS and want to install another version of PHP, you can do it
using Homebrew:
Because port 3306 is exposed, the server recognises it as a database service and
automatically creates environment variables prefixed with DATABASE_.
A list of all the environment variables can be seen by running
symfony var:export (add | tr " " "\n" if you want to view each one on a new
line, and | sort if you want to list them alphabetically):
Now that Drupal can connect to the (empty) database, we can install the site. I
usually do this using Drush, which is added as a dependency via Composer.
The command that I’d usually run is:
cd web
../vendor/bin/drush site-install
However, this will cause an error like this because Drupal cannot connect to the
database when Drush is run in this way.
Error: Class 'Drush\Sql\Sql' not found in Drush\Sql\SqlBase::getInstance()
To fix this, ensure that the command is prefixed with symfony php. This will
ensure that the correct PHP version and configuration is used, and that the
appropriate environment variables are available.
symfony php ../vendor/bin/drush site-install
This also applies to all other Drush commands.
Custom Domain Names
Currently we can only access the site via the localhost URL with a specific
port. The port is determined automatically when the server is started so it can
change if you have multiple projects running.
Symfony server also allows for
adding local domain names through a proxy.
This is useful if you always want to access the site from the same URL, or if
the site relies on using a specific URL such as a multisite setup (multiple
domains served from the same codebase).
The first thing is to add the subdomain to the proxy. In this example, I’m going
to set up a version of the Umami demo installation profile at
https://umami.wip.
# Add umami.wip to the proxy and attach it to this directory
symfony proxy:domain:attach umami
Now we can add it to Drupal’s sites.php file to route requests to the correct
site directory:
// web/sites/sites.php
// This maps https://umami.wip to the sites/umami directory
$sites['umami.wip'] = 'umami';
To create the directory, we can duplicate the default site directory and its
contents.
cp -R web/sites/default web/sites/umami
To create a separate database, we add a new service to the docker-compose.yaml
file and a new MySQL volume to store the data. We can use labels to generate
site specific environment variables.
Now that the Umami site is able to connect to its own database, we can install
Drupal - specifying the installation profile to use and also the site directory
to target.
I'm an Acquia-certified Drupal Triple Expert with 17
years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.