CentOS7+Gunicorn+Nginx部署Django
步驟
gunicron配置
保證防火墻關(guān)閉
保證阿里云服務(wù)器的安全組中配置了相應(yīng)端口號
在這里插入圖片描述
- 安裝gunicron
pip install gunicron
- 將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地址']
- 查看防火墻狀態(tài)公你,并關(guān)閉防火墻
systemctl status firewalld # 查看防火墻狀態(tài)
systemctl stop firewalld # 關(guān)閉防火墻
# systemctl start firewalld # 開啟防火墻
- gunicron運行指令
gunicorn mypoject.wsgi:application -b 0.0.0.0:8000
# mypoject為項目名稱忽洛,wsgi為項目目錄下的wsgi.py文件蔗坯,application固定寫法
<font face="楷體" color=red> 如果項目運行后丟失樣式贩汉,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() # 加入該行
- 在外部瀏覽器中輸入 “http://ip:8000/自己定義的url路由”
http://ip地址:8000/index
nginx配置
- 安裝Nginx
yum install nginx
安裝完成后Nginx會自動運行屠升,可以在外部瀏覽器測試下Nginx是否正常開啟
# 輸入服務(wù)器IP
http://IP地址
看到"Welcome to nginx",說明正常啟動
- 刪除default
- 刪除 /etc/nginx/sites-enabled/default
- 創(chuàng)建 /etc/nginx/sites-enabled/項目名
編輯創(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運行的地址
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è)置 /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 / {
# }
}
- Nginx檢查語法是否錯誤
[root]$ nginx -t
- 啟動Nginx
systemctl start nginx
啟動Django應(yīng)用
gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000
外部瀏覽器測試:http://IP地址/index
- 后臺脫機運行Django應(yīng)用
nohup gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000 &
- 附加知識抢呆; Nginx部署多個web應(yīng)用(Django+Flask)
- 已按照上面方式部署成功Django煮嫌,再部署一個flask,同時運行兩個web
創(chuàng)建 /etc/nginx/sites-enabled/項目名
編輯創(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運行的地址
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è)置緩存過期時間
# }
}
- 記得為flask創(chuàng)建wsgi.py文件
運行flask項目
nohup gunicorn -w 4 -b 127.0.0.1:8892 wsgi:app &
外部瀏覽器輸入:http://IP地址:8001/即可訪問