From b146127513a0191ebcd661eacd4dc90e1d600c64 Mon Sep 17 00:00:00 2001 From: PhongPham Date: Tue, 12 Aug 2025 07:39:58 +0700 Subject: [PATCH] feat: Update deployment script to support root user execution - Modified vps-deploy.sh to run as both root and non-root user - Added dynamic sudo command handling (SUDO_CMD variable) - Set appropriate service user (www-data for root, current user for non-root) - Updated all system commands to use dynamic sudo - Enhanced PostgreSQL setup for root execution - Updated PM2 configuration with proper user permissions - Added deployment update script with root support - Improved security and flexibility for VPS deployment --- vps-deploy.sh | 157 +++++++++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 65 deletions(-) diff --git a/vps-deploy.sh b/vps-deploy.sh index 97cb838..1f94a29 100755 --- a/vps-deploy.sh +++ b/vps-deploy.sh @@ -3,6 +3,7 @@ # 🚀 Laca City Website - Complete VPS Deployment Script # This script sets up your entire website on a VPS with domain configuration # Run this script on your VPS after uploading your project files +# Can be run as root user or regular user with sudo privileges set -e @@ -92,7 +93,7 @@ create_systemd_service() { local working_dir="$4" local user="$5" - sudo tee "/etc/systemd/system/$service_name.service" > /dev/null < /dev/null < /dev/null < /dev/null </dev/null | { cat; echo "0 3 * * * /usr/bin/certbot renew --quiet"; } | sudo crontab - + $SUDO_CMD crontab -l 2>/dev/null | { cat; echo "0 3 * * * /usr/bin/certbot renew --quiet"; } | $SUDO_CMD crontab - print_success "SSL certificate configured and auto-renewal setup" } @@ -280,12 +281,12 @@ setup_ssl() { setup_postgresql() { print_step "Setting up PostgreSQL..." - sudo apt update - sudo apt install -y postgresql postgresql-contrib + $SUDO_CMD apt update + $SUDO_CMD apt install -y postgresql postgresql-contrib # Start and enable PostgreSQL - sudo systemctl start postgresql - sudo systemctl enable postgresql + $SUDO_CMD systemctl start postgresql + $SUDO_CMD systemctl enable postgresql # Generate database password if not provided if [ -z "$DB_PASSWORD" ]; then @@ -294,13 +295,23 @@ setup_postgresql() { fi # Create database and user - sudo -u postgres psql </dev/null || true - sudo chown -R "$CURRENT_USER:$CURRENT_USER" "$PROJECT_DIR" + $SUDO_CMD cp -r ./* "$PROJECT_DIR/" 2>/dev/null || true + $SUDO_CMD chown -R "$SERVICE_USER:$SERVICE_USER" "$PROJECT_DIR" cd "$PROJECT_DIR" @@ -447,7 +458,9 @@ module.exports = { out_file: '/var/log/pm2/laca-city-backend-out.log', log_file: '/var/log/pm2/laca-city-backend.log', max_restarts: 10, - min_uptime: '10s' + min_uptime: '10s', + uid: '$SERVICE_USER', + gid: '$SERVICE_USER' }, { name: 'laca-city-frontend', @@ -464,15 +477,17 @@ module.exports = { out_file: '/var/log/pm2/laca-city-frontend-out.log', log_file: '/var/log/pm2/laca-city-frontend.log', max_restarts: 10, - min_uptime: '10s' + min_uptime: '10s', + uid: '$SERVICE_USER', + gid: '$SERVICE_USER' } ] }; EOF # Create log directory - sudo mkdir -p /var/log/pm2 - sudo chown -R "$CURRENT_USER:$CURRENT_USER" /var/log/pm2 + $SUDO_CMD mkdir -p /var/log/pm2 + $SUDO_CMD chown -R "$SERVICE_USER:$SERVICE_USER" /var/log/pm2 # Start applications with PM2 pm2 start ecosystem.config.js @@ -487,10 +502,10 @@ setup_monitoring() { print_step "Setting up monitoring..." # Install htop and other monitoring tools - sudo apt install -y htop iotop nethogs + $SUDO_CMD apt install -y htop iotop nethogs # Setup logrotate for application logs - sudo tee "/etc/logrotate.d/$PROJECT_NAME" > /dev/null < /dev/null < /dev/null < /dev/null </dev/null; echo "0 2 * * * /usr/local/bin/backup-$PROJECT_NAME") | crontab - @@ -560,6 +575,13 @@ set -e PROJECT_DIR="$PROJECT_DIR" +# Check if running as root +if [ "\$(whoami)" = "root" ]; then + SUDO_CMD="" +else + SUDO_CMD="sudo" +fi + echo "🚀 Starting deployment update..." # Stop applications @@ -589,9 +611,10 @@ npm run build pm2 restart all # Reload Nginx -sudo systemctl reload nginx +\$SUDO_CMD systemctl reload nginx echo "✅ Deployment update completed!" +EOF EOF chmod +x "$PROJECT_DIR/deploy-update.sh" @@ -641,16 +664,20 @@ final_checks() { main() { print_header "🚀 LACA CITY WEBSITE - VPS DEPLOYMENT" - # Check if running as root + # Check execution context and setup appropriate commands if [ "$CURRENT_USER" = "root" ]; then - print_error "Please don't run this script as root. Run as a regular user with sudo access." - exit 1 - fi - - # Check if sudo is available - if ! sudo -n true 2>/dev/null; then - print_error "This script requires sudo access. Please ensure you can run sudo commands." - exit 1 + print_warning "Running as root user. Some operations will be adjusted accordingly." + SUDO_CMD="" + # Set a default non-root user for services + SERVICE_USER="www-data" + else + # Check if sudo is available + if ! sudo -n true 2>/dev/null; then + print_error "This script requires sudo access. Please ensure you can run sudo commands." + exit 1 + fi + SUDO_CMD="sudo" + SERVICE_USER="$CURRENT_USER" fi # Collect configuration @@ -685,11 +712,11 @@ main() { # Update system print_step "Updating system packages..." - sudo apt update && sudo apt upgrade -y + $SUDO_CMD apt update && $SUDO_CMD apt upgrade -y # Install basic tools print_step "Installing basic tools..." - sudo apt install -y curl wget git unzip software-properties-common apt-transport-https ca-certificates gnupg lsb-release openssl + $SUDO_CMD apt install -y curl wget git unzip software-properties-common apt-transport-https ca-certificates gnupg lsb-release openssl # Setup firewall setup_firewall @@ -750,8 +777,8 @@ main() { echo -e " pm2 list # View running processes" echo -e " pm2 restart all # Restart all applications" echo -e " pm2 logs # View application logs" - echo -e " sudo systemctl status nginx # Check Nginx status" - echo -e " sudo certbot renew --dry-run # Test SSL renewal" + echo -e " $SUDO_CMD systemctl status nginx # Check Nginx status" + echo -e " $SUDO_CMD certbot renew --dry-run # Test SSL renewal" print_success "Setup completed! Your website should be accessible at https://$DOMAIN_NAME" }