测试环境:linux centos7下

1、安装uwsgi

python3下安装:

pip3 install uwsgi

python2下安装:

pip install uwsgi

如果是系统自带的python2.7环境下安装的话,有可能会出错:

Command "/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-
jjOBXy/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-3cX7u0-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-jjOBXy/uwsgi/

这时候需要我们先安装一个python开发包:

yum install -y python-devel

然后就可以安装了

2、测试 uwsgi

#python 3.x
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] #Python 3.x 需要 返回字节字符串 #python 2.x
#def application(env, start_response):
# start_response('200 OK', [('Content-Type','text/html')])
# return ["Hello World"]

test.py

运行:

uwsgi --http :8000 --wsgi-file test.py

3、添加并发和监控

并发:

uWSGI 默认启动一个单独的进程和一个单独的线程

我们可以通过 --processes 选项或者 --threads (或者两个选项都使用)来增加更多的进程或者线程

uwsgi --http :8000 --wsgi-file test.py --master --processes 4 --threads 2

将会产生 4 个进程(每个进程 2 个线程),一个主进程(当你的进程死掉时会重新 spawn 一个新的)以及 HTTP 路由器

 监控:

知道发生了什么在生产环境中是极其重要的。stats 子系统允许你 用 JSON 输出 uWSGI 的内部数据:

uwsgi --http :8000 --wsgi-file test.py  --master --processes 4 --threads 2 --stats 127.0.0.1:8181

用 “uwsgitop”监控应用实例

pip isntall uwsgitop

uwsgitop--uwsgi服务器性能查看工具,用法:

uwsgitop 127.0.0.1:8181

3、连接Django和uwsgi

先测试Django项目本身是可运行的:

python manage.py runserver 0.0.0.0:8001

倘若django 项目没问题,我们就可以把uwsgi与Django连接起来

进入到django项目中,以uwsgi启动django

 uwsgi --http :8001 --module WebSite.wsgi

4、编写uwsgi配置文件[uwsgi]

demo

[uwsgi]

socket = 127.0.0.1:8000

chdir = /home/zhi_hu_04

wsgi-file = zhi_hu/wsgi.py

home = /home/mysite_venv

master = true

processes = 4

threads = 2

vacum = true

daemonize=/var/log/uwsgi.log #使进程在后台运行,并将日志打到指定的日志文件

然后放到django项目中

启动uwsgi:

uwsgi --ini path/to/project/uwsgi.ini

然后成功访问到我们的django项目

39.108.132.2xx:9000

5、安装Nginx

yum -y install nginx

启动Nginx

systemctl start nginx.service

关闭Nginx

systemctl stop nginx.service

重启

systemctl restart nginx.service

设置开机启动

systemctl enable nginx

查看nginx 启动状态

systemctl status nginx

查看是否监听

 ss -tnl | grep 80 

查看nginx进程命令

 ps -ef | grep  nginx

pkill -9 nginx

nginx配置

方法一:直接修改nginx.conf:

 server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html; # Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#添加
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8001; #要跟uwsgi中的绑定的socket一致
}
#静态文件

location /static {

alias /home/www/library_system/static/;

}

#日志

access_log /var/log/nginx_access.log;

error_log /var/log/nginx_error.log;

#添加

        error_page 404 /404.html;
location = /40x.html {
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

方法二:添加站点配置

在项目中添加website_nginx.conf

demo

server {
#侦听80端口
listen 80;
#网站域名
server_name www.xxx.com xxx.com www.xxx.xyz xxx.xyz;
#rewrite 规则 如可将www.zhihu086.com 请求转发至 www.zhihu086.com/bbb/
# rewrite ^/$ /bbb/ break;
charset utf-8;
#上传最大限制75M
client_max_body_size 75M; #你的Django项目的媒体文件路径
location /media {
alias /home/zhi_hu_04/media;
} # 静态文件路径
location /static {
alias /home/zhi_hu_04/static;
} # 将动态请求转发到uwsgi
location / {
#一定要和uwsgi的对应
uwsgi_pass 127.0.0.1:8000;
# uwsgi_params文件要新建写入配置
include uwsgi_params;
}
}

然后将站点配置软链接到 /etc/nginx/conf.d 目录下

或者,直接 放到/etc/nginx/conf.d

ln -s /root/workplace/WebSite/website_nginx.conf /etc/nginx/conf.d/website_nginx.conf

最后:

 copy  /etc/nginx下的uwsgi_params 文件 到项目中 

重启nginx  

启动uwsgi.ini

6、Django静态文件收集

把Django所有静态文件收集到同一个static中,不然访问Django的admin页面会找不到静态文件。在django的setting文件中添加:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

然后运行:

python manage.py collectstatic

最新文章

  1. Git避免输入用户名和密码
  2. Android Studio快捷键每日一练(5)
  3. Linux下C语言高手成长路线(转载)
  4. php扩展开发初探
  5. 没有document.getElementByName
  6. HBase eclipse开发环境搭建
  7. "浏览器端" 使用 commonjs 模块规范开发网页应用,像开发 node 那样开发网页应用
  8. Tp5.0 PHPMailer邮件发送
  9. ubuntu工作常用命令及需要留意的点汇总
  10. MySQL执行原理,逻辑分层、更改数据库处理引擎
  11. java读取各种类型文件
  12. ubuntu 解决TXT文本的乱码的问题。
  13. 关系操作符 < > = == <= >= !=
  14. 搜索+剪枝——POJ 1011 Sticks
  15. The <classpath> or <modulepath> for <junit> must include junit.jar if not in Ant's own classpath
  16. MyEclipse中快速查看错误
  17. Letter Combinations of a Phone Number - LeetCode
  18. maven build 失败
  19. htaccess附录:正则表达式、重定向代码
  20. GitHub上最热门的11款开源安全工具

热门文章

  1. input checkbox 选中问题
  2. golang Time to String
  3. 点击edittext并显示其内容
  4. tensorflow 保存训练模型ckpt 查看ckpt文件中的变量名和对应值
  5. serv-u ftp服务器搭建
  6. python3----练习题(爬取电影天堂资源,大学排名,淘宝商品比价)
  7. docker实用命名
  8. iOS开发之-- oc 和 swift混编之自建桥接文件
  9. jquery中return false的作用
  10. codevs 5964 [SDOI2017]序列计数