编辑。
问题在这里已经得到答案:
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 信息,而是让我下载文件。/
:
答案1
打开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