diff --git a/README.md b/README.md index a7580a7..4e721d6 100644 --- a/README.md +++ b/README.md @@ -89,27 +89,67 @@ The application will be available at http://127.0.0.1:8000. **Production mode with Supervisor:** -The application comes with a Supervisor configuration file for production deployment: +The application comes with a Supervisor configuration file for production deployment. Follow these steps carefully: ```bash # Make the start script executable chmod +x start.sh -# Copy the supervisor configuration to the supervisor config directory -cp supervisor.conf /etc/supervisor/conf.d/fastapi-app.conf +# Test the start script manually to verify it works +./start.sh +# Press Ctrl+C to stop after verifying -# Restart supervisor to load the new configuration -supervisorctl reread -supervisorctl update +# Create necessary directories for logs (if not using default location) +sudo mkdir -p /var/log/supervisor +sudo chmod 777 /var/log/supervisor + +# Copy the supervisor configuration to the supervisor config directory +# If you're not running as root, you'll need sudo: +sudo cp supervisor.conf /etc/supervisor/conf.d/fastapi-app.conf + +# Ensure supervisor is installed and running +# On Debian/Ubuntu: +sudo apt-get update && sudo apt-get install -y supervisor +sudo systemctl enable supervisor +sudo systemctl start supervisor + +# Reload supervisor configuration (always do both commands) +sudo supervisorctl reread +sudo supervisorctl update # Check the status of the application -supervisorctl status app-8001 +sudo supervisorctl status app-8001 -# View logs if needed -tail -f /var/log/supervisor/app-8001_stdout.log -tail -f /var/log/supervisor/app-8001_stderr.log +# If it shows BACKOFF or FATAL status, check the logs: +sudo tail -f /var/log/supervisor/app-8001.log + +# To restart the application after making changes: +sudo supervisorctl restart app-8001 + +# To stop the application: +sudo supervisorctl stop app-8001 ``` +### Troubleshooting Supervisor Issues + +If you encounter issues with Supervisor: + +1. **Check permissions**: Ensure the user specified in `supervisor.conf` has access to all required directories + +2. **Check paths**: Verify all paths in `supervisor.conf` and `start.sh` are correct for your environment + +3. **View detailed logs**: Check supervisor logs for specific error messages + ```bash + sudo cat /var/log/supervisor/supervisord.log + ``` + +4. **Debug the start script**: Run the start script manually to see if it works + ```bash + ./start.sh + ``` + +5. **Check environment variables**: Ensure all required environment variables are set in `supervisor.conf` + The application will be available at http://127.0.0.1:8001 when running with Supervisor. ## API Documentation diff --git a/start.sh b/start.sh index 855822f..f068417 100755 --- a/start.sh +++ b/start.sh @@ -1,11 +1,36 @@ #!/bin/bash # Start script for the FastAPI application -# Set environment variables if needed -export PYTHONPATH=/app +# Exit on error +set -e + +# Get the directory of this script +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# Set environment variables +export PYTHONPATH="$DIR" +export PYTHONUNBUFFERED=1 # Navigate to the application directory -cd /app +cd "$DIR" -# Start the application with uvicorn -exec uvicorn main:app --host 0.0.0.0 --port 8001 --workers 4 --no-access-log \ No newline at end of file +# Ensure log directory exists +mkdir -p "$DIR/logs" + +# Detect Python path +PYTHON_PATH=$(which python3 || which python) +UVICORN_PATH=$(which uvicorn || echo "$DIR/venv/bin/uvicorn") + +echo "Starting application with Python at: $PYTHON_PATH" +echo "Using uvicorn at: $UVICORN_PATH" +echo "Working directory: $DIR" + +# Run database migrations if needed +if [ -f "$DIR/alembic.ini" ]; then + echo "Running database migrations..." + "$PYTHON_PATH" -m alembic upgrade head || echo "Migration failed but continuing..." +fi + +# Start the application with uvicorn using absolute paths +echo "Starting application..." +exec "$UVICORN_PATH" main:app --host 0.0.0.0 --port 8001 --workers 1 --no-access-log \ No newline at end of file diff --git a/supervisor.conf b/supervisor.conf index 4af7204..421c62e 100644 --- a/supervisor.conf +++ b/supervisor.conf @@ -1,14 +1,15 @@ [program:app-8001] -command=/app/start.sh -directory=/app -user=root +command=%(here)s/start.sh +directory=%(here)s +user=appuser autostart=true autorestart=true -startretries=5 +startretries=10 numprocs=1 -startsecs=1 -process_name=%(program_name)s_%(process_num)02d -stderr_logfile=/var/log/supervisor/%(program_name)s_stderr.log -stderr_logfile_maxbytes=10MB -stdout_logfile=/var/log/supervisor/%(program_name)s_stdout.log -stdout_logfile_maxbytes=10MB \ No newline at end of file +startsecs=5 +stopwaitsecs=60 +redirect_stderr=true +stdout_logfile=/var/log/supervisor/%(program_name)s.log +stdout_logfile_maxbytes=50MB +stdout_logfile_backups=10 +environment=PYTHONPATH="%(here)s",PATH="/usr/local/bin:/usr/bin:/bin" \ No newline at end of file