��ҳ > ��ѡ���� > ������ > ����

Linux ������ Nginx + PHP5 Fa

Nginx�Ƕ���˹�˱�д��ʮ����������HTTP�����������¼������ķ�ʽ��д�������зdz��õ����ܣ�ͬʱҲ��һ���dz���Ч�ķ�������������ƽ�⡣��ӵ��ƥ��Lighttpd�����ܣ�ͬʱ��û��Lighttpd���ڴ�й©���⣬����Lighttpd��mod_proxyҲ��һЩ���Ⲣ�Һܾ�û�и��¡�

�����Ҵ�����������ApacheӦ����Linux�������ϡ�����Nginx����֧��cgi��ʽ���У�ԭ���ǿ��Լ������˴�����һЩ�����ϵ�©������ô���DZ���ʹ��FastCGI��ʽ��ִ��PHP������

�������ҳɹ�������Nginx + PHP5 FastCGI�Ĺ���

���Ȱ�װ������Nginx��

��װNginx

Դ�����������ٷ���ҳ�����ء�Ubuntu 7.10����ֱ��ͨ��apt��װ��Ҳ�����������������µ�deb����

sudo apt-get install nginx

����Ҫ�Լ������Ļ�����Ҫȷ���Լ��Ѿ��б�������PCRE�Ŀ⣨����Nginx��rewriteģ�飬��������Ҫ����ģ��������configureʱʹ��./configure �Cwithout-rewrite�������뷽�����£�

wget http://sysoev.ru/nginx/nginx-0.5.34.tar.gz
tar zxvf nginx-0.5.34.tar.gz
cd nginx-0.5.34
./configure #Ĭ�����ð�װ·��Ϊ/usr/local/nginx ����׷��--prefix=/usr���õ�/usr
make && make install  # installҪ����rootȨ��

Ubuntu��װ֮�����ļ��ṹ����Ϊ��

���Լ�����Ĭ�����ñ����ģ�������/usr/local/nginx�£�������Ŀ¼�ṹ��

������������nginx������Ч������ȷ��80�˿�û������������ʹ�ã���
Ubuntu������

sudo /etc/init.d/nginx start

����������

/usr/local/nginx/sbin/nginx

Ȼ���Ϳ���ͨ��http://localhost/������Ч���ˡ�

Ҫ����nginx���Զ����У����Խ�/usr/local/nginx/sbin/nginx���ӵ�/etc/rc.local�У�Ubuntu����ִ��

update-rc.d nginx defaults

��װPHP5

����������Linux�ϰ�װPHP���кܶ����£������ܶ�ƽ̨�϶����ֳɵ��������������Լ����롣������������Ҫ����cgi��ʽ��php��Ubuntu�¿���ֱ�����У�

sudo apt-get install php-cgi

���ҿ��Ը����Լ���������װһЩ�ֽ����Ż��ͻ�������չ����eaccelerator��apc��xcache�ȡ�

PHP5��CGI��ʽ��һ��������������FastCGI��֧�֣�ֻ��ָ���󶨵ĵ�ַ�Ͷ˿ڲ�����������FastCGI�ķ�ʽ���У����£�

php-cgi -b 127.0.0.1:9000

������������nginxһ�������أ�

����Nginx��PHP FastCGI

�뽫�������ݱ���Ϊfastcgi_params�ļ���������/usr/local/nginx/conf�£�Ubuntu�ɱ�����/etc/nginx�£�����Ϊ���ǵ�FastCGIģ�������˻����Ļ���������

#fastcgi_params
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
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_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
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;
# PHP only, required if PHP was built with �Cenable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

���ر�ע���Ӵֵ�һ�У�PHP-CGI�ر���Ҫ������Ϣ��ȷ��PHP�ļ���λ�á�

������Ҫ��PHP-CGI�������ļ���Ubuntu �ϴ������ļ�λ��/etc/php5/cgi/php.ini���У�����cgi.fix_pathinfoѡ�

cgi.fix_pathinfo=1;

����php-cgi��������ʹ��SCRIPT_FILENAME����������

��������nginx������������php�ļ�����������FastCGI������ִ�У�

server {
    index index.php;
    root  /usr/local/nginx/html;

    location ~ .*.php$ {
        include /usr/local/nginx/conf/fastcgi_params;  #�������Լ�������·����������
        fastcgi_index index.php;
        fastcgi_pass  127.0.0.1:9000; #�������Լ���FastCGI�󶨵ĵ�ַ�Ͷ˿ڽ�������
    }
}

֪ͨNginx������������:

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

Ubuntu�û�����ʹ��init�ű���sudo /etc/init.d/nginx reload

Ȼ������php-cgi -b 127.0.0.1:9000

���������ĵ���Ŀ¼����index.php�����������������ݣ������ٿ�http://localhost/index.php��Ӧ���ܿ���php�ĵ�����Ϣ�ˡ�

����php����

ֱ��ʹ��php-cgi��FastCGI���з�ʽ���������⣨ò��Ӧ���н�������������֪���Ļ����Խ̽��ң���

  1. �������̱���������������������
  2. �����̵�Ч�ʵ�

���ˣ����ǿ�������Lighttpd��spawn-fcgi�����ƽ��̵����С�����spawn-fcgi�ķ������£�

wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 #��ȡLighttpd��Դ����
tar -xvjf lighttpd-1.4.18.tar.bz2
cd lighttpd-1.4.18
./configure  #����
make
cp src/spawn-fcgi /usr/local/bin/spawn-fcgi #ȡ��spawn-fcgi�ij���

�������ǾͿ���ʹ�� spawn-fcgi ������php-cgi��FastCGI������

/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi

������������

Ȼ�����ǿ��Խ����д������뵽/etc/rc.local�ļ��ײ�������ϵͳ������ʱ��Ҳ����ͬʱ����PHP��FastCGI���̡�

ԭ�ģ�http://shiningray.cn/linux-shang-pei-zhi-nginx-php5-fastcgi.html

  • ��һƪ����ʮ��wordpress ���������Ż���ʵ�ü���
  • ��һƪ��ʷ��������8����Ԫ�ֽ𽫳����չ�
  • �˽���Щ�֣��ϵ���˼ ������˼ �õ���˼