欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-24 13:28:42
什么是uwsgi? uwsgi是一个web服务器,它实现了wsgi协议、uwsgi、http等协议。nginx中httpuwsgimodule的作用是与uwsgi服务...

什么是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;
 }
}

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