Installing Nginx, MySQL & PHP5 (And PHP5-FPM) a.k.a (LEMP) On Ubuntu 12.04 LTS

1. Update & upgrade your OS first;

apt-get update
apt-get upgrade

  1. Install Myssql & PHP5-FPM;

apt-get install mysql-server mysql-client php5-fpm

  1. Install Nginx webserver

apt-get install nginx

  1. Start your Nginx!

/etc/init.d/nginx start

or

service nginx start

Try access your web via localhost or via ip
e.g. 127.0.0.1, 192.168.1.1

5. Edit nginx config file;

vi /etc/nginx/nginx.conf

Adjust worker proc & keepalive accordingly;

[…]
    worker_processes    4;
[…]
    keepalive_timeout    2;
[…]

  1. Edit the virtual host config;

vi /etc/nginx/sites-available/default

[…]
server {
        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default ipv6only=on; ## listen for ipv6 if any

        root /usr/share/nginx/www; # you can change to any path you want
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }

        # Only for nginx-naxsi : process denied requests
        #location /RequestDenied {
                # For example, return an error code
                #return 418;
        #}

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ .php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+.php)(/.+)$;
                #fastcgi_pass fastcgi_pass 127.0.0.1:9000;
                fastcgi_pass unix:/tmp/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache’s document root
        # concurs with nginx’s one
        #
        location ~ /.ht {
                deny all;
        }
}
[…]

  1. Reload the config file

/etc/init.d/nginx reload

  1. Install nessary file

apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-xcache

  1. Now reload PHP-FPM:

/etc/init.d/php5-fpm reload

  1. Test your nginx;

vi /usr/share/nginx/www/info.php

Paste this line below;

phpinfo();
?>

  1. Try access that file via web browser
    http:///info.php

    It will show info include mysql module

    Making PHP-FPM Use A Unix Socket
    By default PHP-FPM is listening on port 9000 on 127.0.0.1. It is also possible to make PHP-FPM use a Unix socket which avoids the TCP overhead. To do this, open /etc/php5/fpm/pool.d/www.conf.

    12. Edit file

vi /etc/php5/fpm/pool.d/www.conf

… and make the listen line look as follows:

[…]
;listen = 127.0.0.1:9000
listen = /tmp/php5-fpm.sock
[…]

  1. Then reload PHP-FPM:

/etc/init.d/php5-fpm reload

Done!. Hope it helps. 🙂

By zam

Any Comments?

This site uses Akismet to reduce spam. Learn how your comment data is processed.