zl程序教程

您现在的位置是:首页 >  其他

当前栏目

使用Caddy作为Web服务器

2023-03-14 11:22:21 时间

Caddy是一个由Go语言编写的Web服务器软件,可以自动实现HTTPS部署。

使用caddy作为web服务器

为什么使用Caddy

自己的博客想要使用https,懒得去更新证书,Caddy可以快速解决我的问题。

而且相比nginx,Caddy部署起来更简单,就一个二进制文件;

CaddyFile配置更简单,很多都是默认配置,只要根据自己的情况,添加需要的参数即可。

功能丰富,但是很多我暂时用不到,用到再通过插件的方式添加即可。

对于一个博客应用来说完全够用了。

安装Caddy

在CentOS上可以直接使用yum安装的,也可以下载对应平台的二进制进行安装。

使用caddy作为web服务器

安装命令:

 

  1. yum install yum-plugin-copr -y 
  2. yum copr enable @caddy/caddy -y 
  3. yum install caddy -y 

 

配置

配置文件默认是在/etc/caddy/Caddyfile,配置文件只要修改好自己的域名,如果需要https直接填写上http://xxx就可以了,root执行自己的网站目录即可。

我的网站是hugo生成的静态站,直接配置目录。

如果是php可以直接转发的php_fastcgi。

 

  1. # The Caddyfile is an easy way to configure your Caddy web server. 
  2. # Unless the file starts with a global options block, the first 
  3. # uncommented line is always the address of your site. 
  4. To use your own domain name (with automatic HTTPS), first make 
  5. # sure your domain's A/AAAA DNS records are properly pointed to 
  6. # this machine's public IP, then replace ":80" below with your 
  7. # domain name
  8.  
  9. www.opcai.top https://www.opcai.top http://opcai.top https://opcai.top { 
  10.          
  11.         # Set this path to your site's directory. 
  12.         root * /data/webroot 
  13.  
  14.         # Enable the static file server. 
  15.         file_server 
  16.          
  17.         # Another common task is to set up a reverse proxy: 
  18.         # reverse_proxy /back/* localhost:8080 
  19.  
  20.         # Or serve a PHP site through php-fpm: 
  21.         # php_fastcgi localhost:9000 
  22.  
  23. # Refer to the Caddy docs for more information: 
  24. # https://caddyserver.com/docs/caddyfile 

 

服务管理

 

  1. systemctl enable --now caddy  # 启动并设置开机启动 
  2. systemctl reload caddy # 重载配置 

 

总结

Caddy还有很多强大的功能,慢慢探索。