On beyond quickstart

By Erik, 18 May, 2021

Composer has changed all of our lives. Okay, well, that's a simplification and obvious over-generalization… but if you're reading this rambling, then you probably deal with Drupal at some level and know what I mean.

Drupal has jumped onboard with this method and has been able to move away from keeping everything updated internally and letting external contributed composer packages take care of themselves. This has led to wonderful shortcuts like:

<pre><code>composer create-project drupal/recommended-project</code></pre>

This gives you a pre-configured Drupal code base built on Composer and ready to go… well, almost…

I've noticed that once you create this project, there is a lot of set up still to be done to get a functioning Drupal site running. Especially if you want to be able to use it on a variety of platforms and locations.

I tend to do most of my local development without Docker, VMs, or dedicated environments. Those tools definitely are useful, but I don't particularly need them for my own stuff 99% of the time. So, I have MySQL installed as MariaDb via <code>homebrew</code> and sometimes I actually will just use SQLite as a database backend but I've found the overhead of running MySQL not to be significant. 

My general steps when starting a new project (even if it's only supposed to be temporary) includes the following:

  1. Create Composer project
  2. Initialize git repository
  3. Create local settings.php file and configure Drupal to use it
  4. Modify the project .gitignore file 
  5. Creating local database and credentials in settings.local.php
  6. Install Drush
  7. Use Drush to install the site
  8. Start the internal PHP webserver with Drush in a new terminal tab
  9. Call Drush user:login to gain access to the new site

So, as I mentioned… almost. 

This process has the benefit of allowing continual updates via Composer, a starting format where you can roll back individual steps, and an easy transfer to a hosted solution.


#!/usr/bin/env bash

if [[ -z $1 ]]; then
  echo "Usage: local.sh  "
  exit
fi

localPort=8888
if [[ ! -z $2 ]]; then
  localPort=$2
fi

projectName=$1

# Use --no-install because we want to change "web/" to "docroot/" in composer.json
# before running composer install
composer create-project --no-install drupal/recommended-project $projectName
cd $projectName

# Replace all the "web/" instances to "docroot/" via an in-place sed
sed -i -e 's[web/[docroot/[' composer.json
composer install 

# Start this off as a git repository to be able to see history
git init
git add -A
git commit -m "Initial: composer create-project drupal/recommended-project $projectName"

# To set up local settings, we'll need drush to create a hash_salt
composer require drush/drush
git add -A
git commit -m "composer require drush/drush"

# Copy the default settings.php file in place
cp docroot/sites/default/default.settings.php docroot/sites/default/settings.php

# And patch it to allow the settings.local.php file to be included.
cat  .gitignore
git add .gitignore
git commit -m "Adding *.local.php to gitignore"

# Now go ahead and create the settings.local.php
# Start by generating a new hash for this site
# Drush is in PATH because I include `./vendor/bin/`
hashSalt=$(drush php-eval 'echo \Drupal\Component\Utility\Crypt::randomBytesBase64(55) . "\n";')

currentUser=$(whoami)

cat  docroot/sites/default/settings.local.php

  [
    'default' =>
    [
      'database' => '$projectName',
      'username' => '$currentUser',
      'password' => '',
      'driver' => 'mysql',
      'host' => 'localhost',
      'port' => '3306',
      'collation' => 'utf8mb4_general_ci',
      'prefix' => '',
    ],
  ],
];

\$settings['trusted_host_patterns'] = [
  '^localhost$',
  '^127\.0\.0\.1$',
];
\$settings['skip_permissions_hardening'] = TRUE;

EOF

# Since we're skipping permission hardening, we set this permission manually
# simply removing w on the files, but not the directory will allow
# rm -rf to complete without issue
chmod u-w docroot/sites/default/settings.local.php docroot/sites/default/settings.php

# Before we install, we'll need a database created
mysqladmin create $projectName

# Now a full drush site:install turning off the emails to notify on update checks
drush site:install \
  --yes \
  --site-name="$projectName on localhost" \
  --account-name=user1 \
  --account-mail=erik.peterson@acquia.com \
  --site-mail=erik.peterson@acquia.com \
    standard \
    install_configure_form.enable_update_status_emails=NULL

# Now that "config" and public files are created, deal with them
echo "docroot/sites/default/files" >> .gitignore
git add config .gitignore
git commit -m "Adding config and gitignore for excluding sites/default/files"

# Easier to create Drush alias for @local at this point
mkdir -p drush/sites/
cat  drush/sites/self.site.yml
#
# A minimalist Drush site alias file.
# See https://www.drush.org/latest/site-aliases/ for lots more documentation.
#
# File: self.site.yml
local:
  root: docroot
  uri:  127.0.0.1:${localPort}
  command:
    runserver:
      options:
        default-server: '127.0.0.1:${localPort}/'
    user:
      login:
        arguments: 'admin/reports/status'
EOF

# And to make sure it is in the repository
git add drush/sites/self.site.yml
git commit -m "Adding minimal drush/sites/self.site.yml"

# Take the existing site and make sure we have the initial config committed to the repository
drush @local config:export
git add config/
git commit -m "Adding base config files to repo"

# Using Applescript to tell iTerm to run a new tab with "drush runserver"
# Also officially cd into pwd in first tab so that we end up there out of process
pwd=$(pwd)
osascript