CentOS + nginx + uwsgi + Python multi-site environment construction

Source: Internet
Author: User
Environment: CentOSX646.4nginx1.5.6Python2.7.5 body: 1. install the required class library and Python2.7.5 install the necessary development kit yumgroupinstall & quot; Developmenttools & quot; yuminstallzlib-devel

Environment:

CentOS X64 6.4

Nginx 1.5.6

Python 2.7.5

Body:

I. install the required class library and Python2.7.5

Install necessary development kits

yum groupinstall "Development tools"yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

CentOS comes with Python2.6.6, but we can install Python2.7.5 again:

cd ~wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2tar xvf Python-2.7.5.tar.bz2cd Python-2.7.5./configure --prefix=/usr/localmake && make altinstall

After installation, run the "python2.7" command to enter the python2.7 environment.

II. install Python Package management

Easy_install package https://pypi.python.org/pypi/distribute

Easy installation of Python development kits

cd ~wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gztar xf distribute-0.6.49.tar.gzcd distribute-0.6.49python2.7 setup.py installeasy_install --version

The red part must be "python2.7"; otherwise, it will be installed in the default 2.6 environment.

Pip pack https://pypi.python.org/pypi/pip

The advantage of installing pip is that you can manage Python packages using pip list and pip uninstall. easy_install does not have this function and only supports uninstall.

easy_install pippip --version
3. install uwsgi

Uwsgi: https://pypi.python.org/pypi/uWSGI

Uwsgi parameter details: http://uwsgi-docs.readthedocs.org/en/latest/Options.html

pip install uwsgiuwsgi --version

Test whether uwsgi is normal:

Create a test. py file with the following content:

def application(env, start_response):        start_response('200 OK', [('Content-Type','text/html')])        return "Hello World"

Then run on the terminal:

uwsgi --http :8001 --wsgi-file test.py

Enter http: // 127.0.0.1: 8001 in the browser to check whether "Hello World" output exists. If no output is available, check your installation process.

4. install django
pip install django

Run the following command to test whether django is normal:

django-admin.py startproject demositecd demositepython2.7 manage.py runserver 0.0.0.0:8002

Enter http: // 127.0.0.1: 8002 in the browser to check if django is running normally.

5. install nginx
cd ~wget http://nginx.org/download/nginx-1.5.6.tar.gztar xf nginx-1.5.6.tar.gzcd nginx-1.5.6./configure --prefix=/usr/local/nginx-1.5.6 \--with-http_stub_status_module \--with-http_gzip_static_modulemake && make install
6. configure uwsgi

Uwsgi supports ini, xml, and other configuration methods, but I personally feel that ini is more convenient:

Create uwsgi9090.ini in the/ect/directory and add the following configuration:

[Uwsgi] socket = 127.0.0.1: 9090 master = true // master process vhost = true // multi-site mode no-stie = true // in multi-site mode, the entry module and file workers are not set to 2 // sub-process reload-mercy = 10 vacuum = true // exit and clear the file max-requests = 1000 limit-as = 512buffer-sizi = 30000 pidfile =/var/run/uwsgi9090.pid/pid upon restart file, use the following script to start and stop the process daemonize =/website/uwsgi9090.log

Set uwsgi to start up, and create the uwsgi9090 file in the/ect/init. d/Directory. the content is as follows:

#! /bin/sh# chkconfig: 2345 55 25# Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and# run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your# distro. For CentOS/Redhat run: 'chkconfig --add uwsgi' ### BEGIN INIT INFO# Provides:          uwsgi# Required-Start:    $all# Required-Stop:     $all# Default-Start:     2 3 4 5# Default-Stop:      0 1 6# Short-Description: starts the uwsgi web server# Description:       starts uwsgi using start-stop-daemon### END INIT INFO # Author:   licess# website:  http://lnmp.org PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDESC="uwsgi daemon"NAME=uwsgi9090DAEMON=/usr/local/bin/uwsgiCONFIGFILE=/etc/$NAME.iniPIDFILE=/var/run/$NAME.pidSCRIPTNAME=/etc/init.d/$NAME set -e[ -x "$DAEMON" ] || exit 0 do_start() {    $DAEMON $CONFIGFILE || echo -n "uwsgi already running"} do_stop() {    $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"    rm -f $PIDFILE    echo "$DAEMON STOPED."} do_reload() {    $DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"} do_status() {    ps aux|grep $DAEMON} case "$1" in status)    echo -en "Status $NAME: \n"    do_status ;; start)    echo -en "Starting $NAME: \n"    do_start ;; stop)    echo -en "Stopping $NAME: \n"    do_stop ;; reload|graceful)    echo -en "Reloading $NAME: \n"    do_reload ;; *)    echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2    exit 3 ;;esac exit 0
Uwsgi9090

Then run the following command on the terminal:

-- Add service chkconfig -- add uwsgi9090 -- set chkconfig uwsgi9090 on
7. set nginx

Find the nginx installation directory, open the conf/nginx. conf file, and modify the server configuration.

Server {listen 80; server_name localhost; location/{include uwsgi_params; uwsgi_pass 127.0.0.1: 9090; // The setting must be consistent with that in uwsgi. uwsgi_param UWSGI_SCRIPT demosite. wsgi; // The entry file, that is, wsgi. the position of py relative to the project root directory, ". "is equivalent to a level of directory uwsgi_param UWSGI_CHDIR/demosite; // The Project root directory index index.html index.htm; client_max_body_size 35 m ;}}

Set nginx startup. create an nginx file in the/ect/init. d/Directory. the content is as follows:

#!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig:   - 85 15# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \#               proxy and IMAP/POP3 proxy server# processname: nginx# config:      /usr/local/nginx/conf/nginx.conf# pidfile:     /var/run/nginx.pid  # Source function library.. /etc/rc.d/init.d/functions  # Source networking configuration.. /etc/sysconfig/network  # Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0  nginx="/opt/nginx-1.5.6/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/opt/nginx-1.5.6/conf/nginx.conf"  [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  lockfile=/var/lock/subsys/nginx   start() {    [ -x $nginx ] || exit 5    [ -f $NGINX_CONF_FILE ] || exit 6    echo -n $"Starting $prog: "    daemon $nginx -c $NGINX_CONF_FILE    retval=$?    echo    [ $retval -eq 0 ] && touch $lockfile    return $retval}  stop() {    echo -n $"Stopping $prog: "    killproc $prog -QUIT    retval=$?    echo    [ $retval -eq 0 ] && rm -f $lockfile    return $retval}  restart() {    configtest || return $?    stop    sleep 1    start}  reload() {    configtest || return $?    echo -n $"Reloading $prog: "    killproc $nginx -HUP    RETVAL=$?    echo}  force_reload() {    restart}  configtest() {  $nginx -t -c $NGINX_CONF_FILE}  rh_status() {    status $prog}  rh_status_q() {    rh_status >/dev/null 2>&1}  case "$1" in    start)        rh_status_q && exit 0        $1        ;;    stop)        rh_status_q || exit 0        $1        ;;    restart|configtest)        $1        ;;    reload)        rh_status_q || exit 7        $1        ;;    force-reload)        force_reload        ;;    status)        rh_status        ;;    condrestart|try-restart)        rh_status_q || exit 0            ;;    *)        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"        exit 2esac
Nginx

Then run the following command on the terminal:

-- Add service chkconfig -- add nginx -- set to start chkconfig nginx on
8. test

OK. All configurations are complete and run on the terminal.

service uwsgi9090 startservice nginx start

Enter http: // 127.0.0.1 in the browser. Congratulations! you can see django's "It work ~

9. multi-site configuration

I use the method of running multiple uwsgi services to implement multiple sites.

Repeat step 6 to create uwsgi9091.ini and modify

socket = 127.0.0.1:9091pidfile = /var/run/uwsgi9091.piddaemonize = /website/uwsgi9091.log

Create the service uwsgi9091 and set the startup.

Then modify the nginx configuration file:

server {        listen       80;        server_name  localhost;                location / {                        include  uwsgi_params;            uwsgi_pass  127.0.0.1:9090;            uwsgi_param UWSGI_SCRIPT demosite.wsgi;            uwsgi_param UWSGI_CHDIR /website/demosite;            index  index.html index.htm;            client_max_body_size 35m;        }    }    server {        listen       1300;                location / {                        include  uwsgi_params;            uwsgi_pass  127.0.0.1:9091;            uwsgi_param UWSGI_SCRIPT DjangoStudy.wsgi;            uwsgi_param UWSGI_CHDIR /website/DjangoStudy;            index  index.html index.htm;        }    }
Nginx

Then we can access the new website through http: // 127.0.0.1: 1300.

10. Other firewall configurations

By default, CentOS disables external access to ports 80 and 3306. to access this server from other computers, you must modify the firewall configuration to enable/etc/sysconfig/iptables.

In "-a input-m state -- state NEW-m tcp-p-dport 22-j ACCEPT", add:

-A INPUT m state --state NEW m tcp p dport 80 j ACCEPT-A INPUT m state --state NEW m tcp p dport 3306 j ACCEPT

Save and close the file. run the following command in the terminal to refresh the firewall configuration:

service iptables restart
Install Mysqldb
yum -y install mysql-develeasy_install-2.7 MySQL-python

Pay attention to the red part, easy_install-2.7, otherwise it will be installed in the Python2.6 environment by default.

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.