本文共 4743 字,大约阅读时间需要 15 分钟。
步骤
gunicron配置
保证防火墙关闭
保证阿里云服务器的安全组中配置了相应端口号
pip install gunicron
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地址']
systemctl status firewalld # 查看防火墙状态systemctl stop firewalld # 关闭防火墙# systemctl start firewalld # 开启防火墙
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() # 加入该行
http://ip地址:8000/index
nginx配置
yum install nginx
安装完成后Nginx会自动运行,可以在外部浏览器测试下Nginx是否正常开启
# 输入服务器IPhttp://IP地址
看到"Welcome to nginx",说明正常启动
- 删除 /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; #设置缓存过期时间 # }}
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 / { # } }
[root]$ nginx -t
systemctl start nginx
启动Django应用
gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000
外部浏览器测试:http://IP地址/index
nohup gunicorn lifeblog.wsgi:application -w 4 -b 127.0.0.1:8000 &
创建 /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项目
nohup gunicorn -w 4 -b 127.0.0.1:8892 wsgi:app &
外部浏览器输入:http://IP地址:8001/即可访问
转载地址:http://ngugn.baihongyu.com/