作者:雄鹰访谈录 | 来源:互联网 | 2023-05-19 05:16
测试环境:linux centos7下
1、安装uwsgi
python3下安装:
python2下安装:
如果是系统自带的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”监控应用实例
uwsgitop--uwsgi服务器性能查看工具,用法:
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]
http = :9000
#the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /root/workplace/WebSite
# Django's wsgi file
wsgi-file = WebSite/wsgi.py
#env
virtualenv=/root/Envs/website_env#可以执行指定虚拟环境
#static-map
static-map = /static=/root/workplace/WebSite/collected_static #用于查找静态文件
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2
#monitor uwsgi status
stats = 127.0.0.1:9191
# clear environment on exit
vacuum = true
然后放到django项目中
启动uwsgi:
uwsgi --ini path/to/project/uwsgi.ini
然后成功访问到我们的django项目
5、安装Nginx
启动Nginx
systemctl start nginx.service
关闭Nginx
systemctl stop nginx.service
重启
systemctl restart nginx.service
设置开机启动
查看nginx 启动状态
查看是否监听
查看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一致
#添加
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
方法二:添加站点配置
在项目中添加website_nginx.conf
#website_nginx.conf
#the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)所有获得的请求都会转到8001端口,也就是uwsgi邦定的socket端口
}
# configuration of the server
server {
# the port your site will be served on
listen 9000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /root/workplace/WebSite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass 127.0.0.1:8001; #跟uwsgi绑定的socket一样
include /root/workplace/WebSite/uwsgi_params; # the uwsgi_params file you installed
}
}
然后将站点配置软链接到 /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