Django deployment with mod_wsgi and .htaccess - django

Here is my set-up: Django 1.7; Python 3.4, mod_wsgi, virtualenv, apache2. Django and other pakages are installed after activating the virtualenv.
Need .htaccess file for redirects. I am new to deployment world, so please bear with me.
Here is what I have done so far:
In the virtualhost, I call the wsgi.py file after specifying the path to site-package path for Django. This works fine, however I have 2 questions:
1) I saw a lot of sites which were calling the file.wsgi file from the virtualhost config, and in the .wsgi file they were calling the exec to activate the env and call django app. The Django guide however has a different approach (https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/modwsgi/) and this is the one I implemented. Do I need a file.wsgi if I want to have htaccess deal with the request and then forward it to Django urls.py?
2) If the set-up that I have is ok for deployment, where do i place the .htaccess file and ensure that apache sends the request to htaccess which in turn sends it to Django app?
Thanks

Related

Django + GUnicorn ASGI with SCRIPT_NAME

I have a Django application running with a gunicorn ASGI server and an NGINX reverse proxy for serving static content. All are packaged within a docker container.
Now I want to serve this container behind a reverse proxy with a path prefix, e.g. "mydomain.com/djangoapp/". The problem is, that Django doesn't know it's hosted under a subpath, and for example, the Django admin application then always redirects to the root path "/" instead of "/djangoapp/".
I already read that there are several settings that handle this problem.
I tried setting the "FORCE_SCRIPT_NAME" in the Django settings directly to "/djangoapp". It worked for the admin login page, but after clicking the login button it redirected to the wrong root "/".
I tried setting the "SCRIPT_NAME" environment variable of the gunicorn server to "/djangoapp". It did not apply at all.
I'm running now out of ideas on what else to try. Does anybody else have a solution for this problem?
FORCE_SCRIPT_NAME should work. For the post-login redirect you need to properly set LOGIN_REDIRECT_URL

WSGI problem on Eleastic Beanstalk with Django

Some days ago I have deployed a Django on AWS elastic beanstalk(EB) and it worked fine. Today, after a new deploy, where I did minor changes in view.py, the Django APP on EB has a very big problem and it becomes not accessible anymore. Looking at the log file in AWS EB I read this errors:
Script timed out before returning headers: wsgi.py
End of script output before headers: wsgi.py, referer ...
Do you have any ideas how to solve this issues?
I would like to thank you in advance,
Two potential solutions (depending on your specific situation)
This is a similar question in addressing this specific question
End of script output before headers: wsgi.py deploying python django to AWS EB
Add this WSGIApplicationGroup %{GLOBAL} to your wsgi configuration. It directs your wsgi app- your django app, to "run within the very first Python sub interpreter created when Python is initialised" (from https://modwsgi.readthedocs.io/en/develop/user-guides/application-issues.html#python-simplified-gil-state-api)
The other solution is related to increaseing memory of the instance you are using.

How to set multiple settings.py for sites framework django?

Am trying to set up multiple website with same base. While browsing, came to know Django has Sites framework which I could use.
I didnt get how to set multiple settings.py file with site id. Any ideas anyone?
Thanks in advance :)
To serve multiple sites from the same Django instance you do not need multilple settings.py files.
A simple method is to omit the SITE_ID setting from the Sites framework. Then include the Sites framework middleware:
'django.contrib.sites.middleware.CurrentSiteMiddleware'
This automatically passes a request object to Site.objects.get_current() on every request. It also allows your Django application to detect the current site via request.site.
You would need to make sure to setup multilple virtual hosts using your NGINX or apache instance to route traffic from each site to your server.
you can have multiple setting file for example develop.py and production.py
steps:
create a settings folder inside the project
Add all of the settings file to that folder
while running server
./manage.py runserver -- settings=project_name.settings.required_settingfile
for example:
./manage.py runserver --settings=myproject.settings.develop

Django-CMS text editing not working on Apache

When serving my django-cms application on apache with mod_wsgi I can't get the text-plugin to work.
The difference is that when serving on apache the textedito tries to get an url that does not exist.
On Apache:
/admin/js/iframe/default/wymiframe.html
With manage.py runserver
/static/cms/wymeditor/iframe/default/wymiframe.html
the wymiframe.html file is present in my static folder and is accessible but not via the wrong URL that I get when running the app through apache.
What do I do to get the text-plugin requesting the right URL?
What have you set STATIC_URL to in your settings file? That in part is what controls paths when links are created.
https://docs.djangoproject.com/en/1.4/howto/deployment/wsgi/modwsgi/#serving-the-admin-files
My problem was that I served static content on a subdomain and I had problems with Access Control.
My solution was to set
Header set Access-Control-Allow-Origin *
in my apache config file

Django tutorial on remote server: how to view in my browser?

I'm getting started with a Django tutorial, and I've run into a snag. Having created the sample "mysite" on my usual domain, I want to be able to display it in my browser. The tutorial points me to http://127.0.0.1:8000. However, that's not going to work, as I'm doing this remotely.
[background information]
What I have done, apparently successfully, is
django-admin.py startproject mysite
(created mysite directory containing four files)
python manage.py runserver
(Validating models... 0 errors found, etc.)
The absolute path is
/home/toewsweb/public_html/pythonlab/mysite
What URL should I be able to use to bring this up in my browser?
I also put mysite at
/home/toewsweb/mysite (since it's not supposed to go in a publicly accessible directory)
What URL should I be able to use in this case?
This is a virtual private server, so I have access to httpd.conf. I have downloaded and installed mod_wsgi and have added it to the Apache configuration. I actually did set a subdomain with a DocumentRoot of /home/toewsweb/public_html/pythonlab/mysite; however, when I point the browser to that subdomain, I just get the directory listing.
[/background information]
Right now, I just want to know how to view what I'm working on in my browser.
Thanks!
For development purposes, there's no need to mess about with configuring WSGI (although it's useful to know, as you will need to do it for production). Just start the dev server so that it listens to an external port:
./manage.py runserver 0:8000
This binds to the external IP address, so now you can access your Django site via port 8000 on that server:
http://whatever.my.ip.is:8000
You need to setup the apache WSGIScriptAlias directive in your VirtualHost to properly load python and your site. Django's docs have a great explanation on what you need to do.
Basic configuration
Once you’ve got mod_wsgi installed and activated, edit your httpd.conf file and add:
WSGIScriptAlias / /path/to/mysite/apache/django.wsgi
The first bit above is the url you want to be serving your application at (/ indicates the root url), and the second is the location of a "WSGI file" -- see below -- on your system, usually inside of your project. This tells Apache to serve any request below the given URL using the WSGI application defined by that file.
Next we'll need to actually create this WSGI application, so create the file mentioned in the second part of WSGIScriptAlias and add:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If your project is not on your PYTHONPATH by default you can add:
path = '/path/to/mysite'
if path not in sys.path:
sys.path.append(path)
just below the import sys line to place your project on the path. Remember to replace 'mysite.settings' with your correct settings file, and '/path/to/mysite' with your own project's location.
OR
The other option is to run the dev server so it's accesible externally like so:
python manage.py runserver 0.0.0.0:80
though please DO NOT use this in production. The dev server is single-threaded, and has not been auditing for security.