CentOS6+nginx+uwsgi+mysql+django1.6.6+python2.6.6

1.配置网关

#vi /etc/sysconfig/network
NETWORKING=yes(表示系统是否使用网络,一般设置为yes。如果设为no,则不能使用网络,而且很多系统服务程序将无法启动)
HOSTNAME=centos(设置本机的主机名,这里设置的主机名要和/etc/hosts中设置的主机名对应)
GATEWAY=192.168.1.1(设置本机连接的网关的IP地址。例如,网关为10.0.0.2)

2.配置DNS

#vi /etc/resolv.conf
nameserver: 114.114.114.114 # IBM哥们讲的国内比较快的一个DNS

3.CentOS 修改IP地址

修改对应网卡的IP地址的配置文件
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
修改以下内容
DEVICE=eth0 #描述网卡对应的设备别名,例如ifcfg-eth0的文件中它为eth0
BOOTPROTO=static #设置网卡获得ip地址的方式,可能的选项为static,dhcp或bootp,分别对应静态指定的 ip地址,通过dhcp协议获得的ip地址,通过bootp协议获得的ip地址
BROADCAST=192.168.0.255 #对应的子网广播地址
HWADDR=00:07:E9:05:E8:B4 #对应的网卡物理地址
IPADDR=12.168.1.2 #如果设置网卡获得 ip地址的方式为静态指定,此字段就指定了网卡对应的ip地址
IPV6INIT=no
IPV6_AUTOCONF=no
NETMASK=255.255.255.0 #网卡对应的网络掩码
NETWORK=192.168.1.0 #网卡对应的网络地址
ONBOOT=yes #系统启动时是否设置此网络接口,设置为yes时,系统启动时激活此设备

4.重新启动网络配置

# service network restart 
或
# /etc/init.d/network restart

5.配置epal源(别的源没有的它也有,比如saltstack)

wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm# 下载
chmod +x epel-release-6-8.noarch.rpm# 权限
rpm -ivh epel-release-6-8.noarch.rpm# 安装
yum update

6.安装pip

wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
wget https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py
python get-pip.py# 没有install

https://bootstrap.pypa.io/get-pip.py

变成这个了,2016年04月11日22:08:44更新

wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py

2016年06月12日11:32:45 更新

7.pip安装django

pip install django==1.6.6

8.pip安装mysqldb

yum install mysql-devel mysql mysql-server python-devel
pip install MySQL-python

9.修改mysql的root密码

mysql -uroot
use mysql;#有的版本默认管理数据库不是这个名字(*注意)
MySQL> update user set password=PASSWORD('newpassword') where User='root';
MySQL> flush privileges; //刷新权限,重启mysql也可以
MySQL> quit

或者:

/usr/bin/mysqladmin -u root password 'passw0rd'

10.mysql创建schema

CREATE SCHEMA `xtyw` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
# 这样创建的表不会出现admin中表更新中文内容的时候乱码

11.安装nginx

yum -y install nginx

12.配置nginx

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;
#-----------------------------------------------------
    client_max_body_size 1024m;
    server {
        listen 80;
        server_name 192.168.46.5;
        location / {
            uwsgi_pass 127.0.0.1:9000;
            include uwsgi_params;
            uwsgi_param UWSGI_CHDIR /data/www/helloworld;
            uwsgi_param UWSGI_SCRIPT django_wsgi;
            access_log off;
        }
        location ^~ /static {
            root /data/www/xtyw/;
        }
        location ~* ^.+.(png|mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|docx|xls|exe|ppt|t|tar|mid|midi|wav|rtf|,peg)$ {
            root /data/medias;
            access_log off;
        }
    }

#---------------------------------以下是我添加的内容

then

/etc/init.d/nginx start

13.安装uwsgi

pip install uwsgi

14.开始用uwsgi部署django应用到nginx上

进入项目主目录,即settings.py所在目录,创建uwsgi.ini配置文件

内容如下

[uwsgi]
socket = 0.0.0.0:9000
master = true
pidfile = /etc/nginx/uwsgi.pid
processes = 4
chdir = /data/www/xtyw/
wsgi-file = /data/www/xtyw/xtyw/wsgi.py
profiler = true
memory-report = true
enable-threads = true
logdate = true
limit-as = 6048
daemnize = /data/logs/django.log

执行:

#uwsgi uwsgi.ini
如果是xml文件
#uwsgi -x uwsgi.xml

15.最后附上自动django源码自动重载方法

1.安装工具
yum -y install inotify-tools
2.编写一个监视脚本autoreload.sh
#!/bin/sh

objectdir="/data/www/xtyw"
# 启动inotify监视项目目录, 参数"--exclude" 为忽略的文件或目录正则
/usr/bin/inotifywait -mrq --exclude "(static|logs|shell|.swap|.pyc|.swx|.py~)" --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' --event modify,delete,move,create,attrib ${objectdir} | while read files
do
/bin/touch /data/www/xtyw/shell/reload.set
    continue
done &
3.编写监视文件reload.set(内容即是uwsgi启动脚本)
#!/bin/bash
if [ ! -n "$1" ]
then
    echo "Usages: sh uwsgiserver.sh [start|stop|restart]"
    exit 0
fi

if [ $1 = start ]
then
    psid=`ps aux | grep "uwsgi" | grep -v "grep" | wc -l`
    if [ $psid -gt 4 ]
    then
        echo "uwsgi is running!"
        exit 0
    else
        uwsgi /data/www/xtyw/xtyw/uwsgi.ini
        echo "Start uwsgi service [OK]"
    fi


elif [ $1 = stop ];then
    killall -9 uwsgi
    echo "Stop uwsgi service [OK]"
elif [ $1 = restart ];then
    killall -9 uwsgi
    /usr/bin/uwsgi --ini /data/www/xtyw/xtyw/uwsgi.ini --touch-reload "/data/www/xtyw/shell/reload.set"
    echo "Restart uwsgi service [OK]"

else
    echo "Usages: sh uwsgiserver.sh [start|stop|restart]"
fi

上线:

./autoreload.sh

all done

原文地址:https://www.cnblogs.com/clarke/p/5670785.html