Report this

What is the reason for this report?

Setting up django on nginx, ran in error: "can't chdir to ..."

Posted on September 2, 2020

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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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:

  1. 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:

1. Check Directory Existence

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.

2. Permissions and Ownership

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/

3. Check Gunicorn Executable Path

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.

4. SELinux Context

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.)

5. Restart and Check the Service

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

6. Check Logs for Additional Information

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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.