Installing Drupal 8 on a local environment

I'm currently using Vagrant as my local development environment so this is what I'll be using to install my first Drupal 8 site. For Drupal 7 development I was using Vagrant Drupal Development but thought I'd switch to using Drupal VM for Drupal 8 development because it includes Drupal Console. There are a few different ways to install Drupal 8, like using Drush - the way I currently do it in Drupal 7. However, it appears the best method to install (and maintain) Drupal 8 is to use Composer, therefore Composer needs to be installed on the local environment.

Creating a new project using Drupal Composer

We are creating a Drupal 8 project using a template from Drupal Composer. This allows us to create a Drupal 8 site where we can update and manage all of its dependencies using Composer, a Dependency Manager for PHP. So create a new site in Vagrant and on the command line go into the site root directory and enter the following.

composer create-project --stability dev --no-interaction drupal-composer/drupal-project:8.x-dev 

There should now be a folder called drupal-project in the your site root with all the Drupal 8 files. You need to move everything inside the drupal-project folder into the site root. If you take look at the files downloaded by composer, the main thing to note is that Drupal core files are now in a sub directory called web. This allows the Vendor files to be outside the web root and is better for security reasons. Therefore, we need to ensure our environment is using the web directory as the document root. Your site root directory should now have the structure below:

directory-structure.png

Installing Drupal

Now we have all the Drupal 8 files we can install Drupal by visiting your site at the browser where it will take you through the installation process, or we can do it via Drush at the command line. Remember to cd into the web directory before running the install command:

drush si minimal --locale=en --db-url=mysql://user_name:password@localhost:3306/db_name --site-name="Site Name" --site-mail=email@address.com --account-name="Admin Username" --account-pass=adminpassword --account-mail=email@address.com

When the install is complete you will get the message Congratulations, you installed Drupal!

Cleaning settings.php

I like to remove all the comments in settings.php saving a lot of scrolling time when I need to make changes. If this site is being installed on a development environment, we need to copy the file at /drupal8/web/sites/default/example.settings.local.php, place it in the directory where the settings.php file is and rename it settings.local.php. The settings.local.php file will contain our settings for the development environment only so this means we need uncomment the code to include the settings.local.php and place it at the bottom of the settings.php file:

if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
  include $app_root . '/' . $site_path . '/settings.local.php';
}

Then we need to cut the database settings out of settings.php and place into the settings.local.php file. So the two settings files should look something like:

settings.php

<?php

$config_directories = array();
$settings['hash_salt'] = 'dmy2Y71S3hluhe9GFeoJ0HBK_D-vhrGE0KPe9eCmncLh-fVPzaU_aXiQ3roC3hpTmos6ZSugvw';
$settings['update_free_access'] = FALSE;
$settings['container_yamls'][] = $app_root . '/' . $site_path . '/services.yml';
$settings['file_scan_ignore_directories'] = [
  'node_modules',
  'bower_components',
];
$settings['install_profile'] = 'minimal';
$config_directories['sync'] = '../config/sync';

if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
  include $app_root . '/' . $site_path . '/settings.local.php';
}

settings.local.php

<?php

$databases = array();
$databases['default']['default'] = array (
  'database' => 'db_name',
  'username' => 'password',
  'password' => 'root',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);
$config['system.site']['name'] = 'My Drupal site - DEVELOPMENT';
$settings['file_public_path'] = 'sites/default/files';
$settings['file_private_path'] = '/var/www/drupal8/private';
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';
$config['system.logging']['error_level'] = 'verbose';
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
# $settings['cache']['bins']['render'] = 'cache.backend.null';
# $settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
$settings['extension_discovery_scan_tests'] = TRUE;
$settings['rebuild_access'] = TRUE;
$settings['skip_permissions_hardening'] = TRUE;

Note the location of $config_directories['sync'] has changed as we want to save site/module configurations as files in this directory. I also override the site title to include the word development so I can easily see this in the browser when I'm toggling between dev and production sites.

Gitignore

Go into .gitignore at the site root and include settings.local.php and any other IDE files/directories that are generated. For example:

  # Ignore the local settings file
  web/sites/default/settings.local.php

  # Ignore Netbean related files/folders
  core
  nbproject
  nbproject/*

You can now start adding themes and modules to your new site.