How to Connect Nginx and PHP-FPM on Ubuntu 26.04

This guide installs PHP 8.5 FPM from the Ubuntu 26.04 default repository and connects it to Nginx through a Unix socket …

Ubuntu 26.04Nginx 1.28x86_64About 14 min

Verified guide

This guide was verified by creating a cloud server and running every command in it, from start to finish, exactly as written.

OS
Ubuntu 26.04.1 LTS · x86_64
Installed version
Nginx 1.28.3 (1.28.3-2ubuntu1.12), PHP-FPM 8.5.4 (8.5.4-0ubuntu1.5, CVE-2026-109618·109619 수정 포함)
Test server
DigitalOcean · 1 vCPU / 2 GB RAM / 50 GB SSD (s-1vcpu-2gb, sgp1)
Last checked
2026-10-27

This guide contains affiliate links. If you sign up through them, Codesand may earn a commission. This doesn't affect the content or our assessments.

This guide installs PHP 8.5 FPM from the Ubuntu 26.04 default repository and connects it to Nginx through a Unix socket so PHP files run. It also shows how to check that the installed PHP includes the security fix published in October 2026, and covers writing the server block, testing, changing php.ini settings, fixing 502 and 413 errors, and removing everything.

1. Requirements

If you don't have a server, create a DigitalOcean Droplet (1 vCPU, 2 GB). Every step in this guide was verified on a Droplet (1 vCPU, 2 GB memory, 50 GB SSD, Ubuntu 26.04, Singapore) with Nginx 1.28.3 installed, using a regular user account with sudo privileges.

2. Nginx alone does not run PHP

Nginx cannot execute PHP by itself. It passes PHP files to a separate program called PHP-FPM (FastCGI Process Manager), then sends back the result. First, check what happens when you request a PHP file before PHP-FPM is connected.

echo "<?php echo \"PHP \" . PHP_VERSION;" | sudo tee /var/www/html/test.php
<?php echo "PHP " . PHP_VERSION;

-i shows the response headers together with the body. The PHP code is not executed and the file content is sent as is. Because Content-Type is application/octet-stream, a browser opens a file download dialog.

curl -i http://localhost/test.php
HTTP/1.1 200 OK
Server: nginx/1.28.3 (Ubuntu)
Date: Tue, 27 Oct 2026 08:34:27 GMT
Content-Type: application/octet-stream
Content-Length: 33
Last-Modified: Tue, 27 Oct 2026 08:34:26 GMT
Connection: keep-alive
ETag: "6ae06212-21"
Accept-Ranges: bytes

<?php echo "PHP " . PHP_VERSION;

If the PHP file contains settings such as a database password, those are exposed in the same way. Delete the test file. There is no output.

sudo rm /var/www/html/test.php

3. Install PHP-FPM

Refresh the package lists first. If the lists are old, a PHP version from before the security fix may be installed.

sudo apt update
Hit:1 http://mirrors.digitalocean.com/ubuntu resolute InRelease
Hit:2 http://mirrors.digitalocean.com/ubuntu resolute-updates InRelease
Hit:3 http://mirrors.digitalocean.com/ubuntu resolute-backports InRelease
Hit:4 https://repos-droplet.digitalocean.com/apt/droplet-agent main InRelease
Hit:5 http://security.ubuntu.com/ubuntu resolute-security InRelease
Reading package lists...
Building dependency tree...
Reading state information...
78 packages can be upgraded. Run 'apt list --upgradable' to see them.

Candidate is the version that will be installed. On Ubuntu 26.04, php-fpm from the default repository installs PHP 8.5.

apt-cache policy php-fpm
php-fpm:
  Installed: (none)
  Candidate: 2:8.5+99ubuntu1
  Version table:
     2:8.5+99ubuntu1 500
        500 http://mirrors.digitalocean.com/ubuntu resolute/universe amd64 Packages

php-fpm is a package that pulls in php8.5-fpm, the default PHP on Ubuntu 26.04. Check the version of php8.5-fpm, which is what actually gets installed. If Candidate is 8.5.4-0ubuntu1.5 and the resolute-security repository is listed, this version includes the security fix described in step 4.

apt-cache policy php8.5-fpm
php8.5-fpm:
  Installed: (none)
  Candidate: 8.5.4-0ubuntu1.5
  Version table:
     8.5.4-0ubuntu1.5 500
        500 http://mirrors.digitalocean.com/ubuntu resolute-updates/universe amd64 Packages
        500 http://security.ubuntu.com/ubuntu resolute-security/universe amd64 Packages
     8.5.4-0ubuntu1 500
        500 http://mirrors.digitalocean.com/ubuntu resolute/universe amd64 Packages

Installing php-fpm also installs php8.5-fpm and the command-line php8.5-cli. The end of the output is omitted.

sudo apt install -y php-fpm
Installing:
  php-fpm

Installing dependencies:
  libargon2-1  php8.5-cli     php8.5-fpm
  php-common   php8.5-common  php8.5-readline

Suggested packages:
  php-pear

Summary:
  Upgrading: 0, Installing: 7, Removing: 0, Not Upgrading: 78
  Download size: 7190 kB
  Space needed: 36.8 MB / 48.5 GB available
…

After the installation, check that the Apache web server was not installed along with it. 0 means there are no Apache packages.

dpkg -l | grep -c apache2
0

Check the version of the command-line PHP.

php -v
PHP 8.5.4 (cli) (built: Oct 19 2026 14:35:18) (NTS)
Copyright (c) The PHP Group
Built by Ubuntu
Zend Engine v4.5.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.4, Copyright (c), by Zend Technologies

4. Check the PHP security fix (CVE-2026-109618, CVE-2026-109619)

On October 21, 2026, php.net disclosed two remote code execution vulnerabilities in PHP. A request from a user who is not logged in can be enough to run code on the server, and php.net rated both at CVSS 9.2 (Critical). The advisory says every PHP installation that serves web requests is affected, including PHP-FPM and Apache mod_php. This is a problem in PHP itself, so changing Nginx or PHP-FPM settings does not block it. You need to run a fixed version of PHP.

For PHP 8.5 released by php.net, the fix is in 8.5.12. Ubuntu 26.04 keeps the PHP version number at 8.5.4 in its default repository and adds only the fix. As a result, php -v still shows 8.5.4 after the fix, and you need to check the package version instead. A package version of 8.5.4-0ubuntu1.5 or later includes the fix.

Check the versions of the installed PHP packages. The third column shows 8.5.4-0ubuntu1.5 for every package.

dpkg -l | grep php8.5
ii  php8.5-cli                              8.5.4-0ubuntu1.5                           amd64        command-line interpreter for the PHP scripting language
ii  php8.5-common                           8.5.4-0ubuntu1.5                           amd64        documentation, examples and common module for PHP
ii  php8.5-fpm                              8.5.4-0ubuntu1.5                           amd64        server-side, HTML-embedded scripting language (FPM-CGI binary)
ii  php8.5-readline                         8.5.4-0ubuntu1.5                           amd64        readline module for PHP

You can also search the changelog shipped in the package for the two CVE numbers. If CVE-2026-109618_109619 is printed, the package includes the fix. If nothing is printed, the package is from before the fix.

zgrep -m1 -o "CVE-2026-109618_109619" /usr/share/doc/php8.5-fpm/changelog.Debian.gz
CVE-2026-109618_109619

If PHP-FPM was installed on the server some time ago, run sudo apt update from step 3 and then upgrade the PHP 8.5 packages. --only-upgrade upgrades only packages that are already installed and does not install new ones, and "php8.5*" matches every package whose name starts with php8.5. A Skipping … it is not installed line appears for each package that is not installed. This is not an error. This server already has the fixed version, so the packages are shown as already the newest version. The Skipping lines are omitted from the output below.

sudo apt install -y --only-upgrade "php8.5*"
Reading package lists...
Building dependency tree...
Reading state information...
php8.5-cli is already the newest version (8.5.4-0ubuntu1.5).
php8.5-common is already the newest version (8.5.4-0ubuntu1.5).
…
php8.5-fpm is already the newest version (8.5.4-0ubuntu1.5).
php8.5-readline is already the newest version (8.5.4-0ubuntu1.5).
…
Summary:
  Upgrading: 0, Installing: 0, Removing: 0, Not Upgrading: 78

If packages were upgraded, restart PHP-FPM. Until it restarts, the running PHP-FPM keeps handling requests with the old PHP. There is no output.

sudo systemctl restart php8.5-fpm

For the fixed versions on Ubuntu 24.04 and 22.04, and a comparison of the output before and after the update, see How to Apply the Urgent PHP Security Update (CVE-2026-109618, 109619) on Ubuntu.

5. Check the PHP-FPM status and socket

The service name is php8.5-fpm. It starts as soon as the installation finishes and also starts automatically after a reboot.

systemctl status php8.5-fpm --no-pager | head -5
● php8.5-fpm.service - The PHP 8.5 FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php8.5-fpm.service; enabled; preset: enabled)
     Active: active (running) since Tue 2026-10-27 08:33:54 UTC; 14s ago
 Invocation: ab7add6566ec490aa6cabc782a4899c1
       Docs: man:php-fpm8.5(8)

PHP-FPM receives requests through a Unix socket file instead of a network port. The connection is used only inside the same server, so no port is opened to the outside. The socket file is in the /run/php folder.

ls -l /run/php/
total 4
lrwxrwxrwx 1 root     root     30 Oct 27 08:33 php-fpm.sock -> /etc/alternatives/php-fpm.sock
-rw-r--r-- 1 root     root      4 Oct 27 08:33 php8.5-fpm.pid
srw-rw---- 1 www-data www-data  0 Oct 27 08:33 php8.5-fpm.sock

In the Nginx configuration you use php8.5-fpm.sock. The socket location and permissions are set in the PHP-FPM pool configuration file. The owner and group are www-data, and Nginx also runs as the www-data user, so Nginx can connect to this socket.

grep -E "^(user|group|listen)" /etc/php/8.5/fpm/pool.d/www.conf
user = www-data
group = www-data
listen = /run/php/php8.5-fpm.sock
listen.owner = www-data
listen.group = www-data

6. Create an Nginx server block

Create a folder for the site files. There is no output.

sudo mkdir -p /var/www/myapp

Create the site configuration file. Everything from <<'EOF' to EOF is saved to the file as is, and there is no output.

sudo tee /etc/nginx/sites-available/myapp > /dev/null <<'EOF'
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;

    root /var/www/myapp;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.5-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}
EOF

Each line means the following.

  • index index.php index.html: When a folder address is requested, index.php is looked up first.
  • location ~ \.php$: Applies only to requests whose address ends in .php.
  • include snippets/fastcgi-php.conf: The default PHP settings included in the Ubuntu Nginx package. If the requested PHP file does not exist, Nginx returns 404 instead of passing the request to PHP-FPM.
  • fastcgi_pass unix:/run/php/php8.5-fpm.sock: The socket path you checked in step 5. If your PHP version is different, the number changes too.
  • location ~ /\.ht: Files whose names start with .ht, such as .htaccess, are not served.

Enable the site. When you create a link in sites-enabled, Nginx reads this file.

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/

Checking the configuration shows an error. Both the new file and the existing default file say they use port 80 as default_server.

sudo nginx -t
2026/10/27 08:34:40 [emerg] 10722#10722: a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/myapp:2
nginx: configuration file /etc/nginx/nginx.conf test failed

Remove the link for the default site. The sites-available/default file stays, so you can enable it again later.

sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo systemctl reload nginx

7. Test PHP

Create a test file that prints the PHP version and how PHP is running. phpinfo() shows every server path and setting, so this guide uses a file that prints only the values it needs.

echo '<?php echo "PHP " . PHP_VERSION . " / " . php_sapi_name() . "\n";' | sudo tee /var/www/myapp/test.php
<?php echo "PHP " . PHP_VERSION . " / " . php_sapi_name() . "\n";

This time the result of running the code is shown. fpm-fcgi means PHP-FPM ran it.

curl -i http://localhost/test.php
HTTP/1.1 200 OK
Server: nginx/1.28.3 (Ubuntu)
Date: Tue, 27 Oct 2026 08:34:54 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive

PHP 8.5.4 / fpm-fcgi

Test from another PC (203.0.113.10) as well.

curl http://203.0.113.20/test.php
PHP 8.5.4 / fpm-fcgi

Requests are recorded in the Nginx access log.

sudo tail -n 2 /var/log/nginx/access.log
::1 - - [27/Oct/2026:08:34:54 +0000] "GET /test.php HTTP/1.1" 200 32 "-" "curl/8.18.0"
203.0.113.10 - - [27/Oct/2026:08:34:54 +0000] "GET /test.php HTTP/1.1" 200 32 "-" "curl/8.7.1"

8. Change PHP settings (php.ini)

PHP-FPM uses a different configuration file from the command-line PHP. Settings for PHP that runs on the web are in /etc/php/8.5/fpm/php.ini. As an example, change the upload file size limit (upload_max_filesize) from 2M to 20M. When you accept uploads, change the two values below as well.

  • post_max_size: The size limit for the whole request body. Uploaded files are part of the request body, so this must be larger than upload_max_filesize. The php.net security advisory recommends 32M if you need file uploads and 8M if you don't.
  • session.upload_progress.enabled: Records upload progress in the session. The default is On. In its notice of October 23, 2026, php.net recommended turning this off until the next regular release (CVE-2026-115560). If you don't use upload progress, set it to Off.

Check the current values. A ; at the start of a line means the line is a comment. session.upload_progress.enabled is commented out, so the default On applies.

grep -n -E "^;?(post_max_size|upload_max_filesize|session.upload_progress.enabled) " /etc/php/8.5/fpm/php.ini
693:post_max_size = 8M
851:upload_max_filesize = 2M
1461:;session.upload_progress.enabled = On

sed -i edits the file in place. The third command removes the ; at the start of the line and changes the value to Off. There is no output.

sudo sed -i "s/^upload_max_filesize = 2M/upload_max_filesize = 20M/" /etc/php/8.5/fpm/php.ini
sudo sed -i "s/^post_max_size = 8M/post_max_size = 32M/" /etc/php/8.5/fpm/php.ini
sudo sed -i "s/^;session.upload_progress.enabled = On/session.upload_progress.enabled = Off/" /etc/php/8.5/fpm/php.ini

Check the PHP-FPM configuration for errors, then restart PHP-FPM. After changing php.ini, restart PHP-FPM, not Nginx.

sudo php-fpm8.5 -t
[27-Oct-2026 08:35:13] NOTICE: configuration file /etc/php/8.5/fpm/php-fpm.conf test is successful
sudo systemctl restart php8.5-fpm

Change the test file so it prints the new values.

echo '<?php printf("upload_max_filesize = %s\npost_max_size = %s\nsession.upload_progress.enabled = %s\n", ini_get("upload_max_filesize"), ini_get("post_max_size"), ini_get("session.upload_progress.enabled") ? "On" : "Off");' | sudo tee /var/www/myapp/test.php
<?php printf("upload_max_filesize = %s\npost_max_size = %s\nsession.upload_progress.enabled = %s\n", ini_get("upload_max_filesize"), ini_get("post_max_size"), ini_get("session.upload_progress.enabled") ? "On" : "Off");

All three values are applied to PHP running on the web.

curl http://localhost/test.php
upload_max_filesize = 20M
post_max_size = 32M
session.upload_progress.enabled = Off

When you are done, delete the test file. If you leave a file like this in place, anyone can see the server's PHP version and settings.

sudo rm /var/www/myapp/test.php
curl -i http://localhost/test.php
HTTP/1.1 404 Not Found
Server: nginx/1.28.3 (Ubuntu)
Date: Tue, 27 Oct 2026 08:35:18 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.3 (Ubuntu)</center>
</body>
</html>

9. Troubleshooting

connect() to unix:/run/php/php8.4-fpm.sock failed (2: No such file or directory)

The socket file set in fastcgi_pass in the Nginx configuration does not exist. This often happens when you copy a configuration from another guide or an older server and the PHP version number is different. Below is the result after changing the socket path to php8.4-fpm.sock. nginx -t does not check whether the socket file exists, so the configuration test passes.

curl -i http://localhost/test.php
HTTP/1.1 502 Bad Gateway
Server: nginx/1.28.3 (Ubuntu)
Date: Tue, 27 Oct 2026 08:34:59 GMT
Content-Type: text/html
Content-Length: 166
Connection: keep-alive

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.28.3 (Ubuntu)</center>
</body>
</html>

The cause is recorded in the Nginx error log.

sudo tail -n 1 /var/log/nginx/error.log
2026/10/27 08:34:59 [crit] 11289#11289: *6 connect() to unix:/run/php/php8.4-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: ::1, server: _, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.4-fpm.sock:", host: "localhost"

Check the actual socket name with ls /run/php/, change fastcgi_pass to that path, then run sudo nginx -t and sudo systemctl reload nginx. If PHP-FPM is stopped, the socket file disappears and the same message appears. Other causes are covered in How to Fix Nginx 502 Bad Gateway.

client intended to send too large body

This message is written to error.log along with a 413 Request Entity Too Large response. It means the request body is larger than Nginx's client_max_body_size (1 MB by default). Below is the result of sending a 2 MB file to a PHP file that accepts uploads (upload.php). -F uploads the file as a form, and -w "%{http_code}\n" prints only the response status code.

curl -s -o /dev/null -w "%{http_code}\n" -F "f=@/tmp/2mb.bin" http://localhost/upload.php
413
sudo tail -n 1 /var/log/nginx/error.log
2026/10/27 08:35:30 [error] 11472#11472: *13 client intended to send too large body: 2000208 bytes, client: ::1, server: _, request: "POST /upload.php HTTP/1.1", host: "localhost"

Add client_max_body_size 20M; below server_name _; in the server block. Use the same value as PHP's upload_max_filesize (step 8). -A1 also shows the one line after the matching line.

grep -n -A1 server_name /etc/nginx/sites-available/myapp
4:    server_name _;
5-    client_max_body_size 20M;

After running sudo nginx -t and sudo systemctl reload nginx, the same request returns 200.

curl -s -o /dev/null -w "%{http_code}\n" -F "f=@/tmp/2mb.bin" http://localhost/upload.php
200
PHP files are downloaded instead of executed

The request did not go through the location ~ \.php$ block. As in step 2, the response shows Content-Type as application/octet-stream. Check that the site file is linked in sites-enabled, and that no other site file receives the request first with the same server_name or default_server. Run sudo nginx -T to see the full configuration Nginx actually loaded.

a duplicate default server for 0.0.0.0:80

Both site files say listen 80 default_server. As in step 6, remove the link for the site you don't use (/etc/nginx/sites-enabled/default), or remove default_server from one of the files.

10. Uninstall

Delete the site configuration and the site folder. There is no output.

sudo rm /etc/nginx/sites-enabled/myapp /etc/nginx/sites-available/myapp
sudo rm -rf /var/www/myapp

Enable the Nginx default site again.

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo systemctl reload nginx

Remove the PHP packages together with their configuration files. The beginning of the output is omitted.

sudo apt purge -y php-fpm php8.5-fpm php8.5-cli php8.5-common php8.5-readline php-common
…
Summary:
  Upgrading: 0, Installing: 0, Removing: 6, Not Upgrading: 78
  Freed space: 36.8 MB

Removing php-fpm (2:8.5+99ubuntu1) ...
Removing php8.5-fpm (8.5.4-0ubuntu1.5) ...
Removing php8.5-cli (8.5.4-0ubuntu1.5) ...
Removing php8.5-readline (8.5.4-0ubuntu1.5) ...
Removing php8.5-common (8.5.4-0ubuntu1.5) ...
Removing php-common (2:99ubuntu1) ...
Stopping 'phpsessionclean.service', but its triggering units are still active:
phpsessionclean.timer
Processing triggers for man-db (2.13.1-1build1) ...
Purging configuration files for php-common (2:99ubuntu1) ...
Purging configuration files for php8.5-cli (8.5.4-0ubuntu1.5) ...
Purging configuration files for php8.5-readline (8.5.4-0ubuntu1.5) ...
Purging configuration files for php8.5-fpm (8.5.4-0ubuntu1.5) ...
Purging configuration files for php8.5-common (8.5.4-0ubuntu1.5) ...

Remove the library that was installed with them (libargon2-1). The beginning of the output is omitted.

sudo apt autoremove -y
REMOVING:
  libargon2-1

Summary:
  Upgrading: 0, Installing: 0, Removing: 1, Not Upgrading: 78
  Freed space: 58.4 kB

Removing libargon2-1:amd64 (0~20190702+dfsg-5) ...
Processing triggers for libc-bin (2.43-2ubuntu2.4) ...

Check that no PHP packages are left.

dpkg -l | grep -c php
0

One broken link (php-fpm.sock) remains in the /run/php folder, but /run is cleared on reboot, so you can leave it. When the default page appears again, the cleanup is complete.

curl -s http://localhost | grep -o "<title>.*</title>"
<title>Welcome to nginx!</title>

Try it yourself

No server yet? You can spin up an Ubuntu server in a few minutes with one of these providers. We verified this guide on DigitalOcean.