python2.7 + django1.11
1.基础环境准备
1.1配置virtualenv和virtualenvwrapper
pip install virtualenv
pip install virtualenvwrapper
#如果执行失败,先升级pip ,setuptools
pip install --upgrade pip
pip install --upgrade setuptools
可以超级权限或者普通权限安装,存储的目录不一致,超级权限/usr/local/
,普通权限 ~/.local/
。
设置环境变量:追加以下内容到~/.bashrc
中: 指令:vim /.bashrc
if [ -f /usr/local/bin/virtualenvwrapper.sh ];then
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
fi
if [ -f $HOME/.local/bin/virtualenvwrapper.sh ];then
export WORKON_HOME=$HOME/.virtualenvs
export PATH=$PATH:$HOME/.local/bin/
source $HOME/.local/bin/virtualenvwrapper.sh
fi
if [ -f /usr/bin/virtualenvwrapper.sh ];then
export WORKON_HOME=$HOME/.virtualenvs
source /usr/bin/virtualenvwrapper.sh
fi
执行指令: source ~/.bashrc
1.2 使用virtualenvwrapper
创建虚拟环境
mkvirtualenv venv
进入虚拟环境
workon venv
退出虚拟环境
deactivate
删除虚拟环境(慎用)
rmvirtualenv venv
1.3 进入虚拟环境后在虚拟环境pip安装项目需要的相关模块
pip install django==1.11.28
pip install MySQL-python
说明: 根据自己的项目所涉及到的模块进行安装, MySQL-python 模块安装问题 连接
2.拉项目
2.1 获取项目
在linux的home下创建项目文件夹 : 指令 mkdir online
进入online :指令 cd online
Xshell下快捷键: Ctrl + Alt + F 会打开 Xftp, 将压缩好的项目tar包传输到 /home/online/ 下
解压tar包 :指令 tar xvf tar包名 #解压到当前所在目录下
2.2 项目配置修改
进入django项目内,找到配置文件settings.py修改数据库,等相关配置 : 指令 vim settings.py
2.3 数据库配置
先创建项目需要的库
在django项目目录内执行:
python manage.py migrate
如果修改了model相关内容后,需要先执行:
python manage.py makemigrations
python manage.py migrate
通过以下命令可以创建管理员,并且可以在管理员界面登录进去:
python manage.py createsuperuser
3.uwsgi配置
3.1 查看wsgi.py的配置
首先需要项目在配置时添加了wsgi的配置:
online_api/settings.py里要有:
WSGI_APPLICATION='online_api.wsgi.application'
# 正常django的settings.py中都会有,且自动生成的,看一眼就行
确认项目下存在online_api/wsgi.py:
"""
WSGI config for klbp project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "online_api.settings")
application = get_wsgi_application()
3.2 安装uwsgi
pip install uwsgi
#可以不需要在python的小环境里执行,在最外部执行
在项目目录的同级目录创建uwsgi_script【名字可以自己起,叫什么无所谓】,再创建三个文件,如下所示:
mkdir uwsgi_script
touch uwsgi_script/uwsgi.ini
touch uwsgi_script/uwsgi.log
touch uwsgi_script/uwsgi.pid
自此,项目目录结构:
├── home/
├── iscas-ztb/
├── online/
├── uwsgi_script/
├── uwsgi.ini
├── uwsgi.log
└── uwsgi.pid
└── online_api
├── app01/
├── static/
├── templates/
├── manage.py
└── online_api/
├── settings.py
├── wsgi.py
└── urls.py
uwsgi.log,uwsgi.pid这两个文件为空,uwsgi.ini按照以下进行配置(这里面的路径要根据自己的项目路径找对):
[uwsgi]
master=true
buffer-size=655360
post-buffering=4096
#最大工作线程数量
processes=100
#socket这个是用于与Nginx进行数据交互的端口
socket=服务IP:33366
#不记录request日志,只记录错误日志
disable-logging=true
#指定static文件,static-map = /static=是固定格式
static-map = /static=/home/iscas-ztb/online/online_api/static
#django项目的路径
chdir=/home/iscas-ztb/online/online_api
#对外提供的http服务端口
http=服务器IP:8000
#项目运行的所在环境,这里要填上虚拟环境的目录地址
home=/home/iscas-ztb/.virtualenvs/venv/
module=online_api.wsgi
vacuum=true
uid=root
gid=root
master=true
enable-threads=true
socket=/home/iscas-ztb/online/uwsgi_nginx.sock
chmod-socket=666
pidfile=/home/iscas-ztb/online/uwsgi_script/uwsgi.pid
daemonize=/home/iscas-ztb/online/uwsgi_script/uwsgi.log
添加system服务,可以使用服务启动,默认启动 添加/etc/init.d/uwsgi_online文件,记得添加可执行权限 : 指令 sudo vim /etc/init.d/uwsgi_online
这里注意 Centos 与 Ubuntu 区别:
Ubuntu 如下配置:
#!/bin/bash
if [ ! -n "$1" ];then
echo "Usage: sh uwsgi_online.sh [start|stop|restart]"
exit 0
fi
if [ "$1" = "start" ];then
psid=`ps aux | grep "uwsgi --ini /home/iscas-ztb/online/uwsgi_script/uwsgi.ini" | grep -v "grep" | wc -l`
if [[ $psid -gt 4 ]];then
echo "online uwsgi is running!"
exit 0
else
/usr/local/bin/uwsgi --ini /home/iscas-ztb/online/uwsgi_script/uwsgi.ini
echo "Start online uwsgi service [OK]"
fi
elif [ "$1" = "stop" ];then
ps aux | grep "uwsgi --ini /home/iscas-ztb/online/uwsgi_script/uwsgi.ini" | grep -v "grep" | awk '{print $2}' | xargs kill -9
echo "Stop online uwsgi service [OK]"
elif [ "$1" = "restart" ];then
ps aux | grep "uwsgi --ini /home/iscas-ztb/online/uwsgi_script/uwsgi.ini" | grep -v "grep" | awk '{print $2}' | xargs kill -9
/usr/local/bin/uwsgi --ini /home/iscas-ztb/online/uwsgi_script/uwsgi.ini
echo "Restart online uwsgi service [OK]"
else
echo "Usage: sh uwsgi_online.sh [start|stop|restart]"
fi
运行服务:
systemctl start uwsgi_online #启动服务
systemctl stop uwsgi_online #停止服务
systemctl restart uwsgi_online #重启
systemctl enable uwsgi_online #开机默认启动
使用浏览器访问 服务器IP:8000 就可以访问到平台。
Centos 如下配置:
#!/bin/bash
if [ ! -n "$1" ];then
echo "Usage: sh uwsgi_online.sh [start|stop|restart]"
exit 0
fi
if [ "$1" = "start" ];then
psid=`ps aux | grep "uwsgi --ini /home/iscas-ztb/online/uwsgi_script/uwsgi.ini" | grep -v "grep" | wc -l`
if [[ $psid -gt 4 ]];then
echo "online uwsgi is running!"
exit 0
else
/usr/bin/uwsgi --ini /home/iscas-ztb/online/uwsgi_script/uwsgi.ini
echo "Start online uwsgi service [OK]"
fi
elif [ "$1" = "stop" ];then
ps aux | grep "uwsgi --ini /home/iscas-ztb/online/uwsgi_script/uwsgi.ini" | grep -v "grep" | awk '{print $2}' | xargs kill -9
echo "Stop online uwsgi service [OK]"
elif [ "$1" = "restart" ];then
ps aux | grep "uwsgi --ini /home/iscas-ztb/online/uwsgi_script/uwsgi.ini" | grep -v "grep" | awk '{print $2}' | xargs kill -9
/usr/bin/uwsgi --ini /home/iscas-ztb/online/uwsgi_script/uwsgi.ini
echo "Restart online uwsgi service [OK]"
else
echo "Usage: sh uwsgi_online.sh [start|stop|restart]"
fi
添加/usr/lib/systemd/system/uwsgi-online.service文件
[Unit]
Description=uwsgi-online
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/uwsgi_online start
ExecReload=/etc/init.d/uwsgi_onlie restart
ExecStop=/etc/init.d/uwsgi_online stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
运行服务:
systemctl start uwsgi-online.service #启动服务
systemctl stop uwsgi-online.service #停止服务
systemctl restart uwsgi-online.service #重启
systemctl enable uwsgi-online.service #开机默认启动
使用浏览器访问 服务器IP:8000 就可以访问到平台。
4.nginx 配置
4.1 安装nginx
centos:
yum install nginx
#可以不需要在python的小环境里执行,在最外部执行
ubuntu:
sudo apt-get install nginx
4.2 修改&新增配置文件
Ubuntu:
修改系统默认的nginx配置文件/etc/nginx/nginx.conf
#user www-data;
user root;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/online.conf; # !!!!!!!
#include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
新增以下文件/etc/nginx/conf.d/online.conf 注意执行权限
server {
listen 80;
server_name 服务器IP; #如果有域名这里写域名
charset utf-8;
access_log /home/iscas-ztb/online/logs/nginx/access.log;
error_log /home/iscas-ztb/online/logs/nginx/error.log;
#这里需要创建/home/iscas-ztb/online/logs/nginx
location /static {
alias /home/iscas-ztb/online/online_api/static;
}
#location / {
# root /home/klbp/klbp/klbpfront/dist;
# index index.html;
#}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass 192.168.160.184:33366;
uwsgi_connect_timeout 30;
}
}
Centos:
修改系统默认的nginx配置文件/etc/nginx/nginx.conf
user nginx; 改成 user root;
include /etc/nginx/conf.d/*.conf; 确保此行是打开的
server{}部分全部注释掉
新增以下文件/etc/nginx/conf.d/online.conf 注意执行权限
upstream klbp {
server 服务器IP:33366; #这里要和uwsgi的socket地址一样
}
server {
listen 80;
server_name 服务器IP; #如果有域名这里写域名
charset utf-8;
access_log /home/iscas-ztb/online/logs/nginx/access.log;
error_log /home/iscas-ztb/online/logs/nginx/error.log;
#这里需要创建/home/iscas-ztb/online/logs/nginx
location /static {
alias /home/iscas-ztb/online/online_api/static;
}
#location / {
# root iscas-ztb/online/online_api/static;
# index index.html;
#}
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass online;
uwsgi_connect_timeout 30;
}
}
4.3 uwsgi 去除http 参数
就是去除uwsgi.ini的http内容 就是注释掉 : #http=服务器IP:8000
4.4 合并static
python manage.py collectstatic
4.5 启动服务
Ubuntu:
systemctl start uwsgi_online
systemctl start nginx
Centos:
systemctl start uwsgi-online.service
nginx -c /etc/nginx/nginx.conf 或 nginx 或 systemctl start nginx
补充:
systemctl status nginx #查看nginx
systemctl stop nginx 或 nginx -s stop # 停止nginx
systemctl start nginx # 启动nginx