自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在PHP中使用 Caddy2 協(xié)同服務(wù)

開發(fā) 前端
本文介紹了如何將PHP與Caddy Web服務(wù)器版本2系列集成,以及高級(jí)配置。它還將類似的配置與Apache和Nginx配置進(jìn)行了比較,以簡化從Apache和Nginx到Caddy的遷移。

Caddy Server 是一個(gè)模塊化的現(xiàn)代Web服務(wù)器平臺(tái),支持自動(dòng)HTTPS證書,QUIC和HTTP/2,Zstd和Brotli壓縮,以及各種現(xiàn)代功能以及經(jīng)典的Web服務(wù)器功能,如可配置的虛擬主機(jī),URL重寫和重定向,反向復(fù)制等。

本文介紹了如何將PHP與Caddy Web服務(wù)器版本2系列集成,以及高級(jí)配置。它還將類似的配置與Apache和Nginx配置進(jìn)行了比較,以簡化從Apache和Nginx到Caddy的遷移。

Caddy

安裝

官方安裝文檔:https://caddyserver.com/docs/install

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

確認(rèn)服務(wù)已啟動(dòng)

圖片圖片

服務(wù)命令

systemctl start caddy
caddy start
caddy run # Starts server and blocks indefinitely

systemctl stop caddy
caddy stop

systemctl reload caddy
caddy reload

systemctl restart caddy
caddy stop && caddy start

啟動(dòng)Caddy服務(wù)并使其在系統(tǒng)引導(dǎo)時(shí)自動(dòng)啟動(dòng)

sudo systemctl daemon-reload
sudo systemctl start caddy.service
sudo systemctl enable caddy.service

配置

Caddy可以在許多操作系統(tǒng)和基于Linux的發(fā)行版上使用。Caddy文檔解釋了如何安裝Caddy,并將其配置為隨服務(wù)器啟動(dòng)自動(dòng)運(yùn)行的服務(wù)/守護(hù)程序。

Caddy Server配備了安全和高性能的默認(rèn)配置,這使得它很容易配置最小的配置。

當(dāng)Caddy安裝并配置為系統(tǒng)服務(wù)時(shí),默認(rèn)的 /etc/caddy/Caddyfile 可以用作全局配置文件,并使用建議名稱 /etc/caddy/conf 的子目錄來包含各個(gè)站點(diǎn)的配置文件,類似于Apache和Nginx配置。

/etc/caddy
  ├── Caddyfile
  └── conf/
        └── caddy.tinywan.com.conf

主次配置調(diào)整

主配置:/etc/caddy/Caddyfile

{
    log default {
       format console
       output file /var/log/caddy/system.log
       exclude http.log.access
    }
}

import conf/*

次配置目錄:/etc/caddy/Caddyfile/conf。以后就可以在這個(gè)目錄下新增多個(gè)站點(diǎn)配置了,是不是和Nginx看起來一樣嘍!

例如:caddy.tinywan.com.conf

caddy.tinywan.com {
    # Set this path to your site's directory.
    root * /home/www/website/caddy

    # Enable the static file server.
    file_server
}

應(yīng)用

靜態(tài)網(wǎng)站托管

配置文件

caddy.tinywan.com {
        # Set this path to your site's directory.
        root * /home/www/website/caddy

        # Enable the static file server.
        file_server
}

訪問域名:https://caddy.tinywan.com/

官方默認(rèn)頁面

圖片圖片

自定義頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>開源技術(shù)小棧Caddy2</title>
</head>
<body>
<h1>2024 開源技術(shù)小棧Caddy2入門教程,專注于互聯(lián)網(wǎng)技術(shù)分享</h1>
</body>
</html>

圖片圖片

反向代理

proxy-caddy.tinywan.com {
  reverse_proxy localhost:6140
}

HTTP 重定向添加 www. 子域名

tinywan.com {
  redir https://www.{host}{uri}
}

www.tinywan.com {
    # Set this path to your site's directory.
    root * /home/www/website/full-stack/dist

    # Enable the static file server.
    file_server
}

訪問域名 tinywan.com 會(huì)被重定向到 https://www.tinywan.com/

PHP-FPM 集成

與Apache Web服務(wù)器和Nginx與PHP集成的方式類似,Caddy也使用Caddy的FastCGI反向代理與PHP集成。

其基本思想是,當(dāng)Caddy接收到一個(gè)應(yīng)該用PHP處理的請(qǐng)求(例如,一個(gè)對(duì)帶有 .php 擴(kuò)展名的文件名的請(qǐng)求)時(shí),請(qǐng)求被發(fā)送到PHP-FPM,在那里執(zhí)行PHP應(yīng)用程序,響應(yīng)被發(fā)送回Caddy以返回給用戶。

異常 502 Bad Gateway 錯(cuò)誤日志

2024/02/02 09:42:36.814 ERROR http.log.error dialing backend: dial unix /var/run/php8.0.7-fpm.sock: connect: permission denied

修改用戶組

sudo vim /lib/systemd/system/caddy.service

修改前

User=caddy
Group=caddy

修改后

User=www
Group=www

完整配置

caddy-php.tinywan.com {
  
    # Resolve the root directory for the app
    root * /home/www/website/demo/public

    # Allow caddy to serve static files
    file_server

    # Encode responses in zstd or gzip, depending on the
    # availability indicated by the browser.
    encode gzip

    # Configures multiple PHP-related settings
    php_fastcgi unix//var/run/php8.0.7-fpm.sock
}

目錄 /home/www/website/demo/public 新建文件index.php

<?php
echo "Hello 開源技術(shù)小棧";

訪問地址:https://caddy-php.tinywan.com/,該域名需要提前解析好哦!

圖片圖片

責(zé)任編輯:武曉燕 來源: 開源技術(shù)小棧
相關(guān)推薦

2020-07-28 09:45:29

LinuxCaddyHTTPS

2012-05-01 20:57:26

Sencha Touc

2011-08-10 09:31:41

Hibernateunion

2021-03-09 07:27:40

Kafka開源分布式

2015-08-27 09:46:09

swiftAFNetworkin

2021-06-09 09:36:18

DjangoElasticSearLinux

2022-05-17 08:25:10

TypeScript接口前端

2022-06-23 08:00:53

PythonDateTime模塊

2024-01-18 08:37:33

socketasyncio線程

2019-09-16 19:00:48

Linux變量

2014-07-02 09:47:06

SwiftCocoaPods

2020-04-09 10:18:51

Bash循環(huán)Linux

2024-09-06 11:34:15

RustAI語言

2020-11-30 11:55:07

Docker命令Linux

2022-01-12 08:49:33

CaddyWeb服務(wù)器Go語言

2025-03-21 09:58:59

Python數(shù)據(jù)類型安全

2021-09-10 10:30:22

Java代碼

2023-12-01 09:18:27

AxiosAxios 庫

2015-11-26 10:57:56

DockerOpen vSwitc

2022-10-25 09:07:28

Linuxxargs命令
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)