Workik.

PHP2Shell Response Guide: Patch, Mitigate and Harden PHP

Harden PHP and PHP-FPM Configuration

Configure a secure php.ini baseline, isolate PHP-FPM pools with least privilege, and tighten Apache and nginx rules to reduce future risk.

Patching stops PHP2Shell (CVE-2026-109618 and CVE-2026-109619), and hardening limits what an attacker can do if another flaw appears in the future. Now that the emergency updates at Brightline Logistics are in place, you can lock down your PHP runtime, isolate PHP-FPM (FastCGI Process Manager) pools, and tighten your web server and file permissions. These steps form a defense-in-depth baseline that reduces your exposure across every PHP application you run.

Previous lesson: End-of-Life PHP: Backports or Upgrading to PHP 8.4 | Next lesson: Build a PHP Patch Management Process


Establish a Secure php.ini Baseline

Default PHP configurations often favor developer convenience over production security. Before you adjust process isolation or web server rules, you should lock down the core settings in your php.ini file. Note that this baseline is general security best practice rather than a direct fix for PHP2Shell.

Start by hiding your PHP version, keeping error stack traces off public pages, and locking down session cookies. Look at how the configuration for brightline-portal changes when you apply this baseline.

Before:

ini
Copy

After:

ini
Copy

Each directive closes a specific gap in your production environment:

  • expose_php = Off removes the X-Powered-By HTTP (Hypertext Transfer Protocol) header so scanners cannot easily read your exact PHP version.
  • display_errors = Off and log_errors = On stop PHP from printing file paths and stack traces to browser screens while still recording errors to your server logs for debugging.
  • allow_url_include = Off prevents include and require statements from loading code over HTTP or FTP (File Transfer Protocol), closing off remote file inclusion attacks.
  • session.cookie_httponly = 1 and session.cookie_secure = 1 hide session cookies from client-side scripts and require encrypted HTTPS connections.
  • session.use_strict_mode = 1 forces PHP to reject uninitialized session IDs, which prevents session fixation.

You can also restrict dangerous built-in functions and directory access inside php.ini or your pool configuration:

ini
Copy

disable_functions blocks PHP scripts from calling system shell commands if an attacker finds an application-level flaw. Because some frameworks, deployment scripts, and WordPress plugins rely on functions like proc_open or exec, always test your application in staging before applying this list in production. Keep in mind that disable_functions limits damage from script-level flaws, but it is not a complete defense against engine-level memory corruption.

Similarly, open_basedir limits which directories PHP scripts can read or write. Treat open_basedir as a defense-in-depth layer rather than a strict security boundary.

Common Mistake: Editing /etc/php/8.3/cli/php.ini and assuming the web server picks up the changes. Always verify that you are editing the configuration file for your web SAPI (Server Application Programming Interface), such as /etc/php/8.3/fpm/php.ini, and restart your PHP processes afterward.

A locked-down php.ini file strips away helpful hints and easy tools that attackers rely on after an initial foothold.


Isolate Applications with Dedicated PHP-FPM Pools

By default, PHP-FPM runs a single pool named www under a shared Linux user like www-data. If you host two applications on the same server using that default pool, a compromise in one application gives the attacker read and write access to the other.

Think of PHP-FPM pools like watertight compartments in a ship's hull, where a breach in one compartment stays contained instead of flooding the entire vessel.

To isolate brightline-portal, create a dedicated Linux user and group, then replace the shared pool configuration with an application-specific pool file.

bash
Copy

Next, configure the pool file so it runs as the portal user while still allowing the nginx web server (www-data) to talk to the Unix socket.

Before:

ini
Copy

After:

ini
Copy

Notice two details in the updated configuration:

  1. user and group are set to portal, which means every PHP worker in this pool executes with the Linux permissions of portal. Meanwhile, listen.owner and listen.group remain www-data with mode 0660 so nginx can send requests to /run/php/portal.sock without needing access to the application's files.
  2. security.limit_extensions = .php instructs PHP-FPM to execute only files ending in .php. Even if a misconfigured web server passes a file named avatar.jpg or notes.txt to the socket, PHP-FPM refuses to run it.

Quick Challenge: If you add brightline-invoicing to this same server after upgrading it to PHP 8.4, what four settings in its pool file must differ from portal.conf? You would set a unique pool name ([invoicing]), a separate Linux user (invoicing), a separate group (invoicing), and a dedicated socket path (/run/php/invoicing.sock).

Dedicated PHP-FPM pools turn shared servers into isolated workspaces governed by standard Linux user permissions.


Enforce Least Privilege on the File System

Process isolation only works if your file system permissions back it up. A very common misconfiguration on Linux web servers is setting chown -R www-data:www-data /var/www, which lets a compromised PHP worker overwrite its own source code or plant a backdoor in the web root.

Your PHP runtime user should never have write access to application code, configuration files, or .php scripts. Only specific directories such as storage, cache, or uploads should be writable by the PHP user.

For brightline-portal, you can assign file ownership to a deployment account (such as deploy) and grant the portal group read-only access to the code, while allowing write access only to the storage directory:

bash
Copy

With these permissions in place, the portal PHP-FPM workers can read the application code and write logs or user uploads into storage, but they cannot modify index.php or .env.

Common Mistake: Stopping here. With these permissions, nginx (running as www-data) cannot even see the files in public, so the try_files check in the next section returns 404 for every page. Let nginx pass through the project folder and read only the public directory:

bash
Copy

nginx can now serve public, but it still cannot read .env or any code outside public, and the portal workers still cannot change any code.

Read-only web roots stop attackers from turning a single vulnerable request into a permanent backdoor.


Harden Web Server Hand-Offs in nginx and Apache

Your web server decides which requests reach PHP-FPM in the first place. A sloppy nginx or Apache configuration can let attackers trick the server into passing uploaded files to the PHP interpreter, or allow .php scripts hidden inside an upload directory to run.

In nginx, two mistakes happen frequently: omitting try_files inside the PHP location block, and allowing PHP execution inside public upload directories. Look at how to tighten the nginx configuration for brightline-portal.

Before:

nginx
Copy

After:

nginx
Copy

Adding try_files $uri =404; guarantees that nginx checks whether the exact .php file exists on disk before forwarding the request to /run/php/portal.sock. Alongside that rule, the ^/uploads/.*\.php$ block ensures that even if an attacker manages to write a .php file into your writable upload folder, nginx rejects any request to execute it.

If you use Apache with mod_proxy_fcgi, apply the same rule to your upload directories using a <Directory> block in your virtual host file:

apache
Copy

Never allow a directory to be both writable by the PHP user and executable by the PHP interpreter.


Move Off mod_php to PHP-FPM

During the PHP2Shell response, brightline-www stood out as the highest-risk application at Brightline Logistics because it ran on Apache with mod_php. According to the PHP advisory, mod_php carries the highest risk because a demonstrated exploit can run before any application code runs.

Beyond PHP2Shell, mod_php embeds the PHP interpreter directly inside every Apache worker process. That architecture creates three ongoing operational and security problems:

  • Every Apache worker loads the entire PHP engine into memory, even when serving static images, CSS, or JavaScript files.
  • PHP runs as the Apache user (www-data), which makes per-application user isolation impossible on a multi-site server.
  • You cannot use Apache's faster event MPM (Multi-Processing Module) safely with mod_php, forcing you onto the slower prefork MPM.

Moving brightline-www from mod_php to php8.1-fpm separates the PHP runtime from the web server process. On Ubuntu 22.04, you can perform this migration during a maintenance window with the following commands. First, remove any php_admin_value or php_admin_flag lines from the Apache virtual host (such as the Lesson 4 mitigations) and move them into the FPM pool file. Those directives only work with mod_php, and Apache refuses to start with them once mod_php is disabled.

bash
Copy

Once brightline-www runs on PHP-FPM, you can create a dedicated [www_brightline] pool running under its own Linux user, just as you did for brightline-portal.

Replacing mod_php with PHP-FPM separates web serving from code execution and lets you isolate every site on the host.


Try It Yourself

Review and harden a test PHP-FPM environment (or inspect your staging configuration for an app like brightline-portal) using these four steps:

  1. Check your active web php.ini settings by running php-fpm8.3 -i | grep -E 'expose_php|display_errors|allow_url_include|cookie_httponly' and note any values that do not match the baseline earlier in this lesson.
  2. Inspect your FPM pool directory (/etc/php/8.3/fpm/pool.d/) and verify whether security.limit_extensions = .php is explicitly set and whether your application runs under its own non-root Linux user instead of www-data.
  3. List everything in your web root that the pool user can write to, and confirm that only intended upload or cache directories appear. For a pool running as portal, run: sudo -u portal find /var/www/brightline-portal -writable -not -path '*/storage/*' -not -path '*/bootstrap/cache/*'. Any result outside those directories is too permissive.
  4. Check your nginx or Apache configuration to confirm that upload directories block .php execution and that nginx PHP location blocks include try_files $uri =404;.

In the next lesson, Build a PHP Patch Management Process, you will set up automated image scanning, version pinning, and an incident runbook so your team is ready before the next critical PHP advisory arrives.

Was this lesson helpful?