配置nginx& amp;在ubuntu上的php5-fpm

时间:2011-09-12 21:03:51

标签: nginx php

我正在尝试在ubuntu 11上使用php-fpm(php v 5.3.5)配置nginx.nginx和php5-fpm都设置为以www-data运行。 nginx似乎提供html文件,但没有提供php文件(日志文件生成404错误)。 php5-fpm正在运行并监听nginx尝试连接的同一端口(9000)。配置文件复制如下。文件位于/ var / www(www-data具有对该目录中所有文件的读/写访问权限。)

我怎样才能解决这个问题,以便弄清楚php5-fpm是否正确接收来自nginx的请求,以及是否由于不正确的权限/错误的配置文件位置而无法处理请求。

任何帮助都将不胜感激。

nginx.conf文件:

 user www-data;

    worker_processes 4;

    pid /var/run/nginx.pid;

events {
    worker_connections 768;

}

http 

{

    include mime.types;

    sendfile on;

    tcp_nopush on;

    tcp_nodelay on;

    keepalive_timeout 65;

    types_hash_max_size 2048;

    # server_tokens off;

     server_names_hash_bucket_size 64;

    # server_name_in_redirect off;

    include /etc/nginx/mime.types;

    default_type application/octet-stream;


    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_http_version 1.1;
    gzip_buffers 16 8k;
    gzip_types text/plain text/css text/javascript application/json application/x-javascript text/xml application/xml application/xml+rss;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
站点(已启用/可用)文件夹中的

默认文件:

default

server 

{

    listen   80;

    server_name  localhost;

    access_log  /var/log/nginx/localhost.access.log;

    location / {
        root   /var/www;

        index  index.html index.php;

        try_files $uri $uri/ /index.php?q=$uri&$args;

    }


    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {

      access_log        off;

      expires           30d;

      root /var/www;

    }



## Disable viewing .htaccess & .htpassword
    location ~ /\.ht {

        deny  all;

    }

include php.conf;

}

nginx目录中的php.config文件:

fastcgi_intercept_errors on;


location ~ \.php$

{

    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    fastcgi_param PATH_INFO         $fastcgi_path_info;

    fastcgi_param PATH_TRANSLATED   $document_root$fastcgi_path_info;

    fastcgi_param QUERY_STRING      $query_string;

    fastcgi_param REQUEST_METHOD    $request_method;

    fastcgi_param CONTENT_TYPE      $content_type;

    fastcgi_param CONTENT_LENGTH    $content_length;

    fastcgi_param SCRIPT_NAME       $fastcgi_script_name;

    fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;

    fastcgi_param REQUEST_URI       $request_uri;

    fastcgi_param DOCUMENT_URI      $document_uri;

    fastcgi_param DOCUMENT_ROOT     /var/www;

    fastcgi_param SERVER_PROTOCOL   $server_protocol;

    fastcgi_param GATEWAY_INTERFACE CGI/1.1;

    fastcgi_param SERVER_SOFTWARE   nginx;

    fastcgi_param REMOTE_ADDR       $remote_addr;

    fastcgi_param REMOTE_PORT       $remote_port;

    fastcgi_param SERVER_ADDR       $server_addr;

    fastcgi_param SERVER_PORT       $server_port;

    fastcgi_param SERVER_NAME       $server_name;

    fastcgi_read_timeout 600; # Set fairly high for debugging

    fastcgi_pass  127.0.0.1:9000;

    fastcgi_index index.php;

}

php5-fpm的日志文件输出

configuration file /etc/php5/fpm/main.conf test is successful

nginx的日志文件输出

"GET /index.php HTTP/1.1" 404 31 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1"

2 个答案:

答案 0 :(得分:1)

a回答原来的问题。在你的vhost配置中缺少一个部分,告诉nginx如何处理PHP文件。

示例:

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME PATH_TO_YOUR_WEBSITE_ROOT$fastcgi_script_name;
}

您还可以查看https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04以及是否计划运行多个虚拟主机https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-with-php-pools-on-an-ubuntu-13-04-vps 这两个教程都以一种很好的方式展示了如何设置所有内容。

答案 1 :(得分:1)

您在服务器块中遗漏了一些意图通过php5-fpm传递php文件。

e.g。

location ~ .php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME /var/www$fastcgi_script_name;
}

要编写新的服务器块(/site-enabled中的位),请尝试使用this tool