{"id":101,"date":"2026-07-18T00:00:00","date_gmt":"2026-07-18T00:00:00","guid":{"rendered":"http:\/\/\/var\/www\/html\/?p=101"},"modified":"2026-07-19T16:42:01","modified_gmt":"2026-07-19T16:42:01","slug":"php-fpm-socket-permission-denied-nginx-user-group-mismatch-on-ubuntu-2204-lts","status":"publish","type":"post","link":"https:\/\/staging.butitworkedlocal.com\/?p=101","title":{"rendered":"Fixing PHP-FPM Socket Permission Denied: Nginx User\/Group Mismatch on Ubuntu 22.04 LTS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Resolve Nginx 502 Bad Gateway errors caused by PHP-FPM Unix socket permission issues on Ubuntu 22.04 LTS by correcting user, group, and listen mode configurations.<\/strong><\/p>\n\n\n<p>When deploying or managing web applications on an Ubuntu 22.04 LTS server, it&#039;s common to encounter the dreaded &quot;502 Bad Gateway&quot; error. Often, this indicates a communication breakdown between Nginx, your web server, and PHP-FPM, the FastCGI Process Manager that executes your PHP code. A frequent culprit is a <code>permission denied<\/code> error when Nginx attempts to connect to the PHP-FPM Unix socket, typically stemming from a mismatch in user and group permissions.<\/p>\n\n<p>This guide will walk you through diagnosing and resolving this issue by ensuring Nginx has the necessary access to the PHP-FPM socket, restoring your web application&#039;s functionality.<\/p>\n\n<h3>Symptom &amp; Error Signature<\/h3>\n\n<p>Users attempting to access your website will typically see a &quot;502 Bad Gateway&quot; error page directly from Nginx. On the backend, your Nginx error logs will clearly indicate the permission issue.<\/p>\n\n<p><strong>Browser Output:<\/strong>\n<code><\/code>`\n502 Bad Gateway\nnginx\/1.22.1\n<code><\/code>`<\/p>\n\n<p><strong>Nginx Error Log (<code>\/var\/log\/nginx\/error.log<\/code> or similar):<\/strong>\n<code><\/code>`nginx\n2023\/10\/27 10:35:45 [crit] 12345#12345: *1 connect() to unix:\/var\/run\/php\/php8.1-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.1.100, server: example.com, request: &quot;GET \/index.php HTTP\/1.1&quot;, upstream: &quot;fastcgi:\/\/unix:\/var\/run\/php\/php8.1-fpm.sock:&quot;, host: &quot;example.com&quot;\n<code><\/code>`<\/p>\n\n<p>The key part of this error is <code>(13: Permission denied) while connecting to upstream<\/code>. This specifically tells us that Nginx could not establish a connection to the PHP-FPM Unix socket due to insufficient permissions.<\/p>\n\n<h3>Root Cause Analysis<\/h3>\n\n<p>The &quot;PHP-FPM socket permission denied&quot; error arises from a security mechanism designed to prevent unauthorized processes from interacting with the PHP-FPM service. When Nginx tries to communicate with PHP-FPM via a Unix domain socket, it requires read\/write access to that socket file.<\/p>\n\n<p>Here&#039;s a breakdown of the common underlying reasons:<\/p>\n\n<ol><li> <strong>Nginx User\/Group Mismatch:<\/strong><\/li><li>    *   By default, Nginx on Ubuntu runs as the <code>www-data<\/code> user and group.<\/li><li>    *   PHP-FPM also typically runs its pools as <code>www-data<\/code>. However, if the PHP-FPM pool is configured to run under a different user or group (e.g., <code>php-fpm<\/code>, a custom application user, or the <code>root<\/code> user by mistake), the socket it creates might not be accessible to <code>www-data<\/code>.<\/li><\/ol>\n\n<ol><li> <strong>Incorrect PHP-FPM Pool Socket Configuration (<code>listen.owner<\/code>, <code>listen.group<\/code>, <code>listen.mode<\/code>):<\/strong><\/li><li>    *   The PHP-FPM pool configuration (e.g., <code>\/etc\/php\/8.1\/fpm\/pool.d\/www.conf<\/code>) defines how the Unix socket is created.<\/li><li>    *   If <code>listen.owner<\/code> or <code>listen.group<\/code> are set to users\/groups other than <code>www-data<\/code>, and <code>listen.mode<\/code> is restrictive (e.g., <code>0600<\/code>), Nginx (running as <code>www-data<\/code>) will be denied access.<\/li><li>    *   The <code>listen.mode<\/code> directive determines the file permissions for the socket. A common secure setting is <code>0660<\/code>, which grants read\/write to the owner and group, but denies others. If the Nginx user is not part of the socket&#039;s group, this will fail.<\/li><\/ol>\n\n<ol><li> <strong>Parent Directory Permissions:<\/strong><\/li><li>    *   Even if the socket file itself has correct permissions, the directory containing it (e.g., <code>\/var\/run\/php\/<\/code>) must have appropriate permissions for the Nginx user to traverse and access the socket within. Incorrect directory permissions (e.g., <code>0700<\/code> owned by <code>root<\/code>) can prevent Nginx from even seeing the socket file.<\/li><\/ol>\n\n<ol><li> <strong>SELinux or AppArmor Interference (Less Common on Ubuntu):<\/strong><\/li><li>    *   While less frequent for this specific error on a default Ubuntu setup, security modules like SELinux (on Red Hat-based systems) or AppArmor (on Ubuntu) can enforce additional access controls that might block Nginx from connecting to the socket. This usually manifests with explicit denials in audit logs.<\/li><\/ol>\n\n<h3>Step-by-Step Resolution<\/h3>\n\n<p>Follow these steps to diagnose and correct the PHP-FPM socket permission issue. We&#039;ll assume PHP 8.1 for the examples, adjust versions as needed (e.g., <code>php8.2-fpm<\/code>).<\/p>\n\n<h4>1. Verify Nginx User and PHP-FPM Socket Path<\/h4>\n\n<p>First, confirm the user Nginx is running as and the exact socket path Nginx is configured to use.<\/p>\n\n<ol><li> <strong>Check Nginx User:<\/strong><\/li><li>    Typically, Nginx runs as <code>www-data<\/code>. You can verify this in your main Nginx configuration (<code>\/etc\/nginx\/nginx.conf<\/code>) or by inspecting running processes.<\/li><\/ol>\n\n<pre><code class=\"language-bash\">    grep &quot;user&quot; \/etc\/nginx\/nginx.conf\n    ```\n    Expected output:\n    ```\n    user www-data;\n    ```\n    If not explicitly set, Nginx defaults to the user it was started by (often `root`, then it drops privileges to `www-data`).\n    You can also check process ownership:\n    ```bash\n    ps aux | grep nginx | grep -v grep\n    ```<\/code><\/pre>\n\n<ol><li> <strong>Confirm PHP-FPM Socket Path in Nginx Configuration:<\/strong><\/li><li>    Navigate to your Nginx site configuration (e.g., <code>\/etc\/nginx\/sites-available\/example.com.conf<\/code>) and locate the <code>fastcgi_pass<\/code> directive.<\/li><\/ol>\n\n<pre><code class=\"language-bash\">    grep -r &quot;fastcgi_pass&quot; \/etc\/nginx\/sites-enabled\/\n    ```\n    Example output from a config file:\n    ```nginx\n    # \/etc\/nginx\/sites-available\/example.com.conf\n    location ~ .php$ {\n        include snippets\/fastcgi-php.conf;\n        fastcgi_pass unix:\/var\/run\/php\/php8.1-fpm.sock;\n    }\n    ```<\/code><\/pre>\n\n<h4>2. Inspect PHP-FPM Pool Configuration<\/h4>\n\n<p>The most common cause is incorrect settings in the PHP-FPM pool configuration. We&#039;ll edit the <code>www.conf<\/code> file, which manages the default PHP-FPM pool.<\/p>\n\n<ol><li> <strong>Open the PHP-FPM Pool Configuration:<\/strong><\/li><li>    <code><\/code>`bash<\/li><li>    sudo nano \/etc\/php\/8.1\/fpm\/pool.d\/www.conf<\/li><li>    <code><\/code>`<\/li><\/ol>\n\n<ol><li> <strong>Verify <code>user<\/code> and <code>group<\/code> Directives:<\/strong><\/li><li>    Ensure the <code>user<\/code> and <code>group<\/code> directives match the Nginx user (<code>www-data<\/code>).<\/li><\/ol>\n\n<pre><code class=\"language-ini\">    ; Unix user\/group of processes\n    ; Note: The user and group specified here are used for all processes of this pool.\n    ;       This also affects the ownership of the socket if listen.owner\/listen.group\n    ;       are not set, or set to &#039;0&#039;.\n    user = www-data\n    group = www-data<\/code><\/pre>\n\n<ol><li> <strong>Verify <code>listen<\/code> Directives (Crucial):<\/strong><\/li><li>    These directives control the ownership and permissions of the Unix socket file itself.<\/li><\/ol>\n\n<pre><code class=\"language-ini\">    ; The address on which to accept FastCGI requests.\n    ; Valid syntaxes are:\n    ;   &#039;ip.add.re.ss:port&#039;    - to listen on a TCP socket to a specific address on a specific port;\n    ;   &#039;port&#039;                 - to listen on a TCP socket to all addresses on a specific port;\n    ;   &#039;\/path\/to\/unix\/socket&#039; - to listen on a Unix socket.\n    ; Note: This value is in effect only when the listening domain is a Unix socket.\n    ;       The file created is owned by the user\/group specified in &#039;listen.owner&#039; and &#039;listen.group&#039;.<\/code><\/pre>\n\n<p>; Set permissions for unix socket, if one is used. In general to make it work\n    ; with Nginx you will want to set it to 0660 or 0666.\n    ; listen.owner = www-data\n    ; listen.group = www-data\n    ; listen.mode = 0660\n    <code><\/code>`<\/p>\n\n<p>Ensure these lines are <strong>uncommented<\/strong> (no leading semicolon <code>;<\/code>) and configured as follows:<\/p>\n\n<pre><code class=\"language-ini\">    listen.owner = www-data\n    listen.group = www-data\n    listen.mode = 0660<\/code><\/pre>\n\n<ul><li>  <code>listen.owner = www-data<\/code>: Sets the owner of the socket file to <code>www-data<\/code>.<\/li><li>  *   <code>listen.group = www-data<\/code>: Sets the group of the socket file to <code>www-data<\/code>.<\/li><li>  *   <code>listen.mode = 0660<\/code>: Sets the file permissions to <code>rw-rw----<\/code>. This means the owner (<code>www-data<\/code>) and the group (<code>www-data<\/code>) have read and write access, while others have no access. Since Nginx runs as <code>www-data<\/code>, it will have full access.<\/li><\/ul>\n\n<blockquote><p>[!WARNING]\n    &gt; Do not set <code>listen.mode = 0777<\/code> or similarly permissive modes unless absolutely necessary and you understand the security implications. <code>0660<\/code> with correct owner\/group is typically secure and sufficient.<\/p><\/blockquote>\n\n<ol><li> <strong>Save and Exit:<\/strong><\/li><li>    Save the changes (<code>Ctrl+O<\/code>, then <code>Enter<\/code>) and exit <code>nano<\/code> (<code>Ctrl+X<\/code>).<\/li><\/ol>\n\n<h4>3. Restart PHP-FPM and Nginx<\/h4>\n\n<p>After modifying the PHP-FPM configuration, you <em>must<\/em> restart the PHP-FPM service for changes to take effect. It&#039;s also good practice to restart Nginx.<\/p>\n\n<pre><code class=\"language-bash\">sudo systemctl restart php8.1-fpm\nsudo systemctl restart nginx<\/code><\/pre>\n\n<blockquote><p>[!IMPORTANT]\nIf <code>systemctl restart php8.1-fpm<\/code> fails, check the PHP-FPM logs for syntax errors:\n<code>sudo journalctl -u php8.1-fpm -f<\/code><\/p><\/blockquote>\n\n<h4>4. Verify Socket File Permissions<\/h4>\n\n<p>After restarting PHP-FPM, the socket file should be recreated with the new permissions. Verify its ownership and mode.<\/p>\n\n<pre><code class=\"language-bash\">ls -l \/var\/run\/php\/php8.1-fpm.sock<\/code><\/pre>\n\n<p>Expected output (or similar, depending on PHP version):\n<code><\/code>`\nsrw-rw&#8212;- 1 www-data www-data 0 Oct 27 10:45 \/var\/run\/php\/php8.1-fpm.sock\n<code><\/code>`\nThe <code>s<\/code> at the beginning indicates it&#039;s a socket file. Crucially, the owner and group should both be <code>www-data<\/code>, and the permissions should be <code>rw-rw----<\/code>.<\/p>\n\n<p>If the group is something other than <code>www-data<\/code> (e.g., <code>php-fpm<\/code>), you have two main options:\n1.  <strong>Change <code>listen.group<\/code> in <code>www.conf<\/code> to <code>www-data<\/code><\/strong> (as done in Step 2, and recommended).\n2.  <strong>Add the <code>www-data<\/code> user to that specific group.<\/strong> For example, if the socket&#039;s group is <code>php-fpm<\/code>:\n    <code><\/code>`bash\n    sudo usermod -aG php-fpm www-data\n    sudo systemctl restart nginx\n    <code><\/code>`\n    Then, ensure <code>listen.mode = 0660<\/code> is set in <code>www.conf<\/code> and restart <code>php8.1-fpm<\/code>.<\/p>\n\n<h4>5. Check Parent Directory Permissions<\/h4>\n\n<p>While less common to be the primary cause for <em>this specific error<\/em> after correcting FPM config, incorrect directory permissions for <code>\/var\/run\/php\/<\/code> can also contribute. Ensure the Nginx user can traverse this directory.<\/p>\n\n<pre><code class=\"language-bash\">ls -ld \/var\/run\/php<\/code><\/pre>\n\n<p>Expected output:\n<code><\/code>`\ndrwxr-xr-x 2 root root 60 Oct 27 10:45 \/var\/run\/php\n<code><\/code>`\nThe <code>drwxr-xr-x<\/code> (<code>755<\/code>) permissions allow <code>root<\/code> full access, and all other users (including <code>www-data<\/code>) read and execute (traverse) access. This is generally sufficient. If the permissions are more restrictive (e.g., <code>drwx------<\/code>), you might need to adjust them:<\/p>\n\n<pre><code class=\"language-bash\">sudo chmod 755 \/var\/run\/php\n```<\/code><\/pre>\n\n<h4>6. Test Your Website<\/h4>\n\n<p>After completing the steps and restarting services, try accessing your website again. The &quot;502 Bad Gateway&quot; error should now be resolved, and your PHP application should load correctly.<\/p>\n\n<p>If the issue persists, review the Nginx and PHP-FPM error logs again (e.g., <code>sudo tail -f \/var\/log\/nginx\/error.log<\/code> and <code>sudo journalctl -u php8.1-fpm -f<\/code>) for any new errors or clues. There might be a different underlying problem, or a subtle configuration mistake.<\/p>","protected":false},"excerpt":{"rendered":"<p>Resolve Nginx 502 Bad Gateway errors caused by PHP-FPM Unix socket permission issues on Ubuntu 22.04 LTS by correcting user, group, and listen mode configurations.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63],"tags":[271,55,11,220,111,222],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-runtimes","tag-502-bad-gateway","tag-nginx","tag-permissions","tag-php-fpm","tag-ubuntu-22-04","tag-unix-socket"],"_links":{"self":[{"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=101"}],"version-history":[{"count":1,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=\/wp\/v2\/posts\/101\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.butitworkedlocal.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}