I am running Ubuntu 14.04 on my notebook and I am using Django 1.6.4 (with virtualenv) together with Apache 2.4.7.
I would like to access two django projects through my local apache. Therefore, theses projects are called kleyboldt (name of a guy I am writing for) and kleyboldt2. Normally I store my web projects under /home/nick/Workspace/Web. After watching tutorial I ended up with two domains pointing to the same django project. To be sure I rewrote the config and placed my projects under /var/www.
At the beginning I wrote two files in order to configure apache.
/etc/apache/sites-available/kleyboldt.conf
WSGIPythonPath /var/www/kleyboldt:/var/www/kleyboldt/env/lib/python2.7/site-packages
<VirtualHost *:80>
WSGIScriptAlias / /var/www/kleyboldt/kleyboldt.wsgi
ServerName kleyboldt.com
Alias /static /var/www/kleyboldt/static
<Directory /var/www/kleyboldt/>
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
/etc/apache2/sites-available/kleyboldt2.conf
WSGIPythonPath /var/www/kleyboldt2:/var/www/kleyboldt2/env/lib/python2.7/site-packages
<VirtualHost *:80>
WSGIScriptAlias / /var/www/kleyboldt2/kleyboldt2.wsgi
ServerName kleyboldt2.com
Alias /static /var/www/kleyboldt2/static
<Directory /var/www/kleyboldt2/>
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Afterwards I enabled both sites via a2ensite. I checked the sites-enabled directory and the links are placed correctly. In order to redirect kleyboldt.com and kleyboldt2.com to my local apache I changed my /etc/hosts to:
127.0.0.1 localhost
127.0.1.1 mars (name of my computer)
127.0.0.1 kleyboldt.com
127.0.0.1 kleyboldt2.com
To use WSGI I wrote for each project a seperated wsgi file in the project folder:
/var/www/kleyboldt/kleyboldt.wsgi
import os
import sys
sys.path.append('/var/www/kleyboldt')
os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
/var/www/kleyboldt2/kleyboldt2.wsgi
import os
import sys
sys.path.append('/var/www/kleyboldt2')
os.environ['DJANGO_SETTINGS_MODULE'] = 'kleyboldt2.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I created two independent python environments using virtualenv that are now located under /var/www/kleyboldt/env and /var/www/kleyboldt2/env.
Afterwarsd I got two django test sites when I typed both domains in the URL bar of my browser. To differ I added some settings to the settings.py files and wrote custom views.
/var/www/kleyboldt/homepage/views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("kleyboldt1")
/var/www/kleyboldt2/homepage/views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("kleyboldt2")
When I type localhost in my browser then I get kleyboldt2 back. When I type kleyboldt2.com in my browser then I get kleyboldt2 back too. Unfortunately I see kleyboldt2 also after typing kleyboldt.com in my browser.
Maybe the bug is to simple for me to discover :D Does anybody know a possible solution?
Related
I need a bit of help here. I am new to python and django. For my project i am developing an system which deals with api fetching and insertions. I need this system to be online 24 * 7. so i need to the server to be running all the time.
Using the traditional method of:
python manage.py runserver
runs perfectly.
I even tried :
nohup python manage.py &
but when i access my project with the url. Its showing that it project is not online. How do i fix this?
If you are using Apache then mod_wsgi should be used along with Django for deployment. This is covered here in great detail.
If you want step by step guide then you can have a look at Digital Ocean's Guide.
Your Apache configuration will look like following:
<VirtualHost *:80>
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject python-home=/home/user/myproject/myprojectenv
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py
</VirtualHost>
You will need to create a wsgi.py file which looks like following(the default one):
"""
WSGI config for myproject 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.7/howto/deployment/wsgi/
"""
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Hope this helps.
I recently moved my Django project to Apache server. I am facing a few issues with respect to dependency modules being inaccessible after moving the project to Apache.
Here are my configuration files:
This is my bemoss.conf file in the sites-available directory of Apache:
WSGIPythonPath /home/kruthika/workspace/bemoss_web_ui
WSGIPythonPath /home/kruthika/workspace/rtunetwork/volttron
WSGIPythonPath /home/kruthika/workspace/bemoss_web_ui/helper
<VirtualHost *:80>
WSGIScriptAlias / /home/kruthika/workspace/bemoss_web_ui/bemoss.wsgi
ServerName bemoss.com
Alias /static /home/kruthika/workspace/bemoss_web_ui/static
<Directory /home/kruthika/workspace/bemoss_web_ui/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Here is my bemoss.wsgi file:
import os
import sys
sys.path = ['/home/kruthika/workspace']+sys.path
print sys.path
os.environ['DJANGO_SETTINGS_MODULE']='bemoss_web_ui.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
My project is accessing a few more python files from another project. I added a reference to those in my .conf file above and in settings.py file as given below.
PROJECT_DIR = os.path.dirname(__file__)
print PROJECT_DIR
sys.path.insert(3,os.path.join(PROJECT_DIR,'ZMQHelper'))
sys.path.insert(1,os.path.join(PROJECT_DIR,'helper'))
sys.path.insert(2, '/home/kruthika/workspace/rtunetwork/volttron')
print sys.path
This is my project folder definition:
When I try to access a link, whose views.py file refers to the above mentioned dependencies, it throws a import module error as given below.
My question here is how to refer to my dependent modules/python classes so that the files are imported properly.
All these imports were working fine when I executed the project on Django's development server. Is there some Apache configuration I am missing or doing wrong? I am new to server configurations and I think I am missing something here.
I have a small web application that I have built using Flask and python. With the internal server that I used for developing everything runs fine. However now I want to use apache to start using it. But it doesn`t work. Keep in mind that I have never worked with apache or web based stuff before.
I used this guide as my starting point:
http://flask.pocoo.org/docs/deploying/mod_wsgi/
right now I have my application which is in the file called "/rg/server.py" and looks like this:
app=Flask(__name__)
# all app routes...
if __name__ == '__main__':
app.run(
debug=True,
host="127.0.0.1",
port=80
)
than I have a wsgi file as "/rg/wsgi/minerva.wsgi"
import sys
sys.path.insert(0, /rg)
from server import app as minerva
and finally I have an apache config file in "etc/apach2/sites-available/minerva.com":
<VirtualHost *>
ServerName minerva.test
WSGIDaemonProcess minerva threads=10
WSGIScriptAlias / /rg/wsgi/minerva.wsgi
<Directory /rg>
WSGIProcessGroup minerva
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
Then I updated apache with a2ensite minerva.com which succeded. Then I releaded Apache and no errors. However I cannot acces minerva.test in any way...
If I type in apache2ctl -S it does list minerva.test
I have no idea what is going wrong...
system information:
OS: debian 64bit
python 2.7
The WSGI application entry point must be called 'application' for mod_wsgi. You have:
from server import app as minerva
It should be:
from server import app as application
You aren't even getting that far though, else the line:
sys.path.insert(0, /rg)
would give a syntax error.
Going back further, instead of:
<VirtualHost *>
you should have:
<VirtualHost *:80>
and finally, if 'minerva.test' isn't actually a resolvable host, you will not get anywhere.
So fill out your question with the actual URL you are using in the browser and also indicate whether 'minerva.test' is even listed in local hosts file.
The first thing I would check would be to make sure mod_wsgi is installed and loaded by apache. If that's fine, your setup looks pretty similar to mine with a few minor differences:
I had to add WSGISocketPrefix /var/run/wsgi above my VirtualHost definitoin. See here.
I included user and group values on the WSGIDaemonProcess line.
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 deploying a Django project on Apache. and after configuration, I open the "localhost" in the browser, and nothing showed up and the status bar just keep saying "Waiting for localhost". Here is some info.
1.Environment:
OS: Red Hat Enterprise Linux Server x86
Python: 2.7.2
Django: 1.3.1
Mod_wsgi: 3.3
Apache: 2.2.21
Django project: /var/www/html/server/video1
2.Apache Config file lines added:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias / /var/www/html/server/video1/apache/django.wsgi
Alias /media/ /usr/local/apache2/htdocs/media/
Alias /static/ /usr/local/apache2/htdocs/static/
<Directory "/var/www/html/server/video">
Order Deny,Allow
Allow from all
</Directory>
<Directory "/usr/local/apache2/htdocs/media">
Order Deny,Allow
Allow from all
</Directory>
<Directory "/usr/local/apache2/htdocs/static">
Order Deny,Allow
Allow from all
</Directory>
3.Django.wsgi file:
import os
import os.path
import sys
sys.path.append('/var/www/html/server')
sys.path.append('/var/www/html/server/video')
os.environ['DJANGO_SETTINGS_MODULE'] = 'video1.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
4.settings.py lines added:
MEDIA_ROOT='/usr/local/apache2/htdocs/media/'
MEDIA_URL='/media/'
STATIC_ROOT='/usr/local/apache2/htdocs/static/'
STATIC_URL='/static/'
ADMIN_MEDIA_PREFIX='static/admin'
TEMPLATES_DIR = {
"/var/www/html/server/video1/templates"
}
INSTALLED_APPS = {
'django.contrib.admin'
}
5.restart apache
These are what I did, can someone help me to see if somewhere is wrong?
Thank you very much
The path '/var/www/html/server/video' doesn't match what you have in WSGIScriptAlias.
Besides that, did you actually try and deploy a WSGI hello world program first?
Also try setting LogLevel to info in Apache instead of warn. The Apache error log will then contain messages to say whether the WSGI script file is loaded. Right now not obvious whether your browser can't even contact server, or whether WSGI application loaded, but not responding.