本例是在anaconda虚拟环境webenv下面建立的django项目,名字叫webdev。
激活虚拟环境,在该环境里面安装uwsgi。
conda activate webenv
conda install uwsgi
在django项目webdev目录里创建 uwsgi.ini文件,编辑内容如下。如果遇到目录不存在,比如/var/log/uwsgi,那么自行创建一下。
[uwsgi]
# /path/to/your/project (full path)
chdir = /data/webdev
# Django's wsgi file (locate in project.wsgi.py)
module = webdev.wsgi:application
# the virtualenv (full path)
home = /anaconda3/envs/webenv
# set an environment variable
env = DJANGO_SETTINGS_MODULE=webdev.settings
# process-related settings
# the socket (IP:port or /path/to/your/project/mysite.sock)
socket = 127.0.0.1:8001
# master
master=True
# max number of worker processes
processes = 10
# vacuum means clear environment on exit
vacuum=True
# respawn processes taking more than 20 seconds
harakiri = 20
# respawn processes after serving 5000 requests
max-requests = 5000
# background the process & log
daemonize = /var/log/uwsgi/webdev.log
# /path/to/pid.file (full path)
#pidfile = /tmp/webdev.pid
然后初始化。
uwsgi --ini uwsgi.ini
编辑nginx配置文件 /etc/nginx/conf.d/webdev.conf,内容如下。注意里面的 static 、 media 等配置,涉及到django的 STATIC_ROOT目录。
# the upstream component nginx needs to connect to
upstream webdev {
# 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 192.168.27.7 # substitute your machine's IP address or FQDN
charset utf-8;
access_log /var/log/nginx/access.log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /data/webdev/media; # your Django project's media files - amend as required
}
# Django static
location /static {
alias /data/webdev/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
include uwsgi_params; # the uwsgi_params file you installed
uwsgi_pass webdev;
uwsgi_connect_timeout 20;
}
}
然后重启nginx,访问 IP:8000 验证结果。