首页 小组 文章 搜索 用户

centos7或8源码编译安装caddy

2020-11-12 09:00:37
0
158

个人较喜欢编译安装,因为这样可以用到最新的稳定版本

先安装go及升级git(注centos8不用)

wget  https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh epel-release-latest-7.noarch.rpm

安装go

yum install go -y

升级git(注centos8不用升级),如果你需要使用tls1.3,centos7还需要升级openssl1.1,centos8默认tls1.3

yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm 
yum install git -y

接下来编译caddy了

git clone "https://github.com/caddyserver/caddy.git"
cd caddy/cmd/caddy/
go build 

需要go一会,等待完成,完成后复制文件到bin里去

mv caddy /usr/bin/

接下来创建用户

groupadd --system caddy 
useradd --system --gid caddy --create-home --home-dir /home/caddy/ --shell /usr/sbin/nologin --comment "Caddy web server" caddy

编辑/etc/caddy/Caddyfile文件,设置站点配置文件,网站配置文件将放在vhosts文件夹里

mkdir -p /etc/caddy/vhosts
echo 'import ./vhosts/*' > /etc/caddy/Caddyfile

设置路径的访问权限

chown -R caddy:caddy /etc/caddy

创建用于存放ssl证书的路径

mkdir /etc/ssl/caddy
chown -R root:caddy /etc/ssl/caddy
chmod 0770 /etc/ssl/caddy

创建用于存放日志的路径

mkdir /var/log/caddy
chown -R caddy:root /var/log/caddy 

创建启动文件

vi /lib/systemd/system/caddy.service

输入以下代码

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

保存并退出后创建软连

ln -s /lib/systemd/system/caddy.service /etc/systemd/system/multi-user.target.wants/caddy

载入启动

systemctl daemon-reload

启动

systemctl start caddy

停止

systemctl stop caddy

加入系统启动

systemctl enable caddy

完成

评论