Debian 上的 PHP+FastCGI+nginx

Debian 上的 PHP+FastCGI+nginx

我是 Debian 新手。我想在 Debian 上使用 php。我愿意:

apt-get install php5-cli php5-cgi spawn-fcgi

创建文件/usr/bin/php-fastcgi

#! /bin/sh
PHP_FCGI_CHILDREN=3
PHP_FCGI_MAX_REQUESTS=1000
exec /usr/bin/php5-cgi

创建文件/etc/init.d/init-fastcgi

#!/bin/bash
PHP_SCRIPT="/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f     /usr/bin/php-fastcgi"
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php5-cgi
RETVAL=$?
;;
restart)
killall -9 php5-cgi
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: sudo /etc/init.d/init-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL

阿特尔做:

chmod 755 /usr/bin/php-fastcgi
chmod 755 /etc/init.d/init-fastcgi 

进去/etc/nginx/sites-enabled/default添加:

location ~\.php$ {
 root /srv/www/ekb.mydomain.com/public_html;
 include /etc/nginx/fastcgi_params;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param QUERY_STRING $query_string;
 fastcgi_param SCRIPT_FILENAME /srv/www/ekb.mydomain.com/public_html$fastcgi_script_name;
}

创建目录:

/srv/www/ekb.mydomain.com/public_html
/srv/www/ekb.mydomain.com/logs

创建文件/srv/www/ekb.mydomain.com/public_html/test.php

<?phpinfo();?>

启动服务:

/etc/init.d/init-fastcgi start
/etc/init.d/nginx start

在浏览器中:

 www.ekb.maydomain.com/test.php 

但出现 404 错误。

我做错了什么?

答案1

你真的应该使用 php-fpm 配置 nginx。它的安装、配置和管理更容易,而且速度也更快。

$ apt-get install nginx php5-fpm
$ nano /etc/nginx/nginx.conf

[...]
worker_processes  4;
[...]
keepalive_timeout   2;
[...]


$ nano /etc/nginx/sites-available/default

[...]
server {
    listen   80; ## listen for ipv4; this line is default and implied
    listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

    root /usr/share/nginx/www;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }

    location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            allow ::1;
            deny all;
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #       proxy_pass http://127.0.0.1:8080;
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/www;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            # With php5-cgi alone:
            #fastcgi_pass 127.0.0.1:9000;
            # With php5-fpm:
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
            deny all;
    }
}

启动 nginx 和 php-fpm:

$ service nginx start
$ service php5-fpm start

答案2

尝试编辑/etc/hosts并添加到 localhost 行,如下所示:

127.0.0.1    localhost ekb.maydomain.com

换句话说,添加ekb.maydomain.com到以哪一行开头的行127.0.0.1——它可能看起来与这一行不完全相同。

问题在于“www.ekb.maydomain.com”是真实的 www 地址,而 DNS 解析器无法找到它。通过将该行添加到/etc/hosts,您可以跳过该域的 DNS 解析,而是将其用作 的别名localhost,您的浏览器实际上可以在其中找到在同一台计算机上运行的 Web 服务器。

相关内容