liunx下配置nginx 环境加php fastcgi 环境--此文写于ubuntu 系统下

分类: bordfServer | 标签: | 日期:2009-08-11

1.安装Nginx,执行以下命令

sudo apt-get install nginx

2.安装PHP,这里PHP要用CGI模式运行,所以要安装php-cgi

sudo apt-get install php5-cgi

php.ini的位置 /usr/bin/cgi/php.in
同时,修改php.ini文件的cgi.fix_pathinfo数据为1,默认为0 cgi.fix_pathinfo=1; 这样php-cgi方能正常使用SCRIPT_FILENAME这个变量。
3.安装spawn-fcgi spawn-fcgi是lighttpd的一个用来控制php-cgi的工具,如果系统没有安装GCC编译环境,刚需要在安装lighttpd之前要安装build-essential工具包,安装命令如下

sudo apt-get install build-essential
wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
tar -xvf lighttpd-1.4.19.tar.gz
cd lighttpd-1.4.19/
sudo apt-get install libpcre3-dev
./configure –without-zlib –without-bzip2
make
sudo cp src/spawn-fcgi /usr/local/bin/spawn-fcgi

至此,安装工作算是基本结束了。但是现在还不能用,必须对相关程序进行配置才能达到我们的要求。配置如下
1.配置Nginx的default文件,该文件默认位置在 /etc/nginx/sites-enable/default ,用编辑器打开,可以看到已经有了php的相关配置,只是可能部分没有根据我们的实际情况来配置,并且都是注释的,SO,按照如下形式修改

server {

  listen 80;

  server_name localhost;

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

  location / {

  root /var/www/nginx-default;

  index index.php;

  }

  #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 /var/www/nginx-default;

  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80

  #

  #location ~ \.php$ {

  #proxy_pass http://127.0.0.1;

  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

  #

  location ~ \.php$ {

  fastcgi_pass 127.0.0.1:9000;

  fastcgi_index index.php;

  fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;

  include /etc/nginx/fastcgi_params;

  }

  # deny access to .htaccess files, if Apache’s document root

  # concurs with nginx’s one

  #

  #location ~ /\.ht {

  #deny all;

  #}

  }

  # another virtual host using mix of IP-, name-, and port-based configuration

  #

  #server {

  #listen 8000;

  #listen somename:8080;

  #server_name somename alias another.alias;

  #location / {

  #root html;

  #index index.html index.htm;

  #}

  #}

  # HTTPS server

  #

  #server {

  #listen 443;

  #server_name localhost;

  #ssl on;

  #ssl_certificate cert.pem;

  #ssl_certificate_key cert.key;

  #ssl_session_timeout 5m;

  #ssl_protocols SSLv2 SSLv3 TLSv1;

  #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

  #ssl_prefer_server_ciphers on;

  #location / {

  #root html;

  #index index.html index.htm;

  #}

  #}

配置基本上到这里就结束了。下来让我们启动Nginx,但是启动前必不可少的是启动spawn-fcgi,命令如下

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

如果没有错误信息返回,OK,我们启动我们的主角Nginx吧

sudo /etc/init.d/nginx start

赶紧打开FF访问http://localhost/试试吧

到这里还没结束哦,你重启系统后肯定会发现,为什么Nginx又不能访问PHP了呢,很简单,因为spawn-fcgi没有跟随系统一起重新起动,那我们就把spawn-fcgi自己加到会话里去,如果你的系统使用的GNOME那可以简单的打开系统-》首选项-》会话,并把以下命令添加到启动程序里就OK了

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

声明:本文为耿振的博客 http://blog.bordf.com发布文章。转载务必注明出处

注意:转载须保留全文:请保留本文出处!否则耿振的博客将向你网站的主机商投诉。
本文永久地址:http://blog.bordf.com/236/


3人发表了评论  ↓发表评论↓
  • 标题 liunx –> linux

    Fwolf @ 2009年八月 11日 | 回复

    • 哈哈 ~ 我总拼写错~

      原谅我把~

      bordf @ 2009年八月 21日 | 回复

  • 你问的关于IIS7中文tag的伪静态,没看到你有留邮箱,就给你回复到这里吧

    就是上面一段了!

    衣不如新 @ 2010年五月 21日 | 回复

表情:<( ̄︶ ̄)> | (⊙ˍ⊙) | >﹏< | b( ̄▽ ̄)d | (─.─||) | (^_-)

[ Ctrl+Enter提交 ]