Using nginx+fastcgi to build a Python Web server, this article describes the NGINX+UWSGI combination. Uwsgi is a fast, pure C language developed, self-maintained, developer-friendly WSGI server designed to provide professional Python Web application publishing and development. It is more compliant with the Python Web's standard protocol, faster than fastcgi, and more stable in performance.
First, installation platform
1. Installation Pcre
Copy Code code as follows:
Cd/home
Mkdir-p/home/install/nginx && Cd/home/install/nginx
wget http://ftp.exim.llorien.org/pcre/pcre-8.00.tar.gz
TAR-ZXVF pcre-8.00.tar.gz
CD pcre-8.00
./configure
Make && make install
Cd..
2. Installation Nginx
Copy Code code as follows:
wget http://nginx.org/download/nginx-0.8.50.tar.gz
TAR-ZXVF nginx-0.8.50.tar.gz
CD nginx-0.8.50/
./configure--user=nobody--group=nobody--prefix=/usr/local/nginx--with-http_stub_status_module--with-cc-opt= '- O3 '--with-cpu-opt=opteron
Make && make install
Cd..
3. Installation Python&mysql-python
Copy Code code as follows:
wget http://www.python.org/ftp/python/2.5.1/Python-2.5.1.tgz
TAR-ZXVF python-2.5.1.tgz
CD Python-2.5.1
./configure && make && make install
echo "Export path=\ $PATH:/usr/local/bin" >>/etc/profile
Source/etc/profile
4. Installation Mysql-python
Copy Code code as follows:
Cd.. /
wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.2/mysql-python-1.2.2.tar.gz?r=& Ts=1285248455&use_mirror=nchc
CD mysql-python-1.2.2
Python setup.py Install
5. Install Django
Copy Code code as follows:
wget http://www.djangoproject.com/download/1.2.3/tarball/
TAR-ZXVF django-1.2.3.tar.gz
CD Django-1.2.3
Python setup.py Install
Cd..
6. Installation Uwsgi
Copy Code code as follows:
wget http://projects.unbit.it/downloads/uwsgi-0.9.6.2.tar.gz
TAR-ZXVF uwsgi-0.9.6.2.tar.gz
CD uwsgi-0.9.6.2
Make
CP Uwsgi/usr/bin
Second, platform configuration
1, Platform Environment description
1.1, Project location:/opt/www/purgesys
To create a project method:
Copy Code code as follows:
Cd/opt/www
django-admin.py Startproject Purgesys
1.2, view the project (the following file structure shows that the project has been successfully created)
#tree Purgesys
Copy Code code as follows:
Purgesys
|--__init__.py
|--manage.py
|--settings.py
'--urls.py
2, Nginx related configuration
#cd/usr/local/nginx/conf
#vi django_uwsgi.conf
# Django Project Server { listen 80;
server_name purgesys.domain.com; location/{
uwsgi_pass 127.0.0.1:9000; include Uwsgi_
Params
access_log off;        &NBSP} location ^~/ static { root
/opt/www/purgesys;        &NBSP} location ^~/ admin/{         &NBsp; uwsgi_pass 127.0.0.1:9000;
include uwsgi_params;
access_log off;        &NBSP} location ~* ^.+ \. (mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg) $ {
root /opt/www/purgesys/static;
access_log off;        &NBSP}}
*nginx-0.8.50 default has been added Uwsgi output header support
include Uwsgi_params;
#vi nginx.conf
Append Child configuration file django_uwsgi.conf
http{.....
Include django_uwsgi.conf;
3, UWSGI Configuration
#mkdir-P/home/uwsgi;cd/home/uwsgi
#mkdir-P/opt/www/logs
#echo "" >/opt/www/logs/django.log
#vi Uwsgi.xml
<uwsgi>
<socket>127.0.0.1:9000</socket>
<listen>200</listen>
< master>true</master>
<pidfile>/usr/local/nginx/uwsgi.pid</pidfile>
<processes >8</processes>
<pythonpath>/opt/www/Purgesys</pythonpath>
<pythonpath>/opt/ www/</pythonpath>
<module>django_wsgi</module>
<profiler>true</profiler>
<memory-report>true</memory-report>
<enable-threads>true</enable-threads>
<logdate>true</logdate>
<limit-as>6048</limit-as>
<daemonize>/ Opt/www/logs/django.log</daemonize>
</uwsgi>
4. Create Application Module
Cd/opt/www/purgesys
VI django_wsgi.py
Import os
os.environ[' django_settings_module '] = ' purgesys.settings '
import Django.core.handlers.wsgi
application = Django.core.handlers.wsgi.WSGIHandler ()
5, start the service
#/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
#/usr/local/nginx/sbin/nginx
6, view the process
#[liuts@webserver nginx]# ps-ef|grep uwsgi|grep-v grep
Copy Code code as follows:
Root 21652 1 0 17:12? 00:00:00/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
Root 21653 21652 0 17:12? 00:00:00/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
Root 21654 21652 0 17:12? 00:00:00/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
Root 21655 21652 0 17:12? 00:00:00/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
Root 21656 21652 0 17:12? 00:00:00/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
Root 21657 21652 0 17:12? 00:00:00/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
Root 21658 21652 0 17:12? 00:00:00/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
Root 21659 21652 0 17:12? 00:00:00/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
Root 21660 21652 0 17:12? 00:00:00/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
7. Listening Port
#[liuts@webserver nginx]# Netstat-an|grep 9000
Reference
TCP 0 127.0.0.1:9000 0.0.0.0:* LISTEN
8. Access Test
Input http://serverip/
Done!
* Attach Uwsgi startup script
#cd/home/uwsgi
#vi uwsgiserver.sh
#!/bin/bash
If [$ = start];then
psid= ' ps aux|grep ' uwsgi ' |grep-v ' grep ' |wc-l '
if [$psid-gt 2];then
echo "Uwsgi is running!"
Exit 0
Else
/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
fi
echo "Start uwsgi service [OK]"
elif [$ = s Top];then
killall-9 uwsgi
echo "Stop uwsgi service [OK]"
elif [$ = restart];then killall-9 uwsgi
/usr/bin/uwsgi-x/home/uwsgi/uwsgi.xml
echo "Restart Uwsgi service [OK]"
else
echo "Usages:sh uwsgiserver.sh [Start|stop|restart] "
fi
#sh uwsgiserver.sh Start
[Uwsgi] parsing config file/home/uwsgi/uwsgi.xml
Start UWSGI Service [OK]
If you have any questions or interesting topics can be Weibo with me through the exchange: Http://t.qq.com/yorkoliu
The following are supplementary:
1, Mysql-python installation error is as follows
From Setuptools Import Setup Importerror:
Install first
Python ez_setup.py
In the installation
Python setup.py Install
Put it on the other brothers.
2, my Nginx+uwsgi+django encountered large header request, often appear 502,UWSGI reported is invalid request header size, how to solve my uwsgi configured as [Uwsgi]socket = : 8010master = Truemodule = django_wsgiprocesses = 8home =/path/to/virtualenvharakiri = 20daemonize =/path/to/uwsgi.logpi Dfile =/path/to/uwsgi.pidpythonpath =/path/to/projectpythonpath =/path/to/uwsgibuffer-size = 32768reload-mercy = 8max-requests = 5000socket-timeout = 4
Workaround: The large header can modify the Nginx HTTP head part parameters.
3, Nginx reported 502, but uwsgi normal boot port 9000 also normal monitoring. It is possible that an application has an error cause.