Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy Django project on RedHat

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

like image 305
Creed Avatar asked Jun 02 '16 09:06

Creed


2 Answers

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!

like image 130
FlipperPA Avatar answered Sep 30 '22 15:09

FlipperPA


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()
like image 29
denvaar Avatar answered Sep 30 '22 14:09

denvaar