I was following this tutorial: https://www.howtoforge.com/how-to-install-django-on-centos-8/ on how to set up django on nginx with gunicorn but ran into the following error:
[user@centos-s-1vcpu-2gb-fra1-01 ~]$ systemctl status django
● django.service - django daemon
Loaded: loaded (/etc/systemd/system/django.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Wed 2020-09-02 19:44:16 UTC; 6s ago
Process: 4615 ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:/home/user/djangoprojects/djangoproject.sock djangoproject.wsgi:application (code=exited, status=1/FAILURE)
Main PID: 4615 (code=exited, status=1/FAILURE)
Sep 02 19:44:15 centos-s-1vcpu-2gb-fra1-01 systemd[1]: Started django daemon.
Sep 02 19:44:16 centos-s-1vcpu-2gb-fra1-01 gunicorn[4615]: Error: can't chdir to '/home/user/djangoprojects'
Sep 02 19:44:16 centos-s-1vcpu-2gb-fra1-01 systemd[1]: django.service: Main process exited, code=exited, status=1/FAILURE
Sep 02 19:44:16 centos-s-1vcpu-2gb-fra1-01 systemd[1]: django.service: Failed with result 'exit-code'.
Can someone help me out? I tried a lot of things and researched for a long time, but nothing helped.
Thats the service file:
[Unit]
Description=django daemon
After=network.target
[Service]
User=nginx
Group=nginx
WorkingDirectory=/home/user/djangoprojects/
ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:/home/user/djangoprojects/djangoproject.sock djangoproject.wsgi:application
[Install]
WantedBy=multi-user.target
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi @jonasstettner,
From your systemd Unit file and the systemd status log, I think that Nginx cannot access your Django project directory. So make sure that the directory /home/user/djangoprojects actually exists and the nginx account has permission to read it. You can run following command to check:
- sudo ls -la /home/user/djangoprojects
The error message "Error: can't chdir to '/home/user/djangoprojects'" suggests a potential issue with permissions or the existence of the specified directory in your django.service file. Here are a few steps to troubleshoot and resolve this issue:
Ensure that the directory /home/user/djangoprojects/ exists. If it doesn’t, you need to create it or correct the path in the service file to point to the right location of your Django project.
ls -l /home/user/djangoprojects/
If the directory doesn’t exist, create it with the correct permissions:
mkdir -p /home/user/djangoprojects/
And make sure to place your Django project files in this directory.
The service is configured to run as the nginx user and group. Ensure that the nginx user has the necessary permissions to access the /home/user/djangoprojects/ directory:
sudo chown -R nginx:nginx /home/user/djangoprojects/
Also, make sure that the directory has the correct permissions:
chmod -R 755 /home/user/djangoprojects/
Verify that the path to the gunicorn executable is correct. The path /usr/local/bin/gunicorn in your service file should match the actual location of the gunicorn executable:
which gunicorn
If the path is different, update the ExecStart line in your service file accordingly.
If SELinux is enabled, it may prevent nginx or gunicorn from accessing certain directories. You can check the current SELinux status with:
sestatus
If it’s enforcing, you may need to adjust the SELinux context of your Django project directory or temporarily set SELinux to permissive mode to test if it’s the cause of the issue:
sudo setenforce 0
(Warning: Setting SELinux to permissive mode disables SELinux enforcement and should only be used temporarily for troubleshooting.)
After making the changes, restart the django service and check its status again:
sudo systemctl daemon-reload
sudo systemctl restart django
sudo systemctl status django
If the service still fails, check the journal logs for more detailed error messages that can help in troubleshooting:
sudo journalctl -u django
By following these steps, you should be able to resolve the issue with the django service not starting due to the "can't chdir to..." error. If the problem persists, the error messages from the log can provide further clues for troubleshooting.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.