Skip to content

Instantly share code, notes, and snippets.

@sheabunge
Last active January 25, 2016 12:21
Show Gist options
  • Save sheabunge/54752e32a65c54c2f1d4 to your computer and use it in GitHub Desktop.
Save sheabunge/54752e32a65c54c2f1d4 to your computer and use it in GitHub Desktop.
Getting Started with Drupal and Drush

Getting Started with Drupal and Drush

As part of participating in Google Code-In this year, I am introducing myself to Drupal, as an excersise to see how it works and what it offers as an alternative to WordPress for building content managed websites. As part of this process, I am learning how to install Drupal on an Ubuntu-powered web server running nginx and PHP 7.

To make installing and managing Drupal much easier, we will be shell interface for Drupal known as Drush. Drush is an incredibly useful tool, allowing you to perform a number of various tasks from the command line as a faster and more efficient alternative to using the graphical web based administration.

Setting up the server

First step is to install a web server for Drupal to run on. The first component of this is nginx, a fast and effecient server which can integrate with PHP. It is avaliable in the default Ubuntu repostories and can be installed like so:

sudo apt install nginx

While PHP 5 is avaliable by default, PHP7 has not made it in to the repositories as of the time this article was written, and so a PPA is needed to obtain the latest version:

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.0-fpm php7.0-cli php7.0-json pph7.0-curl php7.0-gd

Finally, Drupal requires a database to save its data into. Drupal works with MySQL, ProgreSQL and SQLite databases. In this example, I will be using a MySQL database, as it is what seems to be best supported by Drupal and what I am most firmiliar with, but it is certainly worthwhile trying out the other systems as well.

First of all, we need to install the MySQL server as well as the PHP extension:

sudo apt install mysql-server php7.0-mysql

Next, we need to set up and configure the server:

sudo mysql_install_db
sudo /usr/bin/mysql_secure_installation

Configuring nginx

In order to get Drupal to work, we need to set up a virtual host with nginx which includes rules for serving PHP files through FPM as well as allowing Drupal's clean URL system to work.

Using the nginx Drupal recipe as a base, create a new .conf file in /etc/nginx/sites-avaliable, ajusting the server_name and root values to match your configuration.

Finally, enable the new Drupal configuration and restart nginx:

sudo ln -s /etc/nginx/sites-available/drupal.conf /etc/nginx/sites-enabled/drupal.conf
sudo nginx -s reload

Installing Drush

While it is possible to install Drupal the traditional way, by downloading the archive from the website and extracting the files, we can use a powerfull tool

At first glance, Drush appears to be avaliable in the standard Ubuntu repositories. However, as is often the case, this version is very outdated, and additionally it has a dependency of the php5 packages, which we do not have installed as we are working with PHP 7.

While there many other existing alternative methods for installing Drush, including manually downloading the binary package and installing through Pear, my favourite method is to install it system-wide through Composer. Once Composer is installed globally, ensure that Composer's global bin directory is in your PATH by adding this line to your ~/.bashrc (or alternative) file:

export PATH="$HOME/.composer/vendor/bin:$PATH"

Once this is done, install Drush globally with Composer using this command:

composer global require drush/drush

Now you should be able to check that Drush is installed:

drush --version

If you recieve an error, check that you restarted your shell after adding the line to ~/.bashrc

Downloading Drupal

Downloading Drupal with Drush is pretty simple. Just change into your site root, and run the drush dl drupal command. You may also want to rename the resulting drupal-x.x folder to something more sensible.

Depending on how things are set up on your server, you may need to ajust the file permissions so Drupal as write access to its files. If the files are owned by the www-data group, then the folder permissions should be 775 and the files 664. This can be done from inside the Drupal installation directory like so:

sudo chown -R www-data:www-data .
find . -type d -exec sudo chmod 775 {} \;
find . -type f -exec sudo chmod 664 {} \;

Creating the Database

Now Drupal is downloaded, we need to create a new MySQL database for it to save its data into. To begin log in to MySQL using this command:

mysql -u root -p

In the mysql> prompt, enter the query to create a new database:

CREATE DATABASE drupal_test;

Next, create a new user and give them permissions to access the database:

CREATE USER drupal@localhost IDENTIFIED BY 'some_password';
GRANT ALL PRIVILEGES ON `drupal_test`.* TO drupal@localhost IDENTIFIED BY 'some_password';
FLUSH PRIVILEGES;

Installing Drupal

Drush can install Drupal as well using the drush site-install command (or drush si for short). There are a number of different arguments for configuring the install, but the basic ones you need to pass in are the database connection details. Here's how that would look using the database and user we set up before:

drush si --db-url=mysql://drupal:some_password@localhost/drupal_test 

You can also change other things about the install, such as the site profile, admin user details, and site name by passing in extra arguments. A full list can be obtained by running drush help si. Here's a basic example:

drush si minimal --db-url=mysql://drupal:some_password@localhost/drupal_test --site-name="My site" --account-mail="someone@example.com"

Installing Drupal Modules

Drupal supports something called modules, which are packages designed to extend the base functionality of Drupal, similar to plugins in WordPress. They can be added and removed with the Drush tool we installed before.

Modules are installed using the same drush dl command we used to download Drupal itself before. For example, here's how to download the popular XML sitemap and Admin Menu modules:

drush dl xmlsitemap
drush dl admin_menu

To enable the modules, use the drush en command:

drush en xmlsitemap admin_menu

And similarly to disable modules, use drush dis:

drush dis admin_menu

And finally to uninstall a module, use drush pmu:

drush pmu admin_menu

So as you can see, it is quite simple to use Drush to manage different aspects of your Drupal installation, including the management of modules. A full list of Drush commands can be found by simply running drush with no subcommands or arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment