0

Here's my folder structure:

~/myprojectdir
    manage.py
    myprojectenv/
        bin/
            activate
            gunicorn
            pip3
            python3
            ...
        lib/
            python3.6
            ...
    fishercoder/
        fishercoder/
            asgi.py
            urls.py
            settings.py
            wsgi.py
            __init__.py
            ...
        blog/
            views.py
            urls.py
            models.py
            admin.py
            apps.py
            templates/
            ...
        catalog/
            views.py
            urls.py
            models.py
            admin.py
            apps.py
            templates/
            ...

I have run source myprojectenv/bin/activate

Here's my /etc/systemd/system/gunicorn.service file:

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/myprojectdir
ExecStart=/home/ubuntu/myprojectdir/myprojectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          myproject.wsgi:application

[Install]
WantedBy=multi-user.target

I've replaced this line:

myproject.wsgi:application

with this

fishercoder.wsgi:application or this

wsgi:application

following the suggestion from this question

Restarted Gunicorn. No luck in either.

My ~/myprojectdir/fishercoder/wsgi.py looks like this:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fishercoder.settings')

application = get_wsgi_application()

Any one could shed any light on this would be greatly appreciated!

Fisher Coder
  • 3,278
  • 12
  • 49
  • 84
  • According to your tree structure, your `wsgi.py` file is in `~/myprojectdir/fishercoder/fishercoder/wsgi.py`. – Laurent LAPORTE Jun 20 '20 at 18:59
  • I copied this `wsgi.py` from there to `~/myprojectdir/fishercoder` also. I have it in both places now. What needs to be done? – Fisher Coder Jun 20 '20 at 19:02
  • I don't see any `__init__.py` file. directories without it are not python packages. – Laurent LAPORTE Jun 20 '20 at 19:03
  • just added it in the question. thanks – Fisher Coder Jun 20 '20 at 19:04
  • Do you have a `setup.py` file? Which directory is your root package? How your `setup.py` file is done? – Laurent LAPORTE Jun 20 '20 at 19:07
  • No, I really don't have a `setup.py` file, I followed this tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04, any instructions for `setup.py` would be great! – Fisher Coder Jun 20 '20 at 19:10
  • ok, I found this one: https://stackoverflow.com/questions/1471994/what-is-setup-py – Fisher Coder Jun 20 '20 at 19:12
  • Yes, you need a `setup.py` file. So that your project and it’s dependencies are installed in your virtualenv. When you deploy, it’s a requirement. It’s also a best practice in development mode. So, follow a good tutorial and it will work. Mind your tree structure too. – Laurent LAPORTE Jun 20 '20 at 19:21
  • thanks, I'm looking at https://github.com/django/django/tree/master/docs now, what's a proper tree structure for production Django project? – Fisher Coder Jun 20 '20 at 19:23
  • No, this is the DJango documentation project. – Laurent LAPORTE Jun 20 '20 at 19:27
  • Hmm, I've gone through this https://github.com/django/django/tree/master/docs and even in this official tutorial, it didn't mention `setup.py` in a Django project, any ideas how to study this? thanks – Fisher Coder Jun 20 '20 at 20:02

2 Answers2

1

If your are in development mode, you can link your virtualenv with your source code so that gunicorn can found it.

To do that, activate your virtualenv and install your project in edit mode:

source /home/ubuntu/myprojectdir/myprojectenv/bin/activate
cd /home/ubuntu/myprojectdir/fishercoder
pip install -e .

Of course you need a setup.py in your project directory.

Your wsgi file can be at any level but it is usually in your root package.

But, currently, your root package seems to be fishercoder, so in your configuration file, you need to write: fishercoder.wsgi:application.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
0

Just in case if someone is also bumped into the same issue, I was able to unblock myself by copying the contents from ~/myprojectdir/fishercoder/ directory up to its parent directory ~/myprojectdir/.

And then change this line in my /etc/systemd/system/gunicorn.service file

ExecStart=/home/ubuntu/myprojectdir/myprojectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          myproject.wsgi:application

to be

ExecStart=/home/ubuntu/myprojectdir/myprojectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          fishercoder.wsgi:application

Apparently this is hacky and not ideal, but at least it works now. I'm still trying to find a tutorial to help me set up setup.py in a more python way.

Fisher Coder
  • 3,278
  • 12
  • 49
  • 84