Installing, configuring and developing for django on ubuntu 10.10 server - django

I wanted to start getting into developing with Django, however, I am unable to figure out how to make it work. I have installed apache2, I have tried many tutorials on configuring apache to run Django, but I just do not understand how it all works together. Can someone give me a dummies guide on how to install it, how things work, and why?

The best way to link between Django and Apache is using WSGI. You will need to install the mod_wsgi apache module to do this.
Next step: modify the apache configuration file to designate where you want the root of your django website.
WSGIScriptAlias / /path/to/mysite/apache/myApp.wsgi
Next, you should create the wsgi file. This is what initializes your django application. An example wsgi file looks like this
import sys
import os
sys.path.insert(0,os.path.normpath(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0,'/path/to/directory/containing/application')
import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'twitmycity.settings'
application = django.core.handlers.wsgi.WSGIHandler()
Once you have this, restart apache
sudo /etc/init.d/apache2 restart
Now, point your browser to the root directory where you established the wsgi handler. This should bring you to the root to your django application. I hope this helps!
Also note, when you make a change to your application, you need to refresh the modified time on the wsgi file to prevent wsgi from just using a cache version of the django application. To do this, execute
touch myApp.wsgi

Related

Flask UWSGI ModuleNotFound

I have a Flask application that I'm trying to configure for a production environment. So far, during testing python app.py inside of a virtualenv works fine.
However, I'm beginning to configure the application to begin using UWSGI. When I attempt to start the Flask app using
$ uwsgi -s /tmp/myapplication.sock --manage-script-name --mount /myapplication=app:app
--virtualenv /path/to/my/venv
I get a
ModuleNotFoundError: No module named 'flask_httpauth'
I was wondering if anyone has experience w/ that authentication module and if they have any advice on what I should do. I'm unsure of why the development server starts without an issue, but the uwsgi server (that I pass the same virtualenv that's used for the dev server) runs into import issues
I was able to resolve this issue by setting the home path to my virtualenv directory (.venv in my case):
# uwsgi.ini
home = /Users/floatingrock/Desktop/projects/google_v2/.venv/
If you're wondering, I got the full path using pwd.

Django Project not being served by Apache

My Django project is not being served while running on Apache 2.2.
All I can see is the folder structure upon hitting the URL of the domain name.
I've mod_wsgi installed but I think it's not linked well.
Can anybody tell what may be the problem?
Here's the URL of the website
http://ccbstca.ccbst.info/
UPDATE 1
You need to install and enable mod_wsgi for apache. And specify WSGIScriptAlias in apache virtual host config. This link might help:
http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/
ORIGINAL
I think You need to name your wsgi script file as a python file (.py)
And following wsgi code might just help:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ppbase.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
If you can post your apache virtual host config file, it would help in answering this.

Do I need to restart apache each time after changing code

I am deploying a Django app on webfaction. All code are uploaded, but sometimes there are still some modification I need to make. My workflow is edit local file in Pycharm and sync the change to remote server (Apache). So, for example, do I need to restart apache every time after changing views.py for the change to take effects?
If you are using modpython yes, you have to reload apache every time or your modules will not be reloaded.
If you are using apache and mod_wsgi then touching the wsgi module is enough.
As other already suggested nginx + gunicorn (or better nginx + uwsgi) are better options than apache + mod_python to serve your django app.
In fact, you do not have to, you just have to « touch » the wsgi.py file (if you are using the mod_wsgi apache2 module). You can do it by uploading the wsgi.py file every time you upload something.

Restarting Apache from Django

I need my Django app to be able to restart the Apache server running it. I'm using mod_python.
Which is the easiest way?
The only way this would be possible is if you gave your Django application root permissions, which is horribly unsafe. But, assuming that your app is running as root, you can run
import subprocess
subprocess.Popen(['/etc/init.d/apache2', 'restart'])
Or whatever command your distribution has for restarting Apache. If the Django app is not root, you may be able to run it with sudo using the pexpect python library.
import pexpect
child = pexpect.spawn('sudo /etc/init.d/apache2 restart')
child.expect('[sudo] password for .*:')
child.sendline(password)
child.interact()
Note that this way requires you to give the apache user the ability to run sudo which is also insecure. In general, I can't see a secure way to accomplish this.

how to restart single apache site on a ubuntu vps rather than all sites

I've got a ubuntu vps and apache mod wsgi installed and serving my django sites.
however i have to restart all of apache rather than the site i have amended and its going to be a bit shonky if i tell clients that i have restarted their site cos i updated another site.
is there a tutorial somewhere to teach me how to configure this? i couldnt find one in googles keywords soup.
I'm already using virtualenvs if it helps.
Assuming you use mod_wsgi in daemon mode on UNIX/Apache 2.X system to run Django and have shell access to your machine all you need to do is touch the wsgi configuration for your project.
touch your_project.wsgi
See mod_wsgi documentation on Reloading Source Code and Django - mod_wsgi wiki for more references.