install.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. # Dynamically determine the project directory
  3. PROJECT_DIR=$(dirname "$(realpath "$0")")
  4. echo "Setting up the project in $PROJECT_DIR..."
  5. # Step 1: Create a virtual environment
  6. if [ ! -d "$PROJECT_DIR/venv" ]; then
  7. echo "Creating a virtual environment..."
  8. python3 -m venv "$PROJECT_DIR/venv"
  9. else
  10. echo "Virtual environment already exists."
  11. fi
  12. # Step 2: Activate the virtual environment
  13. echo "Activating the virtual environment..."
  14. source "$PROJECT_DIR/venv/bin/activate"
  15. # Step 3: Install dependencies
  16. echo "Installing dependencies..."
  17. pip install --upgrade pip
  18. pip install -r "$PROJECT_DIR/requirements.txt"
  19. # Ensure Gunicorn is installed
  20. if ! "$PROJECT_DIR/venv/bin/gunicorn" --version &>/dev/null; then
  21. echo "Installing Gunicorn..."
  22. pip install gunicorn
  23. else
  24. echo "Gunicorn is already installed."
  25. fi
  26. # Step 4: Create a systemd service
  27. SERVICE_FILE="/etc/systemd/system/gogmilestone.service"
  28. if [ ! -f "$SERVICE_FILE" ]; then
  29. echo "Creating systemd service..."
  30. sudo bash -c "cat > $SERVICE_FILE" <<EOL
  31. [Unit]
  32. Description=Gunicorn instance to serve gogmilestone
  33. After=network.target
  34. [Service]
  35. User=$USER
  36. Group=www-data
  37. WorkingDirectory=$PROJECT_DIR
  38. Environment="PATH=$PROJECT_DIR/venv/bin"
  39. ExecStart=$PROJECT_DIR/venv/bin/gunicorn -w 4 -b 0.0.0.0:4001 gogmilestone2:app
  40. [Install]
  41. WantedBy=multi-user.target
  42. EOL
  43. echo "Reloading systemd daemon..."
  44. sudo systemctl daemon-reload
  45. echo "Enabling gogmilestone service..."
  46. sudo systemctl enable gogmilestone
  47. echo "Starting gogmilestone service..."
  48. sudo systemctl start gogmilestone
  49. echo "Service created and started successfully."
  50. else
  51. echo "Service already exists. Skipping service creation."
  52. fi
  53. echo "Setup complete. You can now access the application. Visit http://localhost:4001 to see it in action. use systemctl [start/stop] gogmilestone.service to start/stop the service."