详解Django+uwsgi+Nginx上线最佳实战
时间:2022-04-02 10:23 作者:admin610456
什么是uwsgi?
uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask框架写的程序)通信的一种规范。
WSGI是一种通信协议。 uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。 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
python/' target='_blank'>python3.6.0
上传Python-3.6.0.tar.xz
~$ rz
安装依赖
yum install zlib* gcc openssl openssl-devel libffi-devel -yyum 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 ACCEPTiptables -I INPUT -p tcp -m tcp --dport 8000 -j ACCEPTiptables -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/' target='_blank'>mysqlfind / -name mariadb# 移除全部相关包rpm -qa | grep MySQLrpm -qa | grep mariadb# 添加用户useradd mysql -s /sbin/nologin -M# 解压移动文件tar -xvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gzmv 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/datamkdir -p /data/mysql/log# 手动创建日志文件touch /data/mysql/log/mysqld.log# 修改权限chown -R mysql.mysql /applications/mysqlchown -R mysql.mysql /data/mysql
MySQL配置文件
[client]port=3306socket=/data/mysql/mysql.sock[mysqld]port=3306datadir=/data/mysql/databasedir=/applications/mysqlpid-file=/data/mysql/mysqld.pidsocket=/data/mysql/mysql.sockuser=mysqlcharacter-set-server=utf8mb4default-storage-engine=INNODBcollation-server = utf8mb4_general_ciinit_connect='SET NAMES utf8mb4'max_connections = 1000max_connect_errors = 1200max_allowed_packet = 128Mexplicit_defaults_for_timestamp = truequery_cache_size = 0query_cache_type = 0log_error = /data/mysql/log/error.logslow_query_log = 1slow_query_log_file = /data/mysql/log/slow.loglog_queries_not_using_indexes = 1log_throttle_queries_not_using_indexes = 5long_query_time = 8log_slow_slave_statements = 1min_examined_row_limit = 100expire_logs_days = 5tmpdir = /tmpinnodb_buffer_pool_size = 128M[mysqld_safe]log-error=/data/mysql/log/mysqld.logpid-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/mysqldchmod +x /etc/init.d/mysqld vim /etc/init.d/mysqld
# 修改以下两行basedir=/applications/mysqldatadir=/data/mysql/data
# 查看是否启动netstat -tunlap | grep mysql# 添加服务并设置为开机自启动chkconfig --add mysqldchkconfig 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/.piptouch /root/.pip/pip.confecho '[global]' >> /root/.pip/pip.confecho 'trusted-host=mirrors.aliyun.com' >> /root/.pip/pip.confecho 'index-url=https://mirrors.aliyun.com/pypi/simple/' >> /root/.pip/pip.conf
创建虚拟环境安装依赖
# PublisherPro,一个支持MD轻量级的CMS程式.git clone https://gitee.com/bluemiaomiao/PublisherPro.gitpip3 install virtualenvcd PROJECT_DIRvirtualenv venvsource venv/bin/activatepip3 install -r requestments.txtpip3 install uwsgimkdir logmkdir scripttouch PublisherPro/script/uwsgi.pidtouch PublisherPro/script/uwsgi.statusvim 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# 指定项目的applicationmodule=PublisherPro.wsgi:application# 指定sock的文件路径 socket=/applications/website/PublisherPro/script/uwsgi.sock# 进程个数 workers=5pidfile=/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=pubprogid=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; }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
(责任编辑:admin)