Wed Feb 06 2019

How to run php on Nginx server?

php on Nginx server

PHP is an open source server scripting language used for creating dynamic, powerful web applications and websites. PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP.

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

Here, we show how to setup Nginx web server environment with PHP support.

Let see how -

Install Nginx Web Server

  • Commands: sudo apt install nginx

  • After installing Nginx, the commands below can be used to stop, start and enable Nginx service to always startup when the server boots up.

  • Commands:

sudo systemctl stop nginx.service

sudo systemctl start nginx.service

sudo systemctl enable nginx.service

Install PHP-FPM  and Related Modules

  • After installing Nginx above, run the commands below to install PHP-FPM and related PHP modules. PHP-FPM is a version for Nginx web server while PHP is Apache2. There are many PHP modules that perform different functions... however, there are some important ones that are always needed when developing PHP based websites. The line will allow PHP to function with many popular PHP based websites and applications.

  • Commands:

sudo apt-get install php-fpm php-mcrypt php-cli php-mysql php-gd php-imagick php-recode php-tidy php-xmlrpc

Configure Nginx PHP Settings

  • Now that Nginx and PHP are installed, you may want to configure Nginx to use PHP properly. The default Nginx PHP configuration file is located at /etc/php/7.x/fpm/php.ini

  • The X in the location will be 0 or 1 depending on the php version installed. Open PHP Nginx configuration file by running the commands.

  • Commands:

sudo nano /etc/php/7.1/fpm/php.ini

  • Then edit the file to suit your environments.

  • Next, open the Nginx site configuration file… by default it’s stored at /etc/nginx/sites-available/default

  • If you have a custom file, then edit it to enable Nginx PHP support. Run the commands below to open Nginx default site configuration file.

  • Commands:

sudo nano /etc/nginx/sites-available/default

  • Then make uncomment the highlighted lines to enable Nginx PHP support.

Test PHP-FPM Setup

  • At this point, Nginx and PHP-FPM should be installed and ready.. to test your Nginx PHP settings, create a blank file.

  • Then add the line in the file and save.

  • Save the file and open your browser and browse to the server name or IP address followed by phpinfo.php

  • Now, you’ve successfully installed and configured.

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.