- Computers & Software»
- Computer Science & Programming
How to install \ deploy django on Xampp webserver with wsgiscriptalias on windows
1. Installing XAMPP for Windows
The most common web server to use MySQL, PHP or Perl application is Apache. Many websites use this server – it’s stable, well documented and it’s open-source. Of course you can use Apache as well, and learn all the things about running and maintaining web server and MySQL database. But if you need an “easy to use” and portable web server and still learn all the things about Apache you should consider XAMPP.
You can download XAMPP here. It’s really easy to install it, it’s well documented and it has installer or zip version.
As the installation process is vary well documented, we wouldn't focus on that now.
2. Installing python
Apache itself does not work as python (or django) server, so we need to configure it for our purpose. First thing to do is installing python on our server. You can download it from here. You should download the latest stable version. You should with version 2, until you would read that version 3 is fully supported by Apache and django. Python goes with Windows installer, so it shouldn’t be a problem to install it. Just follow the instructions on the screen. Remember to install Python for all users, because it wouldn’t work with Apache server. Apache server is run on special, system user and it needs access to your python files.
After installing Python you should add a path to python.exe to your system PATH variable.
3. Installing mod_wsgi
We would use python through cgi scripts, and for that we would need to install mod_wsgi. You could get raw version from project website here or compiled version with .so extension - here .Make sure, that you use a right version for your python and apache server.
You should place a file in apache modules folder – xampp\apache\modules and change it’s name for mod_wsgi.so .Next you have to locate and change apache configuration file - \xampp\apache\conf\http.conf. Edit it and at the end of LoadModule section add a line:
LoadModule wsgi_module modules/mod_wsgi.so
At this point we should create a folder for our application. It is the best practice, to set up a folder away from root web folder (not in xampp\htdocs). I’ve created one directly at partition d:\django-app .
Now we have to show our apache server where we store python scripts and how it should use it. In configuration file http.conf we search for “<Directory>” section. At the beginning we have there:
<Directory /> Options FollowSymLinks AllowOverride None Order deny,allow Deny from all </Directory>
Under that we add a WSGI handler with a line:
WSGIScriptAlias /wsgi "d:/django-app/wsgi_handler.py"
It means that we will use file wsgi_handler.py in d:/django-app for handling request from http://localhost/wsgi . The part /wsgi is the part after localhost in web browser address.
Next we have to set permissions to application folder, so no one could edit it. We do it by adding another <Directory> section:
<Directory "d:/django-app"> AllowOverride None Options None Order deny,allow Allow from all </Directory>
Remember that in Apache we change all “\” on “/” in Windows paths.
No we set up our test handler by creating file wsgi_handler.py at d:/django-app .
Inside we should put this code:
def application(env, start_response): start_response("200 OK", ) output = "<html>Hello World! Request: %s</html>" output %= env['PATH_INFO'] return [output]
We can test if it works opening http://localhost/wsgi in web browser.
4. Installing django
We should also install django. Download it from here and unpack. Installing django is done by python script, so we have to use command line. Press start and write cmd in search box (Run in other versions of Windows), then press Enter. A command line console would appear. Move to django directory by using change directory command and run the installer by typing:
You would be asked if you would like to create superuser.
Do it - we will use the user and password to get to admin panel later in the project
To check django installation write:
python >>>import django >>>print django.get_version()
Now you can add a PATH to your django installation in python. Django is installed as a python library in Lib\site-packages\django\ in python directory. Add this folder to your system PATH.
5. First django project
To start django project use:
django-admin.py startproject SimpleCRM
It will creates a folder for a project and initial files. It is important to be at the folder that would be used for django applications (in our case it is d:\django-app)
Now we would edit wsgi_handler.py for using django. Delete previous content an put this code:
import os import sys path = 'd:/django-app' if path not in sys.path: sys.path.append(path) os.environ['DJANGO_SETTINGS_MODULE'] = 'SimpleCRM.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
Now check results at http://localhost/wsgi/