In this recipe, we will learn how to install and set up Nginx as a web server. We will also install PHP to be able to serve dynamic content. We need to install PHP_FPM (FastCGI Process Manager), as Nginx doesn't support the native execution of PHP scripts. We will install the latest stable version available from the Nginx package repository.
Follow these steps to install Nginx with PHP_FPM:
apt
package repository and install Nginx. As of writing this Ubuntu 16.04 repository contains latest stable release of Nginx with version 1.10.0:$ sudo apt-get update $ sudo apt-get install nginx
$ sudo service nginx status
$ nginx -v
$ sudo apt-get install php7.0-fpm
/etc/nginx/sites-available
. We will modify the default site:$ sudo nano /etc/nginx/sites-available/default
index
file and add index.php
as a first option:index index.php index.html index.htm;
location
directives:location / { try_files $uri $uri/ /index.php; } location ~ .php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock ; }
PATH_TRANSLATED
support. Find an option, cgi.fix_pathinfo
, and uncomment it with the value set to 0
:$ sudo nano /etc/php/7.0/fpm/php.ini cgi.fix_pathinfo=0
$ sudo service php7.0-fpm restart $ sudo service nginx restart
index.php
file with some PHP code in it at the path mentioned in the default site configuration:$ sudo nano /var/www/html/index.php <?php phpinfo(); ?>
Here, we have installed the latest stable version of the Nginx server with PHP_FPM to support dynamic content scripted with PHP. The Ubuntu repository for version 16.04 contains the latest stable release of Nginx, So installing Nginx is as easy as a single command. If you are interested in more recent versions Nginx maintains their own package repository for mainline packages. You just need to add repository, the rest of the installation process is similar to a single apt-get install nginx
command.
If you are running the Apache server on the same machine, you may want to change the default port Nginx runs on. You can find these settings under site configurations, located at /etc/nginx/sites-available
. Nginx creates default site configuration with the filename set to default
. Find the lines that start with listen
and change the port from its default, 80
, to any port number of your choice.
After installing Nginx, we need to configure it to support dynamic content. Here, we have selected PHP as a dynamic content processor. PHP is a popular scripting language and very commonly used with web servers for dynamic content processing. You can also add support for other modules by installing their respective processors. After installing PHP_FPM, we have configured Nginx to use PHP_FPM and pass all PHP requests to the FPM module on a socket connection.
We have used two location blocks in configuration. The first block search is for static content, such as files and directories, and then if nothing matches, the request is forwarded to index.php
, which is in turn forwarded to the FastCGI module for processing. This ensures that Nginx serves all static content without executing PHP, and only requests that are not static files and directories are passed to the FPM module.
The following is a brief description of the parameters used under FastCGI configuration:
try_files
configures Nginx to return 404
pages, that is, the page not found error, for any requests that do not match website content. This is limited to static files.fastcgi_param
, you can forward the script name and query string to the PHP FPM process.cgi.fix_pathinfo=0
, under the PHP configuration file php.ini
. By default, PHP is set to search for the exact script filename and then search for the closest match if the exact name is not found. This may become a security risk by allowing an attacker to execute random scripts with simple guesswork for script names. We have disabled this by setting its value to 0
.Finally, after we restart PHP_FPM and Nginx, our server is ready to process static as well as dynamic content. All static content will be handled by Nginx itself, and requests for URLs that end with .php
will be forwarded to PHP_FPM for processing. Nginx may cache the processed result for future use.
If you are running Ubuntu 12.10, you may need to install the following dependencies before adding the Nginx repository to the installation sources:
python-software-properties
and software-properties-common
:$ sudo apt-get install python-software-properties $ sudo apt-get install software-properties-common
$ sudo service apache2 stop $ sudo apt-get remove --purge apache2 apache2-utils apache2.2-bin apache2-common
Nginx maintains their own package repositories for stable and mainline releases. These repositories can be used to get the latest updates of Nginx as and when available. Use the stable repository, - $ sudo add-apt-repository ppa:nginx/stable
.
Use the mainline repository - $ sudo add-apt-repository ppa:nginx/development
.