centos6.5+Django+mysql+nginx+uwsgi
1、nginx设备。
这里使用nginx-1.6.0, 建立shell然后,该脚本运行。
#!/bin/bash nginx_version="nginx-1.6.0" yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel cd soft tar zxvf $nginx_version".tar.gz" cd $nginx_version ./configure --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_stub_status_module --with-http_ssl_module make make install cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local cd .. rm -rf $nginx_version
这里是iptables的设置
iptables -F iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -P INPUT DROP service iptables save2.mysql的安装。在这里mysql使用的是5.6.14,新建一个shell脚本粘贴上以下的文字
#!/bin/bash mysql_version="mysql-5.6.14" yum -y install vim libevent* libtool* autoconf* libstd* ncurse* bison* openssl* cd soft tar zxvf cmake-2.8.12.1.tar.gz cd cmake-2.8.12.1 ./configure && make && make install cd .. rm -rf cmake-2.8.12.1 tar zxvf $mysql_version".tar.gz" cd $mysql_version cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci make && make install groupadd mysql useradd -g mysql mysql chown -R mysql:mysql /usr/local/mysql cd /usr/local/mysql scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --ldata=/var/lib/mysql cp support-files/mysql.server /etc/init.d/mysql chkconfig mysql on chmod 755 /etc/init.d/mysql service mysql start echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile echo "export PATH" >> /etc/profile source /etc/profile mysqladmin -u root password 123456 cd - cd .. rm -rf $mysql_version
安装必要的软件包
yum groupinstall "Development tools" yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel python-devel libxml2 libxml2-devel python-setuptools zlib-devel wget openssl-devel pcre pcre-devel sudo gcc make autoconf automake安装pip软件
wget http://pypi.python.org/packages/source/p/pip/pip-1.0.2.tar.gz --no-check-certificate tar xvfz pip-1.0.2.tar.gz cd pip-1.0.2 python setup.py install
pip --version 查看pip是否安装安装uwsgi
pip install uwsgi uwsgi --version
新建web.py文件,内容例如以下:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"然后在终端执行:
uwsgi --http :8001 --wsgi-file test.py在浏览器内输入:http://127.0.0.1:8001,看是否有“Hello World”输出。
安装django
pip install django
django-admin.py startproject demosite cd demosite
python manage.py runserver 0.0.0.0:8002在浏览器内输入:http://127.0.0.1:8002,检查django是否执行正常。
配置uwsgi
在/ect/文件夹下新建uwsgi9090.ini。加入例如以下配置:
[uwsgi] socket = 127.0.0.1:9090 master = true //主进程 vhost = true //多站模式 workers = 2 //子进程数 reload-mercy = 10 vacuum = true //退出、重新启动时清理文件 max-requests = 1000 limit-as = 512 buffer-sizi = 30000 pidfile = /var/run/uwsgi9090.pid //pid文件,用于以下的脚本启动、停止该进程 daemonize = /website/uwsgi9090.log
设置uwsgi开机启动,在/ect/init.d/文件夹下新建uwsgi9090文件。内容例如以下:
#! /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/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 "$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设置uwsgi开机启动chkconfig --add uwsgi9090 chkconfig uwsgi9090 on
改动nginx的conf文件
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090; //必须和uwsgi中的设置一致
uwsgi_param UWSGI_SCRIPT demosite.wsgi; //入口文件,即wsgi.py相对于项目根文件夹的位置。“.”相当于一层文件夹
uwsgi_param UWSGI_CHDIR /demosite; //项目根文件夹
index index.html index.htm;
client_max_body_size 35m;
}
}service uwsgi9090 start在浏览器输入:http://127.0.0.1。恭喜你能够看到django的“It work”了~
安装mysql-python
pip install mysql-python
測试django与mysql的连接性
在项目中执行python manage.py shell 然后
from django.db import connection cursor=connection.cursor()
版权声明:本文博主原创文章,博客,未经同意不得转载。

浙公网安备 33010602011771号