Fix Supervisor startup issues

- Update start.sh to use dynamic path detection and better error handling
- Fix Supervisor configuration to use proper relative paths with %(here)s
- Resolve stderr logging conflicts by using redirect_stderr=true consistently
- Add proper environment variables to supervisor.conf
- Reduce workers to 1 to prevent memory issues on smaller servers
- Added comprehensive troubleshooting guide to README
- Enhance startup process with detailed logging and migration handling
- Configure proper user and permissions
This commit is contained in:
Automated Action 2025-05-17 21:19:53 +00:00
parent 221dbad51a
commit 9b58aaff62
3 changed files with 91 additions and 25 deletions

View File

@ -89,27 +89,67 @@ The application will be available at http://127.0.0.1:8000.
**Production mode with Supervisor:** **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 ```bash
# Make the start script executable # Make the start script executable
chmod +x start.sh chmod +x start.sh
# Copy the supervisor configuration to the supervisor config directory # Test the start script manually to verify it works
cp supervisor.conf /etc/supervisor/conf.d/fastapi-app.conf ./start.sh
# Press Ctrl+C to stop after verifying
# Restart supervisor to load the new configuration # Create necessary directories for logs (if not using default location)
supervisorctl reread sudo mkdir -p /var/log/supervisor
supervisorctl update 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 # Check the status of the application
supervisorctl status app-8001 sudo supervisorctl status app-8001
# View logs if needed # If it shows BACKOFF or FATAL status, check the logs:
tail -f /var/log/supervisor/app-8001_stdout.log sudo tail -f /var/log/supervisor/app-8001.log
tail -f /var/log/supervisor/app-8001_stderr.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. The application will be available at http://127.0.0.1:8001 when running with Supervisor.
## API Documentation ## API Documentation

View File

@ -1,11 +1,36 @@
#!/bin/bash #!/bin/bash
# Start script for the FastAPI application # Start script for the FastAPI application
# Set environment variables if needed # Exit on error
export PYTHONPATH=/app 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 # Navigate to the application directory
cd /app cd "$DIR"
# Start the application with uvicorn # Ensure log directory exists
exec uvicorn main:app --host 0.0.0.0 --port 8001 --workers 4 --no-access-log 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

View File

@ -1,14 +1,15 @@
[program:app-8001] [program:app-8001]
command=/app/start.sh command=%(here)s/start.sh
directory=/app directory=%(here)s
user=root user=appuser
autostart=true autostart=true
autorestart=true autorestart=true
startretries=5 startretries=10
numprocs=1 numprocs=1
startsecs=1 startsecs=5
process_name=%(program_name)s_%(process_num)02d stopwaitsecs=60
stderr_logfile=/var/log/supervisor/%(program_name)s_stderr.log redirect_stderr=true
stderr_logfile_maxbytes=10MB stdout_logfile=/var/log/supervisor/%(program_name)s.log
stdout_logfile=/var/log/supervisor/%(program_name)s_stdout.log stdout_logfile_maxbytes=50MB
stdout_logfile_maxbytes=10MB stdout_logfile_backups=10
environment=PYTHONPATH="%(here)s",PATH="/usr/local/bin:/usr/bin:/bin"