How to install and configure LEMP (Nginx, MySQL and PHP) server on a Debian 6 (squeeze) VPS

Nginx is a free, open-source, high-performance HTTP server which unlike his ‘friends’, it does not rely on threaded handling of the requests but instead it uses a much more scalable event driven (asynchronous) architecture. This uses a very small and predictable amount of memory under heavy load. Nginx in combination with the simple and very robust FastCGI Process Manager for PHP (PHP-FPM) and the world most popular database server MySQL can give you a lot of power and performance while still using a small memory footprint.

The following article looks at how to install and configure this stack on one of our Debian based VPSes.

1. First, make sure your Debian VPS is fully up-to-date by executing the following:

# apt-get update && apt-get -y upgrade --show-upgraded

2. Next, in order to have the latest stable Nginx, PHP and MySQL in your Debian system we need to include the DotDeb repository to your sources.

# echo -e "deb http://packages.dotdeb.org squeeze all" >> /etc/apt/sources.list

3. Now as we have added the repo to the sources, we need to import its GPG key. To do that execute the following statement:

# gpg --keyserver keys.gnupg.net --recv-key 89DF5277 && gpg -a --export 89DF5277 | apt-key add -

– You should get something like the output below:
gpg: requesting key 89DF5277 from hkp server keys.gnupg.net
gpg: key 89DF5277: public key “Guillaume Plessis ” imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)

4. Now, the DotDeb repo is fully setup so we need to refresh the sources by running:

# apt-get update

– If you have Apache webserver running on your system you need to stop and disable it so Nginx can be started later on. To do this run the following command:

# [[ $(pgrep apache2) ]] && service apache2 stop && update-rc.d -f apache2 disable

5. Install the LEMP (Linux Nginx MySQL and PHP) stack by executing the following commands:

# apt-get -y install nginx
# apt-get -y install php5-fpm php5-gd php5-curl php5-mysql
# apt-get -y install mysql-server

6. Next, configure a simple nginx virtual host directive. To do this, create a new configuration file in ‘/etc/nginx/sites-available/example’ using your favorite editor:

server {
    server_name example.com www.example.com;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log error;

    root /var/www/;
    index index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param  REQUEST_URI    $request_uri;
        fastcgi_param  DOCUMENT_URI   $document_uri;
        fastcgi_param  DOCUMENT_ROOT  $document_root;
        fastcgi_param  REMOTE_ADDR    $remote_addr;
        fastcgi_param  REMOTE_PORT    $remote_port;
        fastcgi_param  SERVER_ADDR    $server_addr;
        fastcgi_param  SERVER_PORT    $server_port;
        fastcgi_param  SERVER_NAME    $server_name;
        fastcgi_param  QUERY_STRING   $query_string;
        fastcgi_param  REQUEST_METHOD $request_method;
        fastcgi_param  CONTENT_TYPE   $content_type;
        fastcgi_param  CONTENT_LENGTH $content_length;

        ## prevent php version info
        fastcgi_hide_header X-Powered-By;

make sure you replace ‘example.com’ with your desired domain name. Also, feel free to set the document root and log paths to your likings.

7. Once the vhost configuration file is created, we need to actually enable it. This can be done by adding a symlink of the newly created vhost directive to ‘/etc/nginx/sites-enabled/’. So execute:

# ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/

8. With all that in place, we are ready to start the services up. Execute the following command in order to reload your Nginx, PHP and MySQL servers:

# for s in nginx php5-fpm mysql; do service $s restart; done

9. Now, create a test PHP info page so we can test if everything is OK.

# echo -e "<?php\n\tphpinfo();\n?>" > /var/www/info.php
# chown -R www-data: /var/www/

Now navigate to ‘http://example.com/info.php’ and you should see PHP’s built-in info page.

It is good idea and recommended to install some cache engine in order to optimize and get things working faster. The PHP-APC cache can drastically improve performance so install it by:

# apt-get -y install php5-apc && service php5-fpm restart

The default APC settings are fine, but of course customizing the options can significantly speed things up, so stay tuned as in some of the next articles we will go deeper into highly optimizing the LEMP stack so you can benefit from using it on your virtual private server.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Categories: Tutorials

View Comments

Leave a Comment