LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2975|回复: 14

在gentoo上搭建nginx+php+mysql高并发环境

[复制链接]
发表于 2008-10-14 13:20:44 | 显示全部楼层 |阅读模式
嘿嘿,我正在搞,遇到很多莫名奇妙的问题。先弄个显眼的标题,大家来添砖加瓦啊。

Nginx ("engine x")是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

但是,Nginx在国内引起广泛关注应该归功于这篇文章Nginx 0.5.31 + PHP 5.2.4(FastCGI)搭建可承受3万以上并发连接数,胜过Apache 10倍的Web服务器
蓝色文字

提供几个gentoo平台上的nginx资料

1。Nginx, WordPress & fun
http://www.dead-end.info/2007/08/26/nginx-wordpress-fun

2。Nginx, Fastcgi, PHP, rewrite config for Drupal
http://drupal.org/node/110224

3。Nginx with PHP as FastCGI on Gentoo Linux
http://www.tummblr.com/linux/ngi ... gi-on-gentoo-linux/

自己瞎弄了两天,总算整了出来,nginx这丫的设置太陌生了,真郁闷 。先写到这里,希望大家多修正完善。完备了说不定还可以进wiki呢,嘿嘿。

安装与设置:
nginx部署一般都是从源码编译安装,方便自由定制。而gentoo特有的USE则可充分利用起来,让nginx安装更加便捷。

1. USE设置
  1. www-servers/nginx fastcgi status
  2. dev-lang/php mysql cgi curl discard-path force-cgi-redirect threads bzip2 gd
  3. www-servers/lighttpd fastcgi
  4. dev-db/mysql perl ssl
复制代码
如果你想用新版的nginx请编辑/etc/portage/pakeage.keywords,加入 “
  1. www-servers/lighttpd fastcgi php mysql ~x86
复制代码

2.设置/etc/nginx/nginx.conf 列表比较长,自己添加了中文注释。
  1. user nginx nginx;
  2. worker_processes 1;error_log /var/log/nginx/error_log info;
  3. events {
  4. use epoll;
  5.         worker_connections  1024;
  6.        }
  7. #设置http服务器及反向代理功能负载
  8.    http {
  9. include                /etc/nginx/mime.types;
  10. default_type        application/octet-stream;
  11. #日志格式
  12. log_format main
  13.         '$remote_addr - $remote_user [$time_local] '
  14.                 '"$request" $status $bytes_sent '
  15.         '"$http_referer" "$http_user_agent" '
  16.         '"$gzip_ratio"';
  17. log_format download '$remote_addr - $remote_user [$time_local] '
  18.                     '"$request" $status $bytes_sent '
  19.                     '"$http_referer" "$http_user_agent" '
  20.                     '"$http_range" "$sent_http_content_range"';
  21. # 设定请求缓冲&
  22. client_header_buffer_size   1k;
  23. large_client_header_buffers 4 4k;
  24. client_header_timeout        10m;
  25. client_body_timeout        10m;
  26. send_timeout                10m;
  27. connection_pool_size                256;
  28. request_pool_size                4k;
  29. #开启gzip
  30. gzip on;
  31. gzip_min_length        1100;
  32. gzip_buffers        4 8k;
  33. gzip_types        text/plain;
  34. output_buffers        1 32k;
  35. postpone_output        1460;
  36. sendfile        on;
  37. tcp_nopush        on;
  38. tcp_nodelay        on;
  39. keepalive_timeout        75 20;
  40. ignore_invalid_headers        on;
  41. #服务器设置
  42. server {
  43.         listen                127.0.0.1;
  44.         server_name        localhost;
  45. access_log        /var/log/nginx/localhost.access_log main;
  46.         error_log        /var/log/nginx/localhost.error_log info;
  47. #设定查看Nginx状态的地址
  48. location  /NginxStatus{
  49.       stub_status            on;
  50.       access_log             off;
  51.        allow   127.0.0.1;
  52.     # auth_basic             "NginxStatus";
  53.     #  auth_basic_user_file conf/htpasswd;
  54.        }
  55. location / {
  56.      root /var/www/localhost/htdocs;
  57.      index index.html index.htm index.php;
  58. if (-f $request_filename/index.html){
  59. rewrite (.*) $1/index.html break;
  60. }
  61. if (-f $request_filename/index.php){
  62. rewrite (.*) $1/index.php;
  63. }
  64. if (!-f $request_filename){
  65. rewrite (.*) /index.php;
  66. }
  67. }
  68. location ~ .*.php$
  69.     {
  70.          #fastcgi_pass  unix:/tmp/php-cgi.sock;
  71.        fastcgi_pass  127.0.0.1:9000;
  72.        fastcgi_index index.php;
  73.               include fastcgi_params;   
  74. fastcgi_param SCRIPT_FILENAME /var/www/localhost/htdocs$fastcgi_script_name;  这个一定要设置,就是被这个浪费我很多时间。
  75. }
  76. }
  77. ##ssl portion
  78. # server {
  79. #         listen                127.0.0.1:443;
  80. #         server_name        localhost;
  81. #
  82. #         ssl on;
  83. #         ssl_certificate                /etc/ssl/nginx/nginx.pem;
  84. #         ssl_certificate_key        /etc/ssl/nginx/nginx.key;
  85. #
  86. #         access_log        /var/log/nginx/localhost.ssl_access_log main;
  87. #         error_log        /var/log/nginx/localhost.ssl_error_log info;
  88. #
  89. #         root /var/www/localhost/htdocs;
  90. # }
  91. }
复制代码

3。设置cgi,调用php
要在nginx上跑php,首先需要启动fastcgi,方法有三种,一是用php自带的fastcgi server,二是用lighttpd带的spawn-fcgi,三是用php-fpm。当然最好的是php-fpm.这个张宴的文里有个很好用的东东。但是,郁闷的说gentoo里php的USE却没有 php-fpm项。我看了php的ebuild,头都大了,希望哪位能修改下贴出来。[url]http://php-fpm.anight.org[/url]
  1. touch /usr/bin/php-fastcgi
  2. vim  /usr/bin/php-fastcgi
  3. /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -f /usr/bin/php-cgi
  4. touch /etc/init.d/init_fastcgi
  5. vim /etc/init.d/init_fastcgi
  6. #!/bin/bash
  7. PHP_SCRIPT=/usr/bin/php-fastcgi
  8. RETVAL=0
  9. case "$1" in
  10. start)
  11. $PHP_SCRIPT
  12. RETVAL=$?
  13. ;;
  14. stop)
  15. killall -9 php
  16. RETVAL=$?
  17. ;;
  18. restart)
  19. killall -9 php
  20. $PHP_SCRIPT
  21. RETVAL=$?
  22. ;;
  23. *)
  24. echo "Usage: php-fastcgi {start|stop|restart}"
  25. exit 1
  26. ;;
  27. esac
  28. exit $RETVAL
  29. chmod +x /usr/bin/php-fastcgi
  30. chmod +x /etc/init.d/init_fastcgi
复制代码
4.测试

/etc/init.d/init_fastcgi start

/etc/init.d/nginx start

然后写个phpinfo.php,http://127.0.0.1/phpinfo.php自己看看喽。  由于整个过程中换了几种方法连接cgi脚本,有些告混淆了,可能有些纰漏,请多多指教!另,我的wordpress还是有些页面会自动下载。rewrite规则有待完善。

更新日志:1.081015 11  添加 php-fastcgi
                2.                 加入php-fpm介绍。
发表于 2008-10-14 15:14:37 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2008-10-14 18:52:51 | 显示全部楼层
今天晚上试一试,lighttpd->nginx
回复 支持 反对

使用道具 举报

发表于 2008-10-14 21:15:06 | 显示全部楼层
lighttpd性能已经不错.除非你的很大型的网站:-)
---
而且nginx配置起来并不是很方便.:-(
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-10-14 21:32:01 | 显示全部楼层
已经搭建好了 但是wordpress的rewrite一直不怎么好  有些页面还是会下载...
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-10-14 21:35:20 | 显示全部楼层
Post by SCys;1894007
lighttpd性能已经不错.除非你的很大型的网站:-)
---
而且nginx配置起来并不是很方便.:-(


其实nginx简直就是天生为gentoo准备的,编译太方便了!不像很多版本要自己设定头疼的参数。

配置来说,相对资料少了点。我就找了差不多两天才基本跑起来。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-10-14 23:02:46 | 显示全部楼层
又更新了下,希望用nginx的人能帮我写wordpress的rewrite规则。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-10-14 23:09:36 | 显示全部楼层
Post by Blahster;1893950
今天晚上试一试,lighttpd->nginx


期待,现在弄好没?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-10-15 02:39:42 | 显示全部楼层

Discuz在nginx下的rewrite规则

Discuz在nginx下的rewrite规则
[php]
   1.  rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;   
   2. rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2page=$3 last;   
   3. rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2extra=page\%3D$4page=$3 last;   
   4. rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;   
   5. rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;   
   6. rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;  
[/php]
回复 支持 反对

使用道具 举报

发表于 2008-10-15 12:03:08 | 显示全部楼层
想怎么写?
我看看,不复杂就帮你下:~P
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表