Set up a new LAMP server

Error message

Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 728 of /home/befused/public_html/modules/comment/comment.module).

Here are some notes on setting up a new Linux server with PHP, MySQL, Apache and related software. This is not a definitive list of everything you need to install, but should be enough to get you started.

Update and Installation

Bring site up to date

  1. apt-get update
  2. aptitude safe-upgrade
  3. aptitude full-upgrade

Install Essentials metapackage & curl

  1. aptitude install build-essential
  2. apt-get install curl

Install Apache, PHP, Pear, MySQL, phpmyadmin and postfix (for emails).

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

Install Subversion and/or Git

  1. apt-get install subversion git

Enable mod_rewrite, which Drupal requires for clean URL's

  1. a2enmod rewrite

Disable cgi and autoindex

  2. a2dismod cgi
  3. a2dismod autoindex

Increase PHP memory limit, upload max filesize and post max filesize. These limits should work for Drupal, and I'd imagine other platforms/frameworks.

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

Install upload progress. This is helpful for the progress bar in Drupal modules such as FileField and Upload

  1. pecl install uploadprogress sed -i '/;
  2. extension_dir directive above/ a\'
  3. /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini

Set the server timezone.

  1. dpkg-reconfigure tzdata

Tell Apache to use Drupal's .htaccess

  1. sed -i '/AllowOverride None/AllowOverride All/' /etc/apache2/sites-available/default

Add users

We shouldn't really SSH in as root (in fact, we have already disabled root access), so we need to add some users.

  1. adduser yourname visudo

Add the following:

  1. yourname ALL=(ALL) ALL

Obviously change yourname in the two examples above to your actual name!


Restart Apache, SSH and MySQL

  1. /etc/init.d/apache2 force-reload
  2. /etc/init.d/ssh force-reload
  3. /etc/init.d/mysql force-reload


Prohibit root login over SSH

  1. sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config

Remove Apache signature from Apache generated pages like 404 pages.

  1. sudo sed -i 's/ServerSignature On/ServerSignature Off/' /etc/apache2/apache2.conf

There is a good tutorial on the Linode website on how to set up a basic firewall, so check it out:


Install APC, which is a PHP accelerator.

  1. pecl install apc
  2. sed -i '/; extension_dir directive above/ a\' /etc/php5/apache2/php.ini

Enable mod_expires

  1. a2enmod expires

Zip files (CSS etc) with mod_deflate to save bandwidth

  1. a2enmod deflate

Configure mod_deflate so it compresses Javascript and CSS files.

  1. sed -i '/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

Enable MySQL query caching

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