criscom
9/22/2016 - 6:42 PM

drupal8 summary of the webcast on September 7, 2016

drupal8 summary of the webcast on September 7, 2016

Building Drupal 8 Sites with Composer and Configuration Management [September 7, 2016]

YouTube link

Composer: tool for dependency management in PHP.
Configuration Management: system to store configuration data in a consistent manner

Requirements:
Composer
Git
Drush

Skip 0 and 1 if you want to install Drupal 8 with the lightning distribution and proceed with 2

0. Instaling Drupal 8 with the drupal-composer/drupal-project

https://github.com/drupal-composer/drupal-project

0.1 Install Drupal using the drupal-composer/drupal-project project, but pass the --no-install flag so that we can update the package repo first.
0.2 Set the package repo to "https://packages.drupal.org/8"
0.3 Clear Composer cache
0.4 Run "composer update" to install the project with packages from drupal.org

github.com/drupal-composer/drupal-project/issues/175#issuecomment-232172960

  composer create-project --stability dev --no-interaction --no-install drupal-composer/drupal-project:8.x-dev drupal8site
  cd drupal8site
  composer config repositories.0 composer https://packages.drupal.org/8

1. Build the Drupal site with Composer

  composer clear-cache
  composer update (installs all the packages we need and adds vendor, web directories and composer.lock)

[1.1 Create the drush aliases file]

vi ~/.drush/d8.aliases.drushrc.php
  <?php

    // Local site.
    $aliases["loc"] = array (
      'uri' => 'loc.drupal8site.com',
      'root' => '/path-to-drupal-directory/drupal8site/web',

    );

invoking this alias you would type: drush use @d8.loc

2. Install Drupal 8 with the aquia/lightning distribution

  $ composer create-project acquia/lightning-project MY_PROJECT

2.1 Create the database
2.2 Add the site to our /etc/hosts file
2.3 Create the VirtualHost entry (webinar)

  <VirtualHost *:80>
    DocumentRoot "/path-to-drupal-directory/drupal8site/web" (when there is a web/ directory then we have to adjust the DocumentRoot accordingly)
    ServerName loc.drupal8site.com
    ServerAlias loc.drupal8site.com.*.xip.io
  </VirtualHost>

2.4 Create VirtualHost entry (bane)

   cd /etc/apache2/sites-available
   sudo cp default.conf my_project.conf
   sudo vi my_project.conf
   sudo a2ensite my_project
   sudo service apache2 reload
   
  <VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName MY_PROJECT.sunlime.it

    ServerAdmin MY_PROJECT@sunlime.at
    DocumentRoot /var/www/MY_PROJECT/docroot

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error_MY_PROJECT.log
    CustomLog ${APACHE_LOG_DIR}/access_MY_PROJECT.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf 

3. Install Drupal

3.1 Install Drupal with Drupal Console (you could also use drush to install Drupal): Standard profile

Change "standard" to "lightning if you want to install the lightning distribution.

  cd docroot/
  drupal site:install standard --site-name "Drupal 8 Site" --langcode en 
  --db-type mysql --db-port 3306 --db-user root --db-pass root -db-host localhost
  --db-name my_project_database --site-mail my_project@sunlime.at --account-name sunlime --account-mail drupal8@sunlime.at --account-pass admin

3.2 Install Drupal with Drupal Console (you could also use drush to install Drupal): Lightning distribution

  cd docroot/
  drupal site:install lightning --site-name "MY_PROJECT" --langcode en 
  --db-type mysql --db-port 3306 --db-user root --db-pass root -db-host localhost
  --db-name my_project_database --site-mail my_project@sunlime.at --account-name sunlime --account-mail drupal8@sunlime.at --account-pass admin

3.3 Install Drupal with Drush:

 cd docroot/
 drush site-install lightning --locale=en --account-name=sunlime --account-pass=sunlime --account-mail=drupal8@sunlime.at --db-url='mysql://lightning:db_password@localhost/db_name' --site-name='MY_PROJECT' --site-mail=MY_PROJECT@sunlime.at

[3.4 Use drush to login]

  drupal8site/docroot cd .. /* (move into project directory) */
  drupal8site drush use @d8.loc
  Site set to @d8.loc
  drush uli /* (log into the site) */

drush use @d8.loc => in this terminal session everytime you type drush, do it for this installation

4. Clean up settings files

4.1 Remove cruft from settings.php => docroot/sites/default/settings.php

clean off comments and unused code;

put below code to the end of your settings.php:

  /**
   * Load local development override configuration, if available.
   *
   * Use settings.local.php to override variables on secondary (staging,
   * development, etc) installations of this site. Typically used to disable
   * caching, JavaScript/CSS compression, re-routing of outgoing emails, and
   * other things that should not happen on development and testing sites.
   *
   * Keep this code block at the end of this file to take full effect.
   */
  if (file_exists(__DIR__ . '/settings.local.php')) {
     include __DIR__ . '/settings.local.php';
   }

Move settings.local.php code at the end of settings.php

If you are using MAMP the 'unix_socket' command will enable CLI tools like drush.

4.2 Create settings.local.php

  cd docroot/sites
  ls -al
  chmod 777 default/
  touch default/settings.local.php
  chmod 555 default/
  ls -al

4.3 Move database credentials to settings.local.php

Then copy / paste the Database settings from settings.php to settings.local.php

5. Commit the repo to Git

5.0 Set up sunlime bitbucket git repro

5.1 Initialize the repo of your project

Do it from the root folder (MY_PROJECT/)

5.2 Make sure settings.local.php is ignored

add settings.local.php to .gitignore; settings.php should go into the repo.

5.3 Commit everything
5.4 Push to a remote repo

6 Recap

Basically, one-time setup stuff

6.1 Downloaded Drupal 8 using Composer
6.2 Installed Drupal using Drupal Console
6.3 Setup our settings.php and settings.local.php files
6.4 Committed everything to git and pushed to a remote repo (13:09 min)

7 Using Composer to manage packages

7.1 Install and uninstall packages
7.2 Force Composer to install the latest dev
7.3 Updating and downgrading projects wiht composer
7.4 Skipping versions and specifying ranges
7.5 Enabling modules with Drush
7.6 How to decide which version pattern to use

7.1 Installing packages
(The official documentation - https://www.drupal.org/node/2718229)

composer require drupal/pathauto
==> don't do that

instead do the following
composer require "drupal/pathauto:^1.0"

the caret means "at least version 1.0"

7.1 Uninstalling packages

composer remove drupal/pathauto

7.2 Force Composer to install the lates dev

composer require "drupal/admin_toolbar:1.x-dev"

=> it clones from the repo and stores the exact commit hash; if you install the project on a different machine it makes sure that it downloads the exact commit (and not the latest one)

7.3 Upgrading and downgrading projects with composer

just specify the exakt version like below

composer require "drupal/admin_toolbar:1.14"  

7.4 Skipping versions and specifying ranges

composer require "drupal/admin_toolbar:^1.14 != 1.16"

install at least version 1.14 but not 1.16

don't do the following

composer require "drupal/admin_toolbar:^1.14 != 1.15 != 1.16"

specify the exact version, instead, or use <= like so

composer require "drupal/admin_toolbar:<= 1.14"

specifying ranges

composer require "drupal/admin_toolbar: <= 1.14 || ^1.16"

install version 1.14 or lower or at least version 1.16

7.5 Enabling modules with drush

Composer is great for downloading modules and dependency management but doesn't know what Drupal is. Therefore we use Drush for enabling modules

drush en -y admin_toolbar admin_toolbar_tools

7.6 How to decide which version pattern to use

What version do you want ?Version pattern to type
Latest stable that is greater than or equal to 1.0^1.0
Always Dev (ignore stables)1.x-dev
Exact version1.14

The caret and the tilde

The caret "^" and the tilde "~" are almost the same but there are differences.

"~1.0" and "^1.0" will produce the exact same result every time.

"~1.0.0" and "^1.0.0" however will not.

The difference is in the upper limit allowed by each operator.

"~" Tilde only allows the last specified number to change. The Tilde "~" only increments the last specified number.

"^" Caret follows semantic versioning, so verything after the "major" version number can change.

Examples

"~1.0" could go up to "1.99"
"^1.0" could go up to "1.99"

"~1.0.0" could go up to "1.0.99"
"^1.0.0" could go up to "1.99.99"

8 Configuration management

8.1 Set the config directory in settings.php
8.2 Export configuration locally
8.3 Manage configuration with Git
8.4 Prep the site to be installed on prodcution with the Configuration installer install profile

8.1 Set the config directory in settings.php

in settings.php

change: $config_directories['sync'] = 'sites/default/files/config_pMqQRLjVzRbeB2KK1FWbJQ7desVjI1xXOZd9ws20PC9Lt86uKKI1EmAkkEDg7C_ICtCh0UZAXA/sync';
to: $config_directories['sync'] = '../config/sync';

remove old sync file:
rm -rf sites/default/files/config_pMqQRLjVzRbeB2KK1FWbJQ7desVjI1xXOZd9ws20PC9Lt86uKKI1EmAkkEDg7C_ICtCh0UZAXA/sync (25:11)

8.2 Export configuration locally

drush cex
Configuration successfully exported to ../config/sync. 

8.4 Prep the site to be installed on prodcution with the Configuration installer install profile

config_installer is intented to be used on a single site to push configuration changes from dev to live

Features can be used on multiple different sites.

  1. Export Database from local to live => and you can push configuration between the two
  2. User the config_installer

drush dl config_installer
drush en config_installer

9 Installing the site on a production server

(28:11) 9.1 Clone the repository to production
9.2 Run "composer install --no-dev"
9.3 Configure database
9.4 Install the site using the Configuration Installer install profile