如何使PHP脚本可执行而不是使用nginx下载
Recently I set up a virtual machine through vagrant and I'm using nginx as a web server. The OS I'm using for the VM is Ubuntu Linux 12.04. The problem is that any script I write in PHP gets downloaded, instead of being executed on the browser. Seeing as the PHP interpreter is normally installed I figured I'd post here the config file of nginx, as this is where the problem is likely to be found. Being a novice in the world of web development, I can't figure out what is out of the ordinary, can you guys take a look and tell me if you see something wrong? Thanks.
server {
listen 80;
server_name localhost;
root /vagrant/www/web;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
#strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# pass the PHP scripts to FastCGI server listening socket
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_pass unix://var/run/php5-fpm.sock;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
# enable global phpMyAdmin
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix://var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
}
最近我通过vagrant建立了一个虚拟机,我正在使用nginx作为Web服务器。 我用于VM的操作系统是Ubuntu Linux 12.04。 问题是我用PHP编写的任何脚本都会被下载,而不是在浏览器上执行。 看到PHP解释器正常安装,我想我会在这里发布nginx的配置文件,因为这是可能找到问题的地方。 作为网络开发领域的新手,我无法弄清楚什么是不寻常的,你们可以看看并告诉我你是否看错了吗? 谢谢。 p>
server {
listen 80;
server_name localhost;
root / vagrant / www / web;
error_log / var / log / nginx / error.log;
access_log /var/log/nginx/access.log;
#strip app.php / prefix如果存在
则重写^ / app \ .php /?(.*) $ / $ 1 permanent;
location / {
index app.php;
try_files $ uri @rewriteapp;
}
位置@rewriteapp {
rewrite ^(。*)$ / app。 php / $ 1 last;
}
#n将PHP脚本传递给FastCGI服务器侦听套接字
位置〜^ /(app | app_dev)\ .php(/ | $){
fastcgi_pass unix:// var /run/php5-fpm.sock;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(。+ \。php)(/。*)$;
包括fastcgi_params;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
fastcgi_param HTTPS off;
}
#enable global phpMyAdmin
location / phpmyadmin {
root / usr / share /;
index index.php index.html index.htm;
location~ ^ / phpmyadmin /( 。+ \ .php)$ {
try_files $ uri = 404;
root / usr / share /;
fastcgi_pass unix://var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
include / etc / nginx / fastcgi_params;
}
location〜* ^ / phpmyadmin /(。+ \。(jpg | jpeg | gif | css | png | js | ico | html | xml | txt))$ {
root / usr / share /;
}
}
location / phpMyAdmin {
rewrite ^ / * / phpmyadmin last;
}
}
code> pre>
div>
As was my suspicion the problem was in this location block:
location ~ ^/(app|app_dev)\.php(/|$) {
fastcgi_pass unix://var/run/php5-fpm.sock;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
The parameter to the location directive is a regular expression that defines only two possible file names: app.php and app_dev.php, thus making any other filename non-executable. For this to change, one needs to either add the names of one's php scripts to the parameter or change the regular expression to a more inclusive form as per Frederic Henri's suggestion.
Looks like you're having double //
to indicate the fastcgi_pass
path of php in your location block, try this instead
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}