Bitnami Django creating multiple projects - django

So I have this Bitnami EC2 instance which had project 'Project' in it by default (at /apps/django/django_projects), so I started following the django tutorial and done everything. I am able to access this project at
http://myIp/Project/.
So after having finished the tutorial I set off to create my own project. I created the project using
django-admin.py startproject DoE
in the same directory as project 'Project' (i.e. /apps/django/django_projects), followed by
python manage.py runserver 0.0.0.0:8000
But the problem is when I go to
http://myIp/DoE/
I get this error:
Not Found
The requested URL /DoE/ was not found on this server.
Any help will be much appreciated. Thanks in advance :)

I've found a way to work around this issue, it's a bit hacky but it does the job.
I got it from here http://wiki.bitnami.org/Components/Django.
But they imply that you only need to do this if you are using an apache webserver which I am not. I am using the django's development server, but nevertheless it works.
Basically I had to create a DoE.conf file in /home/bitnami/apps/django/conf which looks like this:
Alias /static "/opt/bitnami/apps/django/lib/python2.7/site-packages/django/contrib/admin/static"
<Directory '/opt/bitnami/apps/django/lib/python2.7/site-packages/django/contrib'>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias /DoE "/opt/bitnami/apps/django/scripts/DoE.wsgi"
<Directory '/opt/bitnami/apps/django/scripts'>
Order allow,deny
Allow from all
</Directory>
and then create a file named DoE.wsgi in /home/bitnami/apps/django/scripts which looks like this:
import os, sys
sys.path.append('/opt/bitnami/apps/django/django_projects')
sys.path.append('/opt/bitnami/apps/django/django_projects/DoE')
os.environ['DJANGO_SETTINGS_MODULE'] = 'DoE.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
after that I had to include
/opt/bitnami/apps/django/conf/DoE.conf
in my httpd.conf file, which for my case was here: /opt/bitnami/apache2/conf
After this I restarted my machine/server and all was well :)

Related

Django Admin Static Files on Apache

There are quite a few posts on here regarding the same subject, I've tried as many as I thought applicable. Yet, I still have this error. Everything is being served fine for a django site I've built except the static files for admin.
Versions: Django 1.6, Apache 2.4.7, Ubuntu 14.04
Ok - I have ran manage.py collectstatic and here is the static folder:
/srv/bahai-site/bahai-site/soul/static
This contains four folders: 1) admin/ 2) bootstrap/ 3) core/ and 4) flags/
Within these folders, I can see all expected static files and that they contain the code I'd expect.
When looking at a static file on the site itself, something like /static/core/css/desktop_992.css loads correctly while /static/admin/css/base.css throws a 404.
Apache directives in http.conf:
Alias /static/ /srv/bahai-site/bahai-site/soul/static/
<Directory /srv/bahai-site/bahai-site/soul/static/>
Allow from all
</Directory>
WSGIScriptAlias / /srv/bahai-site/soul/soul/wsgi.py
WSGIPythonPath /srv/bahai-site/soul/soul:/srv/envelope/bahai-site/lib/python2.7/site-packages
<Directory /srv/bahai-site/soul/soul>
<Files wsgi.py>
Allow from all
</Files>
</Directory>
Side question: notice the WSGIScriptAlias and WSGIPythonPath only contains one "bahai-site/" directory level, instead of the two that actually exist after "/srv/". Why is the site running fine? Shouldn't that be causing a problem?
Anyway, what am I doing wrong here? How do I get these admin static files up and running?
as far as the pythonpath is concerned I personally don't include that in my apache virtual host config file and it works. Maybe that's not needed? Honestly I'm not all that well versed in server installations. It defintely seems like a permission issue.
I would run collectstatic using sudo, then I would also make sure the static folder has www-data group permission which is the permission apache uses out of the box.
If you need help setting permissions on a folder in Ubuntu check out this link here. https://askubuntu.com/questions/244406/how-do-i-give-www-data-user-to-a-folder-in-my-home-folder
The problem was that I had updated two different configuration files for Apache with competing directives.
Note to Other Beginners:
Directives (i.e. things like ...) can be placed in any file in the apache2 root -- in my case that was any file within /etc/apache2/. (Question to advanced users: is this only for Ubuntu? Or pretty much any Linux/Unix?) This will confuse you because, if you solve a problem using information from multiple online posts/documents, you'll likely have a smorgasbord of various people's Apache configuration preferences/styles. Hence my problem.
As a beginner, you're probably expecting to see some kind of single location you can put your configurations/customizations (in this case directives) into. That's not the case on Apache. You can put any of these anywhere. This is probably good once you advance your knowledge of Apache but it sucks when you're starting out.
My fix here was to go through all the files I touched in the /etc/apache2/ folder and consolidate any rules into one place. This lead to me removing duplicate/conflicting rules, which solved the problem.
Now I think I need to talk to a therapist....

403 not found in EC2 - apache&WSGI error

I have editted the httpd.conf inside apache by adding:
<Directory /home/ec2-user/hqlocal>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / /home/ec2-user/hqlocal/hq_local/apache/django.wsgi
WSGIPythonPath /usr/lib/python2.7/site-packages
# Serving Django admin static files
Alias /static/admin /usr/lib/python2.7/site-packages/django/contrib/admin/static/admin
# Serving Django static files (our own files use in our app), assuming in settings.py, STATIC_ROOT is “/var/www/static/” and our STATIC_URL is “/static/”
Alias /static /var/www/static/
I have tried many solutions that fix the 403 problem like swapping deny and allow, and so on. But none of them works, I still get 403 cannot access '/'
Therefore, I tried commenting line by line.
The result is that the line
WSGIScriptAlias / /home/ec2-user/hqlocal/hq_local/apache/django.wsgi
causing the problem.
By adding this line, the system becomes 403 but without it, when entering the public DNS, the server will point to Apache default page.
So, I assume that Apache may not have permission to read this file. Therefore, i changed the permission to be global. However, it still didn't work properly.
May I know how to fix this?
Thank you very much.
The directory '/home/ec2-user' is likely not readable to the Apache user. Watch:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
for common mod_wsgi setup issues including this one.
Also, you should not need:
WSGIPythonPath /usr/lib/python2.7/site-packages
if mod_wsgi is actually compiled against the system Python 2.7. If it isn't compiled against the system Python 2.7, but another version, you should not be forcing it to use modules installed into another Python version as that will cause problems. You should reinstall mod_wsgi with version compiled against correct Python version you want to use.
There are two possible reasons.
Make sure you set the permission correctly for the directory '/home/ec2-user/hqlocal', you can do this:
chmod -R 755 /home/ec2-user/hqlocal
Disable selinux (if using enforcing mode, you need get permissions for apache in selinux). You can configure the /etc/selinux/config:
SELINUX:disabled
Then reboot the machine.

Deploying a Django App

I'm very sorry for such a simple question-- I'm new at WSGI development, and I'm grateful for any patience you can afford.
I made a Django app; it works great in development mode. I run:
python manage.py runserver
and then direct my browser to 127.0.0.1:8000, and voila, there is my app.
From here I absolutely cannot figure out how to run my app in production mode. I've read several pages like this and this and several others on StackOverflow. But I have no idea of where to even direct my browser to see if my page is working.
I've installed apache2, mod_python, etc., but I think the problem is that my misunderstanding is at such a more basic level. When I've done CGI programs in the past, I direct my browser to webroot/file.html with a form that calls cgi-bin/file.cgi, which generates html output. I don't know if I am supposed to navigate to a .wsgi page, etc.
Under the assumption that I'm supposed to navigate to a .wsgi file, I've also tried making a file containing:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
path = '/home/orserang/nonparametric-protein/src/www/mysite$'
if path not in sys.path:
sys.path.append(path)
and added
WSGIScriptAlias / /path/to/mysite/apache/django.wsgi
to my apache2/httpd.conf file, so that its contents are:
<Location "/mysite/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonOption django.root /mysite
PythonDebug On
PythonPath "['/home/orserang/nonparametric-protein/src/www/mysite'] + ['/home/orserang/nonparametric-protein/src/'] + sys.path"
WSGIScriptAlias /mysite /home/orserang/nonparametric-protein/src/www/mysite/django.wsgi
</Location>
But when I restart apache, it says:
Syntax error on line 8 of /etc/apache2/httpd.conf:
WSGIScriptAlias not allowed here
Given that I don't even know where I should point my browser to get to a Django wsgi page, I figured there is something easy that I'm doing quite wrong.
Perhaps Django WSGI apps require something to run in the background, which will listen for requests (rather than go through apache)?
The online Django documentation on views and databases alone are substantial compared to the documentation for deployment; therefore, my best guess is that this is such a simple thing to do.
Thanks a lot for your help!
The Django Book 2.0 has an overview about this. It's not typically linked to in the Django docs:
Chapter 12: Deploying Django
Look at the "Using Django with Apache and mod_python" section.
You're mixing up mod_python and mod_wsgi deployment methods. Get rid of everything inside the Location directive except for the WSGIScriptAlias line.
I wrote shell script that deploys a django project on apache for linux,
https://github.com/mukulu/shell-scripts/blob/master/deploy-django.sh
You only need to configure couple of variables in first lines of the code,
and it'll figure out the rest.
It pretty much checks and install dependencies for django, writes apache configurations that deploys your project and restart the server.
I'm planning to re-write it in python(I wrote it in a hurry)
Feel free to re-use.
Variables are:
SITE_PREFIX="/djangoproject"
MEDIA_URL="/media"
ADMIN_MEDIA_PREFIX="/static/admin/"
MEDIA_ROOT=""
DJANGO_VERSION="1.3.1"
APACHE2_CONFIG="/etc/apache2/conf.d" #Apache configurations directory in yoru system.
This might be not for a novice, but you can take a look - http://packages.python.org/django-fab-deploy/
It's the library for automating the deploying process. It supports servers based primarily on Debian Lenny or Squeeze.

Django Import errors on production environment

i have a trouble with run django project on production server with Apache and mod_wsgi. This Error happened when i'm start apache and go to site first time or go from other:
ImportError at /
Exception Value: cannot import name MyName
Exception Location /var/www/projectname/appname/somemodule.py
When i'm reload page the error disappears and site work fine. Another point is that this error happened selectively and sometime not appear.
In project i'm use imports without project name prefix (i mean 'from accounts.models import Account' instead 'from projectname.accounts.models import Account').
On development (manage.py runserver) server all work fine without any troubles.
I have used many variations of my apache and wsgi script configurations but problem is not solved.
Here my current projectname.wsgi:
#!/usr/bin/env python
import os, sys, re
sys.path.append('/var/www/projectname')
sys.path.append('/var/www')
os.environ['PYTHON_EGG_CACHE'] = '/var/www/projectname/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Here is some parts from apache config:
<VirtualHost ip:80>
ServerAdmin admin#server.com
DocumentRoot /var/www
ServerName www.projectname.com
WSGIScriptAlias / "/var/www/projectname/projectname.wsgi"
WSGIDaemonProcess projectname threads=5 maximum-requests=5000
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
....
Also i'm use a separate Virtual Host for SSL.
I hope somebody help me.
Thanks!
When I had this problem, this post https://stackoverflow.com/a/10601523/1782780 pointed me towards the answer: I had a bug in a module further up the chain.
As Michal explains in that post:
Python does not just select that one class from the administrative/models.py file. Instead the Python interpreter reads the entire file, creates the module object, executes the code from the imported file in the new modules namespace, and then copies the name Contract from the new module's namespace to the current namespace. So, although it seems that you are importing just one class from a module, an error anywhere in that module can prevent a successful import - in your case it is an error with class ContractForm. The rest of the traceback details what exactly went wrong with that class.
So look further back in your traceback and you might find your problem.
I add a similar problem and the cause was that the 'current directory' was 'www' and not root project dir.
In the django development server the server environment is set according to the current project you are working. Thats why the import errors occur while shifting to Apache.
While using import use " from projectname.appname.models import MyModel ". Be as specefic as you can.
Use sys.path.append('location/to/your_project_foler/') in your case "/var/www"
Just a suggestion:
Also try to set-up django in another url than root. I also had a similar problem while trying to run django on the site root, i.e. :
WSGIScriptAlias /< some url > "/var/www/projectname/projectname.wsgi"
In my case it was a leftover directory from the past (filled with only the pyc compiled files which possibly caused the directory to stuck without deletion) which matched a new python file's name.
In the past a directory tasks was created in a module acme. Within that directory there was the __init__.py and four other python files.
The uwsgi service compiled the the pyc file for each of those python files once a request touched them.
Much later the files in that directory and the directory was removed from the Git source.
However because the .pyc files were in place the directory actually stayed on the server. (The git pull is incremental, we don't pull a complete source from ground zero).
Even more later (recently, now) I committed a tasks.py to that folder where there's this leftover tasks directory.
That made the uwsgi engine not being able to import anything from that file without any detailed reason.
The struggle to realize this took me more than half of my work day.

multiple django projects, mod_wsgi, single domain

I have two distinct Django "projects", which I want to run on a single domain using mod_wsgi. With mod_python, I believe there was a way to do this, where certain url paths would be mapped to one Django project, and other paths mapped to the other project, all at the server level.
Is it possible to do this with mod_wsgi, and if so, how?
Things I have considered: what goes in the Apache virtual host description, what goes into application.wsgi files, etc. But I haven't figured out exactly how to do this.
Thanks!
This shouldn't be complicated. It's just a matter of setting the WSGIScriptAlias directive - you'll need two of these, one for each path, each pointing to a separate .wsgi file which contains your project settings.
I am also working with Apache and I am running multiple Django projects with one domain. There are only two things you have to do:
Modify your Virtual Host files
Since I am using Debian I have one vhost file for each domain I am hosting. In your vhost file you should have multiple vhost sections. One for each project. Inside these sections you can define WSGIScriptAlias.
<VirtualHost *:80>
...
WSGIScriptAlias / /path/to/project1.wsgi
...
</VirtualHost>
<VirtualHost *:80>
...
WSGIScriptAlias / /path/to/project2.wsgi
...
</VirtualHost>
Of course you have to add all the other necessary information. Project 1 and 2 certainly will have different sub-domains. For example project1.yourdomain.com and project2.yourdomain.com.
Write your *.wsgi files
There are many ways to write and store *.wsgi files. I don't know any best practices. In my case I store them in my project folder.
This is an example:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path.append('/path/to/your/project')
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I've seen a lot of other *.wsgi files with more "magic". But this should get you started. You can find a lot of examples all over the internet.
Hope that answers your question. Don't be afraid to ask more questions.