5

I'm trying to deploy my local Django project on my RedHat server. So I install all the libraries and dependencies that I needed (also mod_wsgi).

So, I edit my project's settings and move my local project to the server. But I'm facing an issue: when I try to reach the URL of my project, I have the explorer view.

I also edit the httpd.conf file:

WSGIScriptAlias /var/www/html/virtualEnv/ /var/www/html/virtualEnv/ThirdPartyApplications/ThirdPartyApplications/wsgi.py
    WSGIPythonPath /var/www/html/virtualEnv/ThirdPartyApplications/:/var/www/html/virtualEnv/lib/python2.7/site-packages
    WSGIDaemonProcess http://licops.app.ale-international.com/ python-path=/var/www/html/virtualEnv/ThirdPartyApplications/:/var/www/html/virtualEnv/lib/python2.7/site-packages
    WSGIProcessGroup http://licops.app.ale-international.com/
    <Directory /var/www/html/virtualEnv/ThirdPartyApplications/>
        <Files wsgi.py>
            Order deny,allow
            Allow from all
        </Files>
    </Directory>

EDIT : @FlipperPA

So far, I'm running this conf in my /etc/httpd/conf.d/djangoproject.conf :

WSGISocketPrefix /var/run/wsgi

NameVirtualHost *:448
Listen 448

ServerName http://server.name-international.com
ErrorLog /home/myuser/apache_errors.log

WSGIDaemonProcess MyApp python-path=/var/www/html/MyApp:/var/www/html/MyApp/MyApp/lib/python2.7/site-packages
WSGIProcessGroup MyApp
WSGIScriptAlias /MyApp /home/user/MyApp/MyApp/wsgi.py
Alias /static /var/www/html/MyApp/MyApp/static

Creed
  • 163
  • 1
  • 1
  • 13
  • What versions of RHEL and Apache are you running? That'll affect the answer, as the configuration for Apache 2.4 changed. – FlipperPA Jun 03 '16 at 11:01
  • @FlipperPA I'm running Apache 2.2.15 and RHEL 6.8 – Creed Jun 07 '16 at 07:53
  • It's done @FlipperPA – Creed Jun 13 '16 at 15:02
  • There are two main directory trees you need to track: (1) your virtualenv; (2) your Django project. In the `WSGIDaemonProcess` directive, the `python-path` should point to the root of your virtualenv. Then your `WSGIScriptAlias` should point to the `wsgi.py` in your Django project. What directory is your virtualenv hosted in? It looks like your `python-path` declaration is including your Django project and the system Python, but not your virtualenv? – FlipperPA Jun 13 '16 at 18:07
  • @FlipperPA my virtualenv is hosted in /home/myuser/virtualenv but my django project is on /var/www/html/myproject. I understand what you said, I'll try ;) – Creed Jun 14 '16 at 08:35
  • @FlipperPA I just changed my conf but still can't access the URL myserver.com/myapp : `The requested URL /MyApp/ was not found on this server.` – Creed Jun 14 '16 at 10:49

2 Answers2

6

Here's a working example Apache config I have on CentOS 6.8 with Apache 2.2; I'd name it something like yourproject.conf and include it in /etc/httpd/conf.d so it is included by the master Apache config file:

LoadModule wsgi_module modules/mod_wsgi.so
LoadModule ssl_module modules/mod_ssl.so

WSGISocketPrefix /var/run/wsgi

NameVirtualHost *:443
Listen 443
<VirtualHost *:443>

  ServerName yourservername.com
  ErrorLog /home/yourusername/apache_errors.log

  WSGIDaemonProcess yourproject-https python-home=/home/yourusername/.virtualenvs/yourproject
  WSGIScriptAlias /yourproject /var/www/html/yourproject/yourproject/wsgi.py process-group=yourproject-https application-group=yourproject-https
  WSGIProcessGroup yourproject-https
  Alias /yourproject/static/ /var/www/html/yourproject/static/

  SSLENGINE on 

  SSLCertificateFile /etc/pki/tls/certs/localhost.crt
  SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
  SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
  SSLProtocol all -SSLv2
</VirtualHost>

You may also have to tweak your wsgi.py in your Django project:

import os, sys
from django.core.wsgi import get_wsgi_application

sys.path.append(os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2]))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")
application = get_wsgi_application()

This assumes that your virtualenv is in /home/yourusername/.virtualenvs/yourproject and your Django project is located a /var/www/html/yourproject. It will also write Apache errors to your home directory, in case you run into any snags. Good luck!

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • thanks, but how can I access my app on my server. I tried `http://myserver.com/myproject` But I get the following error : `Not Found The requested URL /myproject/ was not found on this server.` – Creed Jun 09 '16 at 08:15
  • The example above is on port 443 for SSL, so you'd have to access it via https. If you want http instead, remove all lines starting with SSL and switch the 443 to 80. – FlipperPA Jun 09 '16 at 10:28
  • Ok I'm moving slowly but surely. Now, when accessing `http://mysever.com/myproject`, I get the list of the files that are present in the directory :/ – Creed Jun 10 '16 at 08:00
  • Can you add what your Apache configuration looks like now to the end of your answer with an update? – FlipperPA Jun 10 '16 at 15:20
  • 1
    @FlipperPA you should avoid using SSL anymore, use TLS instead, as a result of this [POODLE](https://blog.mozilla.org/security/2014/10/14/the-poodle-attack-and-the-end-of-ssl-3-0/) vulnerability, SSL v3.0 is now completely insecure. – Yassine Sedrani Nov 26 '16 at 02:52
2

This is an example /etc/httpd/conf/httpd.conf file:

<VirtualHost *:80>
    ServerName my.webapp.com
    ServerAlias my.webapp.com

        ErrorLog /var/www/my.webapp.com/error.log

    WSGIDaemonProcess my.webapp.com \
      python-path=/var/www/my.webapp.com/my-django-project:/home/me/virt-envs/md-env/local/lib/python2.7/site-packages
    WSGIProcessGroup my.webapp.com

    DocumentRoot /var/www/my.webapp.com/my-django-project

    Alias /static/ /var/www/my.webapp.com/static/

    WSGIScriptAlias / /var/www/my.webapp.com/my-django-project/my-django-project/wsgi.py \
      process-group=my.webapp.com

    <Directory /var/www/my.webapp.com/my-django-project>
        Order allow,deny
    Satisfy Any
    Allow from all
    Options ExecCGI 
    AddHandler wsgi-script .wsgi
    </Directory>

    <Directory /var/www/my.webapp.com/my-django-project/my-django-project>
    <Files wsgi.py>
    Order deny,allow
    Allow from all
    </Files>
    </Directory>

    <Directory /var/www/my.webapp.com/static>
    Order allow,deny
    Allow from all
    </Directory>

</VirtualHost>

Also wsgi.conf:

LoadModule wsgi_module modules/mod_wsgi.so

And then wsgi.py:

import os
import site
import sys

prev_sys_path = list(sys.path)

for directory in ALLDIRS:
    site.addsitedir(directory)

new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
    new_sys_path.append(item)
    sys.path.remove(item)
sys.path[:0] = new_sys_path

sys.path.append('/var/www/my.webapp.com/my-django-project')
sys.path.append('/var/www/my.webapp.com/my-django-project/my-django-project')

from django.core.wsgi import get_wsgi_application

os.environ["DJANGO_SETTINGS_MODULE"] = "my-django-project.settings"

application = get_wsgi_application()
denvaar
  • 2,174
  • 2
  • 22
  • 26