Python+wsgi+django solution for Python3 and WSGI incompatible configurations

Source: Internet
Author: User
Tags install django in python

Configuring this Python + Wsgi + Django is a twist. View


***********************
Installation Environment:
Centos5.6
Python2.7
mod_wsgi:mod_wsgi-4.4.13
apache:httpd2.2
django:django-1.8.3
***********************

1. Install python:python2.7

Originally the system has brought Python2.6, in order to my pursuit of the ultimate, loaded force perfectionism, I installed the Python3.4. and point the/usr/local/bin/python directly to Python3.4. Set it as the main program for Python.

Here is the root of the tragedy. Because Python3 and MOD_WSGI are incompatible (note: At least until now I have not found a wsgi that can be installed using the Python3), I install it directly using the./configure make Makeinstall command, which causes the system to choose Apach The python version of E is set to python2.6. (Note: wsgi: The Python Web server gateway interface that Apache can connect to and use Python through it)

But since my Django was installed with Python3. Eventually lead to Apache is not connected to Django, I query httpd error_log back and forth countless times, finally found httpd use is python2.6. So can only decisively use Python2, but the new problem arises, because the download of Django version is too high, python2.6 installation can not, so the final result is: Install python2.7!

Command:

TAR-XVF python-2.7.10.tgz
CD Python-2.7
./configure--enable-shared

Make

Make install

Problems may be encountered: Python:error while loading shared libraries:libpython2.7.so.1.0:
Cannot open shared object File:no such file

Cause: python2.7 's library can't be found.

Solution: 1. Into the/etc/ld.so.conf.d/,

2. New file: Vim python2.7.conf

3. Path to add python2.7 library:/usr/local/lib

4. Save and exit, and then execute the command: Ldconfig

2. Install django:django-1.8.3

Go directly to the installation package folder using Python installation

Tar xzvf Django-1.8.3. tar.gz
CD Django-1.8.3

sudo python setup.py install

3. Install mod_wsgi:mod_wsgi-4.4.13

Download Address: Https://pypi.python.org/pypi/mod_wsgi

Install Command:

./configure--with-python=/usr/local/bin/python2.7

Make

Make install

Problems may be encountered:

3-1. Apxs:command not Found

Reason: Apache component Httpd-devel not installed

Solution: Yum Install Httpd-devel

3-2. Compilation failed on make. To view the first line of the error, discover: Sorry, Python Developer package does not appear to be installed.

Reason: Python component python-devel not installed

Solution: Yum Install Python-devel

4. Configure HTTPD (Note: This is important to enable httpd to use Python via Wsgi and access to the Django Web site directory), and it can easily be problematic:

4-1. Add a link to the WSGI dynamic link library. So file for httpd:

1. vim/etc/httpd/conf/httpd.conf

2. Add content: LoadModule wsgi_module modules/mod_wsgi.so

3. Save exit.

4-2. Continue to modify httpd.conf, add the Django project path, such as my new Django Web site path is:/var/www/html/mysite, then configured as follows:

Wsgipythonpath/var/www/html/mysite
Wsgiscriptalias/"/var/www/html/mysite/mysite/wsgi.py"


<directory "/var/www/html/mysite/mysite" >
Order Allow,deny
Allow from all
<files wsgi.py>
Order Allow,deny
Allow from all
</Files>
</Directory>

4-3. Configure WSGI files

General Django will automatically help you configure your wsgi.py files when you build your project. Do not need to match their own, if your WSGI must be installed. My/var/www/html/mysite/mysite/wsgi.py content is as follows:

Import OS
Os.environ.setdefault ("Django_settings_module", "mysite.settings")

From Django.core.wsgi import get_wsgi_application
application = Get_wsgi_application ()

5. Restart httpd service:service httpd restart

At least the configuration problem is basically resolved. If you encounter a new problem, you have to go to StackOverflow to check it again.




Apache+wsgi+django Configuration

What is WSGI?

The official definition of WSGI is the Python Web Server Gateway Interface. As you can see from the name, this thing is a gateway. The role of a gateway is to convert between protocols.

In other words, Wsgi is like a bridge, connected to the Web server, on the other side of the user's application. However, the bridge is very weak, sometimes need other bridges to help to deal with.
What's Django?

Django (pronounced:/ˈdʒæŋ?oʊ/Jang-goh) is an Open-source Web development framework (open source web framework) written in Python, which encourages rapid development and follows the MVC design. Django complied with the BSD copyright, first published in July 2005, and released its first official version of 1.0 in September 2008. The latest release is the Django 1.3.1, released on September 10, 2011.

Django is named after the Belgian jazz musician Django Reinhardt, a gypsy who plays mainly on the guitar and plays the violin.
Design philosophy

The main purpose of Django is to develop a database-driven Web site easily and quickly. It emphasizes code reuse, multiple components can be conveniently in the form of "plug-in" service to the entire framework, Django has many powerful third-party plug-ins, you can even easily develop your own toolkit. This allows Django to be highly scalable. It also emphasizes the rapid development and dry (do not Repeat yourself) principles.

Django's design based on MVC is beautiful:

Object Relational Mappings (orm,object-relational mapping)

By defining your data model as Python, ORM connects the model to the relational database, you get a very easy to use database API, and you can use the original SQL statements in Django as well.

URL Assignment

Using regular expressions to match URLs, you can design any URL without a specific qualification for the frame. As flexible as you like.

Stencil system

With Django's powerful and extensible template language, you can separate design, content, and Python code. and has the inheritable nature.

Form processing

You can easily generate a variety of form models to achieve the validity of the form test. You can easily generate the appropriate form from the model instance that you define.

Cache system

Can be hung in memory buffers or other frameworks to achieve super buffering--to achieve the granularity you need.

Session, user logon and permission checking

Quickly develop user session capabilities.

Internationalization

Built-in internationalization system to facilitate the development of a variety of language sites.

An automated management interface

You don't need to spend a lot of work creating people to manage and update content. Django has a admin site, similar to the content management system.

Software version:

Operating system: ArchLinux Kernel 3.1.0-4-arch x86_64
Web server: apache/2.2.21
python:python/2.7.2
django:1.3.1
wsgi:mod_wsgi/3.3

1. Install Apache and Python

On the archlinux I've installed apahce and Python with Pacman, and then install Mod_wsgi

2. Install Wsgi

Pacman-s Mod_wsgi
Here is ArchLinux, if it is another operating system, please Google.
Once installed, the Mod_wsgi Apache module will be generated, located in/usr/lib/httpd/modules/mod_wsgi.so.
Load modules in Apache configuration file:
LoadModule Wsgi_module modules/mod_wsgi.so

3. Configure Apache Virtual host

<virtualhost *:80>
ServerName webpy.test.com
Wsgiscriptalias//home/max/mysite/django.wsgi

<directory "/home/max/mysite" >
Options FollowSymLinks Indexes
AllowOverride All
Order Deny,allow
Allow from all
</Directory>

ErrorLog "/var/log/httpd/webpy-error.log"
Customlog "/var/log/httpd/webpy-access.log" combined

</VirtualHost>

The red line above is to allow the Wsgi configuration file to be loaded (Django itself supports the WSGI protocol)

4. Install Django

Shell> pacman-s Django
You can also choose to download the source installation, because Django itself is all programmed in Python, so after downloading the Django installation package, go to
Django Source directory, you can install Django by executing the following command:
shell> python setup.py Install

5. Configure Django

Now that we've built a Django project, and after we've built the project folder, Django will automatically generate some project files:
shell> django-admin.py startproject MySite
django-admin.py is a management program for Django itself, and there are many uses in addition to the new project.
MySite is the new project name, which generates a MySite folder in the current directory.
__init__.py __init__.pyc manage.py settings.py urls.py
Above is the file in the MySite directory, which is the initial file for the Django project.

6. Configure the Django Wsgi file

Create a new Django.wsgi file in the Mystei directory, which reads as follows:

Import OS
Import Sys

os.environ[' django_settings_module '] = ' mysite.settings '

Import Django.core.handlers.wsgi
application = Django.core.handlers.wsgi.WSGIHandler ()

Path = '/home/max '

If path not in Sys.path:
Sys.path.append (PATH)

Mysite.settings is the MySite project configuration file, the Django project's configuration file is Python code, and the settings.py file is visible in the MySite directory.
Because the current directory is not in the system's path variable, MOD_WSGI cannot find the settings.py file, to help it find
We append the directory of the MySite directory to the PATH variable.
Sys.path.append (PATH)

7. Start Django Project

Restart the Apache server
shell> apachectl Restart
You can see the Django Welcome page by opening the http://webpy.test.com in the browser.

There is actually a quicker way to start a Django project, which is to perform the following in the directory of the MySite project:
shell> python manage.py runserver 8000
The following message appears:

Django version 1.3.1, using Settings ' mysite.settings '
Development server is running at http://127.0.0.1:8000/
Quit the server with Control-c.

This is when the browser opens the http://127.0.0.1:8000 to see the Django Light blue page.
This actually starts the development server, which can only server one user at a time, so it cannot be used in a production environment.

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.