详解Django+uwsgi+Nginx上线最佳实战


Posted in Python onMarch 14, 2019

什么是uwsgi?

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。

  1. WSGI是一种通信协议。
  2. uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。
  3. uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。

在开始之前

最小化安装CentOS 6

备份网卡文件

~$ mkdir /etc/sysconfig/network-scripts/backup
~$ cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/backup/ifcfg-eth0.backup

配置阿里云镜像源

~$ mkdir /etc/yum.repos.d/old
~$ mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/old/
~$ cd /etc/yum.repos.d/
~$ curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
~$ yum clean all && yum repolist all && yum update -y
~$ reboot

Python3.6.0

上传Python-3.6.0.tar.xz

~$ rz

安装依赖

yum install zlib* gcc openssl openssl-devel libffi-devel -y
yum install pcre pcre-devel pcre-static -y

解压Python-3.6.0.tar.xz

~$ tar -xvf Python-3.6.0.tar.xz
~$ cd Python-3.6.0

修改部分源代码

~$ vim Modules/Setup.dist
# 将该文件的204到209行部分代码取消注释,完成后如下所示:
# Socket module helper for socket(2)
_socket socketmodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
  -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
  -L$(SSL)/lib -lssl -lcrypto

# The crypt module is now disabled by default because it breaks builds

编译安装

~$ ./configure
~$ make -j
~$ make install
~$ cd
~$ rm -rf Python-3.6.0

防火墙

# 恢复默认配置
iptables -F
# 放通3306/8000/80端口
iptables -I INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
iptables -I INPUT -p tcp -m tcp --dport 8000 -j ACCEPT
iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
# 保存规则
/etc/init.d/iptables save

SELinux

关闭SELinux

~$ vim /etc/selinux/config
# 修改配置为如下所示:
SELINUX=permissive

~$ reboot

数据库

二进制方式安装

# 查找相关旧文件并删除
find / -name mysql
find / -name mariadb
# 移除全部相关包
rpm -qa | grep mysql
rpm -qa | grep mariadb
# 添加用户
useradd mysql -s /sbin/nologin -M
# 解压移动文件
tar -xvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.24-linux-glibc2.12-x86_64 /applications/
ln -s /applications/mysql-5.7.24-linux-glibc2.12-x86_64/ /applications/mysql
# 创建配置文件
vim /etc/my.cnf
# 创建相关目录
mkdir -p /data/mysql/data
mkdir -p /data/mysql/log
# 手动创建日志文件
touch /data/mysql/log/mysqld.log
# 修改权限
chown -R mysql.mysql /applications/mysql
chown -R mysql.mysql /data/mysql

MySQL配置文件

[client]
port=3306
socket=/data/mysql/mysql.sock

[mysqld]
port=3306
datadir=/data/mysql/data
basedir=/applications/mysql
pid-file=/data/mysql/mysqld.pid
socket=/data/mysql/mysql.sock
user=mysql
character-set-server=utf8mb4
default-storage-engine=INNODB
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
max_connections = 1000
max_connect_errors = 1200
max_allowed_packet = 128M
explicit_defaults_for_timestamp = true
query_cache_size = 0
query_cache_type = 0
log_error = /data/mysql/log/error.log
slow_query_log = 1
slow_query_log_file = /data/mysql/log/slow.log
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 5
long_query_time = 8
log_slow_slave_statements = 1
min_examined_row_limit = 100
expire_logs_days = 5
tmpdir = /tmp
innodb_buffer_pool_size = 128M

[mysqld_safe]
log-error=/data/mysql/log/mysqld.log
pid-file=/data/mysql/mysqld.pid
# 同步数据
/applications/mysql/bin/mysql_install_db --basedir=/applications/mysql/ --datadir=/data/mysql/data/ --user=mysql

配置并启动

cp /applications/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld 
vim /etc/init.d/mysqld
# 修改以下两行
basedir=/applications/mysql
datadir=/data/mysql/data
# 查看是否启动
netstat -tunlap | grep mysql
# 添加服务并设置为开机自启动
chkconfig --add mysqld
chkconfig mysqld on

初始化数据库

/applications/mysql/bin/mysql_secure_installation
-- 设置用户密码
alter user 'root'@'localhost' identified by '123456';
-- 允许root远程访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Django

配置pip3源

mkdir /root/.pip
touch /root/.pip/pip.conf
echo '[global]' >> /root/.pip/pip.conf
echo 'trusted-host=mirrors.aliyun.com' >> /root/.pip/pip.conf
echo 'index-url=https://mirrors.aliyun.com/pypi/simple/' >> /root/.pip/pip.conf

创建虚拟环境安装依赖

# PublisherPro,一个支持MD轻量级的CMS程式.
git clone https://gitee.com/bluemiaomiao/PublisherPro.git
pip3 install virtualenv
cd PROJECT_DIR
virtualenv venv
source venv/bin/activate
pip3 install -r requestments.txt
pip3 install uwsgi
mkdir log
mkdir script
touch PublisherPro/script/uwsgi.pid
touch PublisherPro/script/uwsgi.status
vim uwsgi.ini

修改项目配置

# PROJECT_DIR/PROJECT_NAME/settings.py
# 设置为生产环境
DEBUG = False
# 配置数据库
DATABASES = {
 'default': {
  'ENGINE': 'django.db.backends.mysql',
  'NAME': 'publisher_pro',
  'USER': 'pubpro',
  'PASSWORD': 'bluemiaomiao',
  'HOST': '192.168.1.203',
  'PORT': '3306',
  'OPTIONS': {'init_command': 'SET default_storage_engine=INNODB;'},
 }
}
# 配置静态文件相关
# STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

创建数据库和用户

CREATE DATABASE `publisher_pro` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
CREATE USER `pubpro`@`localhost` IDENTIFIED BY 'bluemiaomiao' PASSWORD EXPIRE NEVER;
CREATE USER `pubpro`@`%` IDENTIFIED BY 'bluemiaomiao' PASSWORD EXPIRE NEVER;
GRANT All ON `publisher\_pro`.* TO `pubpro`@`%`;

同步数据库

./venv/bin/python3 manage.py makemigrations
./venv/bin/python3 manage.py migrate
./venv/bin/python3 manage.py createsuperuser
./venv/bin/python3 manage.py collectstatic

uwsgi

配置文件内容

# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/applications/website/PublisherPro
# 指定项目的application
module=PublisherPro.wsgi:application
# 指定sock的文件路径  
socket=/applications/website/PublisherPro/script/uwsgi.sock
# 进程个数  
workers=5
pidfile=/applications/website/PublisherPro/script/uwsgi.pid
# 状态文件
stats=/applications/website/PublisherPro/script/uwsgi.status
# 指定IP端口  
http=0.0.0.0:8000
# 指定静态文件
static-map=/static=/applications/website/PublisherPro/static
# 启动uwsgi的用户名和用户组
uid=pubpro
gid=pubpro
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/applications/website/PublisherPro/log/uwsgi.log

创建用户和组并修改权限

# 创建用户
useradd pubpro -s /sbin/nologin -M
# 检查结果
id pubpro
# 修改权限
chown -R pubpro.pubpro /applications/website/PublisherPro/
# 检查结果
ll -d /applications/website/PublisherPro/

测试Django应用

# 启动应用
uwsgi --ini uwsgi.ini
# 重载应用
uwsgi --reload script/uwsgi.pid
# 状态信息
uwsgi --connect-and-read script/uwsgi.status
# 停止应用
uwsgi --stop script/uwsgi.pid

Nginx

server {
 listen 80;
 server_name 192.168.2.108;
 access_log /var/log/nginx/access.log main;
 charset utf-8;
 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;
 error_page 404 /404.html;
 error_page 500 502 503 504 /50x.html;

 # 指定项目路径uwsgi
 location / {
    # 导入一个Nginx模块他是用来和uWSGI进行通讯的
  include uwsgi_params; 
    # 设置连接uWSGI超时时间
  uwsgi_connect_timeout 30; 
    # 指定uwsgi的sock文件所有动态请求就会直接丢给他
  uwsgi_pass unix:/data/PublisherPro/script/uwsgi.sock; 
 }

 # 指定静态文件路径
 location /static/ {
  alias /data/PublisherPro/static;
  index index.html index.htm;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python安装Imaging报错:The _imaging C module is not installed问题解决方法
Aug 22 Python
Python内置函数的用法实例教程
Sep 08 Python
Python基于分水岭算法解决走迷宫游戏示例
Sep 26 Python
Python使用PIL模块生成随机验证码
Nov 21 Python
Tensorflow实现卷积神经网络用于人脸关键点识别
Mar 05 Python
tensorflow实现KNN识别MNIST
Mar 12 Python
Python聊天室程序(基础版)
Apr 01 Python
浅谈django rest jwt vue 跨域问题
Oct 26 Python
Python装饰器的应用场景代码总结
Apr 10 Python
解决Keras使用GPU资源耗尽的问题
Jun 22 Python
python 实现非极大值抑制算法(Non-maximum suppression, NMS)
Oct 15 Python
解决Django transaction进行事务管理踩过的坑
Apr 24 Python
TensorFlow卷积神经网络之使用训练好的模型识别猫狗图片
Mar 14 #Python
Python通过TensorFlow卷积神经网络实现猫狗识别
Mar 14 #Python
python3实现钉钉消息推送的方法示例
Mar 14 #Python
详解Python做一个名片管理系统
Mar 14 #Python
在Python中使用Neo4j的方法
Mar 14 #Python
浅谈Python中eval的强大与危害
Mar 13 #Python
详解python中init方法和随机数方法
Mar 13 #Python
You might like
谈一谈收音机的高放电路
2021/03/02 无线电
[FAQ]PHP中的一些常识:类篇
2006/10/09 PHP
php分页示例代码
2007/03/19 PHP
PHP 微信支付类 demo
2015/11/30 PHP
php版微信公众平台之微信网页登陆授权示例
2016/09/23 PHP
PHP实现无限极分类的两种方式示例【递归和引用方式】
2019/03/25 PHP
js 实现无干扰阴影效果 简单好用(附文件下载)
2009/12/27 Javascript
jquery 实现上下滚动效果示例代码
2013/08/09 Javascript
jQuery插件multiScroll实现全屏鼠标滚动切换页面特效
2015/04/12 Javascript
详解JavaScript正则表达式中的global属性的使用
2015/06/16 Javascript
jQuery EasyUI 布局之动态添加tabs标签页
2015/11/18 Javascript
深入理解setTimeout函数和setInterval函数
2016/05/20 Javascript
vue v-model动态生成详解
2018/06/30 Javascript
JS+H5 Canvas实现时钟效果
2018/07/20 Javascript
vue 循环加载数据并获取第一条记录的方法
2018/09/26 Javascript
在移动端使用vue-router和keep-alive的方法示例
2018/12/02 Javascript
vue.js中ref和$refs的使用及示例讲解
2019/08/14 Javascript
详解Nuxt.js 实战集锦
2019/11/19 Javascript
js实现鼠标拖拽div左右滑动
2020/01/15 Javascript
python中的对象拷贝示例 python引用传递
2014/01/23 Python
Python多维/嵌套字典数据无限遍历的实现
2016/11/04 Python
python3利用ctypes传入一个字符串类型的列表方法
2019/02/12 Python
使用Python做定时任务及时了解互联网动态
2019/05/15 Python
Pandas+Matplotlib 箱式图异常值分析示例
2019/12/09 Python
Django models文件模型变更错误解决
2020/05/11 Python
详解Python流程控制语句
2020/10/28 Python
CSS3只让背景图片旋转180度的实现示例
2021/03/09 HTML / CSS
欧洲最大的球衣网上商店:Kitbag
2017/11/11 全球购物
西班牙香水和化妆品购物网站:Arenal Perfumerías
2019/03/01 全球购物
绘儿乐产品官方在线商店:Crayola.com
2019/09/07 全球购物
财务主管的岗位职责
2013/12/30 职场文书
信息科学与技术专业求职信范文
2014/02/20 职场文书
外出考察学习心得体会
2016/01/18 职场文书
优秀大学生申请书
2019/06/24 职场文书
JavaScript实现贪吃蛇游戏
2021/06/16 Javascript
Springboot配置suffix指定mvc视图的后缀方法
2021/07/03 Java/Android