Ubuntu – Can’t get nginx to run php on ubuntu 16.04 server

fastcginginxPHP

Edit.
Question already got an answer here:
https://serverfault.com/questions/889334/cant-get-nginx-to-run-php-on-ubuntu-16-04-server

I am setting up a server running nginx and I'm trying to get it to run php scripts.

Apparently the greatest article on how to get nginx to run php is this one:
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in-ubuntu-16-04

The following discussion ends on it as the other directions are aged.
What is the easiest way to enable PHP on nginx?

I go by the directions on the digital ocean website but nothing changes, I still cant run php.

I am quite lost in this and any help would be appreciated.

Here is the output of sudo service nginx status:

nginx.service - A high performance web server and a reverse proxy server     
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2017-12-17 13:46:33 GMT; 55min ago
Process: 19056 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 19091 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)    
Process: 19064 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 19059 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 19066 (nginx)
Tasks: 2
Memory: 4.6M

CPU: 406ms
CGroup: /system.slice/nginx.service
       ├─19066 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
       └─19094 nginx: worker process                           

Dec 17 13:46:33 websites systemd[1]: Starting A high performance web server and a reverse proxy server...
Dec 17 13:46:33 websites systemd[1]: Started A high performance web server and a reverse proxy server.
Dec 17 13:48:53 websites systemd[1]: Reloading A high performance web server and a reverse proxy server.
Dec 17 13:48:53 websites systemd[1]: Reloaded A high performance web server and a reverse proxy server.

Here is the output of php -v:

PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.22-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by      Zend Technologies

And here's the config file that I'm using.

Its at /etc/nginx/conf.d/virtual_servers.conf.

server {
    listen 80; 
    server_name openage.org www.openage.org;
    #listen [::]:80 default_server ipv6only=on;

    #root /usr/share/nginx/html;
    root /etc/nginx/html/openage;
    index index.php index.html index.htm;

    #server_name localhost;

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

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }   

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }   

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/openage.org/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/openage.org/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
}

I have already:

  • restarted php-fpm.
  • restarted nginx.
  • force reloaded the browser cache with ctrl-f5.
  • checked /var/log/error for errors but there apparently are no errors for when I run the php file. It's like nginx is just not realizing that it is supposed to run the php instead of just treating it like a regular file.

I am trying to run a script containing only the function phpinfo().
But instead of giving me the php info the browser just offers me to download the file. /:

Best Answer

Open terminal and run the following command:

gksu gedit /etc/nginx/sites-available/default

this will open your nginx config file. edit it with the following text:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name localhost;

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

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php7-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

after saving the file run the following command to restart nginx:

sudo service nginx restart
Related Question