Wednesday, June 12, 2013

The Almost Perfect Joomla! Server

Written by Shayne Bartlett


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.

view source

print?

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

view source

print?

01 [xcache-common]

02 extension = xcache.so

03 [xcache.admin]

04 xcache.admin.user = “mOo”

05 ; xcache.admin.pass = md5($your_password)

06 xcache.admin.pass = “”

07 [xcache]

08 ; ini only settings, all the values here is default unless explained

09 ; select low level shm/allocator scheme implemenation

10 xcache.shm_scheme = “mmap”

11 ; to disable: xcache.size=0

12 ; to enable : xcache.size=64M etc (any size > 0) and your system mmap allows

13 xcache.size = 64M

14 ; set to cpu count (cat /proc/cpuinfo |grep -c processor)

15 xcache.count = 1

16 ; just a hash hints, you can always store count(items) > slots

17 xcache.slots = 8K

18 ; ttl of the cache item, 0=forever

19 xcache.ttl = 0

20 ; interval of gc scanning expired items, 0=no scan, other values is in seconds

21 xcache.gc_interval = 0

22 ; same as aboves but for variable cache

23 xcache.var_size = 64M

24 xcache.var_count = 1

25 xcache.var_slots = 8K

26 ; default ttl

27 xcache.var_ttl = 0

28 xcache.var_maxttl = 0

29 xcache.var_gc_interval = 300

30 xcache.test = Off

31 ; N/A for /dev/zero

32 xcache.readonly_protection = Off

33 ; for *nix, xcache.mmap_path is a file path, not directory.

34 ; Use something like “/tmp/xcache” if you want to turn on ReadonlyProtection

35 ; 2 group of php won’t share the same /tmp/xcache

36 ; for win32, xcache.mmap_path=anonymous map name, not file path

37 xcache.mmap_path = “/dev/zero”

38

39 ; leave it blank(disabled) or “/tmp/phpcore/”

40 ; make sure it’s writable by php (without checking open_basedir)

41 xcache.coredump_directory = “”

42 ; per request settings

43 xcache.cacher = On

44 xcache.stat = On

45 xcache.optimizer = On

46 [xcache.coverager]

47 ; per request settings

48 ; enable coverage data collecting for xcache.coveragedump_directory and xcache_coverager_start/stop/get/clean() functions (will hurt executing performance)

49 xcache.coverager = Off

50 ; ini only settings

51 ; make sure it’s readable (care open_basedir) by coverage viewer script

52 ; requires xcache.coverager=On

53 xcache.coveragedump_directory = “”


Configure PHP FastCgi


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

view source

print?

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.

view source

print?

01 #!/bin/bash

02 BIND=127.0.0.1:9000

03 USER=www-data

04 PHP_FCGI_CHILDREN=5

05 PHP_FCGI_MAX_REQUESTS=1000

06

07 PHP_CGI=/usr/bin/php-cgi

08 PHP_CGI_NAME=`basename $PHP_CGI`

09 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”

10 RETVAL=0

11

12 start() {

13 echo -n “Starting PHP FastCGI: ”

14 start-stop-daemon –quiet –start –background –chuid “$USER” –exec /usr/bin/env — $PHP_CGI_ARGS

15 RETVAL=$?

16 echo “$PHP_CGI_NAME.”

17 }

18 stop() {

19 echo -n “Stopping PHP FastCGI: ”

20 killall -q -w -u $USER $PHP_CGI

21 RETVAL=$?

22 echo “$PHP_CGI_NAME.”

23 }

24

25 case “” in

26 start)

27 start

28 ;;

29 stop)

30 stop

31 ;;

32 restart)

33 stop

34 start

35 ;;

36 *)

37 echo “Usage: php-fastcgi {start|stop|restart}”

38 exit 1

39 ;;

40 esac

41 exit $RETVAL


Make it executable

view source

print?

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


Make sure it stays there

view source

print?

update-rc.d php-fastcgi defaults


Start it

view source

print?

/etc/init.d/php-fastcgi start


We now have PHP FastCGI set up.

Install & Configure mySQL

Install mySQL

view source

print?

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)

view source

print?

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.

view source

print?

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


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

view source

print?

CREATE DATABASE joomla;


Give the new user permissions on the Joomla! database

view source

print?

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


Thats mySQL all setup, leave the mysql environment with

view source

print?

exit

Install & Configure NGINX

Install NGINX via aptitude.

view source

print?

aptitude install nginx


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

view source

print?

/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.

view source

print?

vi /etc/nginx/sites-available/default


The following is a typical NGINX conf file for a Joomla! site

view source

print?

01 server {

02 listen 80;

03 server_name website.com www.website.com;

04

05 access_log /var/log/nginx/website.com-access.log;

06 error_log /var/log/nginx/website.com-error.log;

07

08 large_client_header_buffers 4 8k; # prevent some 400 errors

09

10 root /var/www/website.com/public_html;

11 index index.php index.html;

12 fastcgi_index index.php;

13

14 location / {

15 expires 30d;

16 error_page 404 = @joomla;

17 log_not_found off;

18 }

19

20 location @joomla {

21 rewrite ^(.*)$ /index.php?q=last;

22 }

23

24 # static files

25 location ~* ^. .(jpg|jpeg|gif|css|png|js|ico)$ {

26 access_log off;

27 expires 30d;

28 }

29

30 location ~ \.php {

31 include /etc/nginx/fastcgi_params;

32 keepalive_timeout 0;

33 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

34 fastcgi_pass 127.0.0.1:9000;

35 }

36 }


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

view source

print?

mkdir /var/www/website.com/public_html


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

view source

print?

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.

view source

print?

cd /var/www/website.com/public_html

Install Joomla! (in this case 1.5.15)

view source

print?

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


Next we unpack it

view source

print?

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

view source

print?

rm -R installation


its also a good idea to delete the downloaded Joomla! package

view source

print?

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


at this point I like to set the permissions

view source

print?

chown -R www-data /var/www/website.com/public_html

view source

print?

chgrp -R www-data /var/www/website.com/public_html

view source

print?

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.



The Almost Perfect Joomla! Server

No comments:

Post a Comment