Wednesday, June 12, 2013

Configuring Nginx, MySQL and PHP with FastCGI (spawn-fcgi) On Ubuntu

Nginx (pronounced “engine x”) is a high-performance HTTP server. The most great and important feature from Nginx is low resource consumption. So that, you can allocated your RAM Memory to other process such as MySQL or Memcached. This tutorial shows how you can install Nginx on an Ubuntu 9.10 server with PHP5 support through FastCGI (spawn-fcgi).


Spawn FastCGI (spawn-fcgi) (http://redmine.lighttpd.net/projects/spawn-fcgi) is a project originally to support Lihttpd. On lots of tutorial and how to, if we want to configuring Nginx and FastCGI, we have also install lighttpd to get spawn-fcgi package. At that time there was still no standalone package for spawn-fcgi. Fortunately since version 9.10 ( or 9.04) Ubuntu provide a standalone spawn-fcgi on they’re repository. So that, you don’t have to install Lighttpd anymore. Lets start it!


Install package that we need for Nginx and PHP:


sudo apt-get install php5-cgi nginx spawn-fcgi


Start your Nginx server:


sudo service nginx start


Open your browser and point the adders to your IP or Hostname nginx server. If you see a page said “Welcome to nginx” it mean you are succeed installing nginx.


Now open nginx configuration file. Type:


vim /etc/nginx/sites-available/default


See part some things like this (on my ubuntu this is start on line 47):


#location ~ \.php$ {


#fastcgi_pass 127.0.0.1:9000;


#fastcgi_index index.php;


#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;


#includefastcgi_params;


#}

Uncomment and edit it so its look like this:


location ~ \.php$ {

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;

include fastcgi_params;

}


now save it.


Start your FastCGI. To start a PHP FastCGI daemon listening on port 9000 on localhost and running as the user and group www-data, we run the following command:


sudo spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid


Restart your nginx

sudo service nginx restart


Now create phpinfo.php to test your server. Type:


vi /var/www/nginx-default/info.php


and fill this line:


phpinfo();

?>


Save it.


Now call that file in a browser (e.g. http://192.168.0.100/info.php).


As you see, PHP5 is working, and it’s working through FastCGI, as shown in the Server API line.


To make your nginx start every boot, type this:


sudo update-rc.d nginx defaults


To start FastCGI every boot, open rc.local file:


sudo vim /etc/rc.local


add this before exit line:


/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid


Its time to instal MySQL.


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


You will be asked to provide a password for the MySQL root user


When its done, you have to restart nginx once again.


sudo service nginx restart


call info file in a browser again. If your see word mysql in page appeared it mean you have done your works!



Configuring Nginx, MySQL and PHP with FastCGI (spawn-fcgi) On Ubuntu

No comments:

Post a Comment