如何使PHP脚本可执行而不是使用nginx下载

时间:2015-08-30 21:11:10

标签: php nginx vagrant backend

最近我通过vagrant建立了一个虚拟机,并且我使用nginx作为Web服务器。我用于VM的操作系统是Ubuntu Linux 12.04。问题是我用PHP编写的任何脚本都会被下载,而不是在浏览器上执行。看到PHP解释器正常安装,我想我会在这里发布nginx的配置文件,因为这是可能发现问题的地方。作为网络开发领域的新手,我无法弄清楚什么是不寻常的,你们可以看看并告诉我你是否看错了吗?感谢。

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

2 个答案:

答案 0 :(得分:1)

您的位置块中似乎有两个//来表示php的fastcgi_pass路径,请尝试此操作

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

答案 1 :(得分:0)

我怀疑问题是在这个位置区域:

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

location指令的参数是一个正则表达式,它只定义了两个可能的文件名:app.php和app_dev.php,因此任何其他文件名都不可执行。为了改变这一点,需要根据Frederic Henri的建议,将一个PHP脚本的名称添加到参数中,或者将正则表达式更改为更具包容性的形式。