How to Install Nginx PHP
This tutorial shows how to install Nginx PHP on Ubuntu. Root privileges are required to install the needed software. It is assumed that Nginx is already installed on your machine, and this guide will step through the process of installing and configuring PHP.
Why PHP and Nginx?
PHP and Nginx are both open source and reliable solutions for a server side scripting language and web server. The community presence for both is quite broad allowing for continued development and support for beginners and advanced users alike.
PHP is a very popular server side scripting language often used for web projects. It first appeared in 1995 and is now part of many commercial websites as well as open source projects such as WordPress. According to W3Techs, PHP is used by 82.1% of all websites whose server side language is known by W3Techs. PHP is also the main server side language for many popular CMS's such as WordPress, Drupal, and Joomla.
Nginx has become a widely used web server which is known for its flexibility, scalability, feature set, simple configuration, and low resource consumption. According to Netcraft, in September 2015 Nginx was being used as the web server for over 139 million websites. Nginx is the second most used web server after Apache.
The following section will show a step by step process of how to install Nginx PHP on your existing Nginx server.
Step 1 - Update apt-get
The first step is to ensure you are using the latest version of apt-get
. apt-get
is recommended as an installer and is what we will use to install PHP. It's important to frequently download the latest patched software to help protect against any vulnerabilities. Use the following command to get the latest updates:
sudo apt-get update
Step 2 - Install Nginx PHP
We can use PHP with Nginx by installing PHP-FPM (Fast Process Manager) which is an alternative PHP FastCGI implementation that provides some additional features. In order to install PHP-FPM, go to the terminal and execute the following command.
sudo apt-get install php5-fpm
Step 3 - Configure Nginx PHP
A change in the PHP configuration is needed. Edit your php.ini
file by opening it with a text editor (e.g. VI):
sudo vi /etc/php5/fpm/php.ini
Find the line which has cgi.fix_pathinfo=1
and change the 1
to 0
. This update will eliminate potential security risk by ensuring that the interpreter will only process the exact requested file path.
cgi.fix_pathinfo=0
Once this is complete, restart PHP-FPM with the following command:
sudo service php5-fpm restart
Step 4 - Adding an Nginx PHP Info Page
Setting up an Nginx PHP info page will show the current details of your new PHP configuration. To do this is quite easy. Simply create the following file:
sudo vi /usr/share/nginx/www/info.php
Add the following content to the file:
<?php
phpinfo();
?>
Finally, restart Nginx with:
sudo service nginx restart
The Nginx PHP configuration page will now be accessible by appending /info.php
to your IP address (e.g. http://youripaddress/info.php)
With the addition of PHP to your Nginx web server, you now have many more possibilities in terms of development or using a PHP-based framework or content management system.