This repository provides two essential tools for deploying Celery workers and schedulers with Django on Linux distributions that use systemd (like Fedora, Ubuntu, CentOS, etc.):
setup.sh: A robust, interactive Bash script that automatically creates, configures permissions for, and enables the necessary systemd service files on your server.
Celery systemd Config Generator (Web UI): A single-file HTML/JavaScript application (web/index.html) that allows you to visually configure and generate the exact service file content for quick testing and prototyping.
Before using the tools, ensure the following are configured on your server:
- Django Project: Your project is configured for Celery tasks.
- Virtual Environment (VENV): Celery and its dependencies are installed inside a Python virtual environment.
- Message Broker: A message broker (e.g., RabbitMQ, Redis) is installed and running as a systemd service.
This script handles the heavy lifting of service installation and configuration.
-
Make the script executable:
chmod +x setup.sh -
Run the deployment script interactively:
./setup.sh
The script will prompt you for four key pieces of information:
- Project Name (e.g.,
nhwiren) - Linux User: The non-root user that will own and run the service (e.g.,
nana) - Project Path: The full absolute path to your Django project root (e.g.,
/opt/nhwiren) - VENV Path: The full absolute path to your VENV directory (e.g.,
/opt/nhwiren/.venv)
- Creates and secures log (
/var/log/celery) and run (/var/run/celery) directories with correct ownership (<LINUX_USER>). - Creates
celery-<project>.serviceandcelerybeat-<project>.servicefiles in/etc/systemd/system/. - Adds
ExecStartPresteps to ensure required directories exist and permissions are fixed before service start. - Reloads the systemd daemon and enables the services to start on boot.
- Optionally starts the services immediately and displays their status.
The Celery systemd Config Generator is a visual utility that helps you quickly generate and debug service file content before deploying.
Access the Generator: Open the directory with the index file,
web/index.html in your Canvas environment.
Input Configuration: Fill out the four required fields, ensuring the paths are accurate for your server:
-
Project Name
-
Linux User
-
Project Directory
-
VENV Bin Directory (The path to the VENV's /bin folder).
-
Select Broker: Choose either rabbitmq-server.service or redis.service (or manually adjust if you use a different broker).
-
Run the appropriate permission to get the service to run
DISCLAIMER:
- Copy the generated Celery service files into the systemd directory
sudo cp celery-<project_name>.service /etc/systemd/system/
sudo cp celerybeat-<project_name>.service /etc/systemd/system/
- Assign root ownership to the service files
sudo chown root:root /etc/systemd/system/celery-<project_name>.service
sudo chown root:root /etc/systemd/system/celerybeat-<project_name>.service
- Apply proper read permissions for service files
sudo chmod 644 /etc/systemd/system/celery-<project_name>.service
sudo chmod 644 /etc/systemd/system/celerybeat-<project_name>.service
- Create Celery runtime and log directories
sudo mkdir -p /var/run/celery /var/log/celery
- Set ownership of runtime and log directories
sudo chown -R <user:user> /var/run/celery /var/log/celery
- Assign standard access permissions to directories
sudo chmod -R 755 /var/run/celery /var/log/celery
- Reload the systemd manager configuration
sudo systemctl daemon-reload
- Enable the Celery services to start on boot
sudo systemctl enable celery-<project_name>.service celerybeat-<project_name>.service
- Start both Celery services immediately
sudo systemctl start celery-<project_name>.service celerybeat-<project_name>.service
- Verify the status of both services
sudo systemctl status celery-<project_name>.service celerybeat-<project_name>.service
The two large text areas at the bottom instantly update with the complete, ready-to-use [Unit] and [Service] configurations. Use the Copy to Clipboard buttons to easily grab the content.
- Debugging: Verify the generated
ExecStartcommand and environment variables before deploying. - Manual Deployment: If you prefer not to run the setup script, you can copy the generated configs and place them directly in
/etc/systemd/system/.
| Error Code | Log Message | Cause | Resolution |
|---|---|---|---|
status=203/EXEC |
Failed to execute /path/to/celery: Permission denied |
SELinux or corrupted executable | 1. Check Shebang: Verify the first line of /path/to/.venv/bin/celery points to the correct Python interpreter. 2. Ensure SELinux is permissive or configure policies. |
status=203/EXEC |
Error persists after Shebang check | Environment/Path mismatch | Manual Test: Run the ExecStart command directly as the service user: sudo -u <user> /path/to/.venv/bin/celery -A <project> worker |
status=203/EXEC |
Failure on /mnt/ or non-standard paths |
Restricted mount points | Relocate the project/venv to a standard location such as /opt/ or /srv/. |
- Both tools (
setup.shandweb/index.html) include directory safety checks (ExecStartPre) to prevent failures due to missing or mis-owned log/run directories. - For advanced deployments, consider adding a Flower monitoring service (not generated by default).