Code-Server搭建指南

项目地址:

https://github.com/cdr/code-server

最低配置:

1 GB of RAM
2 cores

系统要求:

Linux, macOS, FreeBSD (amd64(x86-64), arm64; glibc >= v2.17, glibcxx >= v3.4.18)

安装命令:

curl -fsSL https://code-server.dev/install.sh | sh

Debian, Ubuntu

curl -fOL https://github.com/cdr/code-server/releases/download/v3.8.0/code-server_3.8.0_amd64.deb
sudo dpkg -i code-server_3.8.0_amd64.deb
sudo systemctl enable --now code-server@$USER
# Now visit http://127.0.0.1:8080. Your password is in ~/.config/code-server/config.yaml

Fedora, CentOS, RHEL, SUSE

curl -fOL https://github.com/cdr/code-server/releases/download/v3.8.0/code-server-3.8.0-amd64.rpm
sudo rpm -i code-server-3.8.0-amd64.rpm
sudo systemctl enable --now code-server@$USER
# Now visit http://127.0.0.1:8080. Your password is in ~/.config/code-server/config.yaml

Arch Linux

# Installs code-server from the AUR using yay.
yay -S code-server
sudo systemctl enable --now code-server@$USER
# Now visit http://127.0.0.1:8080. Your password is in ~/.config/code-server/config.yaml
# Installs code-server from the AUR with plain makepkg.
git clone https://aur.archlinux.org/code-server.git
cd code-server
makepkg -si
sudo systemctl enable --now code-server@$USER
# Now visit http://127.0.0.1:8080. Your password is in ~/.config/code-server/config.yaml

macOS

brew install code-server
brew services start code-server
# Now visit http://127.0.0.1:8080. Your password is in ~/.config/code-server/config.yaml

Standalone Releases(Other Linux, macOS; glibc >= 2.17 && glibcxx >= v3.4.18)

mkdir -p ~/.local/lib ~/.local/bin
curl -fL https://github.com/cdr/code-server/releases/download/v3.8.0/code-server-3.8.0-linux-amd64.tar.gz \
  | tar -C ~/.local/lib -xz
mv ~/.local/lib/code-server-3.8.0-linux-amd64 ~/.local/lib/code-server-3.8.0
ln -s ~/.local/lib/code-server-3.8.0/bin/code-server ~/.local/bin/code-server
PATH="~/.local/bin:$PATH"
code-server
# Now visit http://127.0.0.1:8080. Your password is in ~/.config/code-server/config.yaml

yarn, npm(FreeBSD, Linux; glibc < v2.17, glibcxx < v3.4.18, node v12; arm32)

yarn global add code-server
# Or: npm install -g code-server
code-server
# Now visit http://127.0.0.1:8080. Your password is in ~/.config/code-server/config.yaml

Docker

# This will start a code-server container and expose it at http://127.0.0.1:8080.
# It will also mount your current directory into the container as `/home/coder/project`
# and forward your UID/GID so that all file system operations occur as your user outside
# the container.
#
# Your $HOME/.config is mounted at $HOME/.config within the container to ensure you can
# easily access/modify your code-server config in $HOME/.config/code-server/config.json
# outside the container.
mkdir -p ~/.config
docker run -it --name code-server -p 127.0.0.1:8080:8080 \
  -v "$HOME/.config:/home/coder/.config" \
  -v "$PWD:/home/coder/project" \
  -u "$(id -u):$(id -g)" \
  -e "DOCKER_USER=$USER" \
  codercom/code-server:latest

For arm32 support there is a popular community maintained alternative:https://hub.docker.com/r/linuxserver/code-server

内网穿透(Frp):

将以下配置写入frpc.ini

[common]
# 填frp服务器地址
server_addr = 
# 填frp服务器端口(同frps.ini中bind_port)
server_port = 

# 日志等级
# trace, debug, info, warn, error
log_level = info

log_max_days = 3

# auth token
# 填frpServer的token
token = 

# set admin address for control frpc's action by http api such as reload
# 网页管理(仪表盘)的设置,可选填,不需要的话可删除该段
# 网页管理(仪表盘)的监听地址
admin_addr = 
# 网页管理(仪表盘)的监听端口
admin_port = 
# 网页管理(仪表盘)的用户名
admin_user = 
# 网页管理(仪表盘)的密码
admin_pwd = 
# Admin assets directory. By default, these assets are bundled with frpc.
# assets_dir = ./static

# connections will be established in advance, default value is zero
pool_count = 5

# if tcp stream multiplexing is used, default is true, it must be same with frps
tcp_mux = true

# decide if exit program when first login failed, otherwise continuous relogin to frps
# default is true
# true为第一次连接失败退出frpClient,false为不退出
login_fail_exit = false

# communication protocol used to connect to server
# now it supports tcp, kcp and websocket, default is tcp
protocol = tcp

# if tls_enable is true, frpc will connect frps by tls
tls_enable = true

# tls_cert_file = client.crt
# tls_key_file = client.key
# tls_trusted_ca_file = ca.crt

# specify udp packet size, unit is byte. If not set, the default value is 1500.
# This parameter should be same between client and server.
# It affects the udp and sudp proxy.
udp_packet_size = 1500

# if you want to expose multiple ports, add 'range:' prefix to the section name
# frpc will generate multiple proxies such as 'tcp_port_6010', 'tcp_port_6011' and so on.
# 我是直接把Code-Server的端口穿透出去,http和https我没穿成
[CodeServer]
type = tcp
# 填内网Code-Server服务器的ip地址
local_ip = 
# 填内网Code-Server服务器的端口
local_port = 
# 填要映射到frp服务器的端口
remote_port = 
# 与frp服务器传输时需不需要加密流量,true为加密,false为不加密
use_encryption = true
# 与frp服务器传输时需不需要压缩流量,true为压缩,false为不压缩
use_compression = true

HTTPS&Nginx反向代理

HTTPS(需要域名):

方案一:

  1. 安装 nginx certbot python3-certbot-nginx
  2. 将以下配置写入 /etc/nginx/sites-available/code-server (需要sudo):

    server {
         listen 80;
         listen [::]:80;
         server_name 你的域名;
    
         location / {
           proxy_pass http://localhost:8080/;
           proxy_set_header Host $host;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection upgrade;
           proxy_set_header Accept-Encoding gzip;
         }
    }
  3. 启用配置:

    sudo ln -s ../sites-available/code-server /etc/nginx/sites-enabled/code-server
    sudo certbot --non-interactive --redirect --agree-tos --nginx -d 你的域名 -m 你的邮箱地址m

方案二:

  1. 安装宝塔面板(其他有一键部署SSL功能的也行)

    curl -sSO http://download.bt.cn/install/install_panel.sh && bash install_panel.sh
  2. 网站--->添加站点(域名填你的域名,FTP和数据库选不创建,PHP版本选纯静态,提交)--->设置--->SSL--->Let's Encrypt(复选框打钩,申请)

反向代理:

方案一:

将以下配置写入创建的Nginx配置文件

方案二:

宝塔面板--->网站--->设置--->反向代理(目标URL:http://你的FrpServer的IP:remote_port,提交)--->找到宝塔的Nginx反向代理配置文件,比照以下配置修改

#PROXY-START/
location ~* \.(php|jsp|cgi|asp|aspx)$
{
    proxy_pass http://FrpServer的IP:穿透对应的端口(remote_port);
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
}
location /
{
    proxy_pass http://FrpServer的IP:穿透对应的端口(remote_port);
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection upgrade;
    proxy_set_header Accept-Encoding gzip;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    
    add_header X-Cache $upstream_cache_status;
    #Set Nginx Cache

    proxy_ignore_headers Set-Cookie Cache-Control expires;
    proxy_cache cache_one;
    proxy_cache_key $host$uri$is_args$args;
    proxy_cache_valid 200 304 301 302 1m;
    expires 12h;
}

#PROXY-END/

参考资料:

  1. https://github.com/cdr/code-server/blob/v3.8.0/doc/guide.md
  2. https://github.com/cdr/code-server/blob/v3.8.0/doc/install.md
  3. https://github.com/fatedier/frp/blob/dev/conf/frpc_full.ini
  4. 还有一篇找不到了,但是跟codeserver用WebSocket有关,会影响Nginx配置
无标签
评论区
头像
文章目录