Deploying django with Apache2 and mod-wsgi in ubuntu14.04

23 views
Skip to first unread message

surajit mishra

unread,
Aug 3, 2017, 7:33:16 AM8/3/17
to Django users
I have developed a project (named pubnet) in django and is trying deploy it on production server(currently my ubuntu 14.04 machine for user testing). The project did work on built-in django server during development but on deploying, it fails to work. All it displays is the apache index.html page on localhost and not the django welcome page.

Here are the steps that I have performed:
INSTALLATIONS:
  • installed apache2
  • installed
    ​lib​
    apache2-mod-wsgi
  • installed mysql-server
  • created a database named pubnet
  • created a django project named pubnet inside /var/www/html/

FILES CREATED/ALTERED:
  • vi /etc/apache2/sites-available/pubnet.conf
 <VirtualHost *:80>

ServerName pubnet
ServerAlias pubnet.com
ServerAdmin root

DocumentRoot /var/www/html/pubnet

Alias /media/ /var/www/html/pubnet/media/
Alias /static/ /var/www/html/pubnet/static/

<Directory /var/www/html/pubnet>
    require all granted
</Directory>

WSGIDaemonProcess pubnet.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup pubnet.com

WSGIScriptAlias / /var/www/html/pubnet/pubnet/pubnet.wsgi

<Directory /var/www/html/pubnet/pubnet>
   require all granted
</Directory>

ErrorLog /var/www/logs/error.log
CustomLog /var/www/logs/custom.log combined

</VirtualHost>
  • vi /var/www/html/pubnet/pubnet/pubnet.wsgi
import os
import sys
sys.path = ['/var/www/html/pubnet'] + sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'pubnet.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
  • vi /var/www/html/pubnet/pubnet/wsgi.py:
import os
import sys
import django.core.handlers.wsgi

# Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path.append(project)

# Add the path to 3rd party django application and to django itself.
sys.path.append('/var/www/html/pubnet')
os.environ['DJANGO_SETTINGS_MODULE'] = 'pubnet.apache.override'

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pubnet.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
  • vi /etc/hosts:
127.0.0.1       localhost
192.168.13.80   pubnet.com
  • sudo a2ensite pubnet
  • sudo service apache2 reload
  • sudo service apache2 restart
  • settings.py:
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '127.0.1.1', '192.168.13.80', 'pubnet.com']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'pubnet',
        'USER': 'root',
        'PASSWORD': 'PASSWORD',
        'HOST': '127.0.0.1',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

​NOTE: While working on it last Saturday, it started working and I was also able to display the django admin page on the browser. However, very strangely on Monday it failed to work. Since then I have tried almost all links and the suggestions in google, installed, uninstalled everything but failed to make it work.

Any help would be highly appreciated.

Thanks.

Thiago Luiz Parolin

unread,
Aug 3, 2017, 9:39:44 AM8/3/17
to django...@googlegroups.com
I am using django and apache in a debian 8 and 9 server and everything is working fine.

Firstly, don't use /var/www/html for python files.

In my server, we have a ordinary user that host python files in his home dir.

I will try enumerate some steps used in one of my projects:
 - with your ordinary user, create your virtualenv
 - put your project in a folder  (sub folder in home of that user)
 - install and secure mysql
 - configure your settings (db settings, settings.py, create database if needed)
 - test your db connection and make migrations, load fixtures and others.
 - install apache2 (in debian libapache2-mod-wsgi-py3 was required)
 - configure apache (for me just a single file /etc/apache2/sites-available/your-site-name.conf was sufficient)
 - adjust directory permissions if needed (/var/www or others)
 - restart apache and all is working..

Hope it helps you.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5d36aaf5-01de-402e-9a1a-02fd6c61bdbf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Antonis Christofides

unread,
Aug 3, 2017, 9:40:31 AM8/3/17
to django...@googlegroups.com

Hi,

It's not easy to know with certainty what has happened, but from the symptoms you describe it seems to me that most probably between Saturday and Monday something may have happened (such as a change of the system's IP address) that means that Apache isn't resolving it to the appropriate "virtual host" (i.e. domain).

In order to avoid such problems, I no longer test deployment on my machine (or in VMs that run on my machine), because I then have to struggle with NAT, firewalls etc. Instead, I test on VMs on Digital Ocean, which have real IP addresses, and can be had for something like $0.01 per hour.

Regards,

Antonis

Antonis Christofides
http://djangodeployment.com
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages