admin 管理员组

文章数量: 887021

参考资料

百度百科-域名绑定服务器IP
百度百科-网站绑定IP
Django官方推荐教程-Nginx+Gunicorn部署Django
CSDN-Nginx+Gunicorn部署Django
CSDN-nginx+gunicorn部署django项目

用到的工具软件

  • FlashFXP:用于像服务器传输文件
  • Navicat:MySQL数据库管理软件

Django、Gunicorn、Nginx之间的关系

博客园

腾讯云服务器

腾讯云轻量服务器

域名

域名

域名与公网IP绑定

记录管理->添加记录->选择www访问->将你注册号的云服务器的公网IP填写进去->保存->更多操作->开启

打开本地电脑的cmd
ping www.你的域名

服务器环境

  • pip
  • python
  • django
  • gunicorn
  • nginx

修改Django项目的settings文件

DEBUG = False# True

ALLOWED_HOSTS = [
    '你的域名'
    ]

服务器上的安装

Gunicorn

pip3 install gunicorn

Nginx

CSDN-CentOS7.8安装nginx

Gunicorn

官方教程
Gunicorn是纯Python服务脚本,可以用于部署Django,最简单的用法是在你的Django项目目录下执行

gunicorn mysite.wsgi:application --bind 0.0.0.0:8000

如果要杀死Gunicorn进程

killall gunicorn

Nginx

Nginx是一个成熟的web服务,可以提供负载均衡和高并发处理。其实只使用Gunicorn也可以发布网站,但是推荐Nginx+Gunicorn这种方式发布Django网站项目

到/etc/nginx/
修改nginx.conf

worker_processes 1;
user root; # 执行启动nginx命令的用户
# user nobody nogroup;
# 'user nobody nobody;' for systems with 'nobody' as a group instead
error_log  /var/log/nginx/error.log warn; # 日志输出位置
pid /var/run/nginx.pid;

events {
  worker_connections 1024; # increase if you have lots of clients
  accept_mutex off; # set to 'on' if nginx worker_processes > 1
  # 'use epoll;' to enable for Linux 2.6+
  # 'use kqueue;' to enable for FreeBSD, OSX
}

http {
  include /etc/nginx/mime.types;
  # fallback in case we can't determine a type
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log combined;
  sendfile on;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

  # keepalive_timeout 65;
  include /etc/nginx/conf.d/*.conf;

  upstream app_server {
    # fail_timeout=0 means we always retry an upstream even if it failed
    # to return a good HTTP response

    # for UNIX domain socket setups
    server unix:/usr/PyCodes/Mysite/gunicorn.sock fail_timeout=0;
    # server 42.192.144.249:8000 fail_timeout=0;

    # for a TCP configuration
    # server 0.0.0.0:8000 fail_timeout=0;
  }

  server {
    # if no Host match, close the connection to prevent host spoofing
    listen 80 default_server;
    return 444;
  }

  server {
    # use 'listen 80 deferred;' for Linux
    # use 'listen 80 accept_filter=httpready;' for FreeBSD
    listen 80;
    client_max_body_size 4G;

    # set the correct host(s) for your site
    server_name afflatushare.com www.afflatushare.com; # 你的域名或IP(带www和不带www),多个域名或IP可用空格间隔

    keepalive_timeout 5;

    # path for static files
    root /usr/PyCodes/Mysite;# 你的项目位置

    location / {
      # checks for static file, if not found proxy to app
      try_files $uri @proxy_to_app; # 项目文件位置
    }

    location @proxy_to_app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $http_host;
      # we don't want nginx trying to do something clever with
      # redirects, we set the Host: header above already.
      proxy_redirect off;
      proxy_pass http://0.0.0.0:8000; # gunicorn启动Django项目的端口
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /path/to/app/current/public;
    }
  }
}

启动Nginx

systemctl start nginx

重启

nginx -s reload

看报错

开发者工具->Console

我的网站

http://www.afflatushare/

本文标签: 腾讯 服务器 网站