django deploy - ubuntu 14.04 and apache2
Question: <https://www.sitepoint.com/deploying-a-django-app-with-mod_wsgi-on-
ubuntu-14-04/>

and

<https://www.youtube.com/watch?v=hBMVVruB9Vs>

This was the first time I deploy a website.And these are the tutorials I
followed.

Now I can access to the server(by typing 10.231.XX.XX) from other machine and
see the Apache2 Ubuntu Default Page.

Then I tried to access my django project. I run:

> python manage.py runserver 8000 Validating models...
>
> 0 errors found August 03, 2016 - 09:44:20 Django version 1.6.1, using
> settings 'settings' Starting development server at <http://127.0.0.1:8000/>
> Quit the server with CONTROL-C.

Then I type 10.231.XX.XX:8000 to try to acess the django page. But I failed.
It said:

> This site can’t be reached
>
> 10.231.XX.XX refused to connect. Search Google for 231 8000
> ERR_CONNECTION_REFUSED

I have tried every thing I can but still can't figure why. (as followed the
website <https://www.sitepoint.com/deploying-a-django-app-with-mod_wsgi-on-
ubuntu-14-04/>) I have apache folder in mysite folder, and in override.py:

    
    
    from mysite.settings import *
    
        DEBUG = True
        ALLOWED_HOSTS = ['10.231.XX.XX']
    

in wsgi.py:

    
    
    import os, sys
    # 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('/home/zhaojf1')
    os.environ['DJANGO_SETTINGS_MODULE'] = '10.231.52.XX.apache.override'
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    

and __init__py is empty.

in /etc/apache2/sites-enabled/000-default.conf :

    
    
    <VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com
    
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    WSGIScriptAlias /msa.html /home/zhaojf1/Web-Interaction/apache/wsgi.py
    
    <Directory "/home/zhaojf1/Web-Interaction-APP">
    <Files wsgi.py>
        Require all granted
    </Files>
    </Directory>
    

I have also restart apache after I do everything.

Thanks for help


Answer: The connection refused error is likely going to come down to Apache being
incorrectly configured for the `VirtualHost` or you accessing wrong port. You
also have other basic mistakes in your `wsgi.py` file as well.

Starting with the `wsgi.py` file, the `DJANGO_SETTINGS_MODULE` value is wrong:

    
    
    os.environ['DJANGO_SETTINGS_MODULE'] = '10.231.52.XX.apache.override'
    

The value is meant to be a Python module path. Having the IP address in there
looks very wrong and is unlikely to yield what you need.

Next is changes to `sys.path`. The location of your project and activation of
any Python virtual environment is better done through options for mod_wsgi in
the Apache configuration file.

That you are adding a home directory into the path is also a flag to potential
other issues you may encounter. Specifically, the user that Apache runs as
often cannot read into home directories as the home directories are not
readable/accessible to others. You may need to move the project out of your
home directory.

As to the Apache configuration, your `VirtualHost` lacks a `ServerName`
directive. If this was an additional `VirtualHost` you added and not the
default (first one appearing in Apache configuration when parsed), it will be
ignored, with all requests going to the first `VirtualHost`. You do show this
as in the default site file, so may be you are okay.

Even so, that `VirtualHost` is set up to listed on port 80. You are trying to
connect to port 8000, so there wouldn't be anything listening.

Next issue is the `WSGIScriptAlias` line.

    
    
    WSGIScriptAlias /msa.html /home/zhaojf1/Web-Interaction/apache/wsgi.py
    

It is strange to have `msg.html` as the mount point as that makes it appear as
if you are accessing a single HTML page, but you have it mapped to a whole
Django project. If you were accessing the root of the host, it also wouldn't
map through to the Django application as you have it mounted at a sub URL.
Thus perhaps need to use:

    
    
    WSGIScriptAlias / /home/zhaojf1/Web-Interaction/apache/wsgi.py
    

Next problem is that the directory specified in `Directory` directive doesn't
match where you said the `wsgi.py` file existed in the `WSGIScriptAlias`. They
should match. So maybe you meant:

    
    
    <Directory /home/zhaojf1/Web-Interaction/apache>
    

Even then that doesn't look right as where is the `apache` directory coming
from. That last directory in the path should normally be the name of the
Django project.

One final thing, you may need to change `ALLOWED_HOSTS` as well. If you find
you start getting bad request errors it probably doesn't match properly.
Change it to `['*']` to see if that helps.

So lots of little things wrong.

Suggestions are:

  * Make sure you read the official Django documentation for setting up mod_wsgi. See <https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/>
  * If you are only wanting to do development at this point, use mod_wsgi-express instead. See <http://blog.dscpl.com.au/2015/04/using-modwsgi-express-with-django.html> and <http://blog.dscpl.com.au/2015/04/integrating-modwsgi-express-as-django.html>

