Nginx is pronounced as “Engine-X”, which is a web server and reverse proxy server. Nginx is well known for its speed and ability to handle large number of requests simultaneously with optimal use of resources.
PHP-FPM stands for “PHP-FastCGI process manager”. CGI refers to the common gateway interface which is scripted to work as a interface between the web server and dynamic content serving programs. It listens on a port much like the web server itself does, and passes the request between the PHP and web server.
This tutorial provides instructions on how to install and configure Nginx with PHP-FPM, which will help you to execute PHP programs in Nginx.
As we discussed earlier in Apache vs Nginx, when compared to Nginx, Apache is relatively slow while handling heavy load and processing large number of requests.
1. Install Nginx
You can either install Nginx from source, or install it using the package management tool that comes with your distro.
For example, on Ubuntu you can install nginx using apt-get as shown below.
$ sudo apt-get install nginx
Start the nginx server as shown below:
$ sudo service nginx start
Go to http://{your-ip-address} and make sure you see the Nginx’s welcome page.
2. Install PHP5-FPM
Next, install php5-fpm using the package management tool that comes with your distro.
For example, on Ubuntu you can install php5-fpm using apt-get as shown below.
$ sudo apt-get install php5-fpm
3. Add PHP Configuration to Nginx
Next, modify the /etc/nginx/sites-available/default file, and add the following lines.
$ sudo vi /etc/nginx/sites-available/default server { listen 80; root /usr/share/nginx/www; index index.php index.html index.htm; server_name example.com; location / { try_files $uri $uri/ /index.html; } error_page 404 /404.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_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
4. Set listen Parameter in php5-fpm www.conf
Next, we need to make the following changes in the php-frpm configuration.
By default, you’ll see the following listen entry in the www.conf file
$ sudo vi /etc/php5/fpm/pool.d/www.conf listen = 127.0.0.1:9000
In the www.conf file, leave everything as it is, and replace the above listen line with the one shown below.
$ sudo vi /etc/php5/fpm/pool.d/www.conf listen = /var/run/php5-fpm.sock
5. Restart the Nginx and PHP5-FPM and Test it
Restart php5-fpm and nginx as shown below
$ sudo service nginx restart $ sudo service php5-fpm restart
Finally, create the following index.php file in the nginx document root, and test it.
$ sudo vi /usr/share/nginx/www <?php phpinfo( ); ?>
Finally, open your browser and go to http://localhost/index.php (or use your ip-address), which will execute the index.php file and display the php information.
Comments on this entry are closed.
A handy tutorial, thanks,
Great article -thanks.
I have same problem. I have already setup environment for node.js and nginx. Now I would like to run php with nginx. I have already installed php and configured nginx for php. But, my browser showing script/code of php instead of executing/run it. I have placed same question on stackoverflow here : http://stackoverflow.com/questions/33409539/nginx-shows-php-code-instead-of-executing
I have made same changes in ‘sites-available/default’ and ‘/etc/php5/fpm/pool.d/www.conf’ as you describe. I have posted by config files in about link, If you wish to get more detail. Please guide me, where to look? It is very urgent. Thanks
You saved my day 🙂 Thank you so much