I'm using Windows XP and want to know how can I create multiple django versions on a single apache service through virtual host(of course).
I'm trying to do that with one instance of python too. Should i create 1 instance of python for each django version or django needs only its eggs to work, so I can have several eggs in just one python version?
You can do something like this in your httpd.conf
NameVirtualHost 0.0.0.0:80
<VirtualHost 0.0.0.0:80>
ServerName myserver.com
ServerAdmin myemail#gmail.com
DocumentRoot "/path/to/html/root"
ErrorLog "/path/to/apache-error.log"
CustomLog "/path/to/apache-access.log" common
Options ExecCGI FollowSymLinks MultiViews
AddHandler wsgi-script .wsgi
WSGIDaemonProcess djangoapp1
WSGIProcessGroup djangoapp1
WSGIScriptAlias / /path/to/djangoapp1.wsgi
Alias /static /path/to/static/files
DirectoryIndex index.html index.cgi
AddHandler cgi-script .cgi .pl
</VirtualHost>
NameVirtualHost 0.0.0.0:81
<VirtualHost 0.0.0.0:81>
ServerName myserver.com
ServerAdmin myemail#gmail.com
DocumentRoot "/path/to/html/root"
ErrorLog "/path/to/apache-error.log"
CustomLog "/path/to/apache-access.log" common
Options ExecCGI FollowSymLinks MultiViews
AddHandler wsgi-script .wsgi
WSGIDaemonProcess djangoapp2
WSGIProcessGroup djangoapp2
WSGIScriptAlias / /path/to/djangoapp2.wsgi
Alias /static /path/to/static/files
DirectoryIndex index.html index.cgi
AddHandler cgi-script .cgi .pl
</VirtualHost>
And then, in your djangoapp1.wsgi/djangoapp2.wsgi script you can define the different django versions and applications:
#!/usr/bin/python
import os
import sys
sys.path.append('')
sys.path.append('/path/to/python2.7/site-packages')
sys.path.append('/path/to/python2.7/dist-packages/Django-1.3-py2.7.egg ')
... etc ...
sys.path.append('/path/to/djangoapp1/src')
os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoapp1.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Method 1:
put django source anywhere you want and manually specify path to django source in your manage.py and wsgi.py:
import os
os.path.insert(0, 'path-to-django-source');
You can also use virtualenv. Virtualenv fixes paths for console apps automatically, however for wsgi.py you still have to write down path's manually.
Method 2:
Use zc.buildout and djangorecipe, it will do all the stuff for you including:
donwloads django
download other modules
creates wsgi.py at project-dir\bin\wsgi
creates manage.py at project-dir\bin\django.exe
All this is done with a single config file buildout.cfg- here you list your modules and other settings, and then you run a command: buildout -N.
However buildout might not be a good solution if you have tight deadlines because there will be things you'll need learn about it but if you are planning to do more python apps I definitely recommend trying it.
Here are some examples for django+buildout setup:
http://www.google.lt/search?q=django+buildout+template+OR+skeleton
An update to your comment
You cannot install two django versions system wide.
What you can do though is either:
Do not install django, just drop the django-base/django folder into your project path. You will have to compile the internationalization files manually (if you use i18n):
cd django\conf
python ..\..\manage.py compilemessages
Or, install django with python setup.py install, but use extra arguments to change installation destination. Python documentation covers few different methods.
Related
I've been trying to work out how I'll go about setting up a Django application on production, when it's ready for deployment. I'm using Django v1.11, and my EC2 is running Ubuntu 14.04. I have been attempting to refer to this guide as reference, however it is not specific to Ubuntu, so I've been experiencing a bit of difficulty in this regard. I've referred to several other resources, but much of what is out there seems to be outdated.
I have a host rule set up on my local machine, pointing www.example.com to my EC2 instance's public IP address.
I have a virtualenv set up which lives in /home/django/example.com/ENV. My Django project lives in /home/django/example.com directly. The project name is mysite, and was generated using django-admin startproject mysite, thus it has the default wsgi.py file inside the /home/django/example.com/mysite directory. The contents of wsgi.py look like:
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
I've tried adding VirtualHost rules such as the following:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
<Directory "/home/django/example.com/mysite">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Similarly, I've tried adding:
Include /etc/apache2/httpd.conf
to /etc/apache/apache2.conf and chucking the following:
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
WSGIPythonHome /home/django/example.com/ENV
WSGIPythonPath /home/django/example.com
<Directory /home/django/example.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
into httpd.conf.
In either case, I've restarted the Apache server directly afterwards.
I'm not getting any further than hitting "500 Internal Server Error" or hitting an "ERR_EMPTY_RESPONSE".
Anyone able to shed some light around a) where I'm going wrong, b) where I can refer to for up-to-date instructions, or c) how I can troubleshoot this?
After a lot of troubleshooting, and consulting with the resource mentioned in Graham's comment, here's what I established was required in my VirtualHost:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Alias /static /home/django/example.com/static
<Directory /home/django/example.com/static>
Require all granted
</Directory>
WSGIDaemonProcess mysite python-path=/home/django/example.com:/home/django/ENV/lib/python3.4/site-packages
WSGIProcessGroup mysite
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /home/django/example.com/mysite/wsgi.py
<Directory /home/django/example.com/mysite>
Require all granted
</Directory>
</VirtualHost>
and here are the contents of wsgi.py that I settled on:
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os, sys
from django.core.wsgi import get_wsgi_application
path = '/home/django/example.com'
if path not in sys.path:
sys.path.append(path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
And it is also worth noting (simply because it wasn't immediately obvious to me) that it is necessary to specify:
STATIC_ROOT = '/home/django/example.com/static'
in settings.py, and to run python manage.py collectstatic to collect static files from all applications into said STATIC_ROOT.
I hope this helps somebody else, in future!
I am new to working with apache and mod_wsgi. But I do have little experience in django, so from some tutorials I tried to run django app through apache webserver using mod_wsgi.
I created mysite in /var/www/
then in mysite/application I created application.wsgi ...
import os
import sys
sys.path.append('/var/www/mysite/application')
os.environ['PYTHON_EGG_CACHE'] = '/var/www/mysite/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
and in /etc/httpd/sites-available I created file named mysite.conf ...
<VirtualHost *:80>
ServerName mysite.com
ServerAdmin id#somewhere.com
ServerAlias mysite.com
DocumentRoot /var/www/mysite/
<Directory /var/www/mysite>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
</Directory>
WSGIDaemonProcess mysite processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup mysite
</Virtualhost>
Then I ran a2ensite mysite.conf, didn't showed any error.
Then in /etc/httpd/hosts/ I added one line my-ipddress mysite.com
I gave permission chmod 777 to all the above files and to folder /var/www/mysite. Now when I open mysite.com on browser I see apahce's default page nothing from django.
I am using fedora 21.
You haven't put in anything in that configuration to serve your Django site via WSGI: you're missing the WSGIScriptAlias line (see the documentation:
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Note that you shouldn't really be putting things in /var/www; and also you shouldn't need to create your own WSGI file, Django creates one for you when you create a project.
I am trying to set up apache with an existing django project using the tutorial in django site here. My os is Ubuntu, and everything is installed (django apache2 libapache2-mod-wsgi)
My conf file
WSGIPythonPath /home/avlahop/development/django/rhombus2/rhombus/
<VirtualHost *:80>
ServerName myrhobmus.com
ServerAlias www.myrhombus.com
WSGIScriptAlias / /home/avlahop/development/django/rhombus2/rhombus/rhombus/wsgi.py
</VirtualHost>
After I created the conf file I ran the a2ensite Ubuntu command for enabling the site.
Putting WSGIPythonPath inside VirtualHost gives me an apache configtest failure
Files inside directive inside directory (as described in the example) gives me the same failure
If I go to www.myrhombus.com I get a Google chrome could not find the specified address message.
What am I doing wrong? Every tutorial on the Internet is using the old file.wsgi while now Django creates this for you but it is a python file with .py extension. How can I serve my django with apache? And If I wanted to go production at my own server where would you put django code and where would you put template files?
EDIT: I am only getting the Index of / page. Is there something I have to do with mysites location in terms of permissions? Could you give me a working example of an django site apache conf file?
I use Django 1.8 and I successfully deployed my project Apache server. I followed basic concept like you and enable Apache this module early.
enable module
You can my project structure this question.Refer this question to understand project structure
--------------this is my virtual host file----------------------------------
WSGIPythonPath /home/umayanga/Desktop/view_site/serialKey_gen_site:/home/umayanga/Desktop/view_site/serialKey_gen_site/myvenv/lib/python3.4/site$
<VirtualHost *:80>
ServerAdmin admin#key.com
ServerName key.com
ServerAlias www.key.com
Alias /templates/ /home/umayanga/Desktop/view_site/serialKey_gen_site/templates/
Alias /static/ /home/umayanga/Desktop/view_site/serialKey_gen_site/static/
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/static">
Require all granted
</Directory>
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/templates">
Require all granted
</Directory>
WSGIScriptAlias / /home/umayanga/Desktop/view_site/serialKey_gen_site/mysite/wsgi.py
<Directory "/home/umayanga/Desktop/view_site/serialKey_gen_site/mysite">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
-----------------wsgi.py---------------------------------------
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
application = get_wsgi_application()
I think this will be help to you.
I am trying to setup a LAMP server in my lab, and I'm having trouble getting Apache to execute the .py files. It instead just downloads them. At first I thought my header might be wrong, but when I changed it, it unfortunately I'm still not executing the .py. On the plus side I can load the site, run the PHP, and CRUD the MySQL. I think the problem might be in how I'm setting up my Virtual Host. Here is the Apache2.conf:
<VirtualHost *:80>
Alias "/SiteIwant" "/var/www/SiteIwant"
ServerName localhost
DocumentRoot /var/www/SiteIwant
CustomLog /var/www/SiteIwant/my_access.log combined
ErrorLog /var/www/SiteIwant/my_error.log
AddType application/x-httpd-php .php
SetEnv LD_LIBRARY_PATH /etc/init.d/mysql
<Directory /var/www/SiteIwant>
Options None ExecCGI
AddHandler cgi-script .cgi .pl .py
#AddHandler mod_python .py
DirectoryIndex index.php
AllowOverride AuthConfig
Order deny,allow
Deny from All
Allow from 999.999.999.0/24 #anonymized for posting here, but working
</Directory>
# <Directory /var/www/SiteIwant/cgi/>
# AllowOverride All
# Options +ExecCGI +SymLinksIfOwnerMatch
# Order allow,deny
# Allow from all
#</Directory>
</VirtualHost>
I've tried it with and without the specification on the cgi folder, and I've chkmod +rwx *.py in /var/www/SiteIwant/cgi. Just for kicks (after that didn't help), I also changed the mode of the python interpreter in /usr/bin and /usr/local/bin to +rwx.
Everything else in the apache2.conf file is as it comes out from current Ubuntu Server-LAMP option install.
I feel very stuck and like I'm missing something stupid/small.
Edit: Should this really be asked on Server Fault?
If I put an AddHandler cgi-script .cgi .pl .py outside the the Virtual Host, I get a 403 permission error, despite chmod 777 the folder.
late answer, I got through there too and got this working by adding ExecCGI in directory option, or for more security, like this:
<Directory /path/to/www/yourfile.py>
Options +ExecCGI
</Directory>
Do you have the apache wsgi module installed and enabled?
This statement in the vhost and/or global config should be enough (and you have it in your config):
AddHandler cgi-script .py
Did you reload Apache after changing the config? If this doesn't work, you should check your webserver's errorlog, it should give some hints on the cause of the problem.
Also, you didn't describe what happens when you visit the URL hosting the python script, do you get an error message?
I have two apps with nearly identical code base (same functionality, just different branding) that I am trying to do virtual hosting with using mod_wsgi and apache.
the virtual host settings for both apps (in two separate files) are identical (with the exception of paths of course)
WSGIPythonHome /home/ubuntu/BASELINE
<VirtualHost *:80>
ServerName appA.com
ServerAlias www.appA.com
ServerAdmin admin#appA.com
DocumentRoot /home/ubuntu/appA_virtualenv/appA-server/appA
ErrorLog /home/ubuntu/appA_virtualenv/appA-server/error.log
CustomLog /home/ubuntu/appA_virtualenv/appA-server/access.log combined
Alias /static/ /home/ubuntu/appA_virtualenv/appA-server/appA/appA/static/
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
WSGIDaemonProcess appA user=www-data group=www-data \
python-path=/home/ubuntu/appA_virtualenv/lib/python2.7/site-packages/ \
home=/home/ubuntu/appA_virtualenv/appA-server/appA/
WSGIProcessGroup appA
WSGIScriptAlias / /home/ubuntu/appA_virtualenv/appA-server/appA.wsgi
<Directory /home/ubuntu/appA_virtualenv/appA-server>
WSGIProcessGroup appA
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
.
<VirtualHost *:80>
ServerName appB.com
ServerAlias www.appB.com
ServerAdmin admin#appB.com
DocumentRoot /home/ubuntu/appB_virtualenv/server/appB
ErrorLog /home/ubuntu/appB_virtualenv/server/error.log
CustomLog /home/ubuntu/appB_virtualenv/server/access.log combined
Alias /static/ /home/ubuntu/appB_virtualenv/server/appB/appB/static/
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
WSGIDaemonProcess appB user=www-data group=www-data \
python-path=/home/ubuntu/appB_virtualenv/lib/python2.7/site-packages/ \
home=/home/ubuntu/appB_virtualenv/server/appB/
WSGIProcessGroup appB
WSGIScriptAlias / /home/ubuntu/appB_virtualenv/server/appB.wsgi
<Directory /home/ubuntu/appB_virtualenv/server>
WSGIProcessGroup appB
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Contents of appA.wsgi
import os
os.environ['PYTHON_EGG_CACHE'] = '/home/ubuntu/appA_virtualenv/appA-server/python-eggs'
from pyramid.paster import get_app, setup_logging
ini_path = '/home/ubuntu/appA_virtualenv/appA-server/appA/development.ini'
setup_logging(ini_path)
application = get_app(ini_path, 'main')
Contents of appB.wsgi
import os
os.environ['PYTHON_EGG_CACHE'] = '/home/ubuntu/appB_virtualenv/server/python-eggs'
from pyramid.paster import get_app, setup_logging
ini_path = '/home/ubuntu/appB_virtualenv/server/appB/development.ini'
setup_logging(ini_path)
application = get_app(ini_path, 'main')
I enabled both sites in apache and realize that when accessing appA, the python scripts in appB's directory is loaded instead. I confirmed this by adding a print statement in the __init__.py of both apps and the text for appB is printed even when I try to access appA.
I then disabled appB by $ sudo a2dissite appB. appB.com fails to load, but appA.com is still loading appB's codes...
I added the following to appB __init__.py (Note: NOT appA)
print sys.path[0]
print os.getcwd()
the outputs are
/home/ubuntu/appA_virtualenv/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg
/home/ubuntu/appA_virtualenv/appA-server/appA
which is beyond my comprehension because the output point correctly to appA's directory, yet the print are added to appB's __init__.py which is located in /home/ubuntu/appB_virtualenv/server/appB/appB/__init__.py
Also, I have already disabled appB and the apache settings no longer refer to appB's directory anywhere anymore, so why is appA still loading appB's scripts?!
what am I doing wrong? What settings do I need to change to get appA to load the scripts in its own directory? Thanks
Additional checks as suggested by Graham
Embedded or Daemon Mode
Daemon mode: mod_wsgi.process_group = 'appA'
Sub Interpreter Being Used
Main Interpreter: mod_wsgi.application_group = ''
Request Environment
PATH_TRANSLATED: '/home/ubuntu/appA_virtualenv/appA-server/appA.wsgi/'
SERVER_NAME: 'appA.com'
SCRIPT_FILENAME: '/home/ubuntu/appA_virtualenv/appA-server/appA.wsgi'
mod_wsgi.application_group: ''
mod_wsgi.process_group: 'appA'
mod_wsgi.version: (3, 4)
Nothing looks an issue on first glance.
Use:
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Embedded_Or_Daemon_Mode
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Sub_Interpreter_Being_Used
to confirm what process and application group they are each executing in.
Use:
http://code.google.com/p/modwsgi/wiki/DebuggingTechniques#Displaying_Request_Environment
to see what SERVER_NAME is set to for each in requests.
Edit your question with the results.
UPDATE 1
Where there are two VirtualHost's in the Apache configuration, one cause of requests always ending up at the first VirtualHost and never reaching the second is because NameVirtualHost directive isn't being specified for the port the VirtualHost's are listening on. Normally on Linux this isn't a problem though for port 80 as it would be on by default.
The other reason it can occur is because the host name used in the URL doesn't actually match either the ServerName or ServerAlias in the desired VirtualHost. When this happens Apache will fallback to directing the URL to the first VirtualHost it found when the Apache configuration file was read. To catch when this is occurring, it is generally a good idea to declare a default VirtualHost which is read first and which denies all access. At least this way a request will not end up at the wrong VirtualHost and will be blocked.
For details of this issue with it falling back to the first read VirtualHost, see discussion in:
http://blog.dscpl.com.au/2012/10/requests-running-in-wrong-django.html
Try defining the default VirtualHost and see whether this issue of it falling back to the first VirtualHost is the issue. Then you can workout what is the issue with the Apache configuration which is causing that to occur.