Nginx + uWSGI + DJango + Python + Mysql build a reliable python web server

Source: Internet
Author: User
Tags install django

1. install required tools

Yum-y install gcc-c ++ rpm-build mysql * libtool-ltdl * libtool automake autoconf libtool make setuptool

Ii. Compile and install python2.7.5

1. Download python2.7.5 and save it to/data/qtongmon/software.

Http://www.python.org/ftp/python/


2. decompress the file

Tar xvf Python-2.7.5.tar.bz2


3. Create an installation directory

Mkdir/usr/local/python27


4. Install python

./Configure -- prefix =/usr/local/python27 -- enable-shared

Make

Make install


5. Modify the old version of ln to point to note: After modification, the usage of yum may be affected)

Mv/usr/bin/python/usr/bin/python2.4.3

Ln-s/usr/local/python27/bin/python/usr/bin/python


6. yum is written in python. Upgrading the new version of python will cause yum to become unavailable. The following is an error message when running yum:

There was a problem importing one of the Python modules

Required to run yum. The error leading to this problem was:

No module named yum

Solution:

Find the yum file and edit the py file.

[Root @ TC-Boss ~] # Which yum

/Usr/bin/yum

[Root @ TC-Boss ~] # Vi/usr/bin/yum

Set

#! /Usr/bin/python

Changed:

The results of whereis python are from earlier python versions)

#! /Usr/bin/python2.6

Then save OK.



Ii. Install uWSGI

Download the latest uWSGI version

Wget http://projects.unbit.it/downloads/

Because I used xml to configure the deployment of django app at last, libxml needs to be compiled into uWSGI during compilation.

Yum-y install libxml2-devel

The rest is simple.

Tar zxvf uwsgi-1.9.17.tar.gz

Cd uwsgi-1.9.17

Make

Cp uwsgi/usr/sbin/uwsgi

If an error occurs: python: error while loading shared libraries: libpython2.7.so. 1.0:

Cannot open shared object file: No such file

Solve the problem as follows:

Vim/etc/ld. so. conf. d/python2.7.conf

Add the following content:/usr/local/python27/lib (the lib directory in the actual python installation path)

Save and exit and run:

Ldconfig

You can.


Now, even if uWSGI is installed, the next step is to install Nginx.


3. Install Nginx

Refer to another article: http://galean.blog.51cto.com/7702012/1268352

Note that the following parameter must be added during compilation: -- with-cc-opt = '-O3' -- with-cpu-opt = opteron

[Root @ htuidc nginx-1.4.2] #./configure -- with-http_stub_status_module -- prefix =/usr/local/nginx -- with-cc-opt = '-O3' -- with-cpu-opt = opteron


4. Install MySql-python

Wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download

Tar zxvf MySQL-python-1.2.3.tar.gz

Cd MySQL-python-1.2.3

Python setup. py install


ImportError: No module named setuptools solution:

Wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz

Tar zxvf setuptools-0.6c11.tar.gz

Cd setuptools-0.6c11

Python setup. py build

Python setup. py install


/Usr/bin/ld: cannot find-lpython2.7 error. solution:

Cd/usr/lib

Ln-s/usr/local/python27/lib/libpython2.7.so libpython2.7.so

Ll-a | grep python

Lrwxrwxrwx. 1 root 39 Oct 1 16:39 libpython2.7.so->/usr/local/python27/lib/libpython2.7.so

Drwxr-xr-x. 3 root 4096 May 3 python2.6


Error: command 'gcc 'failed with exit status 1

Solution:

Yum-y install mysql-devel

After the installation is complete, go to the site-packages directory under your python installation directory and check whether the following files exist. If yes, the installation is successful.

MySQL_python-1.2.3c1-py2.6-linux-i686.egg: Linux

Test Method

1) Run python to enter the python Runtime Environment

2) enter the following python code for testing.

Import MySQLdb

Test = MySQLdb. connect (db = 'mydb', host = 'localhost', user = 'root', passwd = 'P ')

Cur = test. cursor ()

Cur.exe cute ('show databases ;')

For data in cur. fetchall ():

Print data


5. Install Django

Wget https://www.djangoproject.com/m/releases/1.5/Django-1.5.4.tar.gz

Tar xzvf Django-1.5.4.tar.gz

Cd Django-1.5.4

Python setup. py install


Test Django Installation

Enter python in the terminal to enter the python interaction mode. Enter the following:

>>> Import django

Django. VERSION

The version number is displayed normally.


Vi. nginx Configuration

Server {

Listen 80; # listen for ipv4; this line is default and implied

# Listen [:]: 80 default ipv6only = on; # listen for ipv6

Server_name blog.hysia.com;

Access_log/var/log/nginx/blog.hysia.com-access. log;

Error_log/var/log/nginx/blog.hysia.com-error. log;


Location /{

Uwsgi_pass 127.0.0.1: 8630;

Include uwsgi_params;

}

}


7. Configure the Django app

The configuration is very simple, and there is almost no need to change any file in your app.

First, create a wsgi. py file in your app directory. The content is as follows:

Import OS, sys

If not OS. path. dirname (_ file _) in sys. path [: 1]:

Sys. path. insert (0, OS. path. dirname (_ file __))

OS. environ ['django _ SETTINGS_MODULE '] = 'setting'


From django. core. handlers. wsgi import WSGIHandler

Application = WSGIHandler ()


Create a django. xml file in the app directory as the configuration file for uWSGI running. The content is as follows:

<Uwsgi>

<Socket> 127.0.0.1: 8630 </socket>

<Chdir>/data/web/vpssite </chdir>

<Pythonpath>... </pythonpath>

<Module> wsgi </module>

</Uwsgi>


The last step is to run uWSGI as follows:

Uwsgi-x/data/web/vpssite/django. xml

In this way, your Django app can be held by uWSGI. Django. xml configuration is far more than these such as log files, memory restrictions, and so on, specific can refer to the http://projects.unbit.it/uwsgi/wiki/Example


This article is from the galean blog, please be sure to keep this source http://galean.blog.51cto.com/7702012/1302198

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.