Installing Drupal in Debian/Ubuntu GNU/Linux

Table of Contents

1 Installing Debian Packages

A fast way to do it with mysql may be the next:

$ sudo apt-get install drupal7 drush

Drush is an useful tool to facilitate the manage of our drupal by command line, by example, updates of modules, backups, etc.

Really, it's better download the last version of drupal that the debian package, due to my policy of use drush to upgrades. But install drupal7 as debian package is a good idea to install depedencies.

$ cd /var/www/
$ sudo wget -c
$ sudo tar -xvzf drupal-7.32.tar.gz
$ sudo chown -R www-data.www-data drupal-7.32
$ sudo chmod g+w -R drupal-7.32
$ sudo mv drupal-7.32 mysite

2 Configuring Apache

To install a drupal site, I would like enable clean urls, to it I will create the file mysite in /etc/apache2/sites-available/mysite.conf, with the next content:

<VirtualHost *:80>

  ServerAdmin webmaster@localhost
  ServerName mysite

  DocumentRoot /var/www/mysite

  <Directory /var/www/mysite>
		RewriteEngine on
		RewriteBase /
		RewriteCond %{REQUEST_FILENAME} !-f
		RewriteCond %{REQUEST_FILENAME} !-d
		RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


Due to in this moment don't have the domain of the site it will be possible enable the host in /etc/hosts       localhost       mysite

Later, we can enable it so:

$ sudo a2enmod rewrite
$ sudo a2ensite mysite

3 Configuring Nginx

To install it:

$ sudo apt-get install nginx*

An alternative solution to apache is to serve Nginx in sites-available:

server {
	listen 80;
	server_name mysite;
	root /var/www/mysite/;
	include /etc/nginx/sites-available/drupal.include;

In /etc/nginx/sites-available/drupal.include:

# Drupal config

location = /favicon.ico {
	access_log off;
	log_not_found off;

# Pls read

# Default location
location / {
	try_files $uri /index.php?q=$uri&$args;

	# Not a regexp, but a prefix match that prevents execution of regexps
location ^~ /sites/default/files/private {
	deny all;

location ~ /\. {
	deny all;

location ~ \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl|sw[op]|bak|orig|save)$ {
	deny all;

# PHP via FGCI protocol
location ~* /index.php {
	try_files $uri =404;
	fastcgi_pass unix:/var/run/php5-fpm.sock;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	fastcgi_index index.php;
	include fastcgi_params;

Restart nginx:

$ sudo /etc/init.d/nginx restart

It's a good idea make your own benchmarks ;-)

$ ab -n 1000 -c 100 http://mysite

4 Creation of the database in Mysql

$ mysql -u root -p 
mysql> create database drupal7;
mysql> create user 'drupal7'@'localhost' identified by 'lapasswd';
mysql> grant all privileges on drupal7.* to 'drupal7'@'%' with grant option;

5 Creation of the database in Postgresql

First of all, you must install the needed debian packages:

$ sudo apt-get install postgresql-9.1 postgresql-client php5-pgsql

Now, you can create the database and the user to postgres.

$ createuser dpsql --pwprompt --encrypted
$ createdb dpsql --owner=dpsql

To use the psql client on local you can modify the line local in /etc/postgresql/9.1/main/pghba.conf:

local             all            all                   trust

If you want change the pass you can do:

$ psql -U dpsql dpsql -W
dpsql=> ALTER USER dpsql with password 'thepassword';

6 Web install

First, you can remove permissions to CHANGELOG.txt to avoid security problems:

$ cd /var/www/mysite/
$ chmod 600 CHANGELOG.txt

So enabled the site, you can go to http://mysite/install.php and the installation is done step by step

In the step 4 (Set up database) you can find database name, database username and database password with the data provided.

7 Drush install

Alternatively, you can use drush instead of a web installation

$ drush site-install standard --account-name=admin --account-pass=admin 

8 Simple backup system

You can do a tar file every day at 13:00 with:

$ crontab -e

And introducing a line such as

0 13 * * * /bin/tar -cvzf /var/backups/drupal7-$(date "+%d-%m-%Y").tar.gz /var/www/drupal7
0 13 * * * cd /var/www/drupal7; drush sql-dump > /var/backups/drupal7-$(date "+%d-%m-%Y").sql

You must check if the cron user has permissions to make the backup, sure. Perhaps, you need create a sh script, if you have troubles.

9 Postfix

You will need send emails from drupal, an unix solution is postfix

$ sudo apt-get install postfix

10 Ssl

The first steps is enable ssl in apache and create the ssl certificates.

$ sudo a2enmod ssl
$ sudo service apache2 restart
$ sudo mkdir /etc/apache2/ssl
$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt

Later, we can create virtualhost mysite-ssl.conf

<VirtualHost *:443>

  ServerAdmin webmaster@localhost
  ServerName mysite:443

  DocumentRoot /var/www/mysite

  <Directory /var/www/mysite>
		RewriteEngine on
		RewriteBase /mysite
		RewriteCond %{REQUEST_FILENAME} !-f
		RewriteCond %{REQUEST_FILENAME} !-d
		RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key


We can enable the virtualhost

$ sudo a2ensite default
$ sudo service apache2 reload

Modify $baseurl in settings.php. For example, $baseurl = htts://

And enable securelogin

$ cd /var/www/mysite/
$ drush dl securelogin
$ drush en securelogin

Finally, I use go to the configuration to request https in forms.

11 Mongodb as cache

11.1 Install mongo

apt-get install mongodb mongodb-clients mongodb-server

11.2 Install mongo in php

pecl install mongo

11.3 Install mongo in drupal

cd /var/www/mysite
drush dl mongodb 
drush en mongodb_cache mongodb_migrate mongodb_session mongodb_field_storage mongodb_queue mongodb

12 Nagios

$ sudo apt-get install nagios3
$ sudo htpasswd -c htpasswd.users nagiosadmin

Go to http://localhost/nagios3/

13 Memcache

13.1 Installing debian packages

$ sudo apt-get install memcached libmemcached-tools
$ sudo apt-get install php5-dev php-pear make php5-memcache

13.2 Check memcached

You can take a look to /etc/memcached.conf, try allocating as much as 1/4 of your total available RAM.

Restart Memcached and Apache for these change to go into effect:

$ sudo /etc/init.d/memcached restart
$ sudo /etc/init.d/apache2 restart

To check if memcached is running:

$ sudo netstat -tap | grep memcached

You must see something such as:

tcp 0 0 localhost:11211 *:* LISTEN 25266/memcached

13.3 Installing Memcached for Drupal 7

You can use drush, or from /admin

$ drush en memcache memcache_admin

Finally, you must add the next lines in settings.php

$conf['cache_backends'][] = 'sites/all/modules/memcache/';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
$conf['page_cache_without_database'] = TRUE;
$conf['page_cache_invoke_hooks'] = FALSE;

14 References

15 License

Copyright (C) 2013 David Arroyo Menéndez Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in GNU Free Documentation License.


Author: David Arroyo Menéndez

Created: 2016-10-23 dom 01:20

Emacs 24.4.1 (Org mode 8.2.10)