• Eric Muyser

    "What I want for myself, I want for others" - Wallace D Wattles

    Today I’m going to do a quick run down from start to finish successfully installing and running PHP applications on LAMP. We’ll be using Nginx, PHP5, and MySQL. My OS of choice is a Debian VPS; your experience may be differ on other distros. My host of choice is Rackspace Cloud Servers. By the end, we should be able to run web apps such as WordPress, LemonStand, Joomla, etc.


    First, we need to create our VPS. That is extremely easy on Rackspace, Linode, etc. Simply log into your backend UI and create a new server. You should be given or mailed credentials.


    You will need to point a DNS A record to the provided IP address.


    Next, we need to SSH into our box (or use a provided backend console). So we open up our command-line Console. I will assume you are using root, if not, you will need to sudo or su before each command.


    Run this command: ssh [email protected] and login.


    Currently, the offered Debian version is Lenny (with Squeeze coming soon I imagine). So, in order to simplify things, we’re actually going to upgrade to Squeeze.


    When I mention to edit a file, you can do so simply like this in command-line: nano /path/to/file/name.ext

    If you use nano, then simply use ctrl+o to save and ctrl+x to exit.


    First, open nano /etc/apt/sources.list in your favorite editor (vi, nano, etc), change instances of lenny to squeeze, and add dotdeb’s repositories, like this:


    1. deb http://http.us.debian.org/debian/ squeeze main contrib non-free
    3. deb-src http://http.us.debian.org/debian/ squeeze main contrib non-free
    5. deb http://security.debian.org/ squeeze/updates main contrib
    7. deb-src http://security.debian.org/ squeeze/updates main contrib
    9. deb http://php53.dotdeb.org stable all
    11. deb http://packages.dotdeb.org stable all
    13. deb-src http://php53.dotdeb.org stable all
    15. deb-src http://packages.dotdeb.org stable all


    Then run this command: apt-get update


    Did you get a GPG error? Check the key after the NO_PUBKEY and run this command:

    gpg –keyserver keys.gnupg.net –recv-key PASTE_HERE_KEY

    gpg -a –export PASTE_HERE_KEY | sudo apt-key add -

    If there are two keys you may need to add two. Then run this command again: apt-get update (to manually update keys: apt-key update)

    We need to create a file that will let the system know we want it to upgrade next startup: touch /etc/udev/kernel-upgrade


    Then type reboot, and let the system shutdown. Give it a minute, and reconnect via SSH.


    Now, run this command: aptitude full-upgrade


    To install Nginx we run this command: aptitude install nginx

    To setup MySQL we run this command: aptitude install mysql-server mysql-client

    To install PHP we run this command: aptitude install php5-fpm php5-cgi php5-dev php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ps php5-pspell php5-recode php5-snmp php5-tidy php5-xmlrpc php5-xsl php5-common php5-mysql

    We should enable PHP short tags, so edit /etc/php5/fpm/php.ini and change short_open_tag to On.


    Now, let’s try restarting Nginx and PHP FPM to test, run these commands:

    /etc/init.d/php5-fpm restart

    /etc/init.d/nginx restart

    Did yours throw a warning and 2 errors as well? If so,

    1) Remove the line that starts with # hash from this file: /etc/php5/fpm/conf.d/ps.ini

    2) Delete the extension mentioned in there error, idn.ini from the conf.d folder: /etc/php5/conf.d/idn.ini


    Retry restarting the services again.


    In order to get a site up and running we need to edit /etc/nginx/sites-available/default. When you’ve created your website directory, edit that file.


    In that file, duplicate the “server” definition, change the server_name to your host, like this (I’ve included rewrites which should work with some web software):


    1. server {
    3. listen 80; ## listen for ipv4
    5. server_name mysite.com;
    7. access_log /var/log/nginx/localhost.access.log;
    9. location / {
    11. root /home/myuser/mysite/www;
    13. index index.php;

    14. if (-e $request_filename) {

    15. break;

    16. }

    17. set $test_var true;

    18. if ($request_method = POST) { set $test_var false; }

    19. if ($http_X_Requested_With !~ XMLHttpRequest) { set $test_var false; }

    20. if ($test_var = true) {

    21. rewrite ^([^.|][^/])$ $1/ permanent;

    22. }

    23. rewrite ^(.)$ /index.php?q=$1 last;

    24. }

    25. location ~ .php$ {

    26. fastcgi_pass;

    27. fastcgi_index index.php;

    28. fastcgi_param SCRIPT_FILENAME /home/myuser/mysite/www$fastcgi_script_name;

    29. include fastcgi_params;

    30. }

    31. }

    Now reboot Nginx.


    Note: Nginx configuration files are touchy. Spaces are used as part of the syntax (the one between if and bracket for example). Use nginx -t to validate config syntax.
    Note: A quick way to give full permissions when installing a web application (from within it’s directory): chmod -R 0777 ./
    Note: As far as I can tell, minify has trouble converting relative URI’s to absolute URI’s, so you may have to hardcode the absolute path.
    Note: You will likely want to take security precautions, run PHP under a different username, only give cache folders write permission, etc. That is out of the breadth of this guide; maybe in future posts or comments.


    Thanks for reading. Good luck and have fun!