Roman
Roman

Reputation: 451

Nginx does not run php after ssl certificate installation

I have just set up ubuntu 18.04 server for a new site. The problem is that php stoped working after ssl certificate installation via certbot.

This is example.com config file located at /etc/nginx/sites-available:

server {

        root /var/www/example.com/html;
        index index.html index.htm index.php index.nginx-debian.html;

        server_name example.com www.example.com;

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

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    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
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

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


        listen 80;
        listen [::]:80;

        server_name example.com www.example.com;
        return 404; # managed by Certbot


}

Now https://example.com returns application/octet-stream instead of running index.php

I have checked if firewall allows connection: lsof -i :443

nginx   30662     root   13u  IPv6  26978      0t0  TCP *:https (LISTEN)
nginx   30662     root   14u  IPv4  26979      0t0  TCP *:https (LISTEN)
nginx   30665 www-data   13u  IPv6  26978      0t0  TCP *:https (LISTEN)
nginx   30665 www-data   14u  IPv4  26979      0t0  TCP *:https (LISTEN)

System:

Ubuntu 18.04.3 LTS x86_64
nginx version: nginx/1.14.0
PHP Version 7.0.33-0ubuntu0.16.04.7
firewall: ufw 0.36

Upvotes: 1

Views: 4041

Answers (2)

Ahmed Abdelazim
Ahmed Abdelazim

Reputation: 741

Try This

server {

        root /var/www/example.com/html;
        index index.html index.htm index.php index.nginx-debian.html;

        server_name example.com www.example.com;
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;  
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                fastcgi_read_timeout 180;
        }

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

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    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
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

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


        listen 80;
        listen [::]:80;

        server_name example.com www.example.com;
        return 404; # managed by Certbot


}

Upvotes: 1

gounane
gounane

Reputation: 381

You have to add this directive to your configuration nginx :

location ~* \.php$ {
    #you have to put the path your php-fpm socket file
    fastcgi_pass unix:/path_to_your_socket_file/php7.3-fpm.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

then you have to restart your nginx service.

Source : https://www.linode.com/docs/web-servers/nginx/serve-php-php-fpm-and-nginx/

Let me now if something wrong

Upvotes: 1

Related Questions