从零用云服务器搭建一个网站

2021/3/6 linux

# 获得一台云服务器

华为云注册链接 (opens new window)

# 创建用户

# 创建一个有 root 权限的用户
sudo adduser demo
# 并将其加入 sudoers
# root ALL=(ALL) ALL
# demo ALL=(ALL) ALL
sudo nano /etc/sudoers

# 安装依赖

更新依赖

# 更新依赖
sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y

安装 docker,后续的部署全部通过 docker docker 安装文档 (opens new window)

# step 1: 安装必要的一些系统工具
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

使普通用户可以使用 docker

# 添加到用户组
sudo gpasswd -a $USER docker
# 重新登录
# 重启
service docker restart

安装 docker-compose (懒人必备)

sudo curl -L "https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

# 本地打包 网站 docker 镜像

vuepress (opens new window)

编写网站内容

mkdir vuepress-starter && cd vuepress-starter
yarn init
yarn add -D vuepress # npm install -D vuepress
mkdir docs && echo '# Hello VuePress' > docs/README.md
yarn docs:dev
yarn docs:build

# 编写 Dockerfile

FROM nginx:latest

RUN mkdir /app
COPY ./docs/.vuepress/dist /app/dist
COPY ./docker/conf.d /etc/nginx/conf.d

EXPOSE 80 443

打包镜像

docker build -f docker/Dockerfile -t xxx/vuepress-starter:latest .
docker push xxx/vuepress-starter:latest

# 服务器端编写 docker-compose.yml

version: '2'
services:
  vuepress-starter:
    container_name: vuepress-starter
    image: swr.cn-east-3.myhuaweicloud.com/huaweicloud-dreamhunter/vuepress-starter:latest
    restart: always
    ports:
      - '80:80'

启动, 冲

docker-compose up -d

# 附录

# 安装 zsh screenfetch

sudo apt install zsh screenfetch
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

# vscode 链接服务器

使用 ms-vscode-remote.remote-ssh 插件 让操作服务器和在本地一样方便

上次更新: 7/19/2022, 1:38:51 PM