Skip to content

Instantly share code, notes, and snippets.

@ghalusa
Last active March 5, 2020 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghalusa/8efc5135857dca305ef7e29205a6a66c to your computer and use it in GitHub Desktop.
Save ghalusa/8efc5135857dca305ef7e29205a6a66c to your computer and use it in GitHub Desktop.
Installing Drupal 8 Using Composer

Installing Drupal 8 Using Composer

See: https://www.drupal.org/docs/develop/using-composer/using-composer-to-install-drupal-and-manage-dependencies#download-core

Step 1

Substitute TARGET_DIRECTORY with the actual directory

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

If composer runs out of memory, increase PHP's memory_limit. This could be set to a value such as 2G, or unlimited -1.

$ which composer
/usr/bin/composer
$ php -d memory_limit=-1 /usr/bin/composer create-project drupal-composer/drupal-project:8.x-dev TARGET_DIRECTORY --stability dev --no-interaction

Step 2

Make sure Apache can write...

$ sudo chown -R YOUR_USERNAME:www-data TARGET_DIRECTORY

Step 3

Set up a MySQL/MariaDB database for the site.

Step 4

Go to the site with a browser, and complete setting-up the site.

Step 5

Trusted host configuration

Update web/sites/default/settings.php

$settings['trusted_host_patterns'] = [
  '^example\.com$',
];

Step 6

Add Installer Paths

Add to composer.json under installer-paths:

"htdocs/modules/custom/{$name}": ["type:drupal-custom-module"],
"htdocs/themes/custom/{$name}": ["type:drupal-custom-theme"]

Step 7

Set the correct permissions for the default directory and settings.php

$ chmod 555 TARGET_DIRECTORY/web/sites/default
$ chmod 444 TARGET_DIRECTORY/web/sites/default/settings.php

Optional

Rename public directory from "web" to "htdocs"

Find all instances of web/ and replace with htdocs/ in the composer.json file.

See: https://scriptun.com/drupal-8/public-directory-from-web-to-public-html

Before

"extra": {
    "composer-exit-on-patch-failure": true,
    "patchLevel": {
        "drupal/core": "-p2"
    },
    "drupal-scaffold": {
        "locations": {
            "web-root": "web/"
        }
    },
    "installer-paths": {
        "web/core": ["type:drupal-core"],
        "web/libraries/{$name}": ["type:drupal-library"],
        "web/modules/contrib/{$name}": ["type:drupal-module"],
        "web/profiles/contrib/{$name}": ["type:drupal-profile"],
        "web/themes/contrib/{$name}": ["type:drupal-theme"],
        "drush/Commands/contrib/{$name}": ["type:drupal-drush"]
    }
}

After

"extra": {
    "composer-exit-on-patch-failure": true,
    "patchLevel": {
        "drupal/core": "-p2"
    },
    "drupal-scaffold": {
        "locations": {
            "web-root": "htdocs/"
        }
    },
    "installer-paths": {
        "htdocs/core": ["type:drupal-core"],
        "htdocs/libraries/{$name}": ["type:drupal-library"],
        "htdocs/modules/contrib/{$name}": ["type:drupal-module"],
        "htdocs/profiles/contrib/{$name}": ["type:drupal-profile"],
        "htdocs/themes/contrib/{$name}": ["type:drupal-theme"],
        "drush/Commands/contrib/{$name}": ["type:drupal-drush"]
    }
}

Then, regenerate autoload declarations by running the following command:

composer dump-autoload

Create the config/sync directories

cd TARGET_DIRECTORY
mkdir -p config/sync

Update web/sites/default/settings.php

$settings['config_sync_directory'] = '/var/www/vhosts/TARGET_DIRECTORY/config/sync';

Add "custom" directories to modules and themes

Add these 2 lines to the composer.json file under installer-paths.

"htdocs/modules/custom/{$name}": ["type:drupal-custom-module"],
"htdocs/themes/custom/{$name}": ["type:drupal-custom-theme"]

The result should resemble this:

"installer-paths": {
    "htdocs/core": ["type:drupal-core"],
    "htdocs/libraries/{$name}": ["type:drupal-library"],
    "htdocs/modules/contrib/{$name}": ["type:drupal-module"],
    "htdocs/profiles/contrib/{$name}": ["type:drupal-profile"],
    "htdocs/themes/contrib/{$name}": ["type:drupal-theme"],
    "drush/Commands/contrib/{$name}": ["type:drupal-drush"],
    "htdocs/modules/custom/{$name}": ["type:drupal-custom-module"],
    "htdocs/themes/custom/{$name}": ["type:drupal-custom-theme"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment