Install apache2.2 + mod_wsgi + Django In ubuntu (1)

Source: Internet
Author: User
Tags install django pip install django

To enable Apache, nginx, or lighthttpd to support python, you can use mod_python. However, since the open-source project mod_python seems dead, the official website address is:

Www.modpython.org/


This project has not continued since 2009. The Python version has reached 3.2, but the last version of mod_python

mod_python-3.3.1.tgz                  2009-10-03 20:45 

Only Python 2.5 is supported, so if you must use mod_python, you can only install Python 2.5 or a later version.

I want to use the latest Python version, so I have to abandon the mod_python method.

Another method is the mod_wsgi + Django method we will introduce.

1. Install apache2

Command installation or source code installation are acceptable. Here I use the command installation method:

Sudo apt-Get install Apache

The latest version is v2.2. If you want to install other versions, use the official installation package provided by apache2.

During the installation process, apache2 will install a service apache2 for us to start, stop, and restart the apache service.

Start:

Sudo service apache2 start or

Sudo apachectl start


Stop:

Sudo service apache2 stop or

Sudo apachectl stop


Restart:

Sudo service spache2 restart or

Sudo apachectl restart

Note that sudo should not be less; otherwise, the permission is insufficient and it will fail.

After installation, start the service, test the Apache server, enter http: // localhost in the browser, and press enter to check the effect. If you see the following, it indicates that Apache is successfully installed:

It works!This is the default web page for this server.The web server software is running but no content has been added, yet.

Here we will talk about the configuration file of Apache in Linux. The name and location of the configuration file are different from those in windows. After Apache is installed in windows, all the files are under a directory, including executable programs, corresponding dynamic libraries, and configuration files, but it is separated in Linux. The executable program is

/Usr/sbin/apache2

The configuration file is in

/Etc/apache2

The website (Web) file is in:

/Var/WWW

Of course, you can configure and modify

/Etc/apache2/sites-available/Default

The following fields of this file

DocumentRoot/var/WWW

For example, if you change to/var/temp

Change this line:

DocumentRoot/var/temp

The configuration file is not in httpd. conf, but apache2.conf, and nothing is configured here, such as the port is in ports. CONF file, and the root directory of the website is mentioned above

/Etc/apache2/sites-available/Default

In this file. Although there are also httpd. CONF file, but httpd. conf is empty. In fact, you can add some configuration here, because httpd will be added in apache2.conf. conf is added to its file.

2. Install mod_wsgi

There are three ways to install mod_wsgi: source code installation, binary file installation, and command installation.

A. Command installation:

Currently, unbuntu provides two sources, one for 2.x Python and the other for 3.x Python:

Libapache2-mod-wsgi-Python wsgi adapter module for Apache

Libapache2-mod-wsgi-py3-Python 3 wsgi adapter module for Apache

B. Binary File Installation:

Download the binary files from the following address and copy them to the corresponding folder:

Http://ftp.cn.debian.org/debian/pool/main/m/mod-wsgi/libapache2-mod-wsgi_3.3-4_i386.deb

C. Source Code installation:

Download the source code through the following link:

Http://code.google.com/p/modwsgi/downloads/detail? Name1_mod_wsgi-3.3.tar.gz

Decompress the package to the directory and use configure to generate makefile,

./configure

Errors found:

Apxs: Command not found

After surfing the Internet with a bunch of Google, some people found that httpd. devel and so on were missing, and they could not solve the problem. Then they found the answer on the official website of mod_wsgi:

Http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide

It turns out that you want to install the apache2-dev by using the following command:

sudo apt-get install apache2-dev

Another dependent library is Python-Dev.

sudo apt-get install python-dev

Now, we can use configure again to find that makefile is successfully generated.

Next

makemake install

If there are no errors, we have successfully installed mod_wsgi.

Another way is to check whether the following file does not exist:

/Usr/lib/apache2/modules/mod_wsgi.so

3. Configure Apache to load mod_wsgi

If your Apache is running, stop it first.

sudo service apache2 stop

Configure Apache and add the following line to/etc/Apache/httpd. conf:

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

Note that the above/usr/lib/apache2/modules/mod_wsgi.so is my installation path. If you are installing other directories, make corresponding adjustments, use the find command to view the location where so is installed:

sudo find / -name mod_wsgi.so

Restart Apache

sudo service apache2 start

If the system cannot find your mod_wsgi.so, the apache service will fail to start.

4. Install Django

Install necessary tools first

sudo apt-get install python-setuptoolssudo apt-get install python-pip


Install Django

sudo pip install django

Django is installed in the sub-folder under the python path, as follows:

/Usr/local/lib/python2.7/dist-packages/Django

You can run the following command to check whether the installation is successful:

sudo find / -name django

If the above output is displayed, the installation is successful.

5. Test

To facilitate the test, we create a new Virtual Host:

sudo vim /etc/hosts

Use the above command to develop the hosts file and add the following lines:

192.168.0.107 wsgi. testserver

Replace the above IP address with your own IP address. Save and exit.

As we mentioned earlier, our website is

/Var/WWW directory, So we CD to this directory, create a project directory, create a project file

cd /var/wwwsudo mkdir wsgicd wsgisudo vim main.wsgi

Enter the following code in Main. wsgi:

def application(environ, start_response):    status = '200 OK'    output = 'Hello World!'     response_headers = [('Content-type', 'text/plain'),                        ('Content-Length', str(len(output)))]    start_response(status, response_headers)     return [output]

 

Save and launch.

Here, I will explain some of the above.

To make the directory structure clearer, we create our own directory wsgi instead of directly under the root directory of WWW, and then create a python file, which is used to generate a hello World webpage, and tell the Web server-here is Apache, the processing status is successful-200, that's simple.

Next, let Apache load our own project website, go to the Apache configuration directory/etc/Apache/sites-available, and create a configuration file for our website.

cd /etc/apache/sites-availablesudo vim wsgi

Enter the following content in the newly created wsgi file:

<VirtualHost *:80>     ServerName wsgi.testserver    DocumentRoot /var/www/wsgi     <Directory /var/www/wsgi>        Order allow,deny        Allow from all    </Directory>     WSGIScriptAlias / /var/www/wsgi/main.wsgi </VirtualHost>

Activate our website:

sudo a2ensite wsgi

Load our website:

sudo service apache2 reload

At this time, you open the browser, enter the http://wsgi.testserver, You can see hello World. If you see it, it means everything is OK. If you don't see it, you can leave a message to me. Let's study it together.

This is troublesome and time-consuming. It took me a long time, and a server error 500 was found in the middle. Finally, it was ruled out one by one and found that it was main. the wsgi file is caused by a syntax error. Because I first engaged in Web server services (previously C ++ was working on desktop and network applications), it took a lot of time to figure out the cause of the error. Later I thought that wsgi is a python file, so I will use it to check the effect of running the file in Python.

Python main. wsgi

The result tells me that there is a syntax error, after fixing this error, enter the http://wsgi.testserver in the browser and see the desired result.

It is explained that the above Python script is saved as the suffix wsgi, but this is not a rule, you can also define it as another, such as the commonly used suffix name py, or simply do not use any suffix name, no problem.

 

Another point is that although we have installed Django above, we have not used this framework at all, because I think we should first write something with wsgi + python, then you can use Django to create your own web application to better understand What Django has helped me and how to use it. This is like using Win32 programming first, and then using MFC, you can better understand MFC and use it better.

 

In the future, I will continue to write about Django in other blog posts.

 

See the document:

Https://docs.djangoproject.com/en/1.2/howto/deployment/modwsgi/

Http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/


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.