add auto install

This commit is contained in:
mei 2024-11-30 10:53:22 +08:00
parent 322f02366b
commit 472746ee90
3 changed files with 89 additions and 3 deletions

View File

@ -9,16 +9,23 @@
支持部署环境:
- Docker
- Systemd
部署方式:
自动(推荐):
```bash
curl -sSL https://git.mei.lv/mei/short-url/raw/branch/main/auto.sh -o auto.sh && bash auto.sh
```
手动:
```shell
git clone https://git.mei.lv/mei/short-url.git
docker build -t url-shortener:v0.1.0 .
docker build -t url-shortener:latest .
mkdir /opt/url-shortener
cd /opt/url-shortener
wget https://git.mei.lv/mei/short-url/raw/branch/main/docker-compose.yaml
docker compose up -d
```
部署成功后,服务会在 `8567` 端口上启动
### 迁移
替换 `data/` 目录下的 `shorturl.db` 文件即可

79
auto.sh Normal file
View File

@ -0,0 +1,79 @@
#!/bin/bash
# Ensure root
if [ "$EUID" -ne 0 ]; then
echo -e "\e[31m请使用 root 用户运行此脚本\033[0m"
exit 1
fi
# Install pkgs
echo -e "\e[32m正在安装必要的包\033[0m"
if command -v apt-get &> /dev/null; then
apt-get update && apt-get install -y wget git
elif command -v yum &> /dev/null; then
yum install -y wget git
else
echo -e "\e[31m无法自动安装必要的包 wget git,请自行手动安装\033[0m"
exit 1
fi
# Check Docker install
if ! command -v docker &> /dev/null; then
echo "Docker未安装正在为您安装Docker..."
bash <(curl -sSL https://linuxmirrors.cn/docker.sh) --source mirrors.tuna.tsinghua.edu.cn/docker-ce --source-registry registry.hub.docker.com --install-latested true
if ! command -v docker &> /dev/null; then
echo -e "\e[31mDocker安装失败请检查网络或手动安装Docker。\033[0m"
exit 1
fi
fi
# Get info
read -p "您希望使用的安装目录(默认为/opt/url-shortener): " InstallDir
read -p "您的域名(示例: https://u.mei.lv ): " DomainName
if [ -z "$InstallDir" ]; then
InstallDir="/opt/url-shortener"
fi
if [ ! -d "$InstallDir" ]; then
echo "目录 $InstallDir 不存在,正在创建..."
mkdir -p "$InstallDir"
if [ $? -ne 0 ]; then
echo -e "\e[31m创建目录失败\033[0m"
exit 1
fi
echo -e "\e[32m目录 $InstallDir 创建成功。\033[0m"
else
echo -e "\e[32m目录 $InstallDir 已存在。\033[0m"
fi
# Get Code
cd "$InstallDir" || { echo -e "\e[31m进入目录 $InstallDir 失败,请检查路径。\033[0m"; exit 1; }
git clone https://git.mei.lv/mei/short-url.git
if [ $? -ne 0 ]; then
echo -e "\e[31m克隆仓库失败请检查网络连接或权限。\033[0m"
exit 1
fi
wget https://git.mei.lv/mei/short-url/raw/branch/main/docker-compose.yaml
if [ $? -ne 0 ]; then
echo -e "\e[31m下载 docker-compose.yaml 文件失败,请检查网络连接或权限。\033[0m"
exit 1
fi
echo "NEXT_PUBLIC_BASE_URL=$DomainName" > "$InstallDir/.env"
if [ $? -ne 0 ]; then
echo -e "\e[31m创建 .env 文件失败\033[0m"
exit 1
fi
# Build Docker image
docker build -t url-shortener:latest .
if [ $? -ne 0 ]; then
echo -e "\e[31m构建 Docker 镜像失败\033[0m"
exit 1
fi
# Start image
docker compose up -d
if [ $? -ne 0 ]; then
echo -e "\e[31m启动 Docker 容器失败\033[0m"
exit 1
fi
echo -e "\e[32m安装和配置完成服务已启动在 8567 端口上。\033[0m"

View File

@ -2,7 +2,7 @@ version: "3"
services:
url-shortener:
container_name: url-shortener
image: url-shortener:v0.1.0
image: url-shortener:latest
restart: always
volumes:
- ./data:/app/data