python uwsgi
这里的python+Django环境部署已经完成,然后了解一下python的uwsgi框架。
Python manager.py runserver, 是一款适合开发阶段使用的服务器,只支持最高200的并发,不适合在生产中使用。在生产环境当中,通常会用到apache或者nginx服务器作为Django的生产服务器。以nginx为例子。但是在部署之前,首先要掌握在Django和nginx之间的一个程序,就是wsgi,web服务器网关接口,英文叫, Web Server Gateway Interface 缩写叫WSGI,是Python应用程序或框架和web服务器之间的一种接口。
部署用uwsgi模块,首先安装
pip3 install uwsgi
然后配置环境变量,创建软连接
命令:ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
Django和WSGI的结合
把准备好的Django项目存放到/opt/下
[root@localhost bin]# cp -r /root/Desktop/OurBlog/ /opt/
清除pycharm的配置文件,初始的导入文件
测试
python3 manage.py runserver 0.0.0.0:8000
注意:
systemctl stop firewalld 关闭防火墙
然后尝试使用uwsgi启动Django项目
这里要关注到容易忽视的一个文件
这个是项目的uwsgi文件
uwsgi --http 10.10.16.229:8000 --file Qshop/wsgi.py --static-map=/static=static
uwsgi 模块名称
–http 协议
10.10.16.229:8000 主机端口
–file uwsgi要使用的解析文件,一般django自己创建
–static 静态目录
django+nginx+uwsgi
上面用命令启动了uwsgi,但是体验不太好,现在这里使用ini文件启动uwsgi服务器
在项目同等级目录下创建script目录
然后在当中编写uwsgi.ini文件,内容如下
[uwsgi]
chdir=/opt/OurBlog #项目目录
module=OurBlog.wsgi:application #指定项目的application
socket=/opt/script/uwsgi.sock #指定sock的文件路径
workers=5 #进程个数
pidfile=/opt/script/uwsgi.pid
http=192.168.2.69:8000 #指定IP端口
static-map=/static=/opt/OurBlog/static #指定静态文件
uid=root #用户
gid=root #组
master=true #启用主进程
vacuum=true #自动移除unix Socket和pid文件当服务停止的时候
enable-threads=true #启用线程
thunder-lock=true #序列化接受的内容,如果可能的话
harakiri=30 #设置自中断时间
post-buffering=4096 #设置缓冲
daemonize=/opt/script/uwsgi.log #设置日志目录
然后启动uwsgi服务
Uwsgi --ini uwsgi.ini(脚本名称)
创建uwsgi文件的配置文件目录和文件
编写内容
授权
启动uwsgi
杀死进程重启
安装nginx服务器
直接安装nginx是没有的,所以,需要wget
命令:wget -c https://nginx.org/download/nginx-1.12.2.tar.gz
解压
命令:tar -zxvf nginx-1.12.2.tar.gz && cd nginx-1.12.2
自定义配置
命令:./configure
编译安装
命令:make && make install
创建软连接
命令:ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
启动nginx查看效果
修改nginx的配置文件
进行nginx配置,(配置nginx.conf,配置之前进行备份)
备份
开始配置
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name Qshop; 服务的名称
charset utf-8; 编码格式
access_log logs/host.access.log main; 访问日志
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_log /var/log/nginx/error.log error; 错误日志,默认没有,需要手动创建
location / {
include uwsgi_params; 加载uwsgi_params
uwsgi_connect_timeout 30; 连接的超时时间 不要加冒号不要加冒号不要加冒号
uwsgi_pass unix:/opt/script/uwsgi.sock; uwsgi.sock通讯的文件地址
}
location = /static/{
alias /opt/Qshop/static; 静态文件的目录
index index.html index.htm;
}
杀死当前状态下的所有Nginx服务
Pkill -9 nginx
从新启动
这里如果出现下面错误:
说明你的错误日志没有创建,就需要创建了,到/var/log/下mkdir nginx就行
然后应该就可以访问了