Question

Cant get Nginx to run simple php-file // 502 Bad gateway

Hey i want to install a wordpress-site to my debian10 server with nginx but just cant get things to run. To reduce problems i created simplest possbile “hello world” index.html and index.php files for testing.

<html>
 <head>
  <title>PHP-Test</title>
 </head>
 <body>
 <?php echo '<p>Hallo Welt</p>'; ?>
 </body>
</html>

When ive the index.html in my directory /var/www/EXAMPLE.COM it shows the example site correctly, but whatever i try, when i replace it with the index.php testsite it always shows “502 - Bad Gateway”.

For server administration i use the amp-webinterface (which uses nginx too) on the subdomain amp.EXAMPLE.COM, but i guess that shouldnt be a problem. (that webinterface is working completly fine)

server {
    listen 80;
    listen [::]:80;
    
    server_name amp.EXAMPLE.COM;

    if ($host = amp.EXAMPLE.COM) {
        return 301 https://$host$request_uri;
    }

    return 404;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name amp.EXAMPLE.COM;

    # Replace the below as appropriate according to certificate locations and
    # whatever SSL settings you want. The below reflects a standard certbot
    # configuration
    ssl_certificate /etc/letsencrypt/live/amp.EXAMPLE.COM/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/amp.EXAMPLE.COM/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    
    location / {
        proxy_pass http://localhost:8080;  # Or whatever local IP and port ADS is listening on
        proxy_set_header        Host $host;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        Upgrade $http_upgrade;
        proxy_set_header        Connection "Upgrade";
        proxy_set_header        X-AMP-Scheme $scheme;
        proxy_read_timeout      86400s;
        proxy_send_timeout      86400s;
        proxy_http_version      1.1;
        proxy_redirect          off;
        proxy_buffering         off;
        client_max_body_size    10240M;

        # The following nine lines will only work if nginx and AMP are on the same host
        error_page 502 /NotRunning.html;
        location = /NotRunning.html {
            root /opt/cubecoders/amp/shared/WebRoot;
            internal;
        }

        location /shared/ {
            alias /opt/cubecoders/amp/shared/WebRoot/;
        }
    }
}

nginx -t

nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on [::]:443, ignored
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on 0.0.0.0:443, ignored
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "amp.EXAMPLE.COM" on [::]:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

In the nginx/error.log are no errors, except the conflicting-server-name-warn-thing

sites-enabled

server {
    if ($host = www.EXAMPLE.COM) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = EXAMPLE.COM) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
	listen [::]:80;
    server_name EXAMPLE.COM www.EXAMPLE.COM;
    root  /var/www/EXAMPLE.COM;
    index index.php index.html index.htm index.nginx-debian.html;

	 location / {
        location ~* \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9001;
            try_files $uri @yii =404;
        }
    }


server {
    listen 443;
	listen [::]:443;
    server_name EXAMPLE.COM www.EXAMPLE.COM;
    root  /var/www/EXAMPLE.COM;
    index index.php index.html index.htm index.nginx-debian.html;
	
	location / {
        location ~* \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass 127.0.0.1:9001;
            try_files $uri @yii =404;
        }
    }


    ssl_certificate /etc/letsencrypt/live/EXAMPLE.COM/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/EXAMPLE.COM/privkey.pem; # managed by Certbot
}

instead of fastcgi_pass i already directly linked to the php8.0-fpm.sock but it didnt changed anything, so i switched to this location block i found online.

Gave perms alrady with sudo chmod -R 755 /var/www/EXAMPLE.COM/ sudo chown www-data:www-data -R /var/www/EXAMPLE.COM/ and always restartet nginx. I also removed old php7.3, now only have 8.0 remaining and installed php8.0-fpm (php8.0-fpm is positive responding to systemctl status (see below); php-fpm is not)

● php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-03-16 14:08:33 UTC; 1h 24min ago
     Docs: man:php-fpm8.0(8)
 Main PID: 100460 (php-fpm8.0)
   Status: "Processes active: 0, idle: 2, Requests: 11, slow: 0, Traffic: 0req/sec"
    Tasks: 3 (limit: 9508)
   Memory: 18.9M
   CGroup: /system.slice/php8.0-fpm.service
           ├─100460 php-fpm: master process (/etc/php/8.0/fpm/php-fpm.conf)
           ├─100461 php-fpm: pool www
           └─100462 php-fpm: pool www

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
October 11, 2021
Accepted Answer

Hello,

As far as I can see you have set up Nginx to proxy the traffic to port 9001. However it is quite possible that the PHP-FPM service is not listening on that port.

To check this you could run the following command:

netstat -plant | grep 9001

If this is the case, you could check the listen address for your PHP FPM service in the PHP FPM config file. If it is set to Unix Socket file, you might have to update your Nginx config to match the PHP FPM configuration.

For more information on how to do that I would recommend the following tutorial:

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mariadb-php-lemp-stack-on-debian-10

Best, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel