502 Bad Gateway with PHP

bumble

New member
Hi everyone

I’m attempting to install PHP with a NGINX web server. I’ve install PHP 8.1 and done what I think is the correct config, but i can’t reach index.php on the server. It gives a 502 Error. If I hit index.html all is well.

I did an apt install php-fpm php-mysql
I edited my .conf file.
completed a nginx -t which had no issues
restarted nginx

My config:

nginx 1.26.3
Ubuntu 22.04.5 LTS

my .conf for the domain:

NGINX:
server {
    location / {
        root   /var/www/html/<my folder>
         try_files $uri $uri/ /index.php$is_args$args;
    }
    server_name  <domain name>;

    index index.php index.html index.htm;

    
    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php8.1-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location ~ /\.ht {
             deny all;
    }
 (other stuff for port 443 ssl )

Any hits on what I can try much appreciated

thanks
Susan
 
Hi everyone

I’m attempting to install PHP with a NGINX web server. I’ve install PHP 8.1 and done what I think is the correct config, but i can’t reach index.php on the server. It gives a 502 Error. If I hit index.html all is well.

I did an apt install php-fpm php-mysql
I edited my .conf file.
completed a nginx -t which had no issues
restarted nginx

My config:

nginx 1.26.3
Ubuntu 22.04.5 LTS

my .conf for the domain:

NGINX:
server {
    location / {
        root   /var/www/html/<my folder>
         try_files $uri $uri/ /index.php$is_args$args;
    }
    server_name  <domain name>;

    index index.php index.html index.htm;

   
    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php8.1-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location ~ /\.ht {
             deny all;
    }
 (other stuff for port 443 ssl )

Any hits on what I can try much appreciated

thanks
Susan
Ensure that the PHP-FPM service is running.
Code:
sudo systemctl status php8.1-fpm
Confirm that the socket path specified in the NGINX configuration matches the actual socket path used by PHP-FPM. The default path for PHP 8.1 is typically /var/run/php/php8.1-fpm.sock. You can verify this in the PHP-FPM configuration file, usually located at /etc/php/8.1/fpm/pool.d/www.conf. Look for the line:
Code:
listen = /var/run/php/php8.1-fpm.sock
If the socket path is correct, ensure that the NGINX configuration is properly set up. Here’s a refined version of your configuration:
Code:
server {
    listen 80;
    server_name <domain name>;

    root /var/www/html/<my folder>;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}
After making changes, test the NGINX configuration and restart if no issues.
 
Thanks
Everything looks fine for config and services running.

however, this line:

Code:
include snippets/fastcgi-php.conf;

I do not have a snippets folder under /etc/nginx 🤔
 
Back
Top