How I setup my local computer for Drupal development on a new site

Here are some steps I use when I need to do local Drupal development on a site that's already live. These steps are also helpful for setting up a new project.

Get the code

cd /Applications/MAMP/htdocs
git clone git@github.com:siteexample/siteexample.git

Set up local virtual hosts

Edit /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf. Here's an example httpd-vhosts.conf file:
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
 
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    DocumentRoot /Applications/MAMP/htdocs/siteexample/docroot
    ServerName localhost:80
</VirtualHost>
 
<VirtualHost *:80>
ServerName local.siteexample.com
DocumentRoot /Applications/MAMP/htdocs/siteexample/docroot
</VirtualHost>

Edit /etc/hosts file as sudo

sudo nano /etc/hosts
Here's an example /etc/hosts file:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost
 
# Add local vhosts below this line.
127.0.0.1 local.siteexample.com

Create a new database and user

# log in to mysql
mysql -uroot -proot
# In the prompt (mysql>):
create database siteexample;
create user 'siteexample'@'localhost' identified by 'siteexample';
grant all on siteexample.* to 'siteexample'@'localhost';
exit

Set up bash and drush aliases by editing ~/.bash_profile

# Alias to get to server document root
alias cdhtdocs='cd /Applications/MAMP/htdocs'
# Alias to change to the new site folder
alias siteexamplecd='cd /Applications/MAMP/htdocs/siteexample/docroot'
# Aliases to change to the new site folder and instruct drush to use the correct multisite
alias siteexampledrush='cd /Applications/MAMP/htdocs/siteexample/docroot; drush use local.siteexample.com'
Restart MAMP and also open up a new terminal window so your bash aliases are recognized.

Update settings.php

<?php
$databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'siteexample',
      'username' => 'siteexample',
      'password' => 'siteexample',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

Sync files from the test server (not needed if using the Stage File Proxy module)

Add siteexample to your ~/.ssh/config file, ie. :
Host siteexample
  HostName staging-1234.prod.hosting.acquia.com
  User siteexample
Get files, ignore some directories and file types.
rsync -chavzP --stats --exclude 'css' --exclude 'js' --exclude *'.pdf' --exclude *'.PDF' --exclude *'.mov' --exclude *'.MOV' --exclude '*.mp4' --exclude '*.MP4' siteexample:~/test/sites/default/files /Applications/MAMP/htdocs/siteexample/docroot/sites/default

Get a copy of the db

Place the db dump in ~/projects/siteexample/dbdumps/prod

DB Import and drush commands for restoring local site

If you need to, empty the database before importing:
mysql -usiteexample -psiteexample
DROP DATABASE siteexample;
CREATE DATABASE siteexample;
exit
Restore the site:
# Switch to the directory where you saved the dbdump.
cd ~/projects/siteexample/dbdumps/prod
# Store the name of the latest file that was added to the directory to a variable
LATESTFILE=$(ls -t | head -1);
# Unzip the compressed db dump to a temp file
gunzip -c $LATESTFILE >temppp.sql;
# Import the db.
# The command below uses pipe viewer to see the progress of the db import
# To install pipe viewer with Mac HomeBrew:	Run "brew install pv" to get the latest version.
# See http://www.ivarch.com/programs/pv.shtml
pv temppp.sql | mysql -usiteexample -psiteexample siteexample;
# Remove temp file.
rm temppp.sql;
# Change to the new site folder and instruct drush to use the correct multisite
siteexampledrush
# Turns of CSS and JS preprocessing for theming.
drush vset preprocess_css 0;
drush vset preprocess_js 0;
# Runs registry rebuild, not necessary for all sites, but comes in handy sometimes.
# If you don't have this drush plugin, you can just  drush dl registry_rebuild and drush will download it into your .drush folder. 
drush rr;
# Set the private file path.
# You will have to create a folder for the private files. I choose /Applications/MAMP/htdocs/private but it really could be anything.
drush vset file_private_path "/Applications/MAMP/htdocs/private";
# Turns off automatic cron.
drush vset cron_safe_threshold 0;
# Disables some modules (Acquia reporting module, Acquia varnish purge, and memcache.)
drush dis acquia_spi acquia_purge memcache -y;
# Sets Acquia Solr dbs to read only
drush solr-vset --yes --id=acquia_search_server_1 apachesolr_read_only 1
# Sets the local Solr instance as the default
# Commented out; only needed if you are developing solr related features.
# drush vset --yes apachesolr_default_environment solr
# Clears caches
drush cc all;
# Creates a test admin account.
drush ucrt testadmin --mail="testadmin@example.com" --password="testadmin"
drush urol "administrator" --mail="testadmin@example.com"
# Log in as test admin:
drush uli testadmin@example.com;

Internal References

Article Type

General