Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set php ini settings in nginx config for just one host

Tags:

php

nginx

config

One can set error_reporting in nginx.conf like so:

fastcgi_param   PHP_VALUE   error_reporting=E_ALL;

But if I do this in one server block, will it affect all the others as well? Should I change php settings in all server blocks simultaneously?

like image 809
x-yuri Avatar asked Aug 19 '15 16:08

x-yuri


People also ask

What is PHP-FPM in nginx?

PHP-FPM (FastCGI Process Manager) is an alternative to FastCGI implementation of PHP with some additional features useful for sites with high traffic. It is the preferred method of processing PHP pages with NGINX and is faster than traditional CGI based methods such as SUPHP or mod_php for running a PHP script.

Where is PHP INI in nginx?

Nginx uses PHP FPM and php. ini is usually located in /etc/php/8.1/fpm/php. ini .

What is FastCGI pass in nginx?

FastCGI is a protocol based on the earlier CGI, or common gateway interface, protocol meant to improve performance by not running each request as a separate process. It is used to efficiently interface with a server that processes requests for dynamic content.


2 Answers

You can set PHP_VALUE per server and this will affect that server only. If you need equal PHP_VALUE for all your servers with PHP, use include files.

For example (debian), create /etc/nginx/conf.d/php_settings.cnf:

fastcgi_param PHP_VALUE "upload_max_filesize=5M;\n error_reporting=E_ALL;";

Then include this file into any server or location config you need:

server {
  ...
  location ~ \.php$ {
    ...
    include /etc/nginx/conf.d/php_settings.cnf;
  }
  ...
}
like image 132
Aleksey Deryagin Avatar answered Sep 21 '22 02:09

Aleksey Deryagin


If every host on your server runs in its own PHP-FPM pool, than adding fastcgi_param PHP_VALUE ... to one nginx host will not affect the other ones.

If on the other hand all nginx hosts use one PHP-FPM pool, you should specify PHP_VALUE for every host you have (error_reporting=E_ALL for one of them, empty value for others). Since fastcgi_param passes PHP_VALUE if specified, and doesn't pass if not. In a while all workers will have PHP_VALUE=error_reporting=E_ALL, unless you explicitly set PHP_VALUE in other hosts.

Additionally, fastcgi_param PHP_VALUE ... declarations override one another (the last one takes effect).

Steps to reproduce:

  1. apt install nginx php5-fpm

  2. /etc/nginx/sites-enabled/hosts.conf:

    server {
        server_name  s1;
        root  /srv/www/s1;
        location = / {
            include  fastcgi.conf;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_param  PHP_VALUE  error_reporting=E_ERROR;
        }
    }
    
    server {
        server_name  s2;
        root  /srv/www/s1;
        location = / {
            include  fastcgi.conf;
            fastcgi_pass  unix:/var/run/php5-fpm.sock;
        }
    }
    
  3. Add s1, s2 to /etc/hosts

  4. Change pm to static, pm.max_children to 1 in /etc/php5/fpm/pool.d/www.conf

  5. cat /srv/www/s1/index.php:

    <?php var_dump(error_reporting());
    
  6. systemctl restart php5-fpm && systemctl restart nginx

  7. curl s2 && curl s1 && curl s2

    int(22527)
    int(1)
    int(1)
    
like image 39
x-yuri Avatar answered Sep 20 '22 02:09

x-yuri