Django Centos 6.4 installation instructions for noobs.
So you want to build your first django centos 6.4 based web site? This is quite easy to install and configure. I’ll cover how to install Python/Django on your centos.
- CentOS 6.4 (For the matter of fact, this tutuorial is good for all the CentOS 6.x series)
- Apache (httpd)
What is Python?
“Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Its syntax is said to be clear and expressive.” from Wikipedia.
What is Django?
“Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.” from https://www.djangoproject.com/
you may need the EPEL repositories for Centos.
[code lang=”bash”]cd /opt/
rpm -Uvh epel-release-6-8.noarch.rpm
rm epel-release-6-8.noarch.rpm -f[/code]
[code lang=”bash”]yum install python
test python by typing: “python” and you should see something similar to: (CTRL+D to exit)
At the time of writing (September 30 2013), the version of python@EPEL is 2.6.6
Django works with any Python version from 2.6.5 to 2.7. It also features experimental support for versions 3.2 and 3.3. All these versions of Python include a lightweight database called SQLite so you won’t need to set up a database just yet unless you need other SQL
Django works with several DB engines. for simplicity, Install SQLite:
yum install sqlite
If you need other SQL Server else then SQLite, you can follow:
MySQL/MongoDB/CouchDB on RHEL/Centos 6.3
If you don’t need SQL and you installed Python 2.5 or later, you can skip this step for now.
You can install using EPEL repositories any yum (like in the old article I wrote), but I recommend to use the easy_install method from the python-setuptools package, which I’ll show next:
[code lang=”bash”]yum install python-setuptools
Test Django by typing python on your command and then:
[code lang=”bash”]import django
As you can see, the RPM version is version 1.5.4, while the current release in the EPEL version is 1.3.1. Internationalization: in template code is not available in 1.3.1 for example but only from Django 1.4
From the command, cd into a directory where you’d like to store your app, then run the following command:
[code lang=”bash”]django-admin.py startproject mysite
Note: if you are installing Django using EPEL repos, the command will be django-admin and not django-admin.py.
[code lang=”bash”]python manage.py runserver
You’ve started the Django development server, a lightweight Web server – easier to startwithout having to deal with configuring a production server — such as Apache — until you’re ready for production.
Browse to http://127.0.0.1:8000/ with your Web browser. You’ll see a “Welcome to Django” page. It worked!
To change port:
[code lang=”bash”]python manage.py runserver 8080
If you need to start the server to answer not only locally, use:
[code lang=”bash”]python manage.py runserver 0.0.0.0:8000
Remember: Apache loads Django environment when starting and keep running it even when source is changed! I suggest you to use Django ‘runserver’ (which automatically restarts on source code changes) in development sessions, unless you need some Apache-specific features.
Edit mysite/settings.py. It’s a normal Python module with module-level variables representing Django settings.
Django using Apache
To run your Django application inside apache – use either mod_python or mod_wsgi, Support for mod_python will be deprecated in a future release of Django. If you are configuring a new deployment, you are strongly encouraged to consider using mod_wsgi or any of the other supported backends.
OPTION A: Install mod_python (NOT recommended)
For this to work, you must have apache installed and configured.
[code lang=”bash”]yum install mod_python
python using mod_python
you need to configure you apache/VirtualHost to:
AddHandler mod_python .py
PythonHandler mod_python.publisher | .py
AddHandler mod_python .psp .psp_
PythonHandler mod_python.psp | .psp .psp_
Create a ‘test.py’ file in your apache server. put inside:
[code lang=”py”]<% req.write(“Hello World!”) %>[/code]
and browse to www.your.server/test.py and you should see the “Hello World!” there.
Django using mod_python
Edit your httpd.conf file:
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonOption django.root /mysite
Option B: Install mod_wsgi (RECOMMENDED)
Deploying Django with Apache and mod_wsgi is the recommended way to get Django into production.
[code lang=”bash”]yum install mod_wsgi
to use mod_wsgi, create an apache folder inside your project and create a django.wsgi file:
[code lang=”apache” inline=”yes”]import os, sys
os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘mysite.settings’
application = django.core.handlers.wsgi.WSGIHandler()
and configure your apache with:
[code lang=”apache” inline=”yes”]<VirtualHost *:80>
WSGIScriptAlias / /var/www/django/mysite/apache/django.wsgi
# Alias /robots.txt /var/www/django/mysite/static/robots.txt
# Alias /favicon.ico /var/www/django/mysite/static/favicon.ico
Alias /static/admin/ /usr/lib/python2.6/site-packages/django/contrib/admin/media/
Alias /static/ /var/www/django/mysite/static/
Alias /media/ /var/www/django/mysite/media/
Allow from all
Allow from all
Allow from all
read more on: multiple django sites with apache & mod_wsgi
You’ve Python / Django installed on your Centos 6.4 and a new project template waiting for you to work on.
* this article is an edited version of an older article.