nginx+uwsgi部署django[1.11.8] 到 ubuntu记录

ubuntu18上使用nginx+uwsgi部署django1.11.8

版本请对号入座

  1. 如果你在调试过程中,设置失败,建议先行卸载nginx再重装
    可能在调试中乱改了源文件的代码,所以建议重装再进行调试
	# 卸载
	sudo apt-get --purge remove nginx
	# 安装
	sudo apt-get update
	sudo apt-get install nginx
	#测试nginx配置是否正确
	sudo nginx -t
	# 重启nginx,每次改完nginx参数
	sudo service nginx restart
  1. 安装uwsgi
#对于 Python3.x 版本:
sudo apt-get install python3-dev
sudo apt-get install python3-pip 
sudo pip3 install uwsgi
  1. uwsgi.ini参数的设置
# 这个是项目的绝对路径图,根据对应的项目路径修改
/home/yice/PycharmProjects/YC_Ops

# 任意路径的都可以创建 uwsgi.ini 文件
sudo vim uwsgi.ini
#----------------------------uwsgi.ini内容起始线--------------------------------------
# 以下是 uwsgi.ini的文件内容
# mysite_uwsgi.ini file
[uwsgi]
 
# Django-related settings
# 和nginx的文件的联系端口
socket=:8005

# the base directory (full path) 项目的局对路径 
chdir = /home/yice/PycharmProjects/YC_Ops
# Django's wsgi file
# 注意这里是要设置 项目名称.wsgi
module = YC_Ops.wsgi

# the virtualenv (full path) 这里是虚拟环境的设置
# home = /path/to/virtualenv
 
# process-related settings
# master
master = true
# maximum number of worker processes 最大进程数
processes = 10
# the socket (use the full path to be safe 绝对路径的.sock  !!注意这里是和nginx关联的关键
socket = /home/yice/PycharmProjects/YC_Ops.sock
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum = true
#-----------------------uwsgi.ini内容分割线-------------------------------------
  1. nginx参数的设置
    !!!注意,由于版本的不一样,nginx的设置有所区别,导致启动不成功!
    !!!这里的版本是ubuntu的nginx,我们要修改的文件只有 /etc/nginx/sites-enabled/default
    !!!只需要改 /etc/nginx/sites-enabled/default,如已改过其他文件,建议重装nginx
    以下是default的文件内容设置,对比下面,新增源文件中不存在的部分即可
 21 server {
 22         listen 80 default_server;
 23         listen [::]:80 default_server;
 24 
 25         # SSL configuration
 26         #
 27         # listen 443 ssl default_server;
 28         # listen [::]:443 ssl default_server;
 29         #
 30         # Note: You should disable gzip for SSL traffic.
 31         # See: https://bugs.debian.org/773332
 32         #
 33         # Read up on ssl_ciphers to ensure a secure configuration.
 34         # See: https://bugs.debian.org/765782
 35         #
 36         # Self signed certs generated by the ssl-cert package
 37         # Don't use them in a production server!
 38         #
 39         # include snippets/snakeoil.conf;
 40 
 41         root /var/www/html;
 42 
 43         # Add index.php to the list if you are using PHP
 44         index index.html index.htm index.nginx-debian.html;
 45 
 46         server_name localhost;  # 如果绑定了域名,server_name 域名/服务器IP,本地调试则用localhost
 47 
 48         location / {
 49                 # First attempt to serve request as file, then
 50                 # as directory, then fall back to displaying a 404.
 51                 include uwsgi_params;
 52                 uwsgi_pass 127.0.0.1:8005;   # 这行端口对应uwsgi的端口
 53                 uwsgi_read_timeout 30;       # 设置超时时间,设置太短,会导致异步阻塞报错
 54                 # try_files $uri $uri/ =404;  把该行注释掉,否则访问会出现404
 55         }
 56 
 57         location /static {
 58 
 59         expires 30d;
 60         autoindex on;
 61         add_header Cache-Control private;
 62         alias /home/yice/PycharmProjects/YC_Ops/static/;  # 对应设置静态文件的读取路径,否则项目无法读取静态文件
 63     }
 64 

修改以上的nginx的default的文件后记得要重启nginx, sudo /etc/init.d/nginx restart
  1. 启动项目
    去到刚刚设置的uwsgi.ini路径
    sudo --ini uwsgi.ini

  2. 大功告成!

你可能感兴趣的:(后段开发笔记)