lighttpd + PHP(fastcgi) 配置

php(fastcgi) 5.2.6 编译参数

./configure  --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-gd=/usr --enable-calendar --with-zlib --with-bz2 --with-curl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-openssl --enable-zend-multibyte --with-gettext --enable-exif --with-png-dir=/usr --with-jpeg-dir=/usr --with-libxml-dir=/usr --enable-gd-native-ttf --enable-dom --with-freetype-dir=/usr --with-iconv-dir=/usr --enable-fastcgi

lighttpd 1.5.0 编译参数

./configure --prefix=/usr/local/lighttpd --with-mysql=/usr/bin/mysql_config --with-openssl --with-pcre --with-bzip2 --enable-lfs --with-linux-aio

添加fastcgi 及lighttpd执行用户

groupadd www
useradd -g www -s /sbin/nologin -d /dev/null www

复制lighttpd源码包内 doc/spawn-php.sh 并修改如下

  1. #!/bin/bash
  2.  
  3. ## ABSOLUTE path to the spawn-fcgi binary
  4. SPAWNFCGI="/usr/local/lighttpd/bin/spawn-fcgi"
  5.  
  6. ## ABSOLUTE path to the PHP binary
  7. FCGIPROGRAM="/usr/local/php/bin/php-cgi"
  8.  
  9. ## TCP port to which to bind on localhost
  10. FCGIPORT="1026"
  11.  
  12. ## SOCKET to which to bind on localhost
  13. FCGISOCKET="/tmp/php-fastcgi1.sock"
  14.  
  15. ## number of PHP children to spawn
  16. PHP_FCGI_CHILDREN=16
  17.  
  18. ## maximum number of requests a single PHP process can serve before it is restarted
  19. PHP_FCGI_MAX_REQUESTS=1000
  20.  
  21. ## IP addresses from which PHP should access server connections
  22. FCGI_WEB_SERVER_ADDRS="127.0.0.1"
  23.  
  24. # allowed environment variables, separated by spaces
  25. ALLOWED_ENV="HOME PATH USER"
  26.  
  27. ## if this script is run as root, switch to the following user
  28. USERID=www
  29. GROUPID=www
  30.  
  31.  
  32. ################## no config below this line
  33.  
  34. if test x$PHP_FCGI_CHILDREN = x; then
  35.   PHP_FCGI_CHILDREN=5
  36. fi
  37.  
  38. export PHP_FCGI_MAX_REQUESTS
  39. export FCGI_WEB_SERVER_ADDRS
  40.  
  41. ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS"
  42.  
  43. # port
  44. #if test x$UID = x0; then
  45. #  EX="$SPAWNFCGI -p $FCGIPORT -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN"
  46. #else
  47. #  EX="$SPAWNFCGI -p $FCGIPORT -f $FCGIPROGRAM -C $PHP_FCGI_CHILDREN"
  48. #fi
  49.  
  50. # socket
  51. if test x$UID = x0; then
  52.   EX="$SPAWNFCGI  -s $FCGISOCKET -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN"
  53. else
  54.   EX="$SPAWNFCGI  -s $FCGISOCKET -f $FCGIPROGRAM -C $PHP_FCGI_CHILDREN"
  55. fi
  56.  
  57. # copy the allowed environment variables
  58. E=
  59.  
  60. for i in $ALLOWED_ENV; do
  61.   E="$E $i=${!i}"
  62. done
  63.  
  64. # clean the environment and set up a new one
  65. env - $E $EX

保存为 spawn-php1.sh 放到 /etc/lighttpd 下, 多个fastcgi实例可复制此文件并修改相关端口号及socket

lighttpd 相关配置 lighttpd.conf 如下

  1. server.use-ipv6 = "disable" # 缺省为禁用
  2. server.event-handler = "linux-sysepoll" # Linux环境下epoll系统调用可提高吞吐量
  3. #server.max-worker = 10 # 如果你的系统资源没跑满,可考虑调高  lighttpd进程数
  4. server.max-fds = 4096 # 默认的,应该够用了,可根据实际情况调整
  5. server.max-connections = 4096 # 默认等于 server.max-fds
  6. server.network-backend = "linux-sendfile"
  7. server.max-keep-alive-requests = 0 # 在一个keep-alive会话终止连接前能接受处理的最大请求数。0为禁止
  8.  
  9. # 设置要加载的module
  10. server.modules = (
  11.     "mod_rewrite",
  12.     "mod_redirect",
  13. #    "mod_alias",
  14.     "mod_access",
  15. #    "mod_cml",
  16. #    "mod_trigger_b4_dl",
  17.     "mod_auth",
  18.     "mod_expire",
  19. #    "mod_status",
  20. #    "mod_setenv",
  21.     "mod_proxy_core",
  22.     "mod_proxy_backend_http",
  23.     "mod_proxy_backend_fastcgi",
  24. #    "mod_proxy_backend_scgi",
  25. #    "mod_proxy_backend_ajp13",
  26. #    "mod_simple_vhost",
  27.     "mod_evhost",
  28. #    "mod_userdir",
  29. #    "mod_cgi",
  30.     "mod_compress",
  31. #    "mod_ssi",
  32. #    "mod_usertrack",
  33. #    "mod_secdownload",
  34. #    "mod_rrdtool",
  35.     "mod_accesslog" )
  36.  
  37. # 网站根目录
  38. server.document-root = "/var/www/"
  39.  
  40. # 错误日志位置
  41. server.errorlog = "/var/log/lighttpd/error.log"
  42.  
  43. # 网站Index
  44. index-file.names = ( "index.php", "index.html",
  45.                                  "index.htm", "default.htm" )
  46.  
  47. # 访问日志, 以及日志格式 (combined), 使用X-Forwarded-For可越过代理读取真实ip
  48. accesslog.filename = "/var/log/lighttpd/access.log"
  49. accesslog.format = "%{X-Forwarded-For}i %v %u %t \"%r\" %s %b  \"%{User-Agent}i\" \"%{Referer}i\""
  50.  
  51. # 设置禁止访问的文件扩展名
  52. url.access-deny = ( "~", ".inc", ".tpl" )
  53.  
  54. # 服务监听端口
  55. server.port = 80
  56.  
  57. # 进程id记录位置
  58. server.pid-file = "/var/run/lighttpd.pid"
  59.  
  60. # virtual directory listings 如果没有找到index文件就列出目录。建议disable。
  61. dir-listing.activate = "disable"
  62.  
  63. # 服务运行使用的用户及用户组
  64. server.username = "www"
  65. server.groupname = "www"
  66.  
  67. # gzip压缩存放的目录以及需要压缩的文件类型
  68. compress.cache-dir = "/tmp/lighttpd/cache/compress/"
  69. compress.filetype = ("text/plain", "text/html")
  70.  
  71. # fastcgi module
  72. # for PHP don't forget to set cgi.fix_pathinfo = 1 in the php.ini
  73. $HTTP["url"] =~ "\.php$" {
  74.     proxy-core.balancer = "round-robin"
  75.     proxy-core.allow-x-sendfile = "enable"
  76. #    proxy-core.check-local = "enable"
  77.     proxy-core.protocol = "fastcgi"
  78.     proxy-core.backends = ( "unix:/tmp/php-fastcgi1.sock","unix:/tmp/php-fastcgi2.sock" )
  79.     proxy-core.max-pool-size = 16
  80. }
  81.  
  82. # 权限控制
  83. auth.backend = "htpasswd"
  84. auth.backend.htpasswd.userfile = "/var/www/htpasswd.userfile"
  85.  
  86. # 基于 evhost 的虚拟主机 针对域名
  87. $HTTP["host"] == "a.lostk.com" {
  88.     server.document-root = "/var/www/lostk/"
  89.     server.errorlog = "/var/log/lighttpd/lostk-error.log"
  90.     accesslog.filename = "/var/log/lighttpd/lostk-access.log"
  91.  
  92.     # 设定文件过期时间
  93.     expire.url = (
  94.         "/css/" => "access 2 hours",
  95.         "/js/" => "access 2 hours",
  96.     )
  97.  
  98.     # url 跳转
  99.     url.redirect = (
  100.         "^/$" => "/xxx/index.html",
  101.     )
  102.  
  103.     # url 重写 (cakephp可用)
  104.     url.rewrite = (
  105.         "^/(css|js)/(.*)$" => "/$1/$2",
  106.         "^/([^.]+)$" => "/index.php?url=$1",
  107.     )
  108.  
  109.     # 权限控制
  110.     auth.require   = ( "" =>
  111.          (
  112.             "method" => "basic",
  113.             "realm" => "admin only",
  114.             "require" => "user=admin1|user=admin2"  # 允许的用户, 用户列表文件 在上面配置的auth.backend.htpasswd.userfile 里
  115.         ),
  116.     )
  117. }
  118.  
  119. # 针对端口的虚拟主机
  120. $SERVER["socket"] == "192.168.0.1:8000" {
  121.     server.document-root        = "/var/www/xxx/"
  122.     server.errorlog = "/var/log/lighttpd/test-error.log"
  123.     accesslog.filename = "/var/log/lighttpd/test-access.log"
  124.  
  125.    # ...
  126. }

# fastcgi 以及 lighttpd 启动脚本

  1. #!/bin/sh
  2. startphp(){
  3.     /etc/lighttpd/spawn-php1.sh
  4.     /etc/lighttpd/spawn-php2.sh
  5. }
  6.  
  7. starthttpd(){
  8.     /usr/local/lighttpd/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
  9. }
  10.  
  11. start(){
  12.     startphp
  13.     starthttpd
  14. }
  15.  
  16. stopphp(){
  17.     killall php-cgi
  18. }
  19.  
  20. stophttpd(){
  21.     killall lighttpd
  22. }
  23.  
  24. stop(){
  25.     killall php-cgi lighttpd
  26. }
  27.  
  28. case "$1" in
  29.     start)
  30.         start
  31.         ;;
  32.     startphp)
  33.         startphp
  34.         ;;
  35.     starthttpd)
  36.         starthttpd
  37.         ;;
  38.     stop)
  39.         stop
  40.     ;;
  41.     stopphp)
  42.         stopphp
  43.         ;;
  44.     stophttpd)
  45.         stophttpd
  46.         ;;
  47.     *)
  48.         echo "Usage: lighttpd {start|stop|startphp|starthttpd|stopphp|stophttpd}"
  49.         RETVAL=1
  50. esac
Tags: fastcgi, lighttpd, php

相关日志

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

还没有评论。

发表评论

(必填)

(必填)


*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Comment moderation is enabled. Your comment may take some time to appear.