Setting up Django and your web server with uWSGI and nginx

本文详细介绍了如何使用Django、uWSGI和nginx搭建Web服务器,包括创建Django项目、uWSGI安装配置、nginx安装测试、静态文件部署等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置Django和基于uWSGI和nginx的Web服务器

概念

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

创建Django项目

pip install Django
django-admin.py startproject mysite
cd mysite

使用本地IP及默认8000端口

uWSGI安装和配置

pip install uwsgi

uWSGI测试

创建test.py测试文件

# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2

运行uWSGI

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

通过浏览器访问:http://localhost:8000
浏览器显示 “Hello World”
测试流程:
the web client <-> uWSGI <-> Python

测试Django项目

运行项目:

python manage.py runserver 0.0.0.0:8000

运行uWSGI:

uwsgi --http :8000 --module mysite.wsgi

测试流程:
the web client <-> uWSGI <-> Django

nginx

安装nginx

sudo apt-get install nginx
sudo /etc/init.d/nginx start # start nginx

测试nginx

浏览器访问:http://localhost:80/
显示:Welcome to nginx,代表nginx正常启动
测试流程:
the web client <-> the web server

为nginx配置站点

  • 为项目添加uwsgi_params文件
  • 创建项目配置文件mysite_nginx.conf
# mysite_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)
}
# configuration of the server
server {
    # the port your site will be served on
    listen 8000;
    # 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 /path/to/your/mysite/static; # your Django project's static files - amend as required
    }
    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass django;
        include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}
  • 将配置文件链接到/etc/nginx/sites-enabled
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

部署静态文件

配置静态文件
在项目文件夹下的settings.py中配置静态文件夹路径

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

然后运行:

python manage.py collectstatic

nginx测试:重启nginx

sudo /etc/init.d/nginx restart

测试静态文件是否已经部署
http://example.com:8000/media/media.png

nging->uWSGI->test.py

运行uWSGI:

uwsgi --socket :8001 --wsgi-file test.py

浏览器访问:http://localhost:8000/
测试流程:
the web client <-> the web server <-> the socket <-> uWSGI <-> Python

使用Unix Sockets代替ports

编辑项目配置文件

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)

重启nginx
运行uWSGI

uwsgi --socket mysite.sock --wsgi-file test.py

浏览器访问:http://localhost:8000/
如果因为权限问题无法访问,尝试一下方法运行uWSGI

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)

通过nginx和uWSGI运行Django项目

运行
uwsgi –socket mysite.sock –module mysite.wsgi –chmod-socket=664

配置uWSGI启动文件

创建项目uWSGI启动文件’mysite_uwsgi.ini’

# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
module = project.wsgi
# the virtualenv (full path)
home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true

通过启动文件启动uWSGI:

uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

Emperor mode

# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
# run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data

通过sudo启动uWSGI:

sudo uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data

配置uWSGI开机启动

打开/etc/rc.local,添加一下代码:

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值