Secure Nginx + PHP‑FPM: Prevent Cross‑Site & Directory Access with open_basedir

This guide explains how to secure Nginx + PHP‑FPM by configuring php‑fpm.conf, adding fastcgi_param directives, editing php.ini, or using a .user.ini file to enforce open_basedir restrictions and prevent cross‑site and cross‑directory access, while also enabling slow‑log monitoring for performance.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Secure Nginx + PHP‑FPM: Prevent Cross‑Site & Directory Access with open_basedir

Overall restriction via php-fpm.conf

In php-fpm.conf you can set environment variables and php_admin_value directives to limit script access. Example:

env[TMP]=/tmp/
env[TMPDIR]=/tmp/
env[TEMP]=/tmp/
php_admin_value[sendmail_path]=/usr/sbin/sendmail -t -i -f [email protected]
php_admin_value[open_basedir]=/home/wwwroot/www:/tmp/:/var/tmp/:/proc/
php_admin_value[session.save_path]=/tmp/
php_admin_value[upload_tmp_dir]=/tmp/
slowlog=/usr/local/php/var/log/$pool.log
request_slowlog_timeout=3s

These settings confine PHP scripts to the specified directories.

Method 1 – Add fastcgi_param in Nginx

Insert the following line into the server block or the included fastcgi.conf:

fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/:/proc/";

$document_root is the root directory defined by Nginx. The /tmp and /proc directories must be accessible; /proc allows PHP to read system load information. This method works for each virtual host.

Method 2 – Configure in php.ini

Append to php.ini:

[HOST=www.iamle.com]
open_basedir=/home/wwwroot/www.iamle.com:/tmp/:/proc/
[PATH=/home/wwwroot/www.iamle.com]
open_basedir=/home/wwwroot/www.iamle.com:/tmp/:/proc/

This approach may not suit wildcard domains (e.g., *.iamle.com).

Method 3 – Use a .user.ini file in the site root

Enable user‑defined ini files by setting in php.ini:

user_ini.filename=".user.ini"
user_ini.cache_ttl=300

Create a .user.ini file in the document root containing: open_basedir=/home/wwwroot/www.iamle.com:/tmp/:/proc/ No need to restart Nginx or php‑fpm, but the file should be read‑only for security.

For more details see the official PHP manual.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

fastcgiphp-fpmopen_basedir
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.