Django快速部署代码,采用nginx+gunicorn,在此纪录一下
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile on;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/xml text/css
text/comma-separated-values
text/javascript
application/x-javascript
application/atom+xml;
# Configuration containing list of application servers
upstream app_servers {
#server 0.0.0.0:8000;
server 127.0.0.1:8000;
# ..
# .
}
# Configuration for Nginx
server {
# Running port
listen 80;
# Settings to serve static files
location /static {
include /etc/nginx/mime.types;
# Example:
# root /full/path/to/application/static/file/dir;
alias /home/pyProject/RichardBlog/collect_static;
#静态文件路径,很重要
}
# Serve a static file (ex. favico)
# outside /static directory
location = /favico.ico {
root /app/favico.ico;
}
# Proxy connections to the application servers
# app_servers
location / {
proxy_pass http://app_servers;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
deploydjango.sh
sudo apt-get -y install python-pip
sudo apt-get -y install nginx
sudo apt-get -y install wget
sudo pip install gunicorn
sudo cp nginx.conf /etc/nginx/nginx.conf
sed -i -e 's?{
{staticfilepath}}?'$PWD'/'$1'/collectstatic?g' nginx.conf
sudo service nginx restart
./manage.py collectstatic
gunicorn "$1.wsgi"
两个文件保存到项目根目录,chorm赋予读写权限,运行
sudo ./deploydjango.sh XXX 即可,
需要注意的是静态文件的部署路径。文中已经进行注释。