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. /:

  • show us the result of: sudo service nginx status and php -v – MehrdadEP Dec 17 '17 at 14:23
  • i added it to the question. – Hermann Ingjaldsson Dec 17 '17 at 14:47
  • 1
    How did you configure php ? Post your config edits to php and nginx. restart php-fpm and nginx , clear your browser cache, look in you nginx logs for errors, what php script ????? We need more information. – Panther Dec 17 '17 at 14:52
  • what is the error message your getting? – George Udosen Dec 17 '17 at 14:52
  • @HermannIngjaldsson your nginx status looks fine, what is the error exactly? – MehrdadEP Dec 17 '17 at 14:54

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
  • I placed this text into the config file, restarted nginx and the problem persists. /: – Hermann Ingjaldsson Dec 17 '17 at 15:26
  • check this location to see your version of the file and edit it in nginx conf: /var/run/php7-fpm.sock – MehrdadEP Dec 17 '17 at 15:28
  • did you place you php test file in this folder? /usr/share/nginx/html – MehrdadEP Dec 17 '17 at 15:30
  • Theres no /var/run/php7-fpm.sock but there is a /var/run/php/php7.0-fpm.sock. Its a socket and im not sure how to see the content of it. Im not quite understanding what youre asking for actually. – Hermann Ingjaldsson Dec 17 '17 at 15:35
  • There is an nginx welcome html in /usr/share/nginx/html yes. – Hermann Ingjaldsson Dec 17 '17 at 15:36

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.