阅读 699

Django部署到服务器

Django是一个大而全的框架,不像Flask那样精简,而在实际的开发部署中,总会出现问题,本文介绍使用Nginx和uwsgi进行部署,从而可以提高网站并发性

更多信息请点击

环境要求

Python >= 3.6
Djando >= 2.1
Nginx >= 1.2
复制代码

安装 && 配置

  • 若直接Git本源码,只需要修改数据的名称和密码即可

  • 若非Git本源码,具体部署步骤如下,相比Nginx,Apache配置更简便,本部署基于Nginx

在工程目录下修改setting.py

# Application definition

INSTALLED_APPS = [
    'blog',
]
# Database

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'blogx',
        'USER': 'root',
        'PASSWORD': '416798',
        'HOST': 'localhost',
        'PORT': '3306',
    },
}
# Internationalization

LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
MEDIA_ROOT = os.path.join(BASE_DIR, "static/media")
复制代码

在工程目录下修改urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url

urlpatterns = [
    path('admin/', admin.site.urls),
    # 二级应用分发
    url('^', include('blog.urls')),

]
复制代码

在应用目录下新建urls.py

from django.urls import path
from . import views
from django.conf.urls import url

urlpatterns = [
    url(r'^$', views.index),
    url(r'^Python/', views.python),
]
复制代码

在应用目录下的__init__.py添加代码

import pymysql
pymysql.install_as_MySQLdb()
复制代码

迁移 && 数据

生成偏移文件

python manage.py makemigrations
复制代码

添加到数据库

python manage.py migrate
复制代码

创建管理员

python manage.py createsuperuser
复制代码

生产 && 部署

在工程根目录下建立blog.xml文件

<uwsgi>
    <socket>127.0.0.1:8999</socket><!-- 内部端口,自定义 -->
     <chdir>/data/wwwroot/blog/</chdir><!-- 项目路径 -->
    <module>project.wsgi</module>
     <processes>4</processes> <!-- 进程数 -->
    <daemonize>uwsgi.log</daemonize><!-- 日志文件 -->
</uwsgi>

复制代码

配置Nginx代理服务器

  • Http配置
server {
        listen       80;
        server_name  blog.gaozhe.top;
        rewrite ^(.*) https://$server_name$1 permanent;
        location /static {
            alias /var/www/blog/static/;
        }
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8999;
           uwsgi_param UWSGI_SCRIPT blog.wsgi;
           uwsgi_param UWSGI_CHDIR /data/wwwroot/blog;

        }
      }

复制代码
  • Https配置
 server {
    listen 443;
    server_name blog.gaozhe.top;
    location /static {
            alias /var/www/blog/static/;
        }
    ssl on;
    ssl_certificate  /usr/local/nginx/conf/ssl/214903559130065.pem;
    ssl_certificate_key  /usr/local/nginx/conf/ssl/214903559130065.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
       include uwsgi_params;
           uwsgi_pass 127.0.0.1:8999;
           uwsgi_param UWSGI_SCRIPT blog.wsgi;
           uwsgi_param UWSGI_CHDIR /data/wwwroot/blog;

    }
}
复制代码
关注下面的标签,发现更多相似文章
评论