1、上传项目至目标服务器并安装项目所需的模块
包整项目能正常通过runserver启动并且检查没有问题

2、安装uwsgi 并使用uWSGI启动这个服务

执行这条命令的时候:一定要在这个项目目录中~
uwsgi —http 192.168.31.123:80 —file teacher/wsgi.py —static-map=/static=static

3、使用配置文件启动uWSGI[ini]
[uwsgi]
# 项目目录
chdir=/opt/project_teacher/teacher/
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 指定项目的application
module=teacher.wsgi:application
# 指定sock的文件路径
socket=/opt/project_teacher/script/uwsgi.sock
# 启用主进程
master=true
# 进程个数
workers=5
pidfile=/opt/project_teacher/script/uwsgi.pid
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/opt/project_teacher/script/uwsgi.log
4、安装Nginx
vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
# 下面这行centos根据你自己的操作系统修改比如:OS/rehel
# 6是你Linux系统的版本,可以通过URL查看路径是否正确
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

安装nginx

yum -y install nginx

添加配置文件

vim teacher.conf # 这个名字随便起,最好是和项目一个名字
server {
listen 80;
server_name 10.129.205.183 ;
access_log /var/log/nginx/access.log main;
charset utf-8;
gzip on;
gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;

  1. error_page 404 /404.html;
  2. error_page 500 502 503 504 /50x.html;
  3. # 指定项目路径uwsgi
  4. location / {
  5. include uwsgi_params;
  6. uwsgi_connect_timeout 30;
  7. uwsgi_pass unix:/opt/project_teacher/script/uwsgi.sock;
  8. }
  9. # 指定静态文件路径
  10. location /static/ {
  11. alias /opt/project_teacher/teacher/static/;
  12. index index.html index.htm;
  13. }
  14. }

5、添加默认静态文件目录
vim ../teacher/teacher/settings.py
STATIC_ROOT = os.path.join(BASE_DIR, “static_all”)

Nginx配置静态文件简单修改

6、添加管理脚本
cd /etc/init.d/
vim manage_teacher
#! /bin/sh
# chkconfig: 345 85 15
# 上面一行注释:哪些Linux级别需要启动manage_teacher(3,4,5);启动序号(85);关闭序号(15)。
# description: manage script is the teacher daemon.
# Author: luotianshuai
# 指定项目目录
PROJECT_DIR=”/opt/project_teacher/teacher”
# 指定脚本目录在哪里
SCRIPTS_DIR=”/opt/project_teacher/script”
# 描述
DESC=”teacher daemon”
# 名称
NAME=”teacher”
# 脚本名称
SCRIPT_FILENAME=”manage_teacher.sh”
# 脚本目录名称
SCRIPTNAME=pwd/$SCRIPT_FILENAME

  1. # PID
  2. PID="uwsgi.pid"
  3. # 启动函数
  4. d_start(){
  5. # 进入到项目目录
  6. cd $SCRIPTS_DIR
  7. # 判断这个PID是否存在
  8. if [ ! -f $PID ];then
  9. echo -e "\n\033[34m$NAME项目启动中........\033[0m"
  10. # 如果不存在执行
  11. uwsgi --ini uwsgi.ini
  12. killall nginx
  13. /etc/init.d/nginx start
  14. # 自动收集静态文件
  15. cd $PROJECT_DIR && python3 manage.py collectstatic --noinput
  16. echo -e "\n\033[32m$NAME 项目启动完成........\033[0m"
  17. exit 0
  18. fi
  19. echo -e "\n\033[33m$NAME 项目已启动请勿重复启动\033[0m"
  20. }
  21. # 关闭函数
  22. # 关闭项目
  23. d_stop(){
  24. # 进入脚本目录
  25. cd $SCRIPTS_DIR
  26. # 判断这个pid文件是否存在
  27. if [ ! -f "uwsgi.pid" ];then
  28. # 这个项目已经关闭了
  29. echo -e "\n\033[33m$NAME 项目已经关闭了请先启动\033[0m"
  30. fi
  31. echo -e "\n\033[34m$NAME 项目关闭中........\033[0m"
  32. echo -e "\nStop $DESC: $NAME"
  33. # 如果没有关闭
  34. uwsgi --stop uwsgi.pid
  35. # 是否停掉Nginx根据实际需要来操作~~!因为Nginx有对静态文件缓存[注意]
  36. killall nginx
  37. /etc/init.d/nginx start
  38. echo -e "\n\033[32m$NAME 项目关闭完成........\033[0m"
  39. }
  40. d_restart(){
  41. d_stop
  42. sleep 1
  43. d_start
  44. }
  45. case "$1" in
  46. start)
  47. echo -e "\nstarting $DESC: $NAME"
  48. d_start
  49. ;;
  50. stop)
  51. echo -e "\nStop $DESC: $NAME"
  52. d_stop
  53. ;;
  54. restart)
  55. echo -e "\nRestart $DESC: $NAME"
  56. d_restart
  57. ;;
  58. *)
  59. echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
  60. exit 3
  61. ;;
  62. esac

添加服务

chkconfig —add manage_teacher

开机启动

[root@localhost init.d]# chkconfig manage_teacher on

查看启动级别

[root@localhost init.d]# chkconfig —list manage_teacher
manage_teacher 0:off 1:off 2:on 3:on 4:on 5:on 6:off

然后就可以通过命令

[root@localhost ~]# service manage_teacher start
[root@localhost ~]# service manage_teacher stop
[root@localhost ~]# service manage_teacher restart