本文用以记录基于 Python + Django 开发网站时,初始化项目流程以及自动部署流程。
Python + Django + Nginx + Gunicorn
网站初始化创建
创建 Virtualenv
1 2 3 4 5
   |  cd path/to/directory/<../web_root> python -m venv virtualenv
  source virtualenv/bin/activate
 
  | 
 
在 Virtualenv 下安装Django, Gunicorn:
1
   | pip install django, gunicorn
   | 
 
新建 Django Project 并上传到 Git 库
1 2 3 4 5 6 7 8 9 10
   | django-admin.py startproject NewProjectName
 
  cd  NewProjectName git init touch README.md git remote add [shortname(origin)] [url] git add . git commit -m 'comments'  git push -u origin master
   | 
 
新建应用命令:python manage.py startapp appName
服务器初始化环境和自动部署
服务器 Required packages:
- nginx
 
- Python 3.6
 
- virtualenv + pip
 
- Git
 
1 2 3 4 5
   | server$ sudo add-apt-repository ppa:deadsnakes/ppa server$ sudo apt update server$ sudo apt install nginx git python36 python3.6-venv
  server$ sudo systemctl start nginx
   | 
 
初始化部署(利用 fabric 实现自动化)
S1. 本地编写自动部署脚本并执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | cd NewProjectName pip install fabric3
  mkdir deploy_tools cd deploy_tools
  touch fabfile.py
 
  touch nginx.template.conf
 
  touch gunicorn-systemd.template.service
 
 
  fab <function_name(deploy)>:host=SERVER_ADDRESS
   | 
 
自动部署脚本一般要实现:  
- 从 Git 库 pull 最新的源代码  
 
- 创建或更新虚拟环境  
 
- 创建或更新环境变量  
 
- 更新静态文件  
 
- 更新数据库
 
S2. 在服务器上修改 Nginx 和 Gunicorn 的配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13
   |  server$ cat ./deploy_tools/nginx.template.conf \     | sed "s/DOMAIN/<hostname>/g" \     | sudo tee /etc/nginx/sites-available/<hostname>
 
  server$ sudo ln -s /etc/nginx/sites-available/<hostname> \     /etc/nginx/sites-enabled/<hostname>
 
  server$ cat ./deploy_tools/gunicorn-systemd.template.service \     | sed "s/DOMAIN/<hostname>/g" \     | sudo tee /etc/systemd/system/gunicorn-<hostname>.service
 
  | 
 
备忘一下 Nginx 和 Gunicorn 的常用配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
   | # nginx.template.conf server {     listen 80;     server_name DOMAIN/IP;
      location /static {         alias /path/to/project/root/static;     }
      location / {         proxy_pass http://unix:/tmp/DOMAIN/IP.socket;         proxy_set_header Host $host;     } }
  # gunicorn-systemd.template.service [Unit] Description=Gunicorn server for DOMAIN/IP
  [Service] Restart=on-failure User=yourusername WorkingDirectory=/path/to/project/root/ EnvironmentFile=/path/to/project/root/.env
  ExecStart=/path/to/gunicorn/bin/gunicorn \     --bind unix:/tmp/DOMAIN/IP.socket \     PROJECT_NAME.wsgi:application
  [Install] WantedBy=multi-user.target
   | 
 
S3. 重新加载 nginx 配置并开启 gunicorn 服务:
1 2 3 4 5 6 7 8 9
   |  server$ sudo systemctl daemon-reload
 
  server$ sudo systemctl reload nginx
 
  server$ sudo systemctl enable gunicorn-<hostname> server$ sudo systemctl start gunicorn-<hostname>
 
  | 
 
自动更新部署
在本地开发源代码更新,push到Git后:
S1. 本地执行自动部署脚本:
1 2
   | cd deploy_tools fab <function_name(deploy)>:host=SERVER_ADDRESS
   | 
 
S2. 服务器端重启gunicorn:
1 2
   | server$ sudo systemctl daemon-reload server$ sudo systemctl restart gunicorn-<hostname>
   | 
 
备忘 nginx 和 gunicorn 的日志信息:
nginx(网站访问日志): /var/log/nginx/access.log error.log
gunicorn 状态:sudo systemctl status gunicorn-<hostname>
gunicorn(网站运行日志): sudo journalctl -u gunicorn-<hostname>
遇到的坑们
重装 Nginx
有些时候服务器上已有的 Nginx 有问题但不清楚之前的配置啊啥的,可以考虑重装 Nginx:
这时候一定要彻底删除所有配置文件!有一次没删干净,重装的 Niginx 还保留着之前的配置就很凉,不管怎么改 sites-enabled 下的配置文件都始终显示欢迎界面,后来发现是 /etc/nginx 下的 nginx.conf 直接被人改了。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
   |  $ sudo apt-get --purge remove nginx
  $ sudo apt-get autoremove
  $ dpkg --get-selections|grep nginx nginx                       install nginx-common                    install nginx-core                  install
  $ sudo apt-get --purge remove nginx $ sudo apt-get --purge remove nginx-common $ sudo apt-get --purge remove nginx-core
 
  $ ps -ef |grep nginx root      7875  2317  0 15:02 ?        00:00:00 nginx: master process /usr/sbin/nginx www-data  7876  7875  0 15:02 ?        00:00:00 nginx: worker process www-data  7877  7875  0 15:02 ?        00:00:00 nginx: worker process www-data  7878  7875  0 15:02 ?        00:00:00 nginx: worker process www-data  7879  7875  0 15:02 ?        00:00:00 nginx: worker process stephen   8321  3510  0 15:20 pts/0    00:00:00 grep --color=auto nginx $ sudo kill  -9  7875 7876 7877 7879
 
  $ sudo  find  /  -name  nginx* $ sudo rm -rf file
 
  $ sudo apt-get update $ sudo apt-get install nginx
 
  | 
 
/etc/nginx/sites-enabled 下的软链接
有时候部署完了之后却被拒绝连接,可以尝试删除 /etc/nginx/sites-enabled 下使用的软链接,直接使用配置文件:
1
   | $ sudo cp /your/path/djangopro_nginx.conf   /etc/nginx/sites-enabled/
   |