zoukankan      html  css  js  c++  java
  • Lighttpd 搭建 Web 服务器

    背景:
          公司项目用到了lighttpd,由于自己没有接触过,所以做下记录。

    简介:
          Lighttpd 是一个德国人领导的开源Web服务器软件,其根本的目的是提供一个专门针对高性能网站,安全、快速、兼容性好并且灵活的web server环境。具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点。支持FastCGI, CGI, Auth, 输出压缩(output compress), URL重写, Alias等重要功能,而Apache之所以流行,很大程度也是因为功能丰富,在lighttpd上很多功能都有相应的实现了。

    安装:
    环境:Ubuntu 11
    下载安装:

    apt-get install lighttpd

    配置文件在:/etc/lighttpd/lighttpd.conf

    View Code
    #指定要装载的模块
    server.modules = (
            "mod_access",
            "mod_alias",
            "mod_compress",
            "mod_redirect",
    #       "mod_rewrite",
    )
    #fastcgi的配置
    #fastcgi.server = ( ".php" => ((
    #                    "host" => "127.0.0.1",
    #                    "port" => "9000",
    #                 )))
    
    fastcgi.server = ( ".php" => ((
                        "socket" => "/tmp/php.socket",
                     )))
    #设置网页服务器的根目录。配置文件必需指定该参数
    server.document-root        = "/var/www"
    #设置上传目录。缺省为 /var/tmp 
    server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
    # 设置错误日志文件的路径
    server.errorlog             = "/var/log/lighttpd/error.log"
    # 设置Lighttpd进程的路径
    server.pid-file             = "/var/run/lighttpd.pid"
    #指定可以运行服务器的用户名和组名,要求lighttp以root权限启动。
    server.username             = "www-data"
    server.groupname            = "www-data"
    #默认读取的文件,index.lighttpd.html 是安装默认的文件,按照顺序来查找执行。
    index-file.names            = ( "index.php", "index.html",
                                    "index.htm", "default.htm",
                                   " index.lighttpd.html","a.html")
    
    url.access-deny             = ( "~", ".inc" )
    # 禁止访问通过扩展的文件的某些类型的源
    static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
    server.port = 80
    server.bind = "192.168.200.201"
    
    ## Use ipv6 if available
    #include_shell "/usr/share/lighttpd/use-ipv6.pl"
    #为目录列表设置编码
    dir-listing.encoding        = "utf-8"
    #启用或则禁用目录列表
    server.dir-listing          = "disable"
    
    compress.cache-dir          = "/var/cache/lighttpd/compress/"
    compress.filetype           = ( "application/x-javascript", "text/css", "text/html", "text/plain" )
    
    #虚拟主机1
    $HTTP["host"] == "bbs.db2.com" {
    server.name = "bbs.db2.com"
    server.document-root = "/var/www/db2/"
    accesslog.filename = "/var/www/db2/access.log"
    accesslog.format = "%{X-Forwarded-For}i %v %u %t \"%r\" %s %b \"%{User-Agent}i\" \"%{Referer}i\""
    }
    #虚拟主机2
    $HTTP["host"] == "bbs.abc.com" {
    server.name = "bbs.abc.com"
    server.document-root = "/var/www/abc/"
    accesslog.filename = "/var/www/abc/access.log"
    }
    #虚拟主机3
    $HTTP["host"] == "bbs.xyz.com" {
    server.name = "bbs.xyz.com"
    server.document-root = "/var/www/xyz/"
    accesslog.filename = "/var/www/xyz/access.log"
    }
    
    
    include_shell "/usr/share/lighttpd/create-mime.assign.pl"
    include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

     

    安装完毕之后,做浏览器里面输入:服务器ip地址,会出现“Index of /”的列表(server.dir-listing = "enable"),点 index.lighttpd.html 文件,直接加载了该html文件的内容。这个文件刚好是在参数 server.document-root 指定的目录中。要是server.dir-listing = "disable"的话,直接输入ip则会出现404报错,需要输入ip+index.lighttpd.html才行。
    自己可以创建一个html文件,如a.html放到server.document-root指定的目录中,再修改 index-file.names 参数,让a.html包含进去,再重启lighttpd,会让a.html展示出来。
    到此,lighttpd基本功能已经成功安装。

    使用:

    1:多虚拟主机的配置:
    1个IP地址配置多个域名,即1个IP多各域名。
    首先,需要修改host名称,如:

    vi /etc/hosts
    192.168.220.201 bbs.abc.com --添加
    192.168.220.201 bbs.xyz.com --添加

    改完之后,需要重启networking 才能生效。
    接着,在/etc/lighttpd/lighttpd.conf 配置文件中添加虚拟主机:

    $HTTP["host"] == "bbs.db2.com" {
    server.name = "bbs.db2.com"
    server.document-root = "/var/www/db2/"
    accesslog.filename = "/var/www/db2/access.log"
    accesslog.format = "%{X-Forwarded-For}i %v %u %t \"%r\" %s %b \"%{User-Agent}i\" \"%{Referer}i\""
    }
    
    $HTTP["host"] == "bbs.abc.com" {
    server.name = "bbs.abc.com"
    server.document-root = "/var/www/abc/"
    accesslog.filename = "/var/www/abc/access.log"
    }
    
    $HTTP["host"] == "bbs.xyz.com" {
    server.name = "bbs.xyz.com"
    server.document-root = "/var/www/xyz/"
    accesslog.filename = "/var/www/xyz/access.log"
    }

    再在/var/www/的目录下创建 db2,abc,xyz目录,把之前写的a.html放入到这些目录中。
    最后,在浏览器里面输入bbs.db2.com,bbs.xyz.com,bbs.abc.com 看看什么效果。

    当然上面的虚拟主机可以单独写一个文件,再在include 到 lighttpd.conf 中。

    2,lighttpd 支持 PHP,即 lighttpd + php-fpm

     安装:

    apt-get install php5-fpm php5

     确保 /etc/php5/fpm/php.ini 中的cgi.fix_pathinfo=1

    cd /etc/lighttpd/conf-available/
    cp 15-fastcgi-php.conf 15-fastcgi-php-spawnfcgi.conf
    
    vi 15-fastcgi-php.conf
    fastcgi.server += ( ".php" =>
            ((
                    "bin-path" => "/usr/bin/php-cgi",
                    "socket" => "/tmp/php.socket",
                    "max-procs" => 1,
                    "bin-environment" => (
                            "PHP_FCGI_CHILDREN" => "4",
                            "PHP_FCGI_MAX_REQUESTS" => "10000"
                    ),
                    "bin-copy-environment" => (
                            "PATH", "SHELL", "USER"
                    ),
                    "broken-scriptfilename" => "enable"
            ))
    )

    开启lighttpd 的 fastcgi 配置,执行:

    lighttpd-enable-mod fastcgi
    lighttpd-enable-mod fastcgi-php

    出现2个软链接:
    ls -l /etc/lighttpd/conf-enabled
    lrwxrwxrwx 1 root root 33 Nov 14 04:52 10-fastcgi.conf -> ../conf-available/10-fastcgi.conf
    lrwxrwxrwx 1 root root 37 Nov 14 04:52 15-fastcgi-php.conf -> ../conf-available/15-fastcgi-php.conf

    修改/etc/lighttpd/lighttpd.conf 配置:添加

    fastcgi.server = ( ".php" => ((
                        "host" => "127.0.0.1",
                        "port" => "9000",
                     )))

    重启lighttpd

    /etc/init.d/lighttpd force-reload

    最后写一个php页面:index.php ,该文件放到/var/www/xyz/目录下面。
    在index-file.names配置文件中的index-file.names选项里添加该php文件。注:index-file.names 选项里当有多个文件时,会按照文件的顺序来去查找页面。

    <?php
    phpinfo();
    ?>

    现在可以在浏览器里面输入查看页面了。
    上面的是通过TCP进行通信链接的,端口是9000
    netstat -ap | grep php
    tcp        0      0 localhost:9000          *:*                     LISTEN      1288/php-fpm.conf
    或则:
    netstat -ntl | grep 9000

    那么通过套接字如何进行呢?

    1,修改/etc/php5/fpm/pool.d/www.conf文件,把
    listen = 127.0.0.1:9000
    改成
    listen = /tmp/php.socket
    2,修改/etc/lighttpd/lighttpd.conf文件,把
    fastcgi.server = ( ".php" => ((
                        "host" => "127.0.0.1",
                        "port" => "9000",

                     )))
    改成
    fastcgi.server = ( ".php" => ((
                        "socket" => "/tmp/php.socket",
                     )))

    最后重启php和lighttpd:
    /etc/init.d/php5-fpm restart
    /etc/init.d/lighttpd restart

    赶紧在浏览器里面输入地址看看是否OK。

     

    这里只是简单的介绍了lighttpd的一些功能,还有很多功能没有写出来,如:跳转等。后期当自己遇到的时候再学再完善笔记。

    参考:
    http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ConfigurationOptions
    http://www.howtoforge.com/installing-lighttpd-with-php5-php-fpm-and-mysql-support-on-ubuntu-12.10
    http://i.linuxtoy.org/docs/guide/ch23s03.html


    ~~~~~~~~~~~~~~~ 万物之中,希望至美 ~~~~~~~~~~~~~~~
  • 相关阅读:
    人生转折点:弃文从理
    人生第一站:大三暑假实习僧
    监听器启动顺序和java常见注解
    java常识和好玩的注释
    182. Duplicate Emails (Easy)
    181. Employees Earning More Than Their Managers (Easy)
    180. Consecutive Numbers (Medium)
    178. Rank Scores (Medium)
    177. Nth Highest Salary (Medium)
    176. Second Highest Salary(Easy)
  • 原文地址:https://www.cnblogs.com/zhoujinyi/p/2769424.html
Copyright © 2011-2022 走看看