在云服务部署 Astro 应用

配置 nodejs 运行环境

安装 nvm

使用 curl 安装(以当前最新版本为例,具体版本号请访问 nvm-sh/nvm 的 GitHub 页面获取):

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v[最新版本号]/install.sh | bash

注意替换[最新版本号]为实际的版本号。

使用 wget 安装(同上,注意版本号):

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v[最新版本号]/install.sh | bash

比如

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

这种方式操作简便,但安装速度和稳定性可能受网络条件影响。

配置 nvm 环境

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

安装 nodejs

# 安装 nodejs v22.8.0
nvm install v22.8.0

安装 screen

screen 包通常位于 EPEL(Extra Packages for Enterprise Linux)仓库中。你需要先启用 EPEL 仓库:

# 下载 epel-release 包(请根据实际版本调整)
wget https://mirrors.aliyun.com/epel/10/Everything/x86_64/Packages/e/epel-release-10-6.el10_0.noarch.rpm
wget https://mirrors.aliyun.com/epel/9/Everything/x86_64/Packages/e/epel-release-9-10.el9.noarch.rpm

# 安装 RPM 包
sudo dnf install -y ./epel-release-9-10.el9.noarch.rpm

修改镜像源

[epel]
name=Extra Packages for Enterprise Linux 9 - $basearch
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=https://download.example/pub/epel/9/Everything/$basearch/
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-9&arch=$basearch&infra=$infra&content=$contentdir
enabled=1
gpgcheck=1
countme=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-9

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 9 - $basearch - Debug
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=https://download.example/pub/epel/9/Everything/$basearch/debug/
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-9&arch=$basearch&infra=$infra&content=$contentdir
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-9
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 9 - $basearch - Source
# It is much more secure to use the metalink, but if you wish to use a local mirror
# place its address here.
#baseurl=https://download.example/pub/epel/9/Everything/source/tree/
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-source-9&arch=$basearch&infra=$infra&content=$contentdir
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-9
gpgcheck=1
  1. 安装 screens
sudo dnf install -y screen

启动 screens

screen -S <会话名>
# 例如
screen -S myscreen
# 列出所有会话
screen -ls
# 恢复会话
screen -r <会话名>
# 关闭会话
Ctrl + a + k
# 强制关闭
screen -X -S <会话ID> quit
# 退出
exit

安装配置 nginx

  1. 安装 nginx
sudo dnf install -y nginx
  1. 配置 nginx
sudo vim /etc/nginx/conf.d/vue.conf

安装配置 git

  1. 安装 git

    sudo dnf install -y git
  2. 配置 git

# 设置默认的 GitHub 账号信息
git config --global user.name "lovecodingnow"
git config --global user.email "lovecodingnow@163.com"
fatal: unable to access 'https://github.com/lovecodingnow/learn-astro.git/': HTTP/2 stream 1 was not closed cleanly before end of the underlying stream

如果遇到上述错误,可以尝试以下方法:

# 设置 HTTP/1.1
git config --global http.version HTTP/1.1
  1. 克隆项目
git clone https://github.com/lovecodingnow/learn-astro.git
  1. 安装依赖
cd learn-astro
# 设置 npm 镜像
npm config set registry https://registry.npmmirror.com
npm install
npm run build

5.配置 nginx:

# 配置 nginx
sudo vim /etc/nginx/conf.d/astro.conf
# 配置 nginx 反向代理
server {
    listen 80;
    server_name astro.lovecodingnow.com;
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
# 测试 nginx 配置
sudo nginx -t
# 重启 nginx
sudo systemctl restart nginx
# HTTP 重定向到 HTTPS
server {
    listen 80;
    server_name learn.ljhrecord.top www.unarrived.top;

    # 强制跳转 HTTPS
    return 301 https://$host$request_uri;
}

# HTTPS 配置(learn.ljhrecord.top)
server {
    listen 443 ssl;
    server_name learn.ljhrecord.top;

    # SSL 证书路径(根据实际路径调整)
    ssl_certificate /etc/nginx/ssl/learn.ljhrecord.top.pem;
    ssl_certificate_key /etc/nginx/ssl/learn.ljhrecord.top.key;

    # SSL 协议和加密套件
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # 网站根目录
    root /astro/learn-astro/dist;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    # HSTS(可选)
    add_header Strict-Transport-Security "max-age=63072000" always;
}

# HTTPS 配置(www.unarrived.top)
server {
    listen 443 ssl;
    server_name www.unarrived.top;

    # SSL 证书路径(根据实际路径调整)
    ssl_certificate /etc/nginx/ssl/www.unarrived.top.pem;
    ssl_certificate_key /etc/nginx/ssl/www.unarrived.top.key;

    # SSL 协议和加密套件
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # 网站根目录
    root /astro/learn-astro/dist;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    # HSTS(可选)
    add_header Strict-Transport-Security "max-age=63072000" always;
}
# 申请证书
sudo certbot --nginx -d learn.ljhrecord.top -d www.unarrived.top
sudo systemctl reload nginx  # 重载配置
sudo certbot certificates # 查看证书有效期和域名
sudo certbot renew --dry-run # 确保无报错
# 启动 nginx
sudo systemctl start nginx
# 开机自启
sudo systemctl enable nginx