Django簡單博客實(shí)戰(zhàn)(八)--- CentOS7+Gunicorn+Nginx部署

CentOS7+Gunicorn+Nginx部署Django

步驟

gunicron配置

保證防火墻關(guān)閉

保證阿里云服務(wù)器的安全組中配置了相應(yīng)端口號(hào)

在這里插入圖片描述
  1. 安裝gunicron
pip install gunicron
  1. 將gunicron加入INSTALLED_APPS中,并將服務(wù)器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. 查看防火墻狀態(tài),并關(guān)閉防火墻
systemctl status firewalld  # 查看防火墻狀態(tài)
systemctl stop firewalld   # 關(guān)閉防火墻
# systemctl start firewalld # 開啟防火墻
  1. gunicron運(yùn)行指令
gunicorn mypoject.wsgi:application -b 0.0.0.0:8000
# mypoject為項(xiàng)目名稱,wsgi為項(xiàng)目目錄下的wsgi.py文件,application固定寫法

<font face="楷體" color=red> 如果項(xiàng)目運(yùn)行后丟失樣式,gunicron出現(xiàn) Not Found 靜態(tài)文件的情況,在根urls加入如下代碼</font>

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會(huì)自動(dòng)運(yùn)行,可以在外部瀏覽器測試下Nginx是否正常開啟

# 輸入服務(wù)器IP
http://IP地址

看到"Welcome to nginx",說明正常啟動(dòng)

  1. 刪除default
  • 刪除 /etc/nginx/sites-enabled/default
  • 創(chuàng)建 /etc/nginx/sites-enabled/項(xiàng)目名

編輯創(chuàng)建的文件

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; #轉(zhuǎn)發(fā)的地址,即Gunicorn運(yùn)行的地址
        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; #設(shè)置緩存過期時(shí)間
 #   }
}
  1. 設(shè)置 /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/項(xiàng)目名" 文件
    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檢查語法是否錯(cuò)誤
[root]$ nginx -t
  1. 啟動(dòng)Nginx
systemctl start nginx

啟動(dòng)Django應(yīng)用

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

外部瀏覽器測試:http://IP地址/index

  1. 后臺(tái)脫機(jī)運(yùn)行Django應(yīng)用
nohup gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000 &
  1. 附加知識(shí); Nginx部署多個(gè)web應(yīng)用(Django+Flask)
  • 已按照上面方式部署成功Django,再部署一個(gè)flask,同時(shí)運(yùn)行兩個(gè)web

創(chuàng)建 /etc/nginx/sites-enabled/項(xiàng)目名

編輯創(chuàng)建的文件

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; #轉(zhuǎn)發(fā)的地址,即Gunicorn運(yùn)行的地址
        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; #設(shè)置緩存過期時(shí)間
 #   }
}
  • 記得為flask創(chuàng)建wsgi.py文件

運(yùn)行flask項(xiàng)目

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

外部瀏覽器輸入:http://IP地址:8001/即可訪問

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容