Apache2 WSGI部署Flask后,如何将Celery设为守护进程自动运行?
Got it, since you've moved your Flask app to Apache2 WSGI and want to skip manually starting Celery every time, here are the most reliable ways to run Celery as a persistent daemon:
Method 1: Use Systemd (Recommended for Modern Linux Distros)
Systemd is the standard init system on most current Linux releases (Ubuntu 16.04+, Debian 9+, etc.) and makes managing daemons straightforward.
Step 1: Create a Systemd Service File
Create a new service file at /etc/systemd/system/celery-worker.service with the following content. Replace placeholders like /path/to/your/project and www-data with your actual project path and the user running your Apache/WSGI process:
[Unit] Description=Celery Worker for Flask App After=network.target rabbitmq-server.target Wants=rabbitmq-server.target [Service] Type=forking User=www-data Group=www-data WorkingDirectory=/path/to/your/project Environment="PATH=/path/to/your/venv/bin" ExecStart=/path/to/your/venv/bin/celery multi start worker1 -A app.app \ --loglevel=INFO --concurrency=10 --pidfile=/var/run/celery/%n.pid \ --logfile=/var/log/celery/%n%I.log ExecStop=/path/to/your/venv/bin/celery multi stopwait worker1 \ --pidfile=/var/run/celery/%n.pid ExecReload=/path/to/your/venv/bin/celery multi restart worker1 -A app.app \ --loglevel=INFO --concurrency=10 --pidfile=/var/run/celery/%n.pid \ --logfile=/var/log/celery/%n%I.log Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
Quick notes on this config:
After=network.target rabbitmq-server.targetensures Celery starts only after RabbitMQ and network services are ready.Environmentpoints to your virtual environment's bin directory (critical to avoid dependency conflicts).- We use
multi startto manage worker processes cleanly, with separate pid and log files. - First create the required directories with:
sudo mkdir -p /var/run/celery /var/log/celeryand set permissions:sudo chown www-data:www-data /var/run/celery /var/log/celery.
Step 2: Enable and Start the Service
Run these commands to activate the daemon:
# Reload systemd to detect the new service sudo systemctl daemon-reload # Start the Celery worker sudo systemctl start celery-worker # Set it to auto-start on system boot sudo systemctl enable celery-worker
Step 3: Check Status and Logs
- Verify the service is running:
sudo systemctl status celery-worker - View real-time logs:
journalctl -u celery-worker -f
Method 2: Use Supervisor (Alternative for Older Systems)
If you're on a Linux distro that doesn't use systemd, Supervisor is a solid option for managing process daemons.
Step 1: Install Supervisor
sudo apt-get install supervisor # For Debian/Ubuntu # OR sudo yum install supervisor # For RHEL/CentOS
Step 2: Create a Supervisor Config File
Create /etc/supervisor/conf.d/celery.conf with this content:
[program:celery] command=/path/to/your/venv/bin/celery -A app.app worker --loglevel=INFO --concurrency=10 -n worker.%h directory=/path/to/your/project user=www-data numprocs=1 autostart=true autorestart=true startsecs=10 stopwaitsecs=600 stdout_logfile=/var/log/celery/celery.log stderr_logfile=/var/log/celery/celery.err.log
Don't forget to create the log directory if needed: sudo mkdir -p /var/log/celery && sudo chown www-data:www-data /var/log/celery
Step 3: Update Supervisor and Start the Process
# Reload Supervisor to load the new config sudo supervisorctl reread sudo supervisorctl update # Start the Celery process sudo supervisorctl start celery
Key Things to Verify
- Environment Consistency: Ensure Celery uses the same configuration (RabbitMQ connection, environment variables) as your WSGI-deployed Flask app. If you rely on env vars for config, add them to your systemd service or Supervisor config.
- Permissions: The user running Celery (usually
www-data) needs read access to your project files and permissions to connect to RabbitMQ. - Virtual Environment: Always use the celery executable from your project's virtual environment to avoid dependency mismatches.
内容的提问来源于stack exchange,提问作者Avi

