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
- You already have Ubuntu 12.04 installed on your server. (Download) (Release notes)
- You know your server's IP address.
- You can point your desired (sub-)domain at your server. Nuances of DNS are not covered here.
- You actually want to set up for Drupal. (These instructions may apply for other LAMP-based CMSs.)
- 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

Setting up Ubuntu for Drupal has never been easier
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:80Set 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/htmlNB: 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:
- http://library.linode.com/securing-your-server#sph_creating-a-firewall
- http://www.thegeekstuff.com/2011/06/iptables-rules-examples/
- http://www.thegeekstuff.com/2011/02/iptables-add-rule/
- [Edit: Ubuntu has a tool available: Uncomplicated Firewall. It's built in to Ubuntu, but disabled by default. https://help.ubuntu.com/community... My thanks to pjcdawkins for the tip! See his linked comment for another tip on
molly-guard.]
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: 128M128MB 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
- My thanks to Tux Tweaks, whose post on this was a great resource during my experiments.
- Another Tux Tweaks post some may find helpful: Installing phpMyAdmin.
- Linode KB on server security.
- Linode KB articles on LAMP setup for numerous distros of Linux.
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!











Comments
Awesome post. Thanks!
Thank you.
Very comprehensive article, one to bookmark for later reference. And excellent mobile website too, one I can actually type in the text box rather than copy/paste from a notepad!
Anyhow, question - will this install Apache 2.2 or 2.4?
Thank you! Mobile-friendliness is one of our mvp requirements now.
This process installs Apache 2.2:
Great article! I did notice, though, that you're using the Drupal wordmark in the sidebar and you have other graphical elements inside the exclusion zone. Might want to take a quick look at http://drupal.org/drupal-media-kit.
My bad. The graphic is merely descriptive.
Anyone looking to experiment with this, I would recommend looking in on Amazon EC2 and related services. You can launch a micro VPS like this in a few minutes and then scale it up using various other cloud services provided by Amazon. The micro instance is free for new users and perfect for running Drupal for a small site.
I think I found an error:
the line:
echo "extension = uploadprogress.so" > /etc/php5/apache2/conf.d/uploadprogress.ini
should be:
echo "extension = uploadprogress.so" > /etc/php5/conf.d/uploadprogress.ini
Thanks. I looked through my history and confirmed that I entered
...and uploadprogress shows as enabled in the Status Report. Is it possible that either path would work?
/etc/php5/apache/conf.d is usually a symlink to /etc/php5/conf.d, so either path should work.
I'm confused about step 4: upload progress (but I'm a linux n00b,so bear with me). Does that line create a document called uploadprogress.ini in the conf.d directory? How is extension = uploadprogress.so added to php.ini?
thanks
I think the path to open the file for editing is:
sudo pico /etc/php5/apache2/php.ini>/code>Then search (CTRL + W) for the <code>File upload
section in that file and add
extension=uploadprogress.solast under that section.
Thanks for this collection of advice!
The simplest firewall tool for Ubuntu is ufw or Uncomplicated Firewall. It's built in to Ubuntu, but disabled by default.
The following will keep the default ports for HTTP, HTTPS and SSH open, and deny everything else. The SSH is important of course, if you're administering a remote server.
Lots more can be found in the ufw help (https://help.ubuntu.com/community...).
sudo ufw allow ssh;sudo ufw allow http;
sudo ufw allow https;
sudo ufw enable;
sudo ufw status;
Also I recommend molly-guard (just run
sudo apt-get install molly-guard). It requires you to enter the server name before shutdown or reboot commands will execute.Thanks for the info! Looks like a powerful and handy way for handling this task. There's obviously a lot of detail when it comes to firewall settings, which is why I didn't get into it in this post, preferring to point to resources. I will update the post.
Out of interest, what's the driver for logging to /var/www/foo/logs? I'd imagine most people would expect to find logs under /var/log - I usually use a pattern such as /var/log/apache2/vhost-foo.access.log
No particular reason, aside from that's the path I've encountered in articles elsewhere. I suppose it's arbitrary.
Wow, what a nice collection of useful updated Drupal+Ubuntu info all in one place! Well done, and very much appreciated!
For anyone interested in the Desktop version of Ubuntu + Drupal, I used a lot of these steps/techniques (and will soon add a few that I missed) for the DrupalPro project, which is a VirtualMachine (aka VirtualBox, VMware, etc) -- built on Ubuntu 12.04 + Unity + LAMP + xdebug, and many other goodies.
Thanks, Mike!
Any reason why installing this way on a brand new rackspace cloud server with 256mb of ram would respond ridiculously slowly? Clean install with very little content and the response time crawls like an injured turtle.
Server performance is affected by any number of things. Speed of processor. Amount of server RAM. Size of swap. Tuning. Caching setups. Re Drupal, modules can affect things, too. I don't know of anything in this setup that would inherently make things slower. I don't know if this applies, but I've read that 64-bit Ubuntu can be slower on smaller instances (less than 4GB RAM).
@Stephen Walsh: is APC installed? If not, run
sudo apt-get install php-apcand try again.I installed APC, and that helped a little bit, but it's still running slow. I'm new to Drupal, so maybe I'll need to take this to a forum, but any basic changes would be helpful. Thanks!
OK firstly, great tutorial.. i followed all the steps. Fantastic thankyou.
I've used memset v1000 with 512MB of ram, Drupal 7 installed with quite a few memory sucking modules (views, context etc)
Sometimes i seem to be having issues with the box running out of memory. (from what it looks like.)
I have to start mysql service again to get it back.
Every 5.0s: free -m Tue Jul 3 13:53:57 2012total used free shared buffers cached
Mem: 429 386 42 0 6 63
-/+ buffers/cache: 317 111
Swap: 0 0 0
I've noticed this get down to '7 free'
Has anyone else encountered this? Any thoughts on what i can try?
Many thanks.
PDOException: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111) in lock_may_be_available() (line 167 of /var/www/david-riches.co.uk/html/includes/lock.inc).this is the error i get.
Solved myself for now.
Turned on gzip compression
sudo a2enmod rewritesudo a2enmod headerssudo a2enmod deflatesudo a2enmod expiresmanage to get a 70% reduction in data savings with these.
oh and make sure you restart apache
service apache2 restartuseful link, see if your site is running deflate/gzip
http://www.gidnetwork.com/tools/g...
nice update Laura
since last time, I screwed up by executing
chown -Ron my entire /var directory instead of just /var/www so had to reinstall Ubuntu (with my Ubuntu One backup)I found this great guide for setting up Ubuntu for Drupa dev http://klau.si/dev and the awesome
sudo aptitude install drupal7but neither that guide nor your update here include installing apc (like your Lucid guide did) so I followed http://www.mcdruid.co.uk/content/...
also following that guide, I could not get my webroot set up in my home dir, so backto /var/www this time I only chowned that and it's fine
they say in love and dev Third Time's the Charm ‡¡‡
Great tutorial. Only one problem. I don't understand what "example.com" relates to in terms of the Drupal 7 folder I have placed in /var/www and localhost. Is localhost example.com? Or is the Drupal 7 folder example.com?
In general, 'example.com' as used in this post should be replaced by the domain of your own site. That said, in the actual path designations where your filesystem resides, you don't really need to use 'example.com' as you can use any path you like. I prefer to use a clear path, as sometimes I may want to develop an alternative version of a site, because it's easy to deploy on a different path and then simply update the virtual host files to take it instantly live ... which calls for keeping the path names clear. You might call your path '/var/www/drupal7/html' or '/srv/sites/version2012/public_html' -- it doesn't matter, as long as you end up pointing to your webroot.
Does this help? Or am I answering the wrong question?
I have your guide bookmarked as a top reference among several others out there.
Unfortunately I see no consensus on naming conventions, specifically TLDs. Or, is it always freestyle in the Drupal community when it comes to naming?
I'm talking about "example.dev" or, maybe better, "example.local" for the localhost.
A post at http://www.wunderkraut.com/blog/y... even suggests "." which I think would make yours "athena.example" if 'example' stands for the project name.
What's smartest, and more vitally, would any of these pose a problem for directory names, Vhosts, .htaccess, or anything else in Apache?
BTW, I am interested mainly in using localdev with Pantheon, which also has on-server dev, adding to the murkiness if I want to switch between them. . . .
Thanks.
Obviously the HTML filter got rid of the brackets I used around the two halves of:
It was supposed to say
in case that wasn't clear in context.
Thanks. In the future you can wrap your code snippets in
<code>foo</code>.Excellent, thanks. As for the best naming practice for your local dev site and especially its extension, here is one train of thought against using .local . . . for one reason that becomes evident:
http://robertjtownsend.com/conten...
It's step 9., BTW.
Q: It's not crystal clear what's best. If your local dev environment uses .dev, and you're pusing that up to an on-server dev tier as well, e.g., Pantheon's, AND .local is a PITA when using dnsmasq . . . then what? Use example.whatever?
Thanks for your opinion!
Thanks so much. This was such a headache trying to figure out. You are a lifesaver.
Thanks for the excellent procedure. BTW, you can skip creating the database by using drush site-install, which can create it for you. Saves a lot of time.
I'm a relative newbie to linux and I am working on upgrading a Drupal site I didn't set up. This tutorial really helped me put all the pieces together for setting up my staging area locally, and I'm well on my way to getting the site up to date. Thanks for putting this out there.
I wish I saw your information a month ago. Most everything I have compiled is on your one page. It would have saved me a month of work. I hope the search engines pick it up and make it a high ranked page. It is rare to see someone who knows so much. See you on Drupal.org. Thanks again...
I don't have Ubuntu 12.04 Server installed. I have the regular Desktop version, and I have installed LAMP and have the database set up properly. But I am getting a blank page when I go to http://localhost/drupal
I just want to be able to experiment with Drupal.
Do I still need to configure named-base virtual hosts?
I have my Drupal files in /var/www/drupal
Not sure what I am doing wrong.
Sorry, I have no experience with the desktop version. Likely you want to edit your hosts file, but what's needed for desktop is something maybe another reader can answer?
FYI: In 12.04, the desktop version is the same as the server version with the GUI and a bunch of extra programs installed like Libre Office and stuff. So the configuration of everything would be the same, just through a terminal window while in the GUI.
Once I start apachi server again after creating the database It logs following error.
The Alias directive in /etc/phpmyadmin/apache.conf at line 3 will probably never match because it overlaps an earlier Alias.So restart fails. All my other sites also does not work now. Please post a fix ASAP!
Sorry, I don't have a ready answer because I don't use phpmyadmin on servers. Perhaps someone else can share an answer here?
Hello. I am a beginner. I think I can follow this tutorial step by step, very clear, but I do not know which is my server IP. Is it my IP address?
I want to run Drupal 7 on my computer with Ubuntu operating system. This tutorial is suitable for what I want?
Thanks in advance for your response
Is this supposed to say php.ini instead of uploadprogress.ini?
Good catch. I will update the post. Thanks!