Search

Debian - Tune Apache/PHP/MySQL for a Compact Flash disk

Contents[Hide]

Debian

After having tuned your Debian server according to the article Install Debian on a server with a Compact Flash main disk, you plan to use it as a LAMP web server.

Once you have installed all the Apache, Php & MySQL packages with aptitude, you then need to do some specific tuning on these packages configuration as your server is running on Compact Flash.

This article describes all the ajustments I've done on my server to setup :

  • Apache to deal with /var/log on tmpfs
  • PHP to save sessions on tmpfs
  • MySQL to fully load the database in memory (small one)

This setup is specific to my needs and has been done to minimize the disk access to the CF.

1. Install the Packages

First thing to do is to install all the LAMP packages :

# aptitude install apache2 php5 mysql-server php5-mysql libapache2-mod-php5
# aptitude install libapache2-mod-proxy-html

Once done, the tuning of the different LAMP modules starts.

2. Setup Apache

2.1. Enable RAM disk

As /var/log is a tmpfs filesystem, we have to setup a script that will create the minimum /var/log/apache2 structure needed by Apache at every boot.

This is done in the file /etc/init.d/apache2-tmpfs

/etc/init.d/apache2-tmpfs

#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          apache2-tmpfs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Required-Start:   
# Required-Stop:    
# Short-Description: Create /var/log/apache2/error.log on tmpfs at startup
# Description:       Create /var/log/apache2/error.log needed by Apache.
### END INIT INFO

#
# main()
#
case "${1:-''}" in
  'start')
   # create the /var/log/apache2/error.log needed by apache
   mkdir /var/log/apache2
   chmod 777 /var/log/apache2
   touch /var/log/apache2/error.log
   chmod 777 /var/log/apache2/error.log
   ;;
  'stop')
   ;;
  'restart')
   ;;
  'reload'|'force-reload')
   ;;
  'status')
   ;;
  *)
   echo "Usage: $SELF start"
   exit 1
   ;;
esac

That script has to be declared for startup (S90) & shutdown (K10) before Apache

# update-rc.d apache2-tmpfs defaults 90 10

One more thing is to enable the most common Apache modules.

# a2enmod rewrite proxy proxy_html headers

2.2. Enable compression

To enable compression in Apache 2, you first need to enable the mod_deflate module :

# a2enmod deflate

In debian, by default, compression will be done for HTML, CSS, JavaScript and XML files.
If you need to modify this setting, you have to edit /etc/apache2/mods-enabled/deflate.conf.

3. Setup PHP

3.1. Enable RAM disk

PHP creates a session record for every connexion. These records have to be moved to /tmp. So, edit /etc/php5/apache2/php.ini as follow :

/etc/php5/apache2/php.ini

...
[Session]
session.save_path = /tmp
...

3.2. Enable compression

To enable GZIP compression for all files served by PHP scripts, you need to edit /etc/php5/apache2/php.ini and modify the following values :

/etc/php5/apache2/php.ini

...
output_buffering = 32768
...
zlib.output_compression = on
...
zlib.output_compression_level = 6
...

Once it's done, you just need to restart the Apache server

# /etc/init.d/apache2 restart

4. MySQL

The MySQL database will be accessed and updated very often, so it will generate some important traffic to the CF drive.

If you do not plan to have some huge database, it is possible to locate the whole database in memory in a tmpfs filesystem. If you do so, you have to load it from disk at startup and to save it back to disk at shutdown or reboot.But with that approach, you have to understand that in case of power failure, any modification to your database since the last proper shutdown will be lost. The last good database backup will be reloaded at startup.

If you plan to do so, first thing to do is to stop MySQL

# /etc/init.d/mysql stop

Then, add the new tmpfs filesystem in /etc/fstab to hold the MySQL database

/etc/fstab

...
tmpfs          /var/lib/mysql       tmpfs      defaults,noatime            0     0

Create the file /etc/init.d/mysql-tmpfs

/etc/init.d/mysql-tmpfs

#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          mysql-tmpfs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Required-Start:   
# Required-Stop:    
# Short-Description: Create the mysql work environment at startup
# Description:       Create the links to hdd and the needed tmpfs files.
### END INIT INFO

#
# main()
#
case "${1:-''}" in
  'start')
   # load /var/lib/mysql from /permanent/mysql/lib/mysql
   /usr/bin/rsync -ArpEogt --delete /permanent/mysql/lib/mysql /var/lib
   # create the /var/run/mysql/... needed by mysql
   mkdir /var/run/mysqld
   chown mysql:root /var/run/mysqld
   #create the mysql-bin.log
   mkdir /var/log/mysql
   touch /var/log/mysql/mysql-bin.log
   chown mysql:root /var/log/mysql
   chown mysql:root /var/log/mysql/mysql-bin.log
   ;;
  'stop')
   if [ ! -d /permanent/mysql ] ; then mkdir /permanent/mysql ; fi
   # unload /var/lib/mysql to /permanent/mysql/lib/mysql
   if [ ! -d /permanent/mysql/lib ] ; then mkdir /permanent/mysql/lib ; fi
   /usr/bin/rsync -ArpEogt --delete /var/lib/mysql /permanent/mysql/lib
   ;;
  'restart')
   ;;
  'reload'|'force-reload')
   ;;
  'status')
   ;;
  *)
   echo "Usage: $SELF start|stop"
   exit 1
   ;;
esac

Declare it for startup (S18) & shutdown (K22)

# update-rc.d mysql-tmpfs defaults 18 22

The configuration is over. You now need to reboot the server to :

  1. Shutdown & save the actual database to /permanent
  2. Startup, create the tmpfs & load the database from /permanent

So, just reboot the server and everything will be operational.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus