Django+Uwsgi+Nginx

一、数据库准备

yum install mariadb-server -y
systemctl start mariadb
 
监听端口
netstat -lntup
 
mysql 进入
grant all on *.* to mydb@'192.168.13.126' identified by ‘123456’ 授权用户
 
给mysql 本地 用户 设置密码
mysqladmin -uroot password '123'
mysql -uroot -p123
 
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pymysql

二、项目准备

mezzanine 夹层下载
pip install mezzanine
创建项目
mezzanine-project myproject
查看位置
which mezzanine-project 
查看文件类型
file /usr/bin/mezzanine-project 
创建一个项目
mezzanine-project myproject 
查看树形目录
tree myproject 
切换目录到项目下
cd myproject
创建数据库
python manage.py createdb 相当于 makemigration、 migrate 两步
运行项目
python manage.py runserver 10.0.0.66:80

三、动静分离架构

nginx 负责处理静态资源,将静态文件集中起来放在同一个目录下,一般在static
将所有静态资源放到static目录下:python manage.py collectstatic
 
uwsgi 处理 动态资源

(一)uwsgi 开启服务

Django自带运行项目的runserve 只能在本地局域网测试运行

1.安装uwsgi

pip install uwsgi

2.启动uwsgi

两种启动方式 :一种 敲命令, 一种 配置文件
centos安装压力测试的工具包:
yum install httpd-tools -y
 压力测试
which ab
ab -n 100000 -c 100 http://10.0.0.66:8000/gallery/
(1)命令启动uwsgi(首先要切换到项目目录下)
uwsgi --http 10.0.0.66:8000 --file mproject/wsgi.py --static-map=/static=/opt/myproject/static

参数说明:

--http 这个就和runserver一样指定IP 端口
--file 这个文件就里有一个反射,如果你在调用他的时候没有指定Web Server就使用默认的
-- static 做一个映射,指定静态文件

测试:访问http://10.0.0.66:8000/

(2)配置文件启动项目

编辑uwsgi.ini文件:

# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/opt/myproject/
# 指定项目的application
module=myproject.wsgi:application
# 指定sock的文件路径       
socket=/opt/myproject/uwsgi.sock
# 进程个数       
workers=5
pidfile=/opt/myproject/uwsgi.pid
# 指定IP端口       
http=10.0.0.66:8000
# 指定静态文件
static-map=/static=/opt/myproject/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true    #master 主进程管理work进程的数量,work 进程执行
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true    #vacuum = true 项目停止自动删除锁文件
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/opt/myproject/uwsgi.log
 
de 删除一个单词
x删除当前下标位置的内容
补充知识
 
启动uwsgi:
uwsgi --ini /opt/mycms/uwsgi.ini

(二)Nginx

优点:
轻量的 web服务器软件 高性能的web server
c语言 代码简洁 配置简单 稳定 性能好 占用资源少 比 apache更好
反响代理/电子邮件代理服务器
占用内存少 并发性能好
 

1.安装 nginx

yum install nginx -y

 2.修改配置

vim /etc/nginx/nginx.conf
访问非静态请时 给uwsgi
静态请求 nginx自己处理(static)

3.修改默认配置文件/etc/nginx/conf.d

grep -Ev '^$|#' /etc/nginx/nginx.conf.default >/etc/nginx/nginx.conf # 过滤空行 和 注释
vim /etc/nginx/nginx.conf #编辑配置文件
server { # 这个server标识我要配置了
  listen 80; # 我要监听那个端口
  server_name 10.0.0.66 ; # 你访问的路径前面的url名称 
  access_log /var/log/nginx/access.log main; # Nginx日志配置
  charset utf-8; # Nginx编码
  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 / { # 这个location就和咱们Django的url(r'^admin/', admin.site.urls),
    include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
    uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
    uwsgi_pass unix:/opt/myproject/uwsgi.sock; # 指定uwsgi的sock文件所有动态请求就会直接丢给他
  }

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

}

主要改:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server { 
        listen 8000;
        server_name 10.0.0.66;
        charset utf-8;
        # 指定项目路径uwsgi
        location / { 
           include uwsgi_params;# 导入一个Nginx模块他是用来和uWSGI进行通讯的
           uwsgi_connect_timeout 30;# 设置连接uWSGI超时时间
           uwsgi_pass unix:/opt/myproject/uwsgi.sock;
        }
     # 指定静态文件路径 location
/static/ { alias /opt/myproject/static/; index index.html index.htm; } } }

4.nginx测试

nginx -t 

5.启动 nginx

systemctl start nginx 
 
 nginx命令补充:
# 启动Nginx通过Nginx访问
/etc/init.d/nginx start
/etc/init.d/nginx stop
 
# 这里有个命令configtest,Nginx配置是重启生效的,如果你修改完了,不知道对    不对又担心影响其他人可以使用它测试
/etc/init.d/nginx configtest
 
# 如果是生产环境的话Nginx正在运行,就不要直接stop start 或者 restart  直接reload就行了
# 对线上影响最低
/etc/init.d/nginx reload 
 
 
 
原文地址:https://www.cnblogs.com/qiaoqianshitou/p/9548372.html