lighttpd学习笔记

1、安装

# 更新软件包列表
apt update
# 安装lighttpd
apt install lighttpd
# 启动lighttpd
systemctl start lighttpd
# 停止lighttpd
systemctl stop lighttpd
# 使得lighttpd开机自启
systemctl enable lighttpd
# 查看lighttd状态
systemctl status lighttpd
# 修改 /var/www/html 目录下的文件所有者和所属组为 www-data,通常在 Lighttpd 下,网站的文件所有者和所属组都会设置为 www-data
chown -R www-data:www-data /var/www/html
# 修改 /var/www/html 目录下的文件和文件夹权限,使其对所有用户可读可执行,对所有用户组可读可执行,但只有所有者可写
chmod -R 755 /var/www/html
# 进入 /var/www/html 目录
cd /var/www/html
# 创建index.html
echo "Welcome to Lighttpd" | sudo tee /var/www/html/index.html

2、http设置

# 新建配置文件
vi /etc/lighttpd/conf-available/docker.gujiakai.top.conf

# 添加以下内容
$HTTP["url"] =~ "^/book($|/)" {
    server.document-root = "/var/www/html/docker.gujiakai.top/book/"
}

# 创建软链接,将配置文件启用
ln -s /etc/lighttpd/conf-available/docker.gujiakai.top.conf /etc/lighttpd/conf-enabled/
# 重启lighttpd服务
systemctl restart lighttpd

3、https设置

# 安装acme脚本
curl https://get.acme.sh | sh -s [email protected]
# 重新加载配置文件
source .bashrc
# 编辑配置文件
vi .bashrc
# 导入cloudflare相关配置
export CF_Token="xxxxxx"
export CF_Account_ID="xxxxxx"
export CF_Zone_ID="xxxxxx"
# dns模式申请ssl证书
acme.sh --issue --dns dns_cf -d docker.gujiakai.top
# 创建目录用于存放证书和密钥
mkdir /etc/lighttpd/ssl
# 安装证书和密钥,并且配置lighttpd自动重新加载
acme.sh --install-cert -d docker.gujiakai.top \
--key-file       /etc/lighttpd/ssl/docker.gujiakai.top.key.pem  \
--fullchain-file /etc/lighttpd/ssl/docker.gujiakai.top.cert.pem \
--reloadcmd     "service lighttpd force-reload"
# 编辑lighttpd配置文件
vi /etc/lighttpd/conf-available/docker.gujiakai.top.conf
# 配置重定向http到https,以及ssl配置
$HTTP["host"] == "docker.gujiakai.top" {
    # Redirect HTTP to HTTPS
    $HTTP["scheme"] == "http" {
        url.redirect = (".*" => "https://%0$0")
    }

    # SSL configuration
    $SERVER["socket"] == ":443" {
        ssl.engine = "enable"
        ssl.pemfile = "/etc/lighttpd/ssl/docker.gujiakai.top.cert.pem"
        ssl.privkey = "/etc/lighttpd/ssl/docker.gujiakai.top.key.pem"
        ssl.honor-cipher-order = "enable"
        ssl.cipher-list = "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"
    }

    server.document-root = "/var/www/html/docker.gujiakai.top/book/"
}
# 重启lightttpd
systemctl restart lighttpd

4、反向代理

# 切换目录
cd /etc/lighttpd/conf-available
# 编辑配置文件
vi chat.njxzc.top.conf
# 配置文件内容,启用反向代理
$HTTP["host"] == "chat.njxzc.top" {
    proxy.server = (
        "" => (
            "chat-backend" => (
                "host" => "127.0.0.1",
                "port" => 3000
            )
        )
    )
}
# 创建软链接
ln -s /etc/lighttpd/conf-available/chat.njxzc.top.conf /etc/lighttpd/conf-enabled/chat.njxzc.top.conf
# 编辑主配置文件
vi /etc/lighttpd/lighttpd.conf
# 启用mod_proxy模块,以支持反向代理功能
server.modules += ("mod_proxy")
# 重启lighttpd服务
systemctl restart lighttpd
# cloudflare配置
export CF_Token="xxxxxx"
export CF_Account_ID="xxxxxx"
# dns模式申请ssl证书
acme.sh --issue --dns dns_cf -d chat.njxzc.top
# 安装证书,并配置lighttpd重新加载命令
acme.sh --install-cert -d chat.njxzc.top \
--key-file       /etc/lighttpd/ssl/chat.njxzc.top.key.pem  \
--fullchain-file /etc/lighttpd/ssl/chat.njxzc.top.cert.pem \
--reloadcmd     "service lighttpd force-reload"
# 重启lighttpd服务
sudo systemctl restart lighttpd