skip to Main Content

Deploying django project on DigitalOcean with apache and mod_wsgi

I’m using

    Ubuntu 16.04
    apache 2.4
    python3.5.2
    django==1.11
    Firebase

Installed Apache : https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-16-04

    sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
    sudo apt-get install libapache2-mod-wsgi-py3
    sudo apt-get install aptitude
    sudo aptitude install apache-dev
    pip3 install mod-wsgi

Installed all pip3 modules
And apache is running

gave permission to www folder as

    sudo chown -R www-data:www-data www/

Created wsgi.py file as executable

    sudo chmod a+x wsgi.py

My wsgi.py file as

    import os
    import sys


    sys.path.append('/var/www/myproject')
    sys.path.append('/usr/local/lib/python3.5/dist-packages')

    os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings"
    #os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") #also tried

    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application() 

apache config file 000-default.conf

    <VirtualHost *:80>
    ServerName www.mysite.in
    ServerAlias mysite.in
    ServerAdmin [email protected]
    DocumentRoot /var/www/
    ErrorLog ${APACHE_LOG_DIR}/mysite_error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/myproject/myproject>
    <Files wsgi.py>
        Require all granted
    </Files>                
    </Directory>                
    WSGIDaemonProcess mysite.in python-path=/var/www/myproject python-home=/usr/local/lib/python3.5/dist-packages
    WSGIProcessGroup mysite.in
    WSGIScriptAlias / /var/www/myproject/myproject/wsgi.py
    </VirtualHost>

Ran command after modifying 000-default.conf file

    sudo service apache2 reload
    sudo a2ensite 000-default.conf
    sudo service apache2 reload
    sudo systemctl restart apache2.service

My project folder structure is

  .
  ├── __pycache__
  │   └── config.cpython-35.pyc
  ├── myproject
  │   ├── __init__.py
  │   ├── __pycache__
  │   │   ├── __init__.cpython-35.pyc
  │   │   └── settings.cpython-35.pyc
  │   ├── settings.py
  │   ├── urls.py
  │   └── wsgi.py
  ├── myprojectapp
  │   ├── admin.py
  │   ├── __init__.py
  │   ├── mail_html.py
  │   ├── migrations
  │   │   └── __init__.py
  │   ├── models.py
  │   ├── __pycache__
  │   │   ├── __init__.cpython-35.pyc
  │   │   └── models.cpython-35.pyc
  │   ├── tests.py
  │   ├── urls.py
  │   └── views.py
  ├── admin.json
  ├── config.py
  ├── db.sqlite3
  ├── firebasesdk-file.json
  ├── manage.py
  ├── requirements.txt
  └── test.py

Inside mysite_error.log file i’m getting error as

  [Thu Oct 24 04:43:11.940530 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] mod_wsgi (pid=6581): Target WSGI script '/var/www/myproject/myproject/wsgi.py' cannot be loaded as Python module.
  [Thu Oct 24 04:43:11.940823 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] mod_wsgi (pid=6581): Exception occurred processing WSGI script '/var/www/myproject/myproject/wsgi.py'.
  [Thu Oct 24 04:43:11.943517 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] Traceback (most recent call last):
  [Thu Oct 24 04:43:11.943754 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "/var/www/myproject/myproject/wsgi.py", line 16, in <module>
  [Thu Oct 24 04:43:11.943897 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]     application = get_wsgi_application()
  [Thu Oct 24 04:43:11.944040 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
  [Thu Oct 24 04:43:11.944133 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]     django.setup(set_prefix=False)
  [Thu Oct 24 04:43:11.944260 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 27, in setup
  [Thu Oct 24 04:43:11.944349 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]     apps.populate(settings.INSTALLED_APPS)
  [Thu Oct 24 04:43:11.944464 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 108, in populate
  [Thu Oct 24 04:43:11.944561 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]     app_config.import_models()
  [Thu Oct 24 04:43:11.944686 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "/usr/local/lib/python3.5/dist-packages/django/apps/config.py", line 202, in import_models
  [Thu Oct 24 04:43:11.944791 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]     self.models_module = import_module(models_module_name)
  [Thu Oct 24 04:43:11.944880 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
  [Thu Oct 24 04:43:11.944945 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]     return _bootstrap._gcd_import(name[level:], package, level)
  [Thu Oct 24 04:43:11.945013 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  [Thu Oct 24 04:43:11.945087 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  [Thu Oct 24 04:43:11.945161 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  [Thu Oct 24 04:43:11.945245 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  [Thu Oct 24 04:43:11.945367 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  [Thu Oct 24 04:43:11.945461 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  [Thu Oct 24 04:43:11.945608 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "/var/www/myproject/myprojectapp/models.py", line 31, in <module>
  [Thu Oct 24 04:43:11.945712 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]     cred = credentials.Certificate('../myproject/firebasesdk-file.json')
  [Thu Oct 24 04:43:11.945799 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]   File "/usr/local/lib/python3.5/dist-packages/firebase_admin/credentials.py", line 83, in __init__
  [Thu Oct 24 04:43:11.945863 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0]     with open(cert) as json_file:
  [Thu Oct 24 04:43:11.945979 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] FileNotFoundError: [Errno 2] No such file or directory: '../myproject/firebasesdk-file.json'

I also have firebasesdk-file.json inside my project

Also tried with virtualenvironment

Where i have created venv and installed all pip3 modules for python3.5.2 inside venv

Changed wsgi.py and 000-default.conf files with respect to venv
nothing helped with vitualenvironment

How to fix this error?

Thanks.

2

Answers


  1. can you check permissions of wsgi.py, if not executable you can try:

    sudo chmod a+x /var/www/myproject/myproject/wsgi.py
    
    Login or Signup to reply.
  2. Your python-home should point to the python binary, on the command line type which python3 the output should be something like /usr/bin/python3 but may be different in your installation try changing the python-home to that.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search