说明:本文档中所有执行步骤都是在root用户下完成的
1、环境基础和软件版本:
操作系统:linux
python版本:Python2.7.5及以上
mysql版本:mysql-5.7.17-1(或者更新版本)
python下mysql驱动:MySQL-python-1.2.5.zip
nginx:nginx-1.12.2.tar.gz
uwsgi:uwsgi-2.0.15.tar.gz
2、安装步骤:
2.1 保证linux系统下已安装完成Python及mysql的相应版本,网上有许多安装资料,在此不做详细说明
2.2 uwsgi源码方式安装:
tar -zxvf uwsgi-2.0.15.tar.gz
cd uwsgi-2.0.15
make
以上步骤完成后,会在uwsgi-2.0.15生成uwsgi文件,此文件为二进制可执行文件,将此文件复制到/usr/sbin/目录下
2.3 nginx源码方式安装:
tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/usr/local/nginx/
make
make install
注:nginx的安装目录为:/usr/local/nginx/
2.4 Django项目文件安装:
将Django项目包DeviceMaster.zip解压到/home/DeviceMaster目录下即可。
3、配置:
3.1 在/home/DeviceMaster目录下新建uwsgi.ini文件,uwsgi.ini的配置如下:
[uwsgi]
# 项目目录
chdir=/home/DeviceMaster/
# 指定项目的application
module=DeviceMaster.wsgi
# 进程个数
workers=5
pidfile=/home/DeviceMaster/uwsgi.pid
# 指定IP端口
socket= :8001
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/home/DeviceMaster/uwsgiconfig/uwsgi.log
# 指定sock的文件路径
socket=/home/DeviceMaster/uwsgiconfig/uwsgi.sock
plugin python = true
3.2 nginx的配置:
进入nginx的安装目录/usr/local/nginx,编辑nginx.conf:
server {
listen 8080;
server_name 127.0.0.1;
charset utf-8;
access_log logs/devicemaster.log main;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001;
}
location /static {
alias /home/DeviceMaster/static/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4、数据库初始化:
4.1、devicemaster数据库创建:
create database devicemaster default character set utf8 collate utf8_general_ci;
4.2、初始化数据添加:
insert into devices_statecount(total_phy,total_virtual,running_phy,running_virtual,createTime) values (2,2,2,2,now());
4.3、数据迁移:
linux:/home/DeviceMaster # python manage.py makemigrations
linux:/home/DeviceMaster # python manage.py migrate
5、项目启动:
5.1 静态文件收集:
进入Django项目部署目录/home/DeviceMaster,执行:
python manage.py collectstatic
执行完后,会将Django项目用到的静态文件全部收集到/home/DeviceMaster/static目录下。
5.2 项目启动:进入Django项目部署目录/home/DeviceMaster,执行:
#uwsgi启动
uwsgi --ini uwsgi.ini
#uwsgi 停止
uwsgi --stop uwsgi.pid
6、mysql启停:
linux:/usr/bin # service mysql status
Checking for service MySQL: unused
linux:/usr/bin # service mysql start
Starting service MySQL: done
linux:/usr/bin # service mysql status
Checking for service MySQL: running