Windows Apache Django Configuration Guide

Source: Internet
Author: User
Tags apache log install django python mysql
I. Software preparation:
1.1 python-2.5.1.msi
1.2 apache_2.2.8-win32-x86-no_ssl.msi
1.3 Django-0.96.1.tar.gz
1.4 mod_python-3.3.1.win32-py2.5-Apache2.2.exe
1.5 MySQL-python-1.2.2.win32-py2.5.exe

Ii. installation steps:
2.1 install Python (run the python-2.5.1.msi and, after completion, set the environment variable)
2.2 mysql-python(run mysql-python-1.2.2.win32-py2.5.exe)
2.3 install Apache (run apache_2.2.8-win32-x86-no_ssl.msi)
2.4 install the module of Apache: mod_python(run mod_python-3.3.1.win32-py2.5-apache2.2.exe. After the installation is complete, you will be prompted to select the Apache path and select the Apache installation path in 2.4)
2.5 install Django
2.5.1 decompress twice to get the folder Django-0.96.1. Put this folder in your development directory. It is recommended that you do not place it on the desktop. It is best to place it in a directory such as D:/dev/. Note that the folder name should be in English and there should be no spaces.
2.5.2 enter the directory at the command prompt and enter: cd d:/dev/Django-0.96.1
Run Python setup. py install.
(At this time, the screen will scroll and display a lot of things. You can't see it clearly)
OK.
2.5.3 perform a simple test first.
At the command prompt, enter: Python
Enter import Django
Enter Django. Version
What I see is:
>>> Import Django
>>> Django. Version (0, 96.099999999999994, none)
>>>
2.5.4 set the environment variable (add ../python2.5.1/lib/Site-packages/Django/bin to the path environment variable)
2.6 create and test a Django Project
C:/django_src> Cd/
C:/> mkdir Django
C:/> Cd Django
C:/Django> django-admin.py startproject myproject
C:/Django> Cd myproject
After the creation is complete, open the settings. py file and set the database. (We recommend that you set it to MySQL. 2.2 you have installed the python MySQL driver. You can use MySQL)
Database_engine = 'mysql' # 'postgresql _ psycopg2 ', 'postgresql', 'mysql', 'sqlite3 'or 'ado _ mssql '.
Database_name = 'django _ demo' # Or path to database file if using sqlite3.
Database_user = 'root' # Not used with sqlite3.
Database_password = 'root' # Not used with sqlite3.
Database_host = '10. 1.120.12 '# Set to empty string for localhost. Not used with sqlite3.
Database_port = ''# Set to empty string for default. Not used with sqlite3.
Synchronize databases:
Before synchronizing data to a database, you must create the database django_demo In the MySQL database. Otherwise, the database DDL cannot be executed.
C:/Django/myproject> Python manage. py syncdb
Start the server that comes with Django:
C:/Django/myproject> Python manage. py runserver
Validating models...
0 errors found.
2.7 enable mod_python handlers for Django
We use Python django-admin.py startproject myproject
Command to create a Django project myproject under the Django directory of drive C. We put the referenced image files, CSS files, and other static Resources in the media directory under the myproject folder.
In this case, modify the three parameters in the settings. py file under the myproject directory to change from relative path to absolute path:
Modified configuration line
# Django settings for myproject project.
'''
Change the relative path to the absolute path. Mainly include:
Media_root
Template_dirs
Static_path
'''
# Absolute path to the directory that holds media.
# Example: "/home/Media/media.lawrence.com /"
Media_root = 'C:/Django/myproject'
Static_path = 'C:/Django/myproject/media'
Template_dirs = (
# Put strings here, like "/home/html/django_templates ".
# Always use forward slashes, even on Windows.
'C:/Django/myproject/templates ',
)
Make sure that the three parameters are absolute paths.
4:
Configure the httpd. conf configuration file of Apache:
First, add a line under the configuration of "dynamic shared object (DSO) support"
Loadmodule python_module modules/mod_python.so
This must be manually added.
5:
The parameters required by Apache are listed as follows:
Project name: myporject
The URL to be accessed is http: // localhost: 80/mysite/
Static resource file storage directory: C:/Django/myproject/Media
Project folder path: C:/Django/myproject
Now, you should add this configuration at the end of the httpd. conf file to let Apache know where to locate the myproject/settings. py file:
Added configuration lines
# Mysite directory path: C:/Django/myproject,
# But for pythonpath, it must be set to the upper-level directory of this directory!
# This site URL: http: // localhost: 80/mysite/
<Location "/mysite/">
Sethandler Python-Program
Pythonpath "SYS. Path + ['C:/Django ']"
Pythonhandler Django. Core. Handlers. modpython
Setenv django_settings_module myproject. Settings
Pythoninterpreter mysite
Pythondebug on
</Location>
For the above configuration, limodou commented: "The above pythonpath mainly adds the directory of myproject to SYS. path so that Django can find it. Absolute path is required.
The django_settings_module set in setevn corresponds to your project name. configuration file. Therefore, in order to import the project name and configuration file, you need to set the pythonpath above.
We recommend that you set pythondebug and pythonautoreload to off during production ."
To enable images, CSS, and scripts to be successfully loaded by Apache, add the following configuration at the end of httpd. conf:
Added configuration lines
# Alias/site_media is used to set a URL access alias for the static file of myproject.
Alias/site_media C:/Django/myproject/Media
<Location "/site_media/">
Sethandler none
</Location>

# Alias/media sets the access alias of a URL for the static Django admin file.
Alias/Medic:/Django-0.95/Django/contrib/admin/Media
<Location "/Media/">
Sethandler none
</Location>

# Configure static file permissions so that Apache can access them
<Directory "E:/Program/python2.5.1/lib/Site-packages/Django/contrib/admin/Media">
AllowOverride none
Options none
Order allow, deny
Allow from all
</Directory>
# Set not to cache. After modifying the Python source code, you can load it yourself. This will be removed during the official release.
Maxrequestsperchild 1

# File types we want to serve statically
# Case insensative match
<Locationmatch "(? I)/. (JPG | GIF | PNG | TXT | ICO | PDF | CSS | JPEG) $ ">
Sethandler none
</Locationmatch>

Iii. Common Errors
3.1 common error 1
Keywords: client denied by server configuration.
Appearance: After accessing http: // localhost: 80/mysite/, if you find that the image is not loaded, or CSS is not applied, and httpd. the absolute path of location configuration in the conf file must be correct.

Explanation:
First, check httpd. conf for alias configuration,
Check whether the path set by alias is correct,
Alias/site_media C:/Django/myproject/Media
Make sure that the path you append is the path where your image or CSS is stored.
Second, check settings. py in the myproject directory of your Django project to ensure that the absolute paths pointed to by the media_root, static_path, and template_dirs parameters are correct.
Finally, if the above parameters are correct, they still do not work. Check Apache log files.
Apache2.2/logs/error. Log
If you find the following error message:
Error Log
[Mon Nov 20 17:27:08 2006] [Notice] child 4172: Starting thread to listen on port 80.
[Mon Nov 20 17:27:08 2006] [Error] [client 127.0.0.1] client denied
Server Configuration: C:/Django/myproject/Media/CSS/global.css,
Referer: http: // localhost: 80/mysite/
The reason is that access to these resource files is denied.
In this case, visit http: // localhost: 80/site_media/CSS/global.css. If an HTTP 403 error occurs, it indicates that the permission is incorrect.
In this case, go to httpd. conf and add the following configuration to allow access to static resource folders:
Added configuration lines
# Add the correct directory block for the region to be accessed
# Otherwise, the following error occurs: client denied by Server Configuration:
C:/Django/myproject/Media/CSS/global.css, Referer: http :/
/Localhost: 80/mysite/
<Directory "C:/Django/myproject/Media/">
Order deny, allow
Allow from all
</Directory>
Restart the apache service.

3.2 common error 2
Keywords: environmenterror: cocould not import settings.
Appearance: After accessing http: // localhost: 80/mysite/, the following error is reported on the webpage:
Page error output
1. mod_python error: "pythonhandler Django. Core. Handlers. modpython"
Traceback (most recent call last ):
File "C:/python24/lib/Site-packages/mod_python/Apache. py", line 299, in handlerdispatch
Result = Object (req)
File "C:/django_src/Django/CORE/handlers/modpython. py", Line 163, in Handler
Return modpythonhandler () (req)
File "C:/django_src/Django/CORE/handlers/modpython. py", line 125, in _ call __
If settings. enable_psyco:
File "C:/django_src/Django/CONF/_ init _. py", line 27, in _ getattr __
Self. _ import_settings ()
File "C:/django_src/Django/CONF/_ init _. py", line 54, in _ import_settings
Self. _ target = settings (settings_module)
File "C:/django_src/Django/CONF/_ init _. py", line 82, in _ init __
Raise environmenterror, "cocould not import settings '% s' (is it on
SYS. Path? Does it have syntax errors ?) : % S "% (self. settings_module, E)
Environmenterror: cocould not import settings 'myproject. setting' (is it
On SYS. Path? Does it have syntax errors ?) : No module named
Myproject. Settings

This is the most common problem to start configuring Django + Apache: D.

Explanation:
This is because Apache cannot find the myproject/settings. py file in the system directory.
First, check whether the path set in pythonpath is correct:
Pythonpath "SYS. Path + ['C:/Django ']"
Note that the 'C:/Django 'path is actually the upper-level directory of our project path C:/Django/myproject! Do not make any mistakes.
Added configuration lines
# Mysite directory path: C:/Django/myproject,
# But for pythonpath, it must be set to the upper-level directory of this directory!
# This site URL: http: // localhost: 80/mysite/
<Location "/mysite/">
Sethandler Python-Program
Pythonpath "SYS. Path + ['C:/Django ']"
Pythonhandler Django. Core. Handlers. modpython
Setenv django_settings_module myproject. Settings
Pythoninterpreter mysite
Pythondebug on
</Location>

3.2 Common Errors 3
When using the SQLite database in Django, the file database is used. When apache is started, the user permission of Apache is incorrect.
When you access http: // localhost: 80/mysite/, the system may report that the session table cannot be found.
If you are too reluctant to bother with file permissions, simply switch your Django database to MySQL.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.