前提条件:
- nginx
- django
- uwsgi
安装和配置uwsgi
在虚拟环境下安装uwsgi
pip3 install uwsgi
创建uwsgi.ini
touch uwsgi.ini
配置uwsgi.ini文件
将以下内容填入uwsgi.ini文件,项目名和目录请修改为自己的项目。
[uwsgi]
# Django-related settings
# Django项目本地端口
# http = :8000 # 直接访问的话使用http请求
socket = :8000
# 项目根目录位置
chdir = /djangoweb/
# wsgi.py文件在项目的中的相对位置
wsgi-file = /djangoweb/uwsgi.py
module = djangoweb.wsgi
# 进程设置,无需变动
# master
master = true
# maximum number of worker processes
# 启动4个uwsgi进程
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
pidfile=uwsgi.pid
daemonize=uwsgi.log
启动uwsgi
uwsgi在哪个目录启动,就会在哪个目录生成uwsgi.pid和uswgi.log文件。
- 启动:uwsgi --ini uwsgi.ini
- 停止:uwsgi --stop uwsgi.pid
- 重启:uwsgi --reload uwsgi.pid
- 强制停止:killall -9 uwsgi
安装和配置Nginx
在服务器上创建目录结构:/blog/
修改目录权限:sudo chmod 777 /var/www/blog
创建static目录,注意顺序是先分配权限,再创建目录:mkdir static
这里/blog/settings.py中的部分设置如下
# 如果DEBUG=True -> 使用项目目录下static内的静态文件
# 如果DEBUG=False -> 使用STATIC_ROOT指定目录下的静态文件
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/static/'
# 设置静态文件查找目录
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
# 如果是home路径下则需要先设置目录权限
# 在模板中使用 fileObj.fileFieldName.url 代表网络可访问的资源路径
MEDIA_URL = '/media/' # 代表访问media的url路径,例如 127.0.0.1/media/1.png
# 无论是否debug,都会访问此路径下的media资源(包括上传和访问)
MEDIA_ROOT = '/media/'
收集所有静态文件到static_root指定目录:python manage.py collectstatic
配置Nginx
进入目录/etc/nginx/sites-enabled/中可以看到一个default文件。
修改default文件为如下内容:
# site_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:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
# 监听主机的端口
listen 80;
# the domain name it will serve for
# server_name .liqian.ink; # 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 /var/www/blog/media; # your Django project's media files - amend as required
}
# 设置静态文件目录
location /static {
alias /var/www/blog/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 uwsgi_params; # the uwsgi_params file you installed
}
}
nginx 启动服务
- 启动服务:nginx
- 查看版本:nginx -v
- 重启服务:nginx -s reload
- 停止服务:nginx -s stop
注意事项
每次修改了Django项目中的模板/视图/URL/配置文件,都需要重启uwsgi服务。
修改Nginx配置文件,都需要重启Nginx服务。
后续
发现一个问题,在另外一台机器上部署的时候无法成功进入django进程。
将/etc/nginx/nginx.conf内容设置为如下内容后,重启成功:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}