MySQL / Redis / Node.js / Go / 前端静态资源部署完整指南
# 更新软件源 sudo apt update && sudo apt upgrade -y # 安装 MySQL 服务器 sudo apt install mysql-server -y # 启动 MySQL 服务 sudo systemctl start mysql sudo systemctl enable mysql # 安全初始化(设置 root 密码等) sudo mysql_secure_installation
# 登录 MySQL sudo mysql # 或使用密码登录 mysql -u root -p # 创建数据库 CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; # 创建用户并授权 CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON myapp.* TO 'myuser'@'localhost'; FLUSH PRIVILEGES;
# 添加 MySQL Yum 仓库 sudo yum install https://dev.mysql.com/get/mysql80-community-release-el8-4.noarch.rpm # 安装 MySQL sudo yum install mysql-community-server -y # 启动服务 sudo systemctl start mysqld sudo systemctl enable mysqld # 获取临时密码 sudo grep 'temporary password' /var/log/mysqld.log
# 编辑 MySQL 配置文件 sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf # 找到 bind-address,修改为: bind-address = 0.0.0.0 # 重启服务 sudo systemctl restart mysql # 在 MySQL 中授权远程用户 CREATE USER 'myuser'@'%' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON myapp.* TO 'myuser'@'%'; FLUSH PRIVILEGES;
# 登录 mysql -u root -p # 查看所有数据库 SHOW DATABASES; # 切换数据库 USE myapp; # 导入 SQL 文件 source /path/to/backup.sql # 或命令行导入 mysql -u myuser -p myapp backup.sql # 导出数据库 mysqldump -u myuser -p myapp > backup.sql # 查看连接数 SHOW STATUS LIKE 'Threads_connected';
sudo apt update sudo apt install redis-server -y # 启动并设置开机自启 sudo systemctl start redis sudo systemctl enable redis # 测试连接 redis-cli ping # 应返回 PONG
sudo nano /etc/redis/redis.conf # 修改绑定地址 bind 0.0.0.0 # 设置密码(可选但推荐) requirepass your_redis_password # 重启 sudo systemctl restart redis
sudo yum install epel-release -y sudo yum install redis -y sudo systemctl start redis sudo systemctl enable redis # 测试 redis-cli ping
# 连接 Redis redis-cli # 带密码连接 redis-cli -a your_password # 常用命令 SET key value GET key DEL key KEYS * FLUSHALL # 清空所有数据 # 查看内存使用 INFO memory
# 安装 nvm curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash # 重新加载 shell 配置 source ~/.bashrc # 安装 LTS 版本 Node.js nvm install --lts nvm use --lts nvm alias default lts/* # 验证 node -v npm -v
# Ubuntu / Debian curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - sudo apt-get install -y nodejs # CentOS / RHEL curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash - sudo yum install -y nodejs
npm install -g pm2 # 验证 pm2 --version
# 启动应用 pm2 start npm --name "myapp" -- start pm2 start dist/index.js --name "myapp" # 查看进程列表 pm2 list pm2 status # 查看日志 pm2 logs myapp pm2 logs myapp --lines 100 # 重启应用 pm2 restart myapp # 停止应用 pm2 stop myapp # 删除应用 pm2 delete myapp # 保存进程列表(重启后自动恢复) pm2 save # 设置开机自启 pm2 startup
# 启动集群模式,自动利用多核 CPU pm2 start npm --name "myapp" -- start -i max # 或指定实例数 pm2 start app.js -i 4
module.exports = { apps: [ { name: 'myapp', script: './dist/index.js', instances: 'max', exec_mode: 'cluster', env: { NODE_ENV: 'production', PORT: '3000' }, error_file: './logs/error.log', out_file: './logs/out.log', time: true, max_memory_restart: '500M', autorestart: true, watch: false } ] };
# 使用配置文件启动 pm2 start ecosystem.config.js # 停止所有 pm2 stop all
#!/bin/bash # deploy.sh APP_NAME="myapp" APP_DIR="/var/www/myapp" cd $APP_DIR # 拉取最新代码 git pull origin main # 安装依赖 npm install # 打包 npm run build # 重启应用 pm2 restart $APP_NAME
# 下载 Go(以 1.22 为例) wget https://go.dev/dl/go1.22.0.linux-amd64.tar.gz # 解压到 /usr/local sudo rm -rf /usr/local/go sudo tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz # 配置环境变量(添加到 ~/.bashrc 或 ~/.profile) echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc echo 'export GOPATH=$HOME/go' >> ~/.bashrc echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bashrc # 重新加载 source ~/.bashrc # 验证 go version
# 安装 g(Go 版本管理器) bash (curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer) echo 'source $HOME/.gvm/scripts/gvm' >> ~/.zshrc source ~/.zshrc # 安装指定版本 gvm install go1.22.5 # 切换版本 gvm use go1.21.0
myproject/ ├── main.go # 主入口 ├── go.mod # 依赖管理 ├── config/ │ └── config.go # 配置 └── ...
# 编译 Linux amd64 CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o myapp # 编译 macOS CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o myapp # 编译 Windows CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o myapp.exe
# 在项目目录 git clone https://github.com/yourname/myproject.git cd myproject go mod download go build -o myapp
sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My Go Application
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root/backend/go # 必须是你 pwd 输出的真实路径
ExecStart=/root/backend/go/im # 必须是二进制文件的完整路径
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
# 重新加载 systemd sudo systemctl daemon-reload # 启动服务 sudo systemctl start myapp # 设置开机自启 sudo systemctl enable myapp # 查看状态 sudo systemctl status myapp # 查看日志 sudo journalctl -u myapp -f
# 安装依赖 npm install # 开发环境预览 npm run dev # 生产环境构建 npm run build # 构建产物在 dist 目录 ls dist/
import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' export default defineConfig({ plugins: [vue()], base: './', // 使用相对路径,适合部署到任意目录 build: { outDir: 'dist', assetsDir: 'assets', sourcemap: false, // 生产环境关闭 sourcemap rollupOptions: { output: { manualChunks: { vendor: ['vue', 'vue-router', 'pinia'] } } } } })
# Ubuntu / Debian sudo apt update sudo apt install nginx -y # CentOS / RHEL sudo yum install nginx -y # 启动 sudo systemctl start nginx sudo systemctl enable nginx
sudo nano /etc/nginx/sites-available/default
server { listen 80; server_name example.com; # 替换为你的域名或 IP root /var/www/myapp/dist; # 静态文件目录 index index.html; # 处理 SPA 路由(Vue/React Router) location / { try_files $uri $uri/ /index.html; } # 静态资源缓存 location /assets/ { expires 1y; add_header Cache-Control "public, immutable"; } # Gzip 压缩 gzip on; gzip_types text/plain text/css application/json application/javascript; }
# 测试配置 sudo nginx -t # 重载配置 sudo systemctl reload nginx
# Ubuntu sudo apt install certbot python3-certbot-nginx -y # CentOS sudo yum install certbot python3-certbot-nginx -y
# 自动配置 Nginx sudo certbot --nginx -d example.com -d www.example.com # 仅获取证书(手动配置) sudo certbot certonly --nginx -d example.com
# 测试续期 sudo certbot renew --dry-run # Certbot 会自动添加定时任务,可查看 sudo systemctl list-timers | grep certbot
server { listen 80; server_name example.com www.example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; root /var/www/myapp/dist; index index.html; location / { try_files $uri $uri/ /index.html; } }
# 前端静态文件 /var/www/ ├── myapp/ │ ├── dist/ # 构建产物 │ └── ... # 后端应用 /opt/ ├── myapi/ # Go/Node 应用 │ ├── myapp │ ├── logs/ │ └── config.yaml └── ... # 数据库数据(MySQL) /var/lib/mysql/ # Redis 数据 /var/lib/redis/
# 设置目录所有者 sudo chown -R www-data:www-data /var/www/myapp # 设置目录权限 sudo chmod -R 755 /var/www/myapp # 日志目录需要写权限 sudo chmod -R 775 /var/log/myapp