Code Server 浏览器版 VS Code 部署教程

Code Server 是由 Coder 公司开发的开源项目,可以将 VS Code 运行在服务器上,通过浏览器进行访问和使用。这意味着你可以在任何设备上,包括平板电脑甚至手机上,获得完整的 VS Code 开发体验。本文将详细介绍如何在搬瓦工 VPS 上部署 Code Server,配置安全访问,并安装常用开发工具链。

一、Code Server 的优势

  • 随时随地开发:只需要浏览器即可编写代码,不受设备限制。
  • 统一开发环境:所有开发环境配置都在服务器上,换电脑不用重新配置。
  • 服务器算力:编译和构建任务在服务器上执行,本地设备性能要求低。
  • 插件生态:支持绝大多数 VS Code 插件,体验与桌面版接近。
  • 终端集成:内置终端可直接操作服务器,无需额外 SSH 连接。

二、环境准备

  • 搬瓦工 VPS 一台,建议 1GB 以上内存(使用优惠码 NODESEEK2026 享受 6.77% 折扣)。
  • 已安装 Docker 和 Docker Compose(参考 Docker 安装教程)。
  • 域名一个(强烈推荐,配置 HTTPS 后更安全)。

2.1 创建项目目录

mkdir -p /opt/code-server/{config,workspace}
cd /opt/code-server

三、Docker Compose 部署

cat > /opt/code-server/docker-compose.yml <<'EOF'
version: '3.8'

services:
  code-server:
    image: lscr.io/linuxserver/code-server:latest
    restart: always
    ports:
      - "8443:8443"
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Asia/Shanghai
      - PASSWORD=your_strong_password_here
      - SUDO_PASSWORD=your_sudo_password_here
      - DEFAULT_WORKSPACE=/config/workspace
    volumes:
      - /opt/code-server/config:/config
      - /opt/code-server/workspace:/config/workspace
EOF

安全提醒:请将 PASSWORDSUDO_PASSWORD 替换为强密码。Code Server 暴露在互联网上,密码强度直接关系到服务器安全。

四、启动服务

cd /opt/code-server
docker compose up -d

查看运行状态:

docker compose ps
docker compose logs code-server

服务启动后,通过 http://VPS_IP:8443 访问,输入密码即可进入 VS Code 界面。

五、配置 Nginx 反向代理

Code Server 涉及代码编辑,强烈建议配置 HTTPS 以保护传输安全。

5.1 安装 Nginx 和 Certbot

apt update
apt install nginx certbot python3-certbot-nginx -y

5.2 创建 Nginx 配置

cat > /etc/nginx/sites-available/code-server <<'EOF'
server {
    listen 80;
    server_name code.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:8443;
        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 X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Accept-Encoding gzip;
    }

    client_max_body_size 200m;
}
EOF

ln -sf /etc/nginx/sites-available/code-server /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx

5.3 申请 SSL 证书

certbot --nginx -d code.yourdomain.com

六、安装开发工具链

Code Server 容器基于 Ubuntu,可以在内置终端中安装各种开发工具:

6.1 安装 Node.js

# 在 Code Server 终端中执行
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt install nodejs -y
node --version
npm --version

6.2 安装 Python

sudo apt install python3 python3-pip python3-venv -y
python3 --version

6.3 安装 Go

wget https://go.dev/dl/go1.22.0.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrc
go version

6.4 安装 Git

sudo apt install git -y
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

七、推荐插件

以下是一些推荐安装的 VS Code 插件:

  • Chinese Language Pack:中文语言包。
  • GitLens:增强 Git 功能。
  • Prettier:代码格式化。
  • ESLint:JavaScript 代码检查。
  • Python:Python 开发支持。
  • Remote Containers:容器化开发支持。

在 Code Server 中通过左侧扩展面板搜索安装即可。

八、安全加固

8.1 使用 HTTPS 哈希密码

可以使用哈希密码代替明文密码:

echo -n "your_password" | npx argon2-cli -e

然后在 docker-compose.yml 中使用 HASHED_PASSWORD 替代 PASSWORD

8.2 配置防火墙

确保只开放必要端口,Code Server 端口不直接暴露:

ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 22/tcp
ufw enable

由于已经配置了 Nginx 反向代理,可以将 Docker 端口绑定改为仅监听本地:

ports:
  - "127.0.0.1:8443:8443"

九、常见问题

插件安装失败

Code Server 使用 Open VSX 而非微软官方扩展市场。如果需要安装微软市场的插件,可以手动下载 .vsix 文件后在 Code Server 中安装。

终端中文乱码

sudo apt install locales -y
sudo locale-gen zh_CN.UTF-8
echo 'export LANG=zh_CN.UTF-8' >> ~/.bashrc

总结

Code Server 让你在任何设备上都能获得完整的 VS Code 开发体验,部署在搬瓦工 VPS 上还能利用服务器的算力进行编译构建。结合 Gitea 代码仓库,可以搭建完整的私有开发平台。选购搬瓦工 VPS 请访问 bwh81.net,使用优惠码 NODESEEK2026 享受 6.77% 折扣。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。