发布于 

安装Code-server使用并配置https

在自己的局域网服务器上安装了code-server,但是markdown预览无法正常显示。

查了一下,需要在https加密模式下才能在浏览器中正常使用功能。 自己的服务器是用WireGuard在虚拟局域网内连接,没有公网ip和域名, 最后用的是本地签名证书。

I 安装&启动

GitHub地址:

安装命令

现在你有两种方式来启动 code-server:

先手动启动生成默认配置文件

  1. 手动启动:如果你不希望 code-server 作为后台服务运行,你可以手动启动它:
1
code-server
  1. 作为系统服务启动:这将使 code-server 在后台运行,并在开机时自动启动。只需执行以下命令:
1
sudo systemctl enable --now code-server@root

在这里,$USER 是环境变量,表示当前用户名。如果你想要以其他用户身份运行 code-server,你需要将 $USER 替换为相应的用户名。

一旦 code-server 启动,你就可以通过 http://localhost:8080 (默认端口是 8080)在你的浏览器中访问它了。如果你在远程服务器上安装了 code-server,你需要替换 localhost 为你服务器的公网 IP 地址或域名,并确保防火墙设置允许流量通过 8080 端口。

首次访问 code-server 时,你会被要求输入一个密码。这个密码是为了保护你的 code-server 实例不被未授权访问。你可以在启动 code-server 时通过 --auth 选项设置密码,或者在 ~/.config/code-server/config.yaml 文件中进行配置。

查看配置文件:

1
cat ~/.config/code-server/config.yaml

password 字段的密码是随机生成的,你可以自己修改

停止Code-Server 服务

1
sudo systemctl stop code-server@root

改动配置文件(vim):

1
vim ~/.config/code-server/config.yaml 

修改 bind-addr 字段的IP为 本地局域网ip

重启Code-Server 服务

1
sudo systemctl restart code-server@root

然后在同一局域网下访问host地址,输入密码即可。

此时是http,没有加载证书

II 配置本地https

使用mkcert 生成相应网址的证书: 示例 本地地址为192.168.1.223

1
2
3
4
# $ mkcert [website addr]  192.168.1.223为服务器的内网网址
# -cert-file [filename] 生成对应crt文件的名称
# -cert-key [filename] 生成对应key文件的名称
$ mkcert -cert-file phone-code-server.crt -key-file phone-code-server.key 192.168.1.223 127.0.0.1

在/root/.local/share/code-server/下创建cert文件夹

1
mkdir  ~/.local/share/code-server/cert

复制刚刚生成的证书文件到~/.local/share/code-server/cert目录

1
2
cp /root/phone-code-server.key ~/.local/share/code-server/cert/
cp /root/phone-code-server.crt ~/.local/share/code-server/cert/

再次改动配置文件(vim):

1
vim ~/.config/code-server/config.yaml 

如下 修改证书位置,刚刚证书文件已经复制过去了

1
2
3
4
5
bind-addr: 192.168.1.223:8080
auth: password
password: df90f81c4355ffe98172dbe1
cert: /root/.local/share/code-server/cert/phone-code-server.crt
cert-key: /root/.local/share/code-server/cert/phone-code-server.key

重启Code-Server 服务

1
sudo systemctl restart code-server@root

因为是本地证书,所以我们要把证书导出,再安装到访问客户端才可以

1
2
3
4
5
mkcert  -install
mkcert -CAROOT
cd /home/
cp /root/.local/share/mkcert/rootCA.pem /home/ #将证书复制到home目录 方便一会导出
mv rootCA.pem rootCA.crt #重命名为crt,方便Windows导入

现在登陆 code-server web

选择资源管理器
选择”open folder”
切换到home目录
信任
选择刚刚重命名的证书文件 点击下载
Windows 安装,受信任的根证书
导入完成后,重新打开浏览器,打开code-server,可以看到已经生效了https
重新登陆后可以到拓展市场,安装简体中文包
选择菜单,终端,使用很方便!!!

完成