Drupal coder

Preparing Ubuntu Server for hosting Drupal sites

With the cheap prices of VPS hosting services these days, pretty much anyone can run his own server. Hosting your Drupal site on a VPS has a lot of advantages over running your site on shared hosting, but is a bit more difficult to get started. Using the following guide, you should be able to set up an Ubuntu Server on your VPS pretty easily. You'll install a full system with Apache, MySQL, SSH Server, PhpMyAdmin, ... We'll also configure the system with some minor tweaks that will cause a major performance boost over a barebones install.

Let's get started...

Installation guide

Let's kick off with updating all our Ubuntu packages to the latest versions available:

sudo apt-get update
sudo aptitude safe-upgrade
sudo aptitude full-upgrade

We'll now install the "essentials" metapackage. A nice package of some programs needed on most servers (like gcc, ...). We'll also install curl for running our Drupal cron scripts.

sudo aptitude install build-essential
sudo apt-get install curl

To run and manage Drupal sites, you need Apache, PHP and MySQL. Let's install those with some helper tools. For sending mails from Drupal, we'll install Postfix, a mail server. We'll also install phpMyAdmin. This will allow you to manage your MySQL databases from your browser. It will be accessible on http://your-server-ip/phpmyadmin.

sudo apt-get install apache2 apache2-threaded-dev php5 php5-dev php-pear php5-gd mysql-server-5.0 phpmyadmin postfix

To enable clean urls in Drupal, we need to enable mod_rewrite.

sudo a2enmod rewrite

Let's also disable 2 Apache modules we won't use.

sudo a2dismod cgi
sudo a2dismod autoindex

To run Drupal a bit more comfortably you might want to increase some rather restrictive limits. We'll increase PHP's memory limit to 128Mb and allow upload and post sizes of 128Mb.

sudo sed -i 's/memory_limit = .*/memory_limit = 128M/' /etc/php5/apache2/php.ini
sudo sed -i 's/upload_max_filesize = .*/upload_max_filesize = 128M/' /etc/php5/apache2/php.ini
sudo sed -i 's/post_max_size = .*/post_max_size = 128M/' /etc/php5/apache2/php.ini

Drupal's FileField and Upload module can show a nice upload progress bar. To make this feature available, you need the uploadprogress extension. Let's enable it.

sudo pecl install uploadprogress
sudo sed -i '/; extension_dir directive above/ a\
extension=uploadprogress.so' /etc/php5/apache2/php.ini

Let's tell our server what time it is by setting up our timezone.

sudo dpkg-reconfigure tzdata

Let's finish by making our server a bit more secure. We'll prohibit root from login in over ssh. We'll also remove Apache's server signature you see under each page generated by Apache (like the directory pages, 404's, ...).

sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sudo sed -i 's/ServerSignature On/ServerSignature Off/' /etc/apache2/apache2.conf

Drupal ships with a .htaccess file. To tell Apache to use this .htaccess file, we need to configure AllowOverride.

sudo sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/sites-available/default

We now have all software packages installed and configured. We'll add a few performance enhancements and then restart our server to apply all our configuration changes.

Some performance tricks

One of the easiest methods to make your installation a lot faster is by enabling opcode caching like APC or eAccelerator. I prefer APC on Ubuntu. So let's enable it. Just hit enter to choose the defaults on all questions.

sudo pecl install apc
sudo sed -i '/; extension_dir directive above/ a\
extension=apc.so' /etc/php5/apache2/php.ini

Two client side performance tricks are enabling mod_deflate and mod_expires. mod_deflate will zip your CSS files etc. and save you some bandwith. mod_expires will make the caching on static files work.
We also need to configure mod_deflate to tell it it needs to compress javascript and css files. Drupal does not do this out of the box.

sudo a2enmod expires
sudo a2enmod deflate
sudo sed -i 's/DEFLATE text\/html text\/plain text\/xml/DEFLATE text\/html text\/plain text\/xml text\/css text\/javascript application\/x-javascript/' /etc/apache2/mods-available/deflate.conf

We also make our queries a bit more speedier by enabling query caching in MySQL.

sudo sed -i 's/query_cache_limit       = 1M/query_cache_limit       = 1M\
query_cache_type        = 1/' /etc/mysql/my.cnf

Restarting our services to apply our configuration changes

To finish our setup we apply all our modifications to the MySQL, PHP, Apache and SSH server configuration by restarting Apache, SSH server and MySQL server.

sudo /etc/init.d/apache2 force-reload
sudo /etc/init.d/ssh force-reload
sudo /etc/init.d/mysql force-reload

Adding some management users

We'll now have a nice server for hosting our Drupal sites. But we might want to add some extra users so we don't have to run everything as root. The following will create an admin user with sudo rights. Use this user to do administrative tasks (updating your server, ...). We'll also add an "ftp" user. This user will own all the files in /var/www, which is where we host our site.

adduser admin
visudo 
add admin ALL=(ALL) ALL
adduser ftp
chgrp -R www-data /var/www
chown -R ftp /var/www
sudo chmod -R 2750 /var/www
# NOTE : "sudo chmod -R 2770" for files directories
sudo usermod -a -G www-data ftp

Uploading files

We have not installed an FTP server. I prefer using SSH/SFTP. To access your server with an (S)FTP client, specify port 22 instead of 21 when connecting. Always upload files to /var/www as the ftp user.

Caution

Although these instruction will give you a fully working server for running your Drupal websites, you should be cautious when you want to run a public server like a VPS or dedicated server. Your first step in adding more security might be configuring a firewall using iptables.

Ubuntu Server versions

I've tested this script on Ubuntu Server Hardy (8.04) but chances are it might work on 9.04 (and later) too.

Some VPS recommendations?

I'm running this site on VPS.NET now. I'm very pleased with the ease of setup, backups, features and price. But you might also want to check out some other services like SliceHost, Linode, ...

And if you want to spend a bit more bucks on superior quality and great support, you might want to choose the Belgian hosting service Priorweb.

AttachmentSize
install.sh_.zip1.18 KB
March 21, 2010Drupal, hosting, server management

Comments

Well your my hero. Incredible way to configure appache2 and with only one key stroke in the .htaccess just removed the # from rewrite base, or something like that, and bang, pretty urls.

It sounds like it's not a big deal, but you don't know! Anyway so glad you know so much about these installs. I'm just playing with a linode right now. I mentioned you, but I couldn't find you after the install, so I went through my history and found you there.

thanks so much,

the gypsy

Nice guide :o)

re Adding some management users

sudo usermod -a -G www-data ftp

What does the -a flag do?

when i enter a command like sudo sed -i then its like the server is putting me on a notepad. I can write but he doenst recon any commands

Drubuntu is not really intended to set up a production server. It's fantastic to get you up and running with an awesome development box in 20 minutes or less.

Thanks so much for posting this. I had missed a number of these steps.

One thing I've had issues getting configured for Drupal/Ubuntu from the command line is getting the correct version of the GD library that is required by ImageAPI. I've tried re-compiling it a couple of times (following the instructions at: http://is.gd/aUKST), but haven't had luck getting the updated libraries to "stick". Here's the message that I see when I go to the Status Report for my Drupal site:

"The installed version of PHP GD does not support image filtering(desaturate, blur, negate, etc). It was probably compiled using the official GD libraries from http://www.libgd.org instead of the GD library bundled with PHP. You should recompile PHP --with-gd using the bundled GD library. See http://www.php.net/manual/en/image.setup.php. An implementation of imagefilter in PHP will be used in the interim."

Any chance you could provide steps to update these libraries from the command line?

Looks good,

However I'd recommend nginx as the server instead of apache.
It is much more resource friendly than apache.

a do it all script built by one of the drush maintainers is at http://drupal.org/project/drubuntu

Thanks for the link. I also can recommend http://articles.slicehost.com/ubuntu-hardy for further reference. I might do a follow up article on virtual hosts too.

Hi, for iptables setup and some tips about backups your readers can take a look here:
http://ao2.it/wiki/How_to_setup_a_Debian_server_on_gandi.net
Check also the other HOWTOs on my wiki.

If I may, in order to configure php extensions you can put new files (e.g. uploadprogess.ini, apc.ini) in /etc/php5/conf.d/ and they will be sourced with no need to change php.ini. Not sure if this mechanism is in the ubuntu version you are using, it surely is in plain Debian.

I am using Gandi.net as VPS provider and it is quite good.

Regards,
Antonio

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options