blog
article
django
/root/blog
(django) root@nick:~/root/blog$ tree
blog
├── manage.py
├── blog
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── article
├── static
└── templates
下面就按照步骤一步步配置。
在虚拟环境下安装uwsgi pip install uwsgi
。
创建uwsgi.ini
,在/root/blog/blog/uwsgi.ini
下。
将以下内容填入uwsgi.ini文件,项目名和目录请修改为自己的项目。
[uwsgi]
# Django-related settings
# Django项目本地端口
socket = :8000
# 项目根目录位置
chdir = /root/blog/
# wsgi.py文件在项目的中的相对位置
wsgi-file = /blog/wsgi.py
module =blog.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.pid
和uswgi.log
文件。
启动:uwsgi --ini uwsgi.ini
停止:uwsgi --stop uwsgi.pid
重启:uwsgi --reload uwsgi.pid
强制停止:killall -9 uwsgi
这里我们启动uwsgi服务,可以通过ps -ef | grep uwsgi
看到已经有四个uwsgi服务启动。
Ubuntu系统下安装:sudo apt-get install nginx
在服务器上创建目录结构:/var/www/blog/
修改目录权限:sudo chmod 777 /var/www/blog
创建static
目录,注意顺序是先分配权限,再创建目录:mkdir static
这里/root/blog/blog/settings.py
中的部分设置如下
# 如果DEBUG=True -> 使用项目目录下static内的静态文件
# 如果DEBUG=False -> 使用STATIC_ROOT指定目录下的静态文件
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/NickBlog/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 = '/var/www/NickBlog/media/'
收集所有静态文件到static_root指定目录:python manage.py collectstatic
进入目录/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 -v
重启服务:nginx -s reload
停止服务:nginx -s stop
模板/视图/URL/配置
文件,都需要重启uwsgi
服务。Nginx
配置文件,都需要重启Nginx
服务。killall -9 uwsgi;
workon django;
cd /root/blog/blog/;
uwsgi --ini /root/blog/blog/uwsgi.ini;
nginx -s reload;
你是不是已经部署成功了呢?
发现一个问题,在另外一台机器上部署的时候无法成功进入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;
# }
#}
更多文章可以访问我的个人博客:码练