Installing Drupal for local web development

Keywords

Now that we've set up our LAMP stack and virtual hosting, we are going to install Drupal. Drupal is a Content Management System (CMS) that allows you to build amazing dynamic websites, with Drupal's platform, modules and themes doing most of the heavy lifting. Drupal is constantly evolving with the Web and continues to get better with time, This can be put down to its community driven approach and the talented people who work at it's core and on the continual maintenance and development of its modules and themes.

Some of the world's best and most famous websites are built using Drupal and it has the rare ability to scale really well from the most simple to the most complex enterprise sites in existence. The only caveat in using it as a development platform, is that it's known to have quite a steep learning curve and does take some commitment to become proficient and to learn the Drupal way of doing things. In this respect the road might be longer, but it definitely arrives at a good place. Visit the Drupal site and Drupal Showcases to get a better feel for what Drupal is capable of.

An essential development and maintenance tool that accompanies Drupal is Drush. Drush is short for "the Drupal Shell" and is a command line utility that adds power and saves a heap of time in areas that would normally require the use of Drupal's user interface and other standard tools. We will install Drush as a pre-requisite to installing Drupal.

Installing Drush

Before installing Drush, we need to install Composer. This is a dependency manager for PHP, similar to what Bundler is for Ruby Gems. So Composer not only installs Drush and a host of other PHP libraries, but also allows us to control the exact versions we wish to use on a project by project basis. However, we'll be using Composer more as a means to install Drush on a global basis, but with the ability to quickly switch between versions if and when we need to. We'll be pretty much following the installation advice found on the Drush website.

Installing Composer

From your Ubuntu desktop, open up a terminal window (ctrl-alt-t) and enter the following:
curl -sS https://getcomposer.org/installer | php
This will get/install Composer using Curl which comes pre-installed with Ubuntu.

Next, we move the composer.phar application file onto our system PATH so that Composer is available to us globally (i.e. we can execute the program just by typing "composer..." from anywhere on our system).
sudo mv composer.phar /usr/local/bin/composer

Installing Drush with Composer

Let's just test that Composer is working and at the same time get it to retrieve some information about Drush.
composer show drush/drush
Composer will go and retrieve a heap of information about the Drush library (takes a few seconds so be patient) including the different versions available and its dependencies (other software it requires). These dependencies are automatically handled by Composer which is nice.

If we pop over to the Drush website we see that Drush7 has the most recent stable version and that Drush8 looks pretty solid and is actually required if you want to use Drupal 8 (currently in Beta and not yet recommended for production sites). Since we're going to be installing the latest version of Drupal 7, we only need to install Drush7. Choosing Drush8 would also probably be fine and would give the added flexibility if we wanted to start using Drupal 8. However, it's not a big deal as Composer makes it super-easy to switch to different Drush versions when we need to.

So let's install the latest stable version of Drush7.
composer global require drush/drush:7.*
Composer will now install the latest version of Drush7 along with all it's dependencies.

We now need to add Drush to our system PATH so that it's also available to execute from any location on our system simply by entering "drush...".
Open your .bashrc file with the default editor.
sudo nano ~/.bashrc
Add this to the bottom of the file:
export PATH = "$HOME/.composer/vendor/bin:$PATH"
Press ctrl-x, enter Y and then press Enter to save and exit the file.

Test that Drush now works simply by entering:
drush
You should see the following type of output.

If you don't and it says something along the lines of Drush not being installed, it probably actually means that composer/vendor/bin wasn't added to the PATH. You can check this directly by typing:
echo $PATH
To fix the problem you may need to restart your terminal session or check that the .bashrc entry is correct. DO NOT follow any advice about installing Drush using apt-get.

Installing and switching to other versions of Drush

You can easily switch to another version of Drush simply by installing it. For example, if you needed to use the latest version of Drush8, you would enter:
composer global require drush/drush:dev-master

The version you require is entered after the colon and the version names are available from when we entered: composer show drush/drush.
You can also use shorthand notation (e.g. 7.*) instead of the full version name, as was the case when we first installed the latest Drush7 version.

Once the new version has installed, you can check it by entering:
drush --version

Changes are also made to files under the ~/.composer directory and at some stage it is worth understanding these details by reading up on the Composer documentation.

System tweaks

We now just need to make a few tweaks to our system to accommodate Drupal and for installing it with Drush.

Edit the Apache configuration file

Open the Apache configuration file by entering:
sudo nano /etc/apache2/apache2.conf

Navigate down to the sections which shows:

<Directory /var/www/>
                Options Indexes FollowSymLinks
                AllowOverride None
                Require all granted
</Directory>


Change the line:
AllowOverride None

To:
AllowOverride All

Press ctrl-x, enter Y and then press Enter to save and exit the file.

Edit the Apache environment variable file

Open the Apache environment variable file by entering:
sudo nano /etc/apache2/envvars

Navigate down to the section which shows:

export    APACHE_RUN_USER=www-data
export    APACHE_RUN_GROUP=www-data


Change to the following, substituting ubuntu_username with your actual Ubuntu username.

export    APACHE_RUN_USER=ubuntu_username
export    APACHE_RUN_GROUP=ubuntu_username

Press ctrl-x, enter Y and then press Enter to save and exit the file.

This allows Apache access to our directories and files without us having to set up complex permissions in our projects for the Apache user (www-data). Security isn't a problem as it's our local development environment and not a live/production server.

Change ownership of phpMyAdmin temporary directory

Since we've changed the user under which Apache is running, we also need to change ownership of phpMyAdmin's temporary directory; otherwise we'll have problems importing databases through phpMyAdmin.

cd /var/lib/phpmyadmin

sudo chown ubuntu_username:ubuntu_username tmp

Obviously make sure you substitute ubuntu_username with your actual Ubuntu username.

Adjust php.ini directives

Some of the default php.ini settings are often inadequate for Drupal projects.

To change these, open the php.ini file by entering:
sudo nano /etc/php5/apache2/php.ini

Find the following directives in the file and change the values to your requirements. The values below should be considered generic, but adequate for many Drupal projects.

upload_max_filesize = 25M
post_max_size = 50M
memory_limit = 512M
max_execution_time = 300

Press ctrl-x, enter Y and then press Enter to save and exit the file.

If you wish to know more about these directives, see core php.ini directives.

Add the Apache rewrite module

Mod_rewrite is pretty much required by Drupal. To enable, enter:
sudo a2enmod rewrite

You will then be prompted to restart Apache. Enter:
sudo service apache2 restart

Note that ALL the above configuration changes require this restart for them to take effect.

Installing Drupal with Drush

Let's now install Drupal on the virtual host site we set up previously.

Navigate to our site:
cd ~/devel.mynewsite.com/public_html

Downloading Drupal

Normally we want to install the Drupal files directly in the root of the public_html directory. However, when Drupal downloads, all the files will be in another directory which by default is named after the version of Drupal it contains (e.g. drupal-7.38). This would be fine if you wanted your site to be located at devel.mynewsite.com/drupal-7.38/ or were setting it up as a subdomain drupal-7.38.devel.mynewsite.com, but we actually just want to set it up in the normal way. Once Drupal has downloaded, we could just move the files (including the hidden files) up a level and delete the empty directory, but because Drush allows us to name this directory, we can just delete the existing public_html directory and name the downloaded directory public_html.

To move up a level:
cd ..

To delete the existing (empty) public_html directory.
rmdir public_html

To download the latest version of Drupal 7 and have the downloaded directory named public_html.
drush dl drupal --drupal-project-rename="public_html"

Installing Drupal

Drush allows us to install and set up an entire Drupal site with a single command.
drush si standard --db-url=mysql://dbusername:dbpassword@localhost/dbname --db-su=root --db-su-pw=rootpass --account-name=Superuser --account-pass=drupal_password --site-name="My New Site"

Okay, so let's break this down to understand exactly what it's doing.

  • si standard means site-install using the standard installation option.
  • --db-url=mysql://dbusername:dbpassword@localhost/dbname indicates the database type, the database username, password and database name. This is all being created on the fly so you simply choose the names and password you want.
  • --db-su=root --db-su-pw=rootpass are the credentials of the mysql root user (created during Setting up a LAMP stack for local web development). These are required to set up the site database on the fly.
  • --account-name=Superuser --account-pass=drupal_password are the log in details for your new site which you choose here.
  • --site-name="My New Site" is the name of your new Drupal site which you also choose here.
  • Enter drush si --help to see more site installation options or visit the Drush website.

    The installation process is very quick and Drush will report that all went okay and remind you of your new Drupal site's username and password.

    If you log in to phpMyAdmin (see Setting up a LAMP stack for local web development) you will see your new database. If you then click on your new database and then click the Privileges tab, you will see your database username.

    It's now time to visit our new site and check it out. Open up a browser and enter the following into the address bar.
    devel.mynewsite.com

    Then enter your username and password to log in to the site. Once logged in, you will see an administration menu appear across the top. Click on "Reports" and then "Status Report". The following screen will display and if all went well there will be no warnings or errors.

    Success! you are now ready to work on your newly installed Drupal site. You also now have a system for very quickly installing more Drupal sites when you need them.

    Richard McCulloch's picture
    Richard is a Director and the Senior Consultant of Single Version Pty Ltd. He is a strong supporter of free and open source software and especially how it can be used to empower businesses large and small. Richard holds degrees in science, BSc(Hons.), and business, MBA, and is a keen web developer and information technologist. He is a member of the Drupal Association and uses Drupal as the CMS of choice due to its power and flexibility and how it keeps pace with the evolving web due to the amazing community of people behind it.