Precisely Drupal: Setting up Ubuntu 12.04 Precise Pangolin LAMP stack for your Drupal 7 site

Updated 26 June 2012 with additional information.

Progress! A lot has changed in the Ubuntu world. Ubuntu 12.04 LTS, codenamed "Precise Pangolin", is out, incorporating many improvements over previous versions. Some commandline syntax has changed. Packages are newer. And creation of the LAMP stack for your Drupal site (with PHP 5.3 and MySQL 5.5) has never been easier.

There are still a few steps to get everything going, but as you'll see, it's a lot easier than it was with Lucid.

Assumptions

  1. You already have Ubuntu 12.04 installed on your server. (Download) (Release notes)
  2. You know your server's IP address.
  3. You can point your desired (sub-)domain at your server. Nuances of DNS are not covered here.
  4. You actually want to set up for Drupal. (These instructions may apply for other LAMP-based CMSs.)
  5. You have a computer with shell (SSH) access to your server, and your account has root or sudo access.

(This post does not cover MAMP, WAMP, DAMP, commandline bootcamp, etc.)

Step by Step

1. Initial Preparation

This part has not changed. To do this, follow steps 1.a. through 1.d. ONLY as outlined in Lucidly Drupal: Setting up Ubuntu 10.4 Lucid LAMP stack for your Drupal site.

NB: Do not proceed beyond step 1.d. Things change after that.

2. Set up Apache

This step includes setting up your VirtualHost configurations. To do this, you need to have Apache2 installed. But now we don't have to do it the old-school way. There's a new apt in town!

apt-get install lamp-server^

This package does just what you'd think: It installs all the LAMP basics: Apache2, MySQL, PHP, and supplementary stuff. It's lovely! Notice the ^ at the end of the line. That's necessary.

NB: If you are not logged in as root or via sudo su -, you will need to prepend most commands in this post with "sudo".

Next restart Apache, again with new command syntax (new since 11.04, that is).

service apache2 restart

Make sure you have the latest updates.

apt-get update
apt-get upgrade --show-upgraded

There's more to install, but first let's get back to the basic setup....

3. Configure VirtualHost stuff

There are a few steps here.

Add your IP address to the ports.conf file

NB: In these instructions, your site's domain is represented by "example.com" and your server's IP address is represented by "12.34.56.78". Every time you see them in the examples here, you should replace them with your domain name and server's IP address, respectively.

nano /etc/apache2/ports.conf

Replace "*:80" with your IP address, so it looks like this:

NameVirtualHost 12.34.56.78:80
Set up your server's default configuration
nano /etc/apache2/sites-available/default

Edit as such:

<VirtualHost 12.34.56.78:80>

You will also want to update the DocumentRoot value to where you intend to install your Drupal root.

DocumentRoot /var/www/example.com/html

NB: If you are using Git to deploy your site, you will want to make sure the DocumentRoot value aligns with the path that will result from your Git checkout. For example, if you are installing from a GitHub project titled "foobar" and inside of it you have a folder "html" that contains your Drupal installation, your DocumentRoot value might be /var/www/example.com/foobar/html/.

Configure name-based virtual hosts

Here you want to create a file in /etc/apache2/sites-available/ for each of your sites on the server.

nano /etc/apache2/sites-available/example.com

Adapt the following code:

<VirtualHost 12.34.56.78:80>
     ServerAdmin admin@example1.com
     ServerName example1.com
     ServerAlias www.example1.com
     DocumentRoot /var/www/example1.com/html/
     ErrorLog /var/www/example1.com/logs/error.log
     CustomLog /var/www/example1.com/logs/access.log combined
</VirtualHost>

Repeat this process for each site you are setting up. Note that separate sites will have different DocumentRoot values, while a Drupal multisite setup will share the same DocumentRoot value.

Create your website folders

First create the folder for your logs. (If you're using Git, the logs path should be outside of your Git repository.)

mkdir -p /var/www/example.com/logs

This command will create your logs folder, and the domain folder containing it.

Now create your website html folder(s). How you do this depends upon how you're going to install your site on the server. If you are using scp or sftp or otherwise copying your Drupal code files directly onto the server, you will want to create the folder to hold them. Remember that your DocumentRoot value you entered above must match where your actual document root ends up being on the server. If you are going to be deploying via Git, you don't need to do that: Git will create it when you git clone the repository onto the server from /var/www/example.com/.

Enable the virtual domain
a2ensite example.com

Do this command for each domain or subdomain you're configuring here.

Reload Apache
service apache2 reload

Assuming that you have configured the DNS for your domain to point to your server's IP address, virtual hosting for your domain should now work. Of course, there's still more server prep to do so you can run Drupal....

4. Install supplementary packages

These provide added functionality required or recommended for Drupal.

GD2

This gives your server image manipulation tools like resizing – necessary for Drupal 7's image module to work.

apt-get install php5-gd

pear

apt-get install php-pear
make
apt-get install make

Now we can install things in other ways than via apt-get.

uploadprogress

uploadprogress provides your Drupal UI the upload progress bar, as opposed to the spinner, when uploading a file.

pecl install uploadprogress

Now add the extension to php.ini (the easy way). This is all one line:

echo "extension = uploadprogress.so" > /etc/php5/apache2/conf.d/php.ini 

[Edit: I encountered no problem with the above command. uploadprogress is installed fine, according to status reports. However, acervulus reports that the above command has an incorrect path, and that the correct path should be:

echo "extension = uploadprogress.so" > /etc/php5/conf.d/uploadprogress.ini

If the first version doesn't work, you might try this other one. –Laura]

Next step: Reload.

service apache2 reload
Drush

If you don't know what Drush is, you need to stop right here and learn about Drush. Installation on Linux is easy. First, add the drush channel.

pear channel-discover pear.drush.org

Now install Drush.

pear install drush/drush

Test by typing:

drush

You should get a nice big output of available Drush commands. Yay!

Git

There are many great arguments for deploying code to your server via Git. Installing Git is easy:

apt-get install git

(Git methods for handling site deployment are outside of the scope of this post.)

5. Boost server security

There are many things you can do to help enhance your site security. Here are some handy links:

Configure firewall rules

There's a lot of discussion about how best to configure these rules. I found these posts to be fairly helpful:

Install Fail2Ban

This helps protect your site from hacking attempts.

apt-get install fail2ban

Configure Fail2Ban by entering the following command:

nano /etc/fail2ban/jail.conf

There you can set bantime and maxretry settings.

Once configured, Fail2Ban monitors your log files for failed login attempts. After an IP address has exceeded the maximum number of authentication attempts, it will be blocked at the network level and the event will be logged in /var/log/fail2ban.log.

Secure MySQL

Magic dust that protects your database.

mysql_secure_installation

The default answers to the prompts should be fine.

Secure PHP

More magic dust, this time for PHP.

apt-get install php5-suhosin

Restart Apache2.

service apache2 restart

6. Create your database(s)

Log into MySQL.

mysql -u root -p

The -u defines the mysql user, which in this case is mysql root user, 'root'. You will be prompted for the MySQL root password. When you get a prompt like this:

mysql>

…you're in!

Now create your database. [In this example, the database name is 'foobar', the database user is 'trelayne', and the user password is 'p4ssw0rd'. Change these to the actual database name, database user and passwords you want for your database. Be sure to note these down, because you'll need this info when you set up your Drupal site.]

create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci;

Note the ";" at the end of the line. That is required for MySQL to execute the command.

Define the user and permissions for the database. I'll keep it easy here:

grant all on foobar.* to 'trelayne' identified by 'p4ssw0rd';

Now wrap this up and quit MySQL:

flush privileges;
quit

7. Configure PHP

nano /etc/php5/apache2/php.ini

This is a big file. You will need to search through the file to find these value configurations.

You will want to boost the default memory limit value.

memory_limit: 128M

128MB is the default for Precise Pangolin. If you're running a lot of modules, or some heavy processes, you may need to increase this memory_limit value even higher. I have used 256M on heavier production sites.

Other settings you may want to change are upload_max_filesize and post_max_size, as these limit how big of files you can upload via the Drupal user interface. Note that PHP is not great at handling big uploads, so if you're running an active community site, you may not want to raise the upload limits – in fact, you may want to lower them!

Refer to http://drupal.org/requirements for details and nuances on php settings.

Then restart Apache2.

service apache2 restart

For clean URLs:

a2enmod rewrite

And again:

service apache2 restart

That should cover it. You now can pull in your system files, import your database, point your settings.php file to your database, and load your site via web browser.

More info

IANASA

I am not a systems administrator. This post is a distillation of what I have learned in DIY Linux efforts over the years, plus info I found on the web, plus some trial and error with regard to Ubuntu 12.04 in particular. I cannot speak to the accuracy or quality of the advices here, though. I'm sure there are corrections and additions needed for this section of the post. I welcome your input!

We want to work with you!