博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django简单博客实战(八)--- CentOS7+Gunicorn+Nginx部署
阅读量:3923 次
发布时间:2019-05-23

本文共 4743 字,大约阅读时间需要 15 分钟。

CentOS7+Gunicorn+Nginx部署Django

步骤

gunicron配置

保证防火墙关闭

保证阿里云服务器的安全组中配置了相应端口号

在这里插入图片描述

  1. 安装gunicron
pip install gunicron
  1. 将gunicron加入INSTALLED_APPS中,并将服务器ip加入ALLOWED_HOSTS中
INSTALLED_APPS = [    'simpleui',    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'haystack',    'blogger',    'comment',    'post',    'ckeditor',    'ckeditor_uploader',	'gunicron', # 加入该行]
ALLOWED_HOSTS = ['IP地址']
  1. 查看防火墙状态,并关闭防火墙
systemctl status firewalld  # 查看防火墙状态systemctl stop firewalld   # 关闭防火墙# systemctl start firewalld # 开启防火墙
  1. gunicron运行指令
gunicorn mypoject.wsgi:application -b 0.0.0.0:8000# mypoject为项目名称,wsgi为项目目录下的wsgi.py文件,application固定写法

如果项目运行后丢失样式,gunicron出现 Not Found 静态文件的情况,在根urls加入如下代码

from django.contrib.staticfiles.urls import staticfiles_urlpatterns  # 加入该行# ... the rest of your URLconf goes here ...urlpatterns += staticfiles_urlpatterns()  # 加入该行
  1. 在外部浏览器中输入 “http://ip:8000/自己定义的url路由”
http://ip地址:8000/index

nginx配置

  1. 安装Nginx
yum install nginx

安装完成后Nginx会自动运行,可以在外部浏览器测试下Nginx是否正常开启

# 输入服务器IPhttp://IP地址

看到"Welcome to nginx",说明正常启动

  1. 删除default
  • 删除 /etc/nginx/sites-enabled/default
  • 创建 /etc/nginx/sites-enabled/项目名

编辑创建的文件

server {    listen 80 default_server;    # listen [::]:80 default_server;    server_name _; # 如果你映射了域名,可以写在这里,如:server_name example.com;    access_log /var/log/nginx/access.log;    error_log /var/log/nginx/error.log;    location / {        proxy_pass http://127.0.0.1:8000; #转发的地址,即Gunicorn运行的地址        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-Proto $scheme;    }    location ~ .*{        proxy_pass http://127.0.0.1:8000;        proxy_set_header    Host              $http_host;        proxy_set_header    X-Real-IP         $remote_addr;        proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;}#   location /static {#       alias /home/xzx/flask_pro/flask_blog/bluelog/bluelog/static/;#       expires 30d; #设置缓存过期时间 #   }}
  1. 设置 /etc/nginx/nginx.conf
http {    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  /var/log/nginx/access.log  main;    sendfile            on;    tcp_nopush          on;    tcp_nodelay         on;    keepalive_timeout   65;    types_hash_max_size 2048;    include             /etc/nginx/mime.types;    default_type        application/octet-stream;    # Load modular configuration files from the /etc/nginx/conf.d directory.    # See http://nginx.org/en/docs/ngx_core_module.html#include    # for more information.    include /etc/nginx/conf.d/*.conf;    include /etc/nginx/sites-enabled/*;  # 添加该行,保证读取 "/etc/nginx/sites-enabled/项目名" 文件    server {       # listen       80 default_server;       # listen       [::]:80 default_server;       # server_name  _;        root         /usr/share/nginx/html;        # Load configuration files for the default server block.        include /etc/nginx/default.d/*.conf;       # location / {       # }    }
  1. Nginx检查语法是否错误
[root]$ nginx -t
  1. 启动Nginx
systemctl start nginx

启动Django应用

gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000

外部浏览器测试:http://IP地址/index

  1. 后台脱机运行Django应用
nohup gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000 &
  1. 附加知识; Nginx部署多个web应用(Django+Flask)
  • 已按照上面方式部署成功Django,再部署一个flask,同时运行两个web

创建 /etc/nginx/sites-enabled/项目名

编辑创建的文件

server {    listen 8001 default_server; # 修改外部访问端口    # listen [::]:80 default_server;    server_name _; # 如果你映射了域名,可以写在这里,如:server_name example.com;    access_log /var/log/nginx/blueblog_access.log;    error_log /var/log/nginx/blueblog_error.log;    location / {        proxy_pass http://127.0.0.1:8892; #转发的地址,即Gunicorn运行的地址        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-Proto $scheme;    }    location ~ .*{        proxy_pass http://127.0.0.1:8892;        proxy_set_header    Host              $http_host;        proxy_set_header    X-Real-IP         $remote_addr;        proxy_set_header    X-Forwarded-For   $proxy_add_x_forwarded_for;}#   location /static {#       alias /home/xzx/flask_pro/flask_blog/bluelog/bluelog/static/;#       expires 30d; #设置缓存过期时间 #   }}
  • 记得为flask创建wsgi.py文件

运行flask项目

nohup gunicorn -w 4 -b 127.0.0.1:8892 wsgi:app &

外部浏览器输入:http://IP地址:8001/即可访问

转载地址:http://ngugn.baihongyu.com/

你可能感兴趣的文章
利用Java的Properties 类读取配置文件信息
查看>>
用java读写ini配置文件
查看>>
java读取和修改ini配置文件实例代码
查看>>
网络字节序与主机字节序
查看>>
inet_aton和inet_network和inet_addr三者比较-《别怕Linux编程》之五
查看>>
组播通信
查看>>
setsockopt 设置socket 详细用法
查看>>
在局域网中实现多播功能
查看>>
什么叫组播地址(Multicast Address )?
查看>>
掌握IP地址知识 子网掩码与子网划分
查看>>
组播地址,IP组播地址
查看>>
什么是组播
查看>>
组播通信
查看>>
Linux网络编程一步一步学-UDP组播
查看>>
Linux C编程---网络编程
查看>>
在Linux创建库函数(1)
查看>>
在Linux创建库函数(2)
查看>>
在Linux创建库函数(3)
查看>>
多VLAN环境下DHCP服务的实现
查看>>
如何在XP下设置dhcp服务的属性?
查看>>