Installing Nginx, MySQL & PHP5 (PHP5-FPM) on Ubuntu 12.04 LTS

From Zam Wiki

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

2. Install Myssql & PHP5-FPM;

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

3. Install Nginx webserver

apt-get install nginx

4. Start your Nginx!

/etc/init.d/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;
[...]

6. Edit the virtual host config;

vi /etc/nginx/sites-available/default
[...]
   server {
           listen 80;
           #listen [::]:80 default ipv6only=on;

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

           server_name _;

           location / {
                   try_files $uri $uri/ /index.html;
           }

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

           error_page 500 502 503 504 /50x.html;
           location = /50x.html {
                   root /usr/share/nginx/www;
           }

           location ~ \.php$ {
                   try_files $uri =404;
                   fastcgi_split_path_info ^(.+\.php)(/.+)$;
                   fastcgi_pass unix:/tmp/php5-fpm.sock;
                   fastcgi_index index.php;
                   include fastcgi_params;
           }

           location ~ /\.ht {
                   deny all;
           }
   }
[...]

7. Reload the config file

/etc/init.d/nginx reload

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

9. Now reload PHP-FPM:

/etc/init.d/php5-fpm reload

10. Test your nginx;

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

Paste this line below;

<?php
      phpinfo();
?>

11. Try access that file via web browser

http://100.100.100.100/info.php

It will show info included 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
[...]

13. Then reload PHP-FPM:

/etc/init.d/php5-fpm reload

blog comments powered by Disqus