How to Install Drupal using Drush on Ubuntu 18.04

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $100 credit.
This credit will be applied to any valid services used during your first 60 days.

Drupal is a content management system (CMS) designed for building custom websites for personal and business use. Built for high performance and scalability, Drupal provides the necessary tools to create rich, interactive “community” websites with forums, user blogs, and private messaging. Drupal also has support for personal publishing projects and can power podcasts, blogs, and knowledge-based systems, all within a single, unified platform.

Drush is a command line tool for creating, administrating, and modifying Drupal websites. Command line tools, like Drush, add functionality through additional command packages. Once installed, Drush is as easy to use as any of the basic Linux commands.

Before You Begin

  1. Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.

  2. Follow our Securing Your Server guide to create a standard user account, harden SSH access, remove unnecessary network services and create firewall rules for your web server; you may need to make additional firewall exceptions for your specific application.

    Note

    This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, visit our Users and Groups guide.

    All configuration files should be edited with elevated privileges. Remember to include sudo before running your text editor.

  3. Install and configure a How to Install a LAMP stack on Ubuntu 18.04

  4. Install Composer and Drush on Ubuntu 18.04

Download and Prepare Drupal 8

  1. Navigate to your site’s document root. If you installed and configured your Apache server using our LAMP stack on Ubuntu 18.04 guide, your document root should be located in the /var/www/html/example.com/public_html/ directory. Replace example.com with your own document root path’s name.

     cd /var/www/html/example.com
    
  2. Download the Drupal 8 tarball. As of writing this guide, Drupal 8.8.3 is the latest version. See Drupal’s download page for their latest core tarball. Replace 8.8.3 with the version number you wish to download.

     sudo wget http://ftp.drupal.org/files/projects/drupal-8.8.3.tar.gz
    
    Caution
    Ensure that the version number matches the Drupal 8 version you wish to download.
  3. Extract the downloaded tarball’s contents into your site’s document root:

    sudo tar -zxvf drupal-8.*.tar.gz --strip-components=1 -C public_html
    
  4. Drupal depends on a PHP graphics library called GD. Install GD and other dependencies:

    sudo apt-get install php-gd php-xml php-dom php-simplexml php-mbstring
    
  5. Create your Drupal 8 installation’s settings.php file from the default settings file. This file will be configured when you run through Drupal’s automated web configuration. See the Install and Configure Drupal on Ubuntu 18.04 guide for more details.

     sudo cp /var/www/html/example.com/public_html/sites/default/default.settings.php /var/www/html/example.com/public_html/sites/default/settings.php
    
  6. Enforce trusted hostnames with those that users will access your site from. With the text editor of your choice, edit your settings.php file replacing the regular expression (RegEx) with a pattern that matches your own site’s URL(s).

    File: /var/www/html/example.com/public_html/sites/default/settings.php
    $settings['trusted_host_patterns'] = array(
      '^www\.example\.com$',
      '^example\.com$',
      );
    Note
    trusted_host_patterns also accepts IP addresses or localhost.

Configure Apache 2.4

  1. Enable Apache’s rewrite module. This module is necessary since Drupal 8 enables Clean URLs by default.

    sudo a2enmod rewrite
    
  2. Specify the rewrite conditions for your Drupal site’s document root in Apache’s configuration file using the text editor of your choice. If you installed and configured your Apache server using LAMP stack on Debian 10 guide, the configuration file for your site is located at /etc/apache2/sites-available/example.com.conf.

    File: /etc/apache2/sites-available/example.com.conf
    <Directory /var/www/html/example.com/public_html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
          RewriteEngine on
          RewriteBase /
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    </Directory>
  3. Change the ownership of your site’s document root from root to www-data. This allows you to install modules and themes, and to update Drupal without being prompted for FTP credentials.

    sudo chown -R www-data:www-data /var/www/html/example.com
    
  4. Restart Apache so all changes are applied.

    sudo systemctl restart apache2
    

Create a Drupal Website with Drush

In this section, you will use Drush to install a Drupal site with just a few commands.

  1. Change the working directory to the location of your new Drupal website. The previous guides created a /var/www/html/example.com/public_html directory, where public_html is the document root or the publicly viewable directory. Replace example.com with your own site’s name.

    cd  /var/www/html/example.com/public_html
    
  2. Your Linode is now ready for you to install a Drupal site. In the command below, replace mysql://username:password@localhost/databasename with your own site’s username, password, and database. For example, if you followed the How to Install a LAMP stack on Ubuntu 18.04 your username is webuser, password is password, and the database is webdata. Also, replace --site-name=example.com with your own website’s name.

    drush si standard --db-url=mysql://username:password@localhost/databasename --site-name=example.com
    
    Note
    Although MySQL accepts passwords with a special character, for example an exclamation point, the drush si standard command does not. If you have a special character in your MySQL password, you may need to change it.
    Note
    If you encounter errors related to writing to the sites/default directory, follow the steps in the Setting the Site’s Ownership and Permissions section to ensure the web server belongs to the current user’s group.

    After the installation is complete, Drush creates a user, named admin, and a random password. An example is pictured below. These credentials are used for the Drupal sign-in page.

  3. Optionally, if you’d like to change the admin’s password, it is best to do so with Drush, rather than sending the password over a non-secure HTTP connection. To update the admin password execute the following command and replace newpass with your new password:

    sudo drush user-password admin user-password=newpass
    

Setting the Site’s Ownership and Permissions

In server administration, there are many options for user and group permissions. The directions below create a site owner and a site owner’s group. The Apache user, named www-data, is added to the site owner’s group. Then, read, write, and execute permissions are granted to both the site owner and the site owner’s group.

  1. To create a new user for the site owner position, see the Add a Limited User Account section of the Setting Up and Securing a Compute Instance guide.

  2. From the public_html directory, change ownership of the site to the site owner and group. Replace example_user below with the chosen owner’s username:

    sudo chown -R example_user:example_user sites/default
    
  3. Add Apache’s www-data user to the site owner’s group:

    sudo usermod -a -G example_user www-data
    
  4. Make sure the permissions are set to allow access for the site owner and site owner’s group:

    sudo chmod -R 770 sites/default
    

    Now, www-data, example_user, and any user within the example_user group has read, write, and execute permissions for the entire Drupal site directory tree.

  5. Restart Apache:

    sudo systemctl restart apache2
    
  6. Finally, check the status of the new site:

    drush status
    
    Note
    When installing new files, like a module or theme, make sure the Apache user has access rights. Use the command ls -al to list the file permissions within a directory to determine which permissions are assigned to it.

Navigate to your site’s domain (or IP address if you did not set up a domain name). Sign-in with the generated username and password to begin creating content for your Drupal site.

Additional Options

There are many ways to set up administration for a website. Below are sections explaining some additional options. It’s important to be aware of multi-site setups and additional security measures. The topics below touch on these subjects.

File Ownership, Permissions, and Security

The above setup is designed for ease of use. However, there are setups designed for tighter security and other considerations.

Multi-site Servers

At a high-level, the steps you will need to follow to begin configuring a Drupal multisite set up are:

This page was originally published on


Your Feedback Is Important

Let us know if this guide made it easy to get the answer you needed.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.