The Almost Perfect Joomla! Server

Traditionally Joomla! has been run in a LAMP setup. Partly because this is how it WAS always done and party because people didn’t know what alternatives there were. Here I would like to present what I consider to be a far better alternative using Ubuntu Server, NGINX, PHP(fastcgi) and mySQL. The result should be a faster Joomla! site.

The one assumption is that you already have a vanilla install of Ubuntu Server, this tutorial was based on Karmic Koala (9.10).

Before we begin lets run through what we will actually be doing.

  • Install PHP
  • Configure PHP FastCgi
  • Install mySQL
  • Install NGINX
  • Configure NGINX
  • Create the mySQL database for Joomla!
  • Setup the Joomla! environment

To get started you need to be connected to your server either directly or via SSH.

Install PHP

Here we install PHP as well as a number of modules that are commonly used.

aptitude install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-xcache php5-mhash php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json

As you may have noticed we installed php5-xcache. For this to take effect we need to edit the php.ini file a little

[xcache-common]
extension = xcache.so
[xcache.admin]
xcache.admin.user = "mOo"
; xcache.admin.pass = md5($your_password)
xcache.admin.pass = ""
[xcache]
; ini only settings, all the values here is default unless explained
; select low level shm/allocator scheme implemenation
xcache.shm_scheme =        "mmap"
; to disable: xcache.size=0
; to enable : xcache.size=64M etc (any size > 0) and your system mmap allows
xcache.size  =                64M
; set to cpu count (cat /proc/cpuinfo |grep -c processor)
xcache.count =                 1
; just a hash hints, you can always store count(items) > slots
xcache.slots =                8K
; ttl of the cache item, 0=forever
xcache.ttl   =                 0
; interval of gc scanning expired items, 0=no scan, other values is in seconds
xcache.gc_interval =           0
; same as aboves but for variable cache
xcache.var_size  =            64M
xcache.var_count =             1
xcache.var_slots =            8K
; default ttl
xcache.var_ttl   =             0
xcache.var_maxttl   =          0
xcache.var_gc_interval =     300
xcache.test =                Off
; N/A for /dev/zero
xcache.readonly_protection = Off
; for *nix, xcache.mmap_path is a file path, not directory.
; Use something like "/tmp/xcache" if you want to turn on ReadonlyProtection
; 2 group of php won't share the same /tmp/xcache
; for win32, xcache.mmap_path=anonymous map name, not file path
xcache.mmap_path =    "/dev/zero"

; leave it blank(disabled) or "/tmp/phpcore/"
; make sure it's writable by php (without checking open_basedir)
xcache.coredump_directory =   ""
; per request settings
xcache.cacher =               On
xcache.stat   =               On
xcache.optimizer =            On
[xcache.coverager]
; per request settings
; enable coverage data collecting for xcache.coveragedump_directory and xcache_coverager_start/stop/get/clean() functions (will hurt executing performance)
xcache.coverager =          Off
; ini only settings
; make sure it's readable (care open_basedir) by coverage viewer script
; requires xcache.coverager=On
xcache.coveragedump_directory = ""

Configure PHP FastCgi

We set PHP FastCGI to run automatically as a system service by creating a small script

vi /etc/init.d/php-fastcgi

note: the line above will open a new file in vi (editor). From there type "I" to go into insert mode. Paste the next bit in then click "esc" to leave edit mode. Finally type ":wq" and enter to write the file and close vi.

Copy the following into your script.

#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=5
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

Make it executable

chmod +x /etc/init.d/php-fastcgi

Make sure it stays there

update-rc.d php-fastcgi defaults

Start it

/etc/init.d/php-fastcgi start

We now have PHP FastCGI set up.

Install  & Configure mySQL

Install mySQL
aptitude install mysql-server mysql-client

During this install you will be asked for a root user password and then to confirm it.

If not for any other reason than securiy it prefer to have a non-root users for the Joomla! database. So next we create a new mySQL user and database for Joomla! and then grant the new user full privilages on the Joomla! database.

Log into mysql as root (Enter the password you entered during the installation of mySQL when prompted)

mysql -u root -p

create new user where username is the username you want to use and password is the password you want to use.

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

Create the db for Joomla!. I have assumed the database will be called joomla

CREATE DATABASE joomla;

Give the new user permissions on the Joomla! database

GRANT ALL ON joomla.* TO 'username'@'localhost';

Thats mySQL all setup, leave the mysql environment with

exit

Install & Configure NGINX

Install NGINX via aptitude.
aptitude install nginx

The install on NGINX doesn't start NGINX so we run the following to get up running

/etc/init.d/nginx start

At this point you should have a working install of NGINX. By opening a browser and pointing to your server you should be greeted with "Welcome to nginx!"

Configure NGINX

For this we assume that the server is running a single instance of Joomla! hence there is only one virtual host although to have multiple is not hard.

vi /etc/nginx/sites-available/default

The following is a typical NGINX conf file for a Joomla! site. Looking through it you should find it fairly self explanatory. Two bits worth making special note of are the #Rewrite and #Static Files configuration. The rewrite config will provide your Joomla! site with nice URL's whilst the Static Files config tells NGINX what mime types to serve directly and sets the expires.

server {
  listen      80;
  server_name website.com www.website.com;

  access_log  /var/log/nginx/website.com-access.log;
  error_log   /var/log/nginx/website.com-error.log;

  large_client_header_buffers 4 8k; # prevent some 400 errors

  root        /var/www/website.com/public_html;
  index           index.php index.html;
  fastcgi_index   index.php;

  location / {
    expires 30d;
    error_page 404 = @joomla;
    log_not_found off;
  }

  # Rewrite
  location @joomla {
    rewrite ^(.*)$ /index.php?q=last;
  }

  # Static Files
  location ~* ^. .(jpg|jpeg|gif|css|png|js|ico)$ {
    access_log        off;
    expires           30d;
  }
  # PHP
  location ~ \.php {
    include /etc/nginx/fastcgi_params;
    keepalive_timeout 0;
    fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_pass    127.0.0.1:9000;
  }
}

By default the nginx install created a nginx-default virtual host but I prefer to have mine setup per-domain just to keep everything tidy.

To start with we create a new location

mkdir /var/www/website.com/public_html

Now its time to test things. To do this we create a file calling phpinfo()

vi /var/www/website.com/public_html/index.php

Into this paste the following

Save it and now direct your browser to your server as was done earlier. All going well you should now have a heap of information about your php install.

We are now ready to install Joomla!, to make thing's a little easier we shall go to the site root.

cd /var/www/website.com/public_html

Install Joomla! (in this case 1.5.15)

wget http://joomlacode.org/gf/download/frsrelease/11396/45609/Joomla_1.5.15-Stable-Full_Package.tar.gz

Next we unpack it

tar -zxvf Joomla_1.5.15-Stable-Full_Package.tar.gz

Once that is complete its time to run the Joomla! installer

Navigate your browser to the site again, you should be presented with the Joomla! installer. Follow this until you get to the Finish page. At this point you need to delete the installation folder, so back in terminal

rm -R installation

It's also a good idea to delete the downloaded Joomla! package

rm Joomla_1.5.15-Stable-Full_Package.tar.gz

at this point I like to set the permissions

chown -R www-data /var/www/website.com/public_html
chgrp -R www-data /var/www/website.com/public_html
chmod -R 700 /var/www/website.com/public_html

Thats it, all going well you should now have Joomla! at the point where you can open a browser, pont to your site and be met with the Joomla! installation procedure.

 

 

blog comments powered by Disqus

Categories

Client Login



Client Support