OpenBSD(4.1): Lighttpd(vhosts), PHP(FastCGI)

来自 ChinaUnix Wiki

目录

Summary

About This Document

Wrote for: OpenBSD Version 4.1
image: Powered.by.OpenBSD.png Contact the author:
Author: Bibby
Website: http://www.OpenBSDonly.org
MSN: michaelbibby#hotmail.com
Mail/GTalk: michaelbibby#gmail.com

About OpenBSD

  • OpenBSD: "Only two remote holes in the default install, in more than 10 years!"
  • 购买 OpenBSD 光盘是支持 OpenBSD 持续发展的重要途径。
  • 用于实践、实验本文档的最佳方法是购买一套 OpenBSD 光盘,欢迎您通过 http://www.openbsd.org/orders.html#china 页面的联系方式与 Bibby 联系购买,或直接参与 OpenBSDonly.org 论坛。价格:
    • CD(4.x -release):¥180。
    • T-Shirt: ¥150。

ENV

  • OpenBSD: 4.1 -release
  • Lighttpd: 1.4.13p2(binary package)
  • PHP: 5.1.6(binary packages)
    • php5-fastcgi(use *UNOFFICIAL* ports). in 4.2 -release or -current, we have a fastcgi subpackage in ports/www/php5/core/.

Installation

# pkg_add lighttpd-1.4.13p2

Configuration: /etc/lighttpd.conf


server.username

server.username            = "www"

server.groupname

server.groupname           = "www"

Default Host

server.port

server.port                = 80

server.document-root

server.document-root        = "/var/www/lighttpd/vhosts/default/htdocs/"

server.errorlog

server.errorlog             = "/var/www/lighttpd/vhosts/default/logs/lighttpd.error.log"

accesslog.filename

accesslog.filename          = "/var/www/lighttpd/vhosts/default/logs/access.log"

Directories

创建目录:

# mkdir -p /var/www/lighttpd/vhosts/default/{htdocs,logs}

# chown -R www:www /var/www/lighttpd/vhosts/default/

Virtual Hosts

Modules

在 server.modules 部分将以下几个模块前的注释去掉:

  • mod_simple_vhost

Hosts

这里将配置以下几个虚拟主机:

  • www.bibby.org
  • www.bibby2.org

对应的目录分别为:

# For virtual host: www.bibby.org

/var/www/lighttpd/vhosts/bibby.org/
/var/www/lighttpd/vhosts/bibby.org/htdocs/
/var/www/lighttpd/vhosts/bibby.org/logs/
# For virtual host: www.bibby2.org

/var/www/lighttpd/vhosts/bibby2.org/
/var/www/lighttpd/vhosts/bibby2.org/htdocs/
/var/www/lighttpd/vhosts/bibby2.org/logs/

Directories & Permissions

创建目录并设置正确的权限:

# mkdir -p /var/www/lighttpd/vhosts/bibby{,2}.org/{htdocs,logs}

# chown -R www:www /var/www/lighttpd/

Configuration

$HTTP["host"] == "www.bibby.org" {
server.document-root = "/var/www/lighttpd/vhosts/bibby.org/htdocs/"
server.errorlog = "/var/www/lighttpd/vhosts/bibby.org/logs/error"
accesslog.filename = "/var/www/lighttpd/vhosts/bibby.org/logs/access"
server.error-handler-404 = “/index.php”
}
  
$HTTP["host"] == "www.bibby2.org" {
server.document-root = "/var/www/lighttpd/vhosts/bibby2.org/htdocs/"
server.errorlog = "/var/www/lighttpd/vhosts/bibby2.org/logs/error"
accesslog.filename = "/var/www/lighttpd/vhosts/bibby2.org/logs/access"
server.error-handler-404 = “/index.php”
}

PHP support(as FastCGI)

php5-fastcgi

从以下地址下载对应版本的 Ports: http://download.pureftpd.org/OpenBSD/misc/ports/

我这里用的是针对 OpenBSD 4.1 -release 的 php5.1-with-fastcgi-openbsd-4.1.tar.gz。

先将原来的 Ports 备份:

# mv /usr/ports/www/php5 /usr/ports/www/php5.bak

然后将下载的 Ports 解压缩:

# tar zxf php5.1-with-fastcgi-openbsd-4.1.tar.gz -C /usr/ports/www/

如果已经安装了 php5-core,则只需要进 /usr/ports/www/php5/fastcgi 目录进行安装即可:

# cd /usr/ports/www/php5/fastcgi/
# make install

安装的包的名字是:

php5-fastcgi-5.1.6p0.tgz

它只安装了一个文件:

/usr/local/bin/php-fcgi

这就是 Lighttpd 需要的,用于提供 PHP 支持的模块。

/etc/lighttpd.conf

在 server.modules 中将 mod_fastcgi 模块前的注释符号去掉,然后在配置文件的最末尾添加以下内容:

fastcgi.server = ( ".php" =>
    ( "localhost" =>
        (
            "socket" => "/tmp/php-fastcgi.socket",
            "bin-path" => "/usr/local/bin/php-fcgi"
        )
    )
)

这是指所有的虚拟主机都使用这个 fastcgi.server。如果需要为不同的虚拟主机设置不同的参数,只要放到对应的 $HTTP["host"] 的大括号内即可。比如:

$HTTP["host"] == "www.bibby.org" {
server.document-root = "/var/www/lighttpd/vhosts/bibby.org/htdocs/"
server.errorlog = "/var/www/lighttpd/vhosts/bibby.org/logs/error"
accesslog.filename = "/var/www/lighttpd/vhosts/bibby.org/logs/access"

fastcgi.server = ( ".php" =>
    ( "localhost" =>
        (
            "socket" => "/tmp/php-fastcgi.socket",
            "bin-path" => "/usr/local/bin/php-fcgi"
        )
    )
)
}

/etc/rc.conf.local

lighttpd_flags="-f /etc/lighttpd.conf"

/etc/rc.local

# Start Lighttpd web server.
if [ X"${lighttpd_flags}" != X"NO" ]; then
        echo -n ' lighttpd'
        /usr/local/sbin/lighttpd ${lighttpd_flags}
fi

Resource Links

个主工具