一. uwsgi
- 安装
- 检验
- 配置uwsgi.ini
1. 安装
pip3 install uwsgi
2. 检验
- 方法一(uwsgi启动文件):
- test.py内容如下:
-
def application(env, start_response): start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)]) return [b"Hello World"]
-
uwsgi --http 0.0.0.0:8000 --wsgi-file testTask/tests.py
-
打开浏览器http://127.0.0.1:8000/
- 方法二(uwsgi启动django项目):
-
uwsgi --http :8000 --file dj_test/wsgi.py
- 如果部署项目uwsgi+django,到这里就结束了,如果需要配合nginx,再接着往下看。
-
3. 配置uwsgi.ini
- 同django的manage.py为同一级目录创建uwsgi.ini
-
[uwsgi] http = :9000 socket = 127.0.0.1:8001 chdir = /Users/conan/vir_env/AutoTest wsgi-file = /Users/conan/vir_env/AutoTest/AutoTest/wsgi.py module = AutoTest.wsgi:application master = true processes = 4 threads = 2 chmod-socket=664 vacuum=true daemonize = /Users/conan/vir_env/AutoTest/uwsgi.log
参数解释:
-
- http 用户访问端口
- chdir 为django 项目路径,该路径下面有manage.py 文件
- wsgi-file django 自带的wsgi.py 文件
- socket 8001 与下面要配置的nginx的端口要保持一致, 因为uwsgi与nginx 通过socket协议8001端口通信
-
二. nginx
- 安装
- 配置nginx.conf
- 启动,重启,关闭
1. 安装
brew install nginx nginx -v # 查看安装版本号 nginx -t # 安装路径
2. 配置nginx.conf
- 同django的manage.py为同一级目录创建nginx.conf
-
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name 127.0.0.1; charset utf-8; access_log /Users/zd/Documents/AutoTest/nginx_access.log; error_log /Users/zd/Documents/AutoTest/nginx_error.log; client_max_body_size 75M; location /static { alias /Users/zd/Documents/AutoTest/static; } location / { root /Users/zd/Documents/AutoTest; include /usr/local/etc/nginx/uwsgi_params; uwsgi_pass 127.0.0.1:8001; } } include servers/*; }
3. 启动,重启,关闭
# 找nginx 安装路径 which nginx cd /usr/local/bin/nginx #重启 nginx -s reload #关闭 nginx -s stop. 或者 kill -9 nginx #是否关闭 ps aux|grep nginx
三. uwsgi+nginx 启动Django项目
1 nginx -t 2 cd /usr/local/etc/nginx 3 把mime.types 文件copy到django 项目与manage.py 同一级
1 uwsgi --ini uwsgi.ini 2 sudo nginx -c nginx.conf #根据报错信息,缺啥文件补充啥,补完之后,重复该命令
浏览器访问:127.0.0.1:9000/index/
四. 加载静态资源
在Django setting.py里加
STATIC_ROOT = os.path.join(BASE_DIR, ‘static‘)
终端执行命令
python3 manage.py collectstatic #收集静态文件
url.py(例如:AutoTest/AutoTest/url.py)
1 from django.conf import settings 2 urlpatterns = [ 3 url(r‘^admin/‘, include(admin.site.urls)), 4 url(r‘‘, include(‘testTask.urls‘)), 5 url(r‘^static/(?P<path>.*)$‘, ‘django.views.static.serve‘, { ‘document_root‘: settings.STATIC_ROOT,}), 8 ]
原文地址:https://www.cnblogs.com/zhang-dan/p/11603844.html
时间: 2024-10-07 01:07:19