Drupal 7: Install Multiple Sites on Ubuntu

Assumptions

LAMP: Ubuntu server 14.04, Apache2, Mysql, PHP 5
Web server IP is 111.111.111.111
We want 3 separate Drupal 7.34 sites, suchlike:

Site Name Web Installation directory Server IP addr port Database DBuser
drupsite01 /var/www/drup01 111.111.111.111 8001 drupdb01 drupus01
drupsite02 /var/www/drup02 111.111.111.111 8002 drupdb02 drupus02
drupsite03 /var/www/drup03 111.111.111.111 8003 drupdb03 drupus03

Assuming also that you've run:

sudo apt-get update

Install LAMP server

sudo apt-get install tasksel
sudo tasksel install lamp-server

Drupal 7 requires PHP gd extension...

sudo apt-get install php5-gd

Enable Apache2 rewrite functions

cd /etc
sudo grep -rwn 'apache2' -e "AllowOverride"

Should give you something like this:

apache2/conf-available/security.conf:9:#   AllowOverride None
apache2/conf-available/serve-cgi-bin.conf:13:            AllowOverride None
apache2/apache2.conf:155:    AllowOverride None
apache2/apache2.conf:160:    AllowOverride None
apache2/apache2.conf:166:    AllowOverride None
apache2/apache2.conf:172:#    AllowOverride None
apache2/apache2.conf:180:# for additional configuration directives.  See also the AllowOverride
apache2/mods-available/alias.conf:18:        AllowOverride None
apache2/mods-available/userdir.conf:6:        AllowOverride FileInfo AuthConfig Limit Indexes

Replace above occurrences of "AllowOverride None" with "AllowOverride All" in apache2.conf:

sudo vi /etc/apache2/apache2.conf
sudo a2enmod rewrite

Apache should respond like this:

Enabling module rewrite.
To activate the new configuration, you need to run:
  service apache2 restart
sudo service apache2 restart
* Restarting web server apache2             
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
                                                                                                     [ OK ]

Install Drupal

Get and unpack latest Drupal 7 (Version 7.34 at time of writing (2015-03-03)) somewhere appropriate, e.g. /tmp

cd /tmp
sudo wget http://ftp.drupal.org/files/projects/drupal-7.34.tar.gz
tar -xvzf drupal-7.34.tar.gz

Create web site structure for Drupal sites:

sudo mkdir /var/www/drup01
sudo mkdir /var/www/drup02
sudo mkdir /var/www/drup03
sudo cp /tmp/drupal-7.34/* /var/www/drup01 -r
sudo cp /tmp/drupal-7.34/* /var/www/drup02 -r
sudo cp /tmp/drupal-7.34/* /var/www/drup03 -r

Create the files subdirectory:

sudo mkdir /var/www/drup01/sites/default/files
sudo mkdir /var/www/drup02/sites/default/files
sudo mkdir /var/www/drup03/sites/default/files
sudo chown www-data:www-data /var/www/drup01/sites/default/files
sudo chown www-data:www-data /var/www/drup02/sites/default/files
sudo chown www-data:www-data /var/www/drup03/sites/default/files

Configure settings.php:

sudo cp /var/www/drup01/sites/default/default.settings.php /var/www/drup01/sites/default/settings.php
sudo cp /var/www/drup02/sites/default/default.settings.php /var/www/drup02/sites/default/settings.php
sudo cp /var/www/drup03/sites/default/default.settings.php /var/www/drup03/sites/default/settings.php
sudo chown www-data:www-data /var/www/drup01/sites/default/settings.php
sudo chown www-data:www-data /var/www/drup02/sites/default/settings.php
sudo chown www-data:www-data /var/www/drup03/sites/default/settings.php

Configure Database Server

Create databases:

mysqladmin -u root -p create drupdb01
mysqladmin -u root -p create drupdb02
mysqladmin -u root -p create drupdb03

Start mysql session, configure Drupal database users and flush:

mysql -u root -p
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON drupdb01.* TO 'drupus01'@'localhost' IDENTIFIED BY 'MYSECRETPASSWORD';
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON drupdb02.* TO 'drupus02'@'localhost' IDENTIFIED BY 'MYSECRETPASSWORD';
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON drupdb03.* TO 'drupus03'@'localhost' IDENTIFIED BY 'MYSECRETPASSWORD';
mysql> FLUSH PRIVILEGES;

Configure web server

To configure virtual hosts, open 000-default.conf for editing...

sudo vi /etc/apache2/sites-available/000-default.conf

...and add these lines...

<VirtualHost *:8001>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/drup01
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:8002>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/drup02
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:8003>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/drup03
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

...and set up ports in ports.conf...

sudo vi /etc/apache2/ports.conf

...and add these lines after the line "Listen 80"...

Listen 8001
Listen 8002
Listen 8003

Reload web server with new settings:

sudo a2ensite
sudo service apache2 reload

Finalise Drupal web-based installation:

Browse to http://111.111.111.111:8001, 8002 and 8003 and complete the Drupal set up for each site, referring to this table:

Site Name Web Installation directory Server IP addr port Database DBuser
drupsite01 /var/www/drup01 111.111.111.111 8001 drupdb01 drupus01
drupsite02 /var/www/drup02 111.111.111.111 8002 drupdb02 drupus02
drupsite03 /var/www/drup03 111.111.111.111 8003 drupdb03 drupus03

Addendum: Stuff you might want to do

1. Tidying up:

Complete the settings.php lockdown. If you're going to import an existing site then do this after you've verified connectivity to your imported database.

chmod 644 /var/www/html/drup01/default/settings.php
chmod 644 /var/www/html/drup02/default/settings.php
chmod 644 /var/www/html/drup03/default/settings.php

2. Grant permissions to your SFTP user

Once you're up and running you may encounter problems when transfering files to your new site. If you're like me you'll be moving modules and stuff around with SFTP using Filezilla or similar, but you won't have write access to the www directory or below.

Fix this by adding your SFTP user to the www-data group we used above to chown the /var/www directory structure. Let's assume your SFTP user login is sftp-user:

sudo adduser sftp-user www-data
sudo chown -R www-data:www-data /var/www
sudo chmod -R g+rw /var/www

3. Install drush

If you have ssh access to your server use drush. 

sudo apt-get install drush

4. Import an existing Drupal site.

I covered export/import of existing sites without drush in this article
If you have access to drush, see this article.