centos6.5+Django+mysql+nginx+uwsgi

centos6.5+Django+mysql+nginx+uwsgi

1、nginx的安装。这里采用nginx-1.6.0, 建立一个shell脚本然后执行。

  1. #!/bin/bash  
  2. nginx_version="nginx-1.6.0"  
  3. yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel  
  4. cd soft  
  5. tar zxvf $nginx_version".tar.gz"   
  6. cd $nginx_version  
  7. ./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  
  8. make  
  9. make install  
  10. cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak  
  11. /usr/local/nginx/sbin/nginx  -c /usr/local/nginx/conf/nginx.conf  
  12. echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local  
  13. cd ..  
  14. rm -rf $nginx_version  
  1. 这里是iptables的设置  
  1. iptables -F  
  2. iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT  
  3. iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT  
  4. iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT  
  5. iptables -A INPUT -i lo -j ACCEPT  
  6. iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT  
  7. iptables -P INPUT DROP  
  8. service iptables save  

2.mysql的安装,在这里mysql使用的是5.6.14,新建一个shell脚本粘贴上下面的文字

  1. #!/bin/bash  
  2. mysql_version="mysql-5.6.14"  
  3. yum -y install vim  libevent*  libtool* autoconf* libstd* ncurse* bison* openssl*  
  4. cd soft  
  5. tar zxvf cmake-2.8.12.1.tar.gz  
  6. cd cmake-2.8.12.1  
  7. ./configure && make && make install  
  8. cd ..  
  9. rm -rf cmake-2.8.12.1  
  10. tar zxvf $mysql_version".tar.gz"  
  11. cd $mysql_version  
  12. 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  
  13. make && make install  
  14. groupadd mysql  
  15. useradd -g mysql mysql  
  16. chown -R mysql:mysql /usr/local/mysql  
  17. cd /usr/local/mysql  
  18. scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --ldata=/var/lib/mysql  
  19. cp support-files/mysql.server /etc/init.d/mysql  
  20. chkconfig mysql on  
  21. chmod 755 /etc/init.d/mysql  
  22. service mysql start  
  23. echo "PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile  
  24. echo "export PATH" >> /etc/profile  
  25. source /etc/profile  
  26. mysqladmin -u root password 123456  
  27. cd -  
  28. cd ..  
  29. rm -rf $mysql_version  


3.开始安装django,这里使用的是django最新稳定版1.6.5

安装必要的软件包

  1. yum groupinstall "Development tools"  
  2. 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软件

  1. wget http://pypi.python.org/packages/source/p/pip/pip-1.0.2.tar.gz --no-check-certificate    
  2. tar xvfz pip-1.0.2.tar.gz     
  3. cd pip-1.0.2    
  4. python setup.py  install    
  1. pip --version  查看pip是否安装  

安装uwsgi

  1. pip install uwsgi  
  2. uwsgi --version  



新建web.py文件,内容如下:

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


然后在终端运行:

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

在浏览器内输入:http://127.0.0.1:8001,看是否有“Hello World”输出。

安装django

  1. pip install django  
  1. django-admin.py startproject demosite  
  2. cd demosite  
  1. python manage.py runserver 0.0.0.0:8002  

在浏览器内输入:http://127.0.0.1:8002,检查django是否运行正常。


配置uwsgi

在/ect/目录下新建uwsgi9090.ini,添加如下配置:

  1. [uwsgi]  
  2. socket = 127.0.0.1:9090  
  3. master = true         //主进程  
  4. vhost = true          //多站模式  
  5. workers = 2           //子进程数  
  6. reload-mercy = 10       
  7. vacuum = true         //退出、重启时清理文件  
  8. max-requests = 1000     
  9. limit-as = 512  
  10. buffer-sizi = 30000  
  11. pidfile = /var/run/uwsgi9090.pid    //pid文件,用于下面的脚本启动、停止该进程  
  12. daemonize = /website/uwsgi9090.log  

设置uwsgi开机启动,在/ect/init.d/目录下新建uwsgi9090文件,内容如下:

  1. #! /bin/sh  
  2. # chkconfig: 2345 55 25  
  3. # Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and  
  4. # run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your  
  5. # distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'  
  6.    
  7. ### BEGIN INIT INFO  
  8. # Provides:          uwsgi  
  9. # Required-Start:    $all  
  10. # Required-Stop:     $all  
  11. # Default-Start:     2 3 4 5  
  12. # Default-Stop:      0 1 6  
  13. # Short-Description: starts the uwsgi web server  
  14. # Description:       starts uwsgi using start-stop-daemon  
  15. ### END INIT INFO  
  16.    
  17. # Author:   licess  
  18. # website:  http://lnmp.org  
  19.    
  20. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  
  21. DESC="uwsgi daemon"  
  22. NAME=uwsgi9090  
  23. DAEMON=/usr/bin/uwsgi  
  24. CONFIGFILE=/etc/$NAME.ini  
  25. PIDFILE=/var/run/$NAME.pid  
  26. SCRIPTNAME=/etc/init.d/$NAME  
  27.    
  28. set -e  
  29. [ -x "$DAEMON" ] || exit 0  
  30.    
  31. do_start() {  
  32.     $DAEMON $CONFIGFILE || echo -n "uwsgi already running"  
  33. }  
  34.    
  35. do_stop() {  
  36.     $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"  
  37.     rm -f $PIDFILE  
  38.     echo "$DAEMON STOPED."  
  39. }  
  40.    
  41. do_reload() {  
  42.     $DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"  
  43. }  
  44.    
  45. do_status() {  
  46.     ps aux|grep $DAEMON  
  47. }  
  48.    
  49. case "$1" in  
  50.  status)  
  51.     echo -en "Status $NAME:  "  
  52.     do_status  
  53.  ;;  
  54.  start)  
  55.     echo -en "Starting $NAME:  "  
  56.     do_start  
  57.  ;;  
  58.  stop)  
  59.     echo -en "Stopping $NAME:  "  
  60.     do_stop  
  61.  ;;  
  62.  reload|graceful)  
  63.     echo -en "Reloading $NAME:  "  
  64.     do_reload  
  65.  ;;  
  66.  *)  
  67.     echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2  
  68.     exit 3  
  69.  ;;  
  70. esac  
  71.    
  72. exit 0  

设置uwsgi开机启动

  1. chkconfig --add uwsgi9090   
  2. chkconfig uwsgi9090 on  


修改nginx的conf文件

  1. server {  
  2.         listen       80;  
  3.         server_name  localhost;  
  4.           
  5.         location / {              
  6.             include  uwsgi_params;  
  7.             uwsgi_pass  127.0.0.1:9090;              //必须和uwsgi中的设置一致  
  8.             uwsgi_param UWSGI_SCRIPT demosite.wsgi;  //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录  
  9.             uwsgi_param UWSGI_CHDIR /demosite;       //项目根目录  
  10.             index  index.html index.htm;  
  11.             client_max_body_size 35m;  
  12.         }  
  13.     }  
  1. service uwsgi9090 start  

在浏览器输入:http://127.0.0.1,恭喜你可以看到django的“It work”了~

安装mysql-python

  1. pip install mysql-python  


测试django与mysql的连接性

  1. 在项目中运行python manage.py shell  
  2.   
  3. 然后  


    1. from django.db import connection  
    2. cursor=connection.cursor() 
原文地址:https://www.cnblogs.com/timssd/p/4570712.html