无法让Nginx在Ubuntu 16.04服务器上运行php


8

编辑。
问题已经在这里得到了答案:https : //serverfault.com/questions/889334/cant-get-nginx-to-run-php-on-ubuntu-16-04-server

我正在设置运行nginx的服务器,并且试图使其运行php脚本。

显然,关于如何使Nginx运行php的最伟大的文章是这篇文章:https : //www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-in- ubuntu-16-04

随着其他方向的发展,下面的讨论就此结束。
在nginx上启用PHP的最简单方法是什么?

我遵循数字海洋网站上的指示,但没有任何变化,我仍然无法运行php。

我对此很迷失,不胜感激。

这是输出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.

这是输出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

这是我正在使用的配置文件。

它在/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
}

我已经:

  • 重新启动php-fpm。
  • 重新启动了nginx。
  • 强制使用ctrl-f5重新加载浏览器缓存。
  • 检查了/ var / log / error是否有错误,但是当我运行php文件时,显然没有错误。就像nginx只是没有意识到它应该运行php一样,而不是像对待常规文件一样对待它。

我正在尝试运行仅包含功能的脚本phpinfo()。但是浏览器并没有给我PHP信息,而是让我下载文件。/


向我们展示以下结果:sudo service nginx statusphp -v
MehrdadEP

我将其添加到问题。
Hermann Ingjaldsson

1
您是如何配置php的?将您的配置编辑发布到php和nginx。重新启动php-fpm和nginx,清除浏览器缓存,在您的nginx日志中查找错误,什么php脚本???我们需要更多信息。

您得到的错误消息是什么?
George Udosen '17

@HermannIngjaldsson您的nginx状态看起来不错,究竟是什么错误?
MehrdadEP

Answers:


2

打开terminal并运行以下命令:

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

这将打开您的nginx配置文件。使用以下文本进行编辑:

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;
    }
}

保存文件后,运行以下命令以重新启动nginx:

sudo service nginx restart

我将此文本放入配置文件,重新启动了nginx,问题仍然存在。/:
Hermann Ingjaldsson

检查此位置以查看文件的版本并在nginx conf中对其进行编辑:/var/run/php7-fpm.sock
MehrdadEP

您是否将php测试文件放置在此文件夹中?/usr/share/nginx/html
MehrdadEP

没有/var/run/php7-fpm.sock,但是有/var/run/php/php7.0-fpm.sock。它是一个套接字,我不确定如何查看它的内容。我不太了解您的实际要求。
Hermann Ingjaldsson

在/ usr / share / nginx / html yes中有一个nginx欢迎html。
Hermann Ingjaldsson
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.