[Update: We've recently posted steps for setting up Ubuntu 12.04 Pangolin for Drupal 7.]
We're frequently setting up servers for development, for staging, for production. I've lately preferred the Debian flavor of Linux, but up until now that had been something of a problem because Debian and Ubuntu did not include the higher-quality php5-gd library, which meant that you either had to compile your own PHP, pull from an alternative source host, or cope with substandard image resizing with limited processing features.
But now we have Ubuntu 10.4 LTS "Lucid" and life is good. Lucid comes with PHP 5.3.x and the proper GD2 library! (Cheers. Applause.)
Still, there's a lot of little steps to set up to get your server (or virtual private server) up and running and ready for Drupal. It's not hard, just detail-oriented work you don't want to do when you're bleary-eyed at the end of the day. In my repetitions of doing this over and over, I've collected some notes, and I thought I'd post them here for my own reference, and perhaps your reference as well. My thanks to our own Brian Vuyk for guidance on the APC installation.
[Edit: Brian also pointed me to Taskel as another way to go. I've not tried it that way yet, so this post doggedly sticks to the step-by-step for now.]
Assumptions
- You already have Ubuntu 10.4 installed on your server.
- You know your server's IP address.
- You already have root or sudo access.
- You want to set up for Drupal. (Of course, many CMSs and blog platforms will have similar requirements, and this post may have some appeal and relevance to people using those tools. But in this post I'm aiming at the particulars for Drupal.)
- You have a computer with shell access to your server, or a properly configured *AMP setup on your computer. (This post does not cover MAMP, WAMP, DAMP or commandline bootcamp.)
Step by Step
-
Initial Preparation
-
Create a public key.
If you do this, you can connect to your server without having to log in. It's also necessary if you want to deploy from a provisioning system or repository like GitHub.
ssh-keygen -t rsa -C "yourname@example.com"
Note: For deployment purposes, you need to generate the key as the user who will be executing the checkouts. For example, if you checkout as root, you will need to be logged in as root when generating the key.
To print out the key (to copy and paste into GitHub, for example):
cat ~/.ssh/id_rsa.pub
-
Install security updates.
FIrst things first.
apt-get update apt-get upgrade --show-upgraded
Note that if you are using a regular shell user account with sudo, you will want to prepend pretty much all commands in this guide with "sudo". For example:
sudo apt-get updatewould be the first command above. -
Set your hostname
You should give your machine a name. This is a machine name for your server. It does mean anything about your domain or what URL you want to use for your site. For this example, we'll use the name "athena":
echo "athena" > /etc/hostname hostname -F /etc/hostname
Next, edit the file "
/etc/hosts". There are a few options for text editor within Linux. I find nano to be easy to use.nano /etc/hosts
[NB: In these instructions, your server's IP address is represented by "12.34.56.78". Every time you see that sequence in the examples here, you should replace them with your server's IP address. Also replace "athena" everywhere it appears with your server's name, and "example.com" with your actual domain.]
127.0.0.1 localhost.localdomain localhost
12.34.56.78 athena.example.com athenaathena.example.com is your "fully qualified domain name" or FQDN. (You will come across this from time to time.)
-
Set your server's base timezone
This part is easy:
dpkg-reconfigure tzdata
You will get pop-up prompts for country, etc. and that's done!
-
-
Set up VirtualHost stuff
-
Install Apache
First things first.
apt-get install apache2
-
Edit
/etc/apache2/ports.confnano /etc/apache2/ports.conf
Replace *:80 with your IP address, so it looks like this:
NameVirtualHost 12.34.56.78:80 -
Edit
/etc/apache2/sites-available/defaultnano /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 -
Configure name-based virtual hosts
Create a file in
/etc/apache2/sites-available/
for each of your sites on the server.
nano /etc/apache2/sites-available/example1.com
<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 multisite setup will share the same DocumentRoot value.
Git note: If you are going to deploy using Git (covered in the next step below), you want to define your DocumentRoot to include your Git repository name and any internal paths to your Drupal document root. 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 would be /var/www/example.com/foobar/html/.
-
Create your website folders
-
First, create the folder for your logs.
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 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 into /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
/etc/init.d/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....
-
-
Build PHP, MySQL and the goodness Drupal loves
-
MySQL Installation
First install the thing.
apt-get install mysql-server
You will be prompted to create the MySQL root password, and re-enter it.
Now we secure the installation.
mysql_secure_installation
You will be prompted to set/change the MySQL root password and other things. You won't need to change the MySQL root password, but you probably want to answer "Y" yes to the other questions.
-
Create your database
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 'rumpole', 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;
[Update: You need to set the CHARACTER SET and COLLATE values, as shown above, or MySQL will default to non-recommended latin1 / latin_swedish_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 'rumpole' identified by 'p4ssw0rd';
Now wrap this up and quit MySQL:
flush privileges; quit
-
Install PHP
Use apt-get to pull down the packages.
apt-get install php5 php5-dev php-pear php5-gd
Note that php5-dev is not necessarily required, except you will need it later for the PECL installation of UploadProgress (which is very nice to have for the Drupal user interface). php5-gd is to enable nice image handling.
-
Configure the
php.inisettings.Edit the appropriate php.ini file:
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 Lucid. If you're running a lot of modules, or some heavy processes, you may need to increase this memory_limit value even higher.)
Now, while you in php.ini, make sure that the following lines are uncommented and have proper values established.
max_execution_time = 30
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
display_errors = Off
log_errors = On
error_log = /var/log/php.log
register_globals = Off
safe_mode = Off
session.cache_limiter: nocacheYou will also need to add lines for PDO support. Find the Dynamic Extensions area in the file, and add these lines:extension=php_pdo.dllextension=php_pdo_mysql.dll[Edit: You don't need to add these in Ubuntu 10.04. As it happens, Lucid comes with PDO extensions already enabled. Thanks to Peter Wolanin for pointing out in comments below what should have been obvious about .dll extensions in Linux. #facepalm]
Refer to http://drupal.org/requirements for details and nuances on php settings.
To have these take effect, restart apache.
/etc/init.d/apache2 restart
-
Install PHP MySQL and Security Packages
-
Let's start with MySQL.
apt-get install php5-mysql
-
Update your sources.
Now be sure the following lines in
/etc/apt/sources.listare uncommented:nano /etc/apt/sources.list
deb http://us.archive.ubuntu.com/ubuntu/ lucid universe
deb-src http://us.archive.ubuntu.com/ubuntu/ lucid universe
deb http://us.archive.ubuntu.com/ubuntu/ lucid-updates universe
deb-src http://us.archive.ubuntu.com/ubuntu/ lucid-updates universedeb http://security.ubuntu.com/ubuntu lucid-security universe
deb-src http://security.ubuntu.com/ubuntu lucid-security universe -
Update what you have so far.
apt-get update
-
Boost security a bit.
Now install the php5-suhosin package to provide additional security.
apt-get install php5-suhosin
-
Restart apache.
To have these take effect, restart apache.
/etc/init.d/apache2 restart
-
-
Enable mod_rewrite
This is easy peasy in Ubuntu because the PHP module is already installed; you just need to enable it:
a2enmod rewrite
And restart apache again:
/etc/init.d/apache2 restart
-
Install PECL UploadProgress.
This allows you to have that nifty upload progress display when uploading files. It's not required, but is useful user feedback, which I recommend highly.
-
First, update.
apt-get update
-
Run the PECL installation of UploadProgress.
pecl install uploadprogress
-
Now save the setting.
Note: This is all one line!
echo "extension = uploadprogress.so" > /etc/php5/apache2/conf.d/uploadprogress.ini
-
Reload Apache to have it take effect.
/etc/init.d/apache2 reload
Done!
-
-
-
Optional
-
Install Git
If you want to use Git to deploy your site code, you will need to install Git itself.
apt-get install git-core
-
Install Drush and Drush Make
Drush installation instructions are in the Drush readme.txt file.
-
Install APC
apt-get install libpcre3-dev pecl install apc
Then edit your php.ini file to add to the extensions area:
; APC
extension=apc.so;
apc.shm_size=64M;Then you can copy over a php file that offers some nice stats:
cp /usr/share/php/apc.php /var/www/example.com/html/apc.php
Note that the path of the destination is your webroot.
Then restart apache.
/etc/init.d/apache2 restart
And you're done. You will be able to get some nice APC stats at http://example.com/apc.php. (You can always protect that file via .htaccess to put an authentication password on it if you like.)
-
Install fail2ban
Fail2ban is a nice little security addition.
apt-get install fail2ban
That's all on that.
-
Install bash-completion
Bash-completion is a new one for me, and is turning out to be very handy. How did I go so long in the dark?
apt-get install bash-completion
-
And that's it. Now your system is ready for site deployment to the folder you defined. Note that after you've deployed your site you will also want to set up a cron job.
IANASA
I am not a sysadmin by profession. This is just documentation of what I do for my own projects, or when tossing up a staging server for one of our in-house or client projects. Any tips for improvement, corrections, etc. are most welcome.
Update: Clean URLs problem?
Recently I've run into an added complication with setting up Lucid: clean urls were not working. rewrite_module was enabled, .htaccess was there, but no go. As it turns out, .htaccess was not being read by default. If this happens to you, here's a fix:
Edit the default site configuration in sites-available:
nano /etc/apache2/sites-enabled# 000-default
A few lines down, under the directory pointing to you docroot, you need to change "AllowOverride None" to "AllowOverride all".
<Directory /var/www/[pathtoyourwebroot]>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>Now restart apache.
/etc/init.d/apache2 restart
And you're done. Clean urls should work for you now.
For more information on this, see http://drupal.org/node/945860. To read about other ways to configure Apache and avoid the performance hit of using .htaccess, see http://drupal.org/node/43788.












Comments
Hey Laura, nice article!
Do you actually develop using PHP 5.3 though? I've been sticking to Ubuntu Karmic for the time being because I'm too lazy to pin PHP to 5.2 in Lucid/Maverick and am scared of the warning-fest that will happen with a couple contribs in 5.3.
Hiding the error reporting is nice for production but doesn't it hurt your dev?
What's your secret? :)
-- Alex
So far we haven't run into problems with php 5.3, and having the better GD2 library counts for much. I'm agnostic on error levels. I know that drupal.org/requirements lists just
E_ALL & ~E_NOTICE. Do you have a recommendation?After reading this article I decided to go for it and upgrade to Maverick on my work laptop. PHP 5.3 is actually not so bad without E_STRICT but I use that for other dev projects so I decided to go ahead and downgrade. I tried a bunch of tips but nothing worked and in the end kinda stumbled upon the fix... I'll try to write about it when I have time! :)
Alex
Rather than 'pecl install uploadprogress', use the dh-make-pecl utility to create a php5-uploadprogress.deb file and install that:
... and the associated ini file snippet will be put in /etc/php5/conf.d
Thanks for posting this! Interesting. Could you explain what the advantage is to this approach?
The primary benefit to this is it lets you act on apc as you would any other debian package. Which means it could be completely removed, or updated without going hunting for all the files. ;)
Are windows instructions intermixed? This:
extension=php_pdo.dll
extension=php_pdo_mysql.dll
doesn't make sense. A linux server would want .so extensions as indicated elsewhere in the writeup.
Hmm. Good question! I got those instructions directly from http://drupal.org/requirements and have been using them for a quite a few weeks now, without apparent problems.
[edited to add:]
As it turns out, there's a pdo.ini file in /etc/php5/apache2/conf.d/ that has the line
extension=pdo.so, which seems to indicate that Lucid has PDO working by default.[/edit]
Other PDO lines in php.ini that are in php.ini by default:
pdo_mysql.cache_size = 2000pdo_mysql.default_socket=
If this is incorrect and these extensions should be different,So it appears the PDO step in these instructions is not necessary, and the .dll files are invisible, as would be expected. (It has been a long time since I played with Windows registries.) I need to update not only this post but the requirements page on d.o as well.Thanks, Peter! I've updated the post, and also updated http://drupal.org/requirements.
Hi Laura,
Thanks for a great article which is very useful!
What I would like to do is use Git like you mention in the article - Deploy via a Git repository - I would love to know how to do this but the tricky part for me is that I'm using Drupal with multi-site set-up.
Any chance of you doing an article on this?
Regards,
JJ
One of the best "How to LAMP Stack" I've ever read.
Thank you! :)
Just wondering, what exactly is the "better" GD library?
Short answer: The GD library was upgraded to GD2 years ago, However, it was a compiled version and Debian distributions of Linux treated it as a fork so you could not install it via apt-get. You had to compile your own php in order to get GD2 with all the nifty new features, and boy that can be fun! (Drupal will give you a warning on the status screen if you don't have the proper GD2 version to support all those great things imagecache/D7 image handling can offer.)
With php 5.3, Debian/Ubuntu shipped with GD2 available. No more compiling!
For more, here's the (surprisingly brief) Wikipedia article on it: http://en.wikipedia.org/wiki/GD_G...
As I understand it there is nothing substandard about the built-in GD library. It's just that advanced filters like blur and saturate are not included. Following your procedure here won't create higher-quality image derivatives. If that's what you're after you should use ImageMagick instead.
Good point! Yes, there's always ImageMagick. It's hard to find meaningful benchmarks, but much that I've read indicates that IM is generally slower. However, with cached images that performance cost (if real) is a one-time thing for each resized version.
For those following the instructions, here's how you'd install ImageMagick:
Thanks, dalin!
Great article. I'm trying it out on a VirtualBox.
Do you go further with your setup? Drush, Memcache, Varnish, Solr? That's where I find it hard to get a grip on. Maybe you'll find the time to write a followup on those.
Thanks, Jan. Drush installation instructions are linked to above. You can always check out my post on installing Drush on Mac for a step-by-step, which is going to be essentially the same as on Linux.
On live sites, Memcache, Varnish and Solr are standard components for us. Memcache and Varnish require a bit more hands-on installation/configuration. Solr requires a Java environment, which takes a bit more setup. We may blog on these in the future (we have our hands full right now), but currently there's some great documentation on all of these on Drupal.org and Groups.Drupal.org.
So when installing the apache2 meta package on lucid, it looks like it is installing the apache2-mpm-worker.
Is that good? I was under the impression that prefork is what you would want unless you are using fastcgi or php-fpm.
So for example if before even installing apache2, you instead just do sudo apt-get install php5, it wants to install apache2-mpm-prefork.
In fact, I don't want to test it, but I wonder if when you install php5 if it isn't just uninstalling mpm-worker and putting mpm-prefork in it's place.
i think that an apc cache size of 64M is too low when running multiple drupal sites. When modifying the apc cache size check your shmmax. Check it anyway because the default value could be 32M
sysctl kernel.shmmax
(if this is lower than your apc cache value)
raise it:
kernel.shmmax = 96000000" in /etc/sysctl.conf
if you don't want to reboot your machine. Change the shmmax on the fly:
sysctl -w kernel.shmmax=96000000
Help needed on performance optimization.
Drupal 7 openpublic admin pages are very very slowly executed.
OpenPublic adds large amount of functionality on top of Drupal core, it needs more resources than bare-bones Drupal. Detailed list of recommended configurations is as follows:
· Minimum of a dual-core processor with 2GB RAM (Linux) or 4GB RAM (Windows). Actual hardware requirements will vary depending on the amount of content and traffic.
-Dual-core with 1 GB of RAM. Should be more than enough for a few test users. Adding memory did not affect performance earlier.
·Web Server - NginX, Apache 1.3 or Apache 2.x. Drupal can also run on Microsoft IIS. Apache is the most tested platform.
-Apache 2.2.14
·PHP – Version 5.2.x or 5.3
-PHP 5.3
·Database – MySQL 4.1 or 5.x, though MySQL 5.1 is strongly recommended.
-MySQL 5.1
·Installing a PHP code optimizer like APC is highly recommended, especially for production use.
-APC installed
·Your memory_limit variable, in php.ini needs to be at least 128M, but 190M is recommended.
-Set to 190M just now.
I just followed your instructions yesterday and my server is running great, thanks!
However, I just did a security scan of my site and the scan complains that my version of PHP is not the latest...
PHP is prone to multiple memory corruption and buffer overflow security vulnerabilities.
PHP Versions Prior to 5.3.3/5.2.14 are affected
IMPACT :
An attacker can exploit these issues to execute arbitrary code, gain access to sensitive information, and bypass security restrictions. Other attacks are also possible.
SOLUTION :
The vendor has released PHP Version 5.3.3 and 5.2.14 to address these issues. It is available for download from the PHP Download Web site.
My php version is
PHP 5.3.2-1ubuntu4.9 with Suhosin-Patch (cli) (built: May 3 2011 00:43:34)
However, it won't upgrade...
sudo apt-get upgrade php5
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
I'm stumped. Your thoughts?
Sorry, Dan, I'm not a sysadmin. However, did you try running just
and then reloading apache via
? I don't think you need to run upgrade for a security release.
Thanks for the help, unfortunately that didn't help, my apt-get thinks php 5.3.2 is most recent. I think this is for another day.
I neglected to include the essential step:
Run this after the update command (which refreshes the list of things to upgrade). If there's a kernel upgrade, you'll want to reboot the server afterwards.
Excellent article. Help me a lot. Thank you.
wow Laura this is great info
I decided to dive into a Linux dev environment after I was unable to use ffmpeg in Drupal on Windows 7
Unfortunately the first guide for Drupal dev on Ubuntu (NOT Drubuntu) I found made me put a symlink to /var/www/ (must be an OLD release issue) so following your guide, in the middle I reinstalled Apache, PHP, and PHPMyAdmin in a more sensible config.
Also I put your php.ini settings at the end of the file so I can tweak them easily later, just remember to comment out the earlier declarations; for dev I usually use much higher settings in Xampp we shall see.