How to Configure PHP for Nginx Server

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM


$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM


$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

When setting up a web server with Nginx to host PHP applications, it’s essential to configure PHP to work seamlessly with Nginx. In this guide, we’ll walk through the steps to configure PHP for an Nginx server, ensuring that PHP scripts are processed correctly.


Before proceeding, make sure you have:

  • Nginx is installed on your server.
  • PHP is installed on your server.
  • Nginx server blocks (virtual hosts) are configured for your websites.

Step 1: Install PHP-FPM

PHP-FPM (FastCGI Process Manager) is a FastCGI implementation for PHP. It is essential for processing PHP scripts with Nginx.

sudo apt-get install php-fpm

This command installs PHP-FPM on a Debian-based system. Adjust the command according to your Linux distribution.

Step 2: Configure PHP-FPM

Edit the PHP-FPM configuration file to adjust settings:

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

You can adjust PHP settings based on your application requirements. For example;

Adjust Memory Limit

Increase the memory limit if your applications require more memory. Look for the memory_limit directive

memory_limit = 256M

Adjust the Maximum Upload Size

If your application involves file uploads, adjust the maximum upload file size:

upload_max_filesize = 20M
post_max_size = 25M

Set the Timezone (Optional)

Set the default timezone

date.timezone = "Your/Timezone"

Replace "Your/Timezone" with your timezone (e.g., “America/New_York”).

Configure Error Reporting

For a production environment, it’s recommended to log errors and not display them to users:

display_errors = Off
log_errors = On

Enable OPCache

Enable OPCache for performance improvement:

opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 1
opcache.save_comments = 1

Now, the entire php.ini file with the settings above would look as follows:

; php.ini

; Memory Limit
memory_limit = 256M

; Maximum Upload Size
upload_max_filesize = 20M
post_max_size = 25M

; Timezone
date.timezone = "Your/Timezone"

; Error Reporting
display_errors = Off
log_errors = On

; OPCache Settings
opcache.enable = 1
opcache.enable_cli = 1
opcache.memory_consumption = 128
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 1
opcache.save_comments = 1

Restart the PHP-FPM service to apply the changes:

sudo systemctl restart php7.4-fpm

Step 3: Configure Nginx to Use PHP

Edit the Nginx server block configuration file for your website:

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

Add or modify the location ~ \.php$ block to include the following:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

This configuration tells Nginx to pass PHP requests to the PHP-FPM process for processing.

Step 4: Test and Reload Nginx

Test the Nginx configuration for syntax errors:

sudo nginx -t

If the test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx


Configuring PHP for an Nginx server involves installing PHP-FPM, adjusting PHP settings, and configuring Nginx to work with PHP scripts. Following these steps ensures that PHP-powered websites work seamlessly on your Nginx server, providing a reliable and performant web hosting environment. Adjust the version numbers in commands based on your PHP version.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.