Files
tg_tinder_bot/bin/install_ubuntu.sh
2025-11-06 15:09:15 +09:00

191 lines
6.0 KiB
Bash

#!/bin/bash
# Script for installing Telegram Tinder Bot on Ubuntu
# This script automates the deployment process on a fresh Ubuntu server
# Usage: ./bin/install_ubuntu.sh [--with-nginx] [--with-ssl domain.com]
set -e # Exit immediately if a command exits with a non-zero status
# Define colors for pretty output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Default settings
INSTALL_NGINX=false
INSTALL_SSL=false
DOMAIN=""
# Parse command line arguments
for arg in "$@"; do
if [[ "$arg" == "--with-nginx" ]]; then
INSTALL_NGINX=true
elif [[ "$arg" == "--with-ssl" ]]; then
INSTALL_SSL=true
# Next argument should be domain
shift
DOMAIN="$1"
if [[ -z "$DOMAIN" || "$DOMAIN" == --* ]]; then
echo -e "${RED}Error: Domain name required after --with-ssl${NC}"
exit 1
fi
fi
shift
done
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} Telegram Tinder Bot Ubuntu Installer ${NC}"
echo -e "${BLUE}========================================${NC}"
# Check if running on Ubuntu
if [ -f /etc/os-release ]; then
source /etc/os-release
if [[ "$ID" != "ubuntu" ]]; then
echo -e "${RED}Error: This script is designed for Ubuntu. Current OS: $ID${NC}"
exit 1
else
echo -e "${GREEN}Detected Ubuntu ${VERSION_ID}${NC}"
fi
else
echo -e "${RED}Error: Could not detect operating system${NC}"
exit 1
fi
# Check for root privileges
if [ "$(id -u)" -ne 0 ]; then
echo -e "${RED}Error: This script must be run as root${NC}"
echo -e "Please run: ${YELLOW}sudo $0 $*${NC}"
exit 1
fi
echo -e "\n${BLUE}Step 1: Updating system packages...${NC}"
apt update && apt upgrade -y
echo -e "${GREEN}✓ System packages updated${NC}"
echo -e "\n${BLUE}Step 2: Installing dependencies...${NC}"
apt install -y curl wget git build-essential postgresql postgresql-contrib
echo -e "${GREEN}✓ Basic dependencies installed${NC}"
echo -e "\n${BLUE}Step 3: Installing Node.js...${NC}"
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
echo -e "${GREEN}✓ Node.js $(node --version) installed${NC}"
echo -e "${GREEN}✓ npm $(npm --version) installed${NC}"
echo -e "\n${BLUE}Step 4: Setting up PostgreSQL...${NC}"
systemctl start postgresql
systemctl enable postgresql
echo -e "\n${BLUE}Please enter a strong password for the database user:${NC}"
read -s DB_PASSWORD
echo
# Create database and user
su - postgres -c "psql -c \"CREATE DATABASE tg_tinder_bot;\""
su - postgres -c "psql -c \"CREATE USER tg_bot WITH PASSWORD '$DB_PASSWORD';\""
su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE tg_tinder_bot TO tg_bot;\""
echo -e "${GREEN}✓ PostgreSQL configured${NC}"
echo -e "\n${BLUE}Step 5: Setting up application directory...${NC}"
mkdir -p /opt/tg_tinder_bot
chown $SUDO_USER:$SUDO_USER /opt/tg_tinder_bot
echo -e "${GREEN}✓ Application directory created${NC}"
echo -e "\n${BLUE}Step 6: Installing PM2...${NC}"
npm install -g pm2
echo -e "${GREEN}✓ PM2 installed${NC}"
echo -e "\n${BLUE}Step 7: Please enter your Telegram Bot Token:${NC}"
read BOT_TOKEN
echo -e "\n${BLUE}Step 8: Creating environment file...${NC}"
cat > /opt/tg_tinder_bot/.env << EOL
# Bot settings
BOT_TOKEN=${BOT_TOKEN}
LOG_LEVEL=info
# Database settings
DB_HOST=localhost
DB_PORT=5432
DB_USER=tg_bot
DB_PASSWORD=${DB_PASSWORD}
DB_NAME=tg_tinder_bot
EOL
chmod 600 /opt/tg_tinder_bot/.env
chown $SUDO_USER:$SUDO_USER /opt/tg_tinder_bot/.env
echo -e "${GREEN}✓ Environment file created${NC}"
echo -e "\n${BLUE}Step 9: Setting up systemd service...${NC}"
cat > /etc/systemd/system/tg-tinder-bot.service << EOL
[Unit]
Description=Telegram Tinder Bot
After=network.target postgresql.service
[Service]
Type=simple
User=${SUDO_USER}
WorkingDirectory=/opt/tg_tinder_bot
ExecStart=/usr/bin/node dist/bot.js
Restart=on-failure
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=tg-tinder-bot
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
EOL
echo -e "${GREEN}✓ Systemd service created${NC}"
if [ "$INSTALL_NGINX" = true ]; then
echo -e "\n${BLUE}Step 10: Installing and configuring Nginx...${NC}"
apt install -y nginx
# Create Nginx configuration
cat > /etc/nginx/sites-available/tg_tinder_bot << EOL
server {
listen 80;
server_name ${DOMAIN:-_};
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;
}
}
EOL
# Enable site
ln -sf /etc/nginx/sites-available/tg_tinder_bot /etc/nginx/sites-enabled/
nginx -t && systemctl restart nginx
echo -e "${GREEN}✓ Nginx configured${NC}"
if [ "$INSTALL_SSL" = true ] && [ ! -z "$DOMAIN" ]; then
echo -e "\n${BLUE}Step 11: Setting up SSL with Certbot...${NC}"
apt install -y certbot python3-certbot-nginx
certbot --nginx --non-interactive --agree-tos --email admin@${DOMAIN} -d ${DOMAIN}
echo -e "${GREEN}✓ SSL certificate installed${NC}"
fi
fi
echo -e "\n${BLUE}Step 12: Clone your repository${NC}"
echo -e "Now you should clone your repository to /opt/tg_tinder_bot"
echo -e "Example: ${YELLOW}git clone https://your-git-repo-url.git /opt/tg_tinder_bot${NC}"
echo -e "Then run the update script: ${YELLOW}cd /opt/tg_tinder_bot && ./bin/update.sh${NC}"
echo -e "\n${GREEN}========================================${NC}"
echo -e "${GREEN} Installation completed! ${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e "Next steps:"
echo -e "1. Clone your repository to ${YELLOW}/opt/tg_tinder_bot${NC}"
echo -e "2. Run the update script to set up the application"
echo -e "3. Start the service with: ${YELLOW}sudo systemctl start tg-tinder-bot${NC}"
echo -e "4. Enable auto-start with: ${YELLOW}sudo systemctl enable tg-tinder-bot${NC}"
echo -e "5. Check status with: ${YELLOW}sudo systemctl status tg-tinder-bot${NC}"