Centos+nginx+uwsgi+python Multi-site environment setup

Source: Internet
Author: User
Tags bz2 ini centos install django iptables pip install django root directory firewall

Environment:

CentOS X64 6.4

Nginx 1.5.6

Python 2.7.5

One: the class library and Python2.7.5 that need to install

Install the necessary development packs

Yum Groupinstall "Development Tools"

Yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

CentOS 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.bz2

Tar xvf python-2.7.5.tar.bz2

CD Python-2.7.5

./configure--prefix=/usr/local

Make && make Altinstall

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

Two: Install Python package management

Easy_install Bag Https://pypi.python.org/pypi/distribute

Easy to install Python development kits

CD ~

wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz

Tar XF distribute-0.6.49.tar.gz

CD distribute-0.6.49

python2.7 setup.py Install

Easy_install--version

The red section must be "python2.7" or it will be installed in the default 2.6 environment.

PIP Package Https://pypi.python.org/pypi/pip

The benefit of installing the PIP is that the PIP list, pip Uninstall manages the Python package, Easy_install does not have this functionality, only uninstall

Easy_install pip

Pip--version

Three: Install Uwsgi

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

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

Pip Install Uwsgi

Uwsgi--version

Test Uwsgi is normal:

Create a new test.py file with the following contents:

DEF application (env, Start_response):

Start_response (' OK ', [(' Content-type ', ' text/html ')]

Return to "Hello World"

Then run at the terminal:

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

In the browser input: http://127.0.0.1:8001 to see if there is "Hello world" output, if there is no output, please check your installation process.

Four: Install Django

Pip Install Django

Test to see if Django is working:

django-admin.py Startproject Demosite

CD Demosite

python2.7 manage.py Runserver 0.0.0.0:8002

Enter in the browser: http://127.0.0.1:8002, check to see if Django is working properly.

V: Installation of Nginx

CD ~

wget http://nginx.org/download/nginx-1.5.6.tar.gz

Tar XF nginx-1.5.6.tar.gz

CD nginx-1.5.6

./configure--prefix=/usr/local/nginx-1.5.6

--with-http_stub_status_module

--with-http_gzip_static_module

Make && make install

VI: Configure UWSGI

Uwsgi support INI, XML and many other configuration, but personal feel ini more convenient:

Create a new Uwsgi9090.ini in the/ect/directory and add the following configuration:

[Uwsgi]

Socket = 127.0.0.1:9090

Master = TRUE//main process

Vhost = TRUE//multi-station mode

No-stie = TRUE//multi-station mode does not set portal modules and files

Workers = 2//number of child processes

Reload-mercy = 10

Vacuum = TRUE///exit, clean files on reboot

max-requests = 1000

Limit-as = 512

Buffer-sizi = 30000

Pidfile =/var/run/uwsgi9090.pid//pid file, for the following script to start, stop the process

Daemonize =/website/uwsgi9090.log

Set the Uwsgi boot up and create a new uwsgi9090 file in the/ect/init.d/directory, 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/bin

desc= "Uwsgi Daemon"

name=uwsgi9090

Daemon=/usr/local/bin/uwsgi

configfile=/etc/$NAME. ini

pidfile=/var/run/$NAME. PID

Scriptname=/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 "$" 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

Then execute at the terminal:

--Adding services

Chkconfig--add uwsgi9090

--Set boot up

Chkconfig uwsgi9090 on

Seven: Set Nginx

Locate the Nginx installation directory, open the conf/nginx.conf file, modify the server configuration

server {

Listen 80;

server_name localhost;

Location/{

Include Uwsgi_params;

Uwsgi_pass 127.0.0.1:9090; Must be consistent with the settings in Uwsgi

Uwsgi_param Uwsgi_script Demosite.wsgi; Portal file, that is, the location of the wsgi.py relative to the project root directory, "." Equivalent to a level of directory

Uwsgi_param Uwsgi_chdir/demosite; Project root directory

Index index.html index.htm;

Client_max_body_size 35m;

}

}

Set the Nginx boot up and create a new Nginx file in the/ect/init.d/directory, as follows:

#!/bin/sh

#

# Nginx-this script starts and stops the Nginx daemon

#

# Chkconfig:-85 15

# Description:nginx is a 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 are 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 "$" in

Start

Rh_status_q && Exit 0

$

;;

Stop

Rh_status_q | | Exit 0

$

;;

Restart|configtest)

$

;;

Reload

Rh_status_q | | Exit 7

$

;;

Force-reload)

Force_reload

;;

Status

Rh_status

;;

Condrestart|try-restart)

Rh_status_q | | Exit 0

;;

*)

echo $ "Usage: $ {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

Exit 2

Esac

Then execute at the terminal:

--Adding services

Chkconfig--add Nginx

--Set boot up

Chkconfig Nginx on

Eight: Testing

OK, all configured, run at terminal

Service uwsgi9090 Start

Service Nginx Start

In the browser input: http://127.0.0.1, congratulations you can see the Django "It work" ~

IX: Other configurations

Firewall settings

CentOS the external access to port 80, 3306 is turned off by default, so to access this server on other computers, you must modify the firewall configuration to open the/etc/sysconfig/iptables

Under "-A input–m State--state new–m tcp–p–dport 22–j ACCEPT", add:

-A INPUT m state--state NEW m TCP p dport J ACCEPT

-A INPUT m state--state NEW m TCP p dport 3306 J ACCEPT

Then save and close the file, run the following command within the terminal, and refresh the firewall configuration:

Service Iptables Restart

Install MySQLdb

Yum-y Install Mysql-devel

easy_install-2.7 Mysql-python

Note the Red section, easy_install-2.7, otherwise it will be installed in the Python2.6 environment by default.

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.