一个nginx的配置下执行PHP脚本

问题描述:

我有以下的nginx服务器模块在其配置一个nginx的配置下执行PHP脚本

server { 
    listen  80; 
    server_name mydomain.com; 
    location /deploy { 
     alias /home/somedir/deploy/; 
     } 

    # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock 
    location ~ \.php$ { 
      try_files $uri =404; 
      fastcgi_pass unix:/var/run/php5-fpm.sock; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      include fastcgi_params; 

    } 
    } 

我想在网址mydomain.com/deploy/erp/index.php或MYDOMAIN执行PHP文件.com/deploy/erp

上面的东西下载php文件而不是执行它。我搜索并找到解决方案,要求您定义一个www目录或其他东西。我只需要在特定网址的特定目录中执行该文件。我有什么选择?

+0

http://blog.chrismeller.com/configuring-and-optimizing-php-fpm-and-nginx-on-ubuntu - 或 - debian – zerkms 2014-12-03 09:56:08

+0

请参阅我的编辑... doesn没有工作。我现在得到一个404! – beNerd 2014-12-03 10:22:12

您还没有告诉nginx如何处理.php文件。您正在使用PHP-FPM假设,你需要这样的事:

server { 
    listen  80; 
    server_name mydomain.com; 

    root /home/somedir; 

    location/{ 
    try_files $uri $uri/ =404; 
    } 

    location /deploy { 
    try_files $uri $uri/ /index.php?$args; 
    } 

    location ~ \.php$ { 
    try_files $uri =404; 
    fastcgi_index index.php; 
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    fastcgi_pass 127.0.0.1:9000; 
    include fastcgi_params; 
    } 
} 
+0

它有效,但有一点变化。你的代码实际上可以在Ubuntu版本12.xx及更低版本上运行。在这些之上,你需要通过unix:/var/run/php5-fpm.sock而不是127.0.0.1:9000 – beNerd 2014-12-03 10:34:18

+0

好极了!这取决于你的fpm设置(例如/etc/php/fpm/php-fpm.conf) - 你可以选择一个TCP或unix套接字(参见:https://github.com/php/php-src/blob/95a609c5d14a2c5295886065a24db8344844ca8b /sapi/fpm/php-fpm.conf.in#L162) – 2014-12-03 10:42:55