• 基于Centos搭建Django 环境搭建


    CentOS 7.2 64 位操作系统

    安装 Django

    先安装 PIP,再通过 PIP 安装 Django

    安装 PIP

    cd /data;
    mkdir tmp;
    cd tmp;
    wget https://bootstrap.pypa.io/get-pip.py;
    python ./get-pip.py;

    使用 PIP,安装 Django

    pip install Django==1.11.7

    安装 Mysql

    安装并启动 mariadb

    因为 CentOS 7 之后的版本都不在提供 Mysql 安装源,这里我们使用 mariadb 代替 mysql,依次执行下列命令

    yum install mariadb mariadb-server -y
    yum install MySQL-python -y
    systemctl start mariadb

    对 mariadb 进行初始化设置

    执行下面命令,根据提示操作
    设置新密码为 test
    默认密码为空,直接回车即可
    mysql_secure_installation

    使用设置的密码登陆 mariadb

    登陆 db,这里假设密码被设置为 root

    mysql -uroot -ptest

    创建一个数据库

    create database mysite;

    成功后,输入 exit 命令退出 db

    exit

    创建 Django 项目

    创建 mysite 项目

    在 /data/ 目录下,创建一个名为 mysite 的 Django 项目

    cd /data/
    django-admin startproject mysite

    修改配置文件,与 Mysql 数据库相关联(SECRET_KEY 配置项无需修改)

    示例代码:/data/mysite/mysite/settings.py

      1 """
      2 Django settings for mysite project.
      3 
      4 Generated by 'django-admin startproject' using Django 1.11.7.
      5 
      6 For more information on this file, see
      7 https://docs.djangoproject.com/en/1.11/topics/settings/
      8 
      9 For the full list of settings and their values, see
     10 https://docs.djangoproject.com/en/1.11/ref/settings/
     11 """
     12 
     13 import os
     14 
     15 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
     16 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
     17 
     18 
     19 # Quick-start development settings - unsuitable for production
     20 # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
     21 
     22 # SECURITY WARNING: keep the secret key used in production secret!
     23 SECRET_KEY = 'm4@g1=hz^08y(9d)v5l!8^*0wbla=oe15s@u8@5^pw=llfz48%'
     24 
     25 # SECURITY WARNING: don't run with debug turned on in production!
     26 DEBUG = True
     27 
     28 ALLOWED_HOSTS = ["*"]
     29 
     30 
     31 # Application definition
     32 
     33 INSTALLED_APPS = [
     34     'django.contrib.admin',
     35     'django.contrib.auth',
     36     'django.contrib.contenttypes',
     37     'django.contrib.sessions',
     38     'django.contrib.messages',
     39     'django.contrib.staticfiles',
     40 ]
     41 
     42 MIDDLEWARE = [
     43     'django.middleware.security.SecurityMiddleware',
     44     'django.contrib.sessions.middleware.SessionMiddleware',
     45     'django.middleware.common.CommonMiddleware',
     46     'django.middleware.csrf.CsrfViewMiddleware',
     47     'django.contrib.auth.middleware.AuthenticationMiddleware',
     48     'django.contrib.messages.middleware.MessageMiddleware',
     49     'django.middleware.clickjacking.XFrameOptionsMiddleware',
     50 ]
     51 
     52 ROOT_URLCONF = 'mysite.urls'
     53 
     54 TEMPLATES = [
     55     {
     56         'BACKEND': 'django.template.backends.django.DjangoTemplates',
     57         'DIRS': [],
     58         'APP_DIRS': True,
     59         'OPTIONS': {
     60             'context_processors': [
     61                 'django.template.context_processors.debug',
     62                 'django.template.context_processors.request',
     63                 'django.contrib.auth.context_processors.auth',
     64                 'django.contrib.messages.context_processors.messages',
     65             ],
     66         },
     67     },
     68 ]
     69 
     70 WSGI_APPLICATION = 'mysite.wsgi.application'
     71 
     72 
     73 # Database
     74 # https://docs.djangoproject.com/en/1.11/ref/settings/#databases
     75 
     76 DATABASES = {
     77     'default': {
     78         'ENGINE': 'django.db.backends.mysql',
     79         'NAME': 'mysite',
     80         'PASSWORD':'test',
     81         'USER': 'root',
     82         'HOST':'127.0.0.1',
     83         'PORT':'3306',
     84     }
     85 }
     86 
     87 
     88 # Password validation
     89 # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
     90 
     91 AUTH_PASSWORD_VALIDATORS = [
     92     {
     93         'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
     94     },
     95     {
     96         'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
     97     },
     98     {
     99         'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    100     },
    101     {
    102         'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    103     },
    104 ]
    105 
    106 
    107 # Internationalization
    108 # https://docs.djangoproject.com/en/1.11/topics/i18n/
    109 
    110 LANGUAGE_CODE = 'en-us'
    111 
    112 TIME_ZONE = 'UTC'
    113 
    114 USE_I18N = True
    115 
    116 USE_L10N = True
    117 
    118 USE_TZ = True
    119 
    120 
    121 # Static files (CSS, JavaScript, Images)
    122 # https://docs.djangoproject.com/en/1.11/howto/static-files/
    123 
    124 STATIC_URL = '/static/'

    创建 Django 数据库

    cd /data/mysite
    python manage.py migrate

    启动 Django

    python manage.py runserver

    如果没有报错,就说明 Django 已经安装成功了,并且跟 Mysql 的连接正常

    按 ctrl+c 退出 Django 服务

    安装 Nginx

    通过 yum 安装 Nginx

    yum install nginx -y

    启动 Nginx 服务

    systemctl start nginx

    访问下面的链接,可以看到 nginx 的欢迎界面 http://118.89.65.22/

    安装 uwsgi

    使用 yum 命令安装 uwsgi

    yum install uwsgi uwsgi-plugin-python -y

    让 Nginx,uwsgi,Django 协同工作

    修改 Nginx 配置文件

    示例代码:/etc/nginx/nginx.conf

     1 # For more information on configuration, see:
     2 #   * Official English Documentation: http://nginx.org/en/docs/
     3 #   * Official Russian Documentation: http://nginx.org/ru/docs/
     4 
     5 user nginx;
     6 worker_processes auto;
     7 error_log /var/log/nginx/error.log;
     8 pid /run/nginx.pid;
     9 
    10 # Load dynamic modules. See /usr/share/nginx/README.dynamic.
    11 include /usr/share/nginx/modules/*.conf;
    12 
    13 events {
    14     worker_connections 1024;
    15 }
    16 
    17 http {
    18     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    19                       '$status $body_bytes_sent "$http_referer" '
    20                       '"$http_user_agent" "$http_x_forwarded_for"';
    21 
    22     access_log  /var/log/nginx/access.log  main;
    23 
    24     sendfile            on;
    25     tcp_nopush          on;
    26     tcp_nodelay         on;
    27     keepalive_timeout   65;
    28     types_hash_max_size 2048;
    29 
    30     include             /etc/nginx/mime.types;
    31     default_type        application/octet-stream;
    32 
    33     # Load modular configuration files from the /etc/nginx/conf.d directory.
    34     # See http://nginx.org/en/docs/ngx_core_module.html#include
    35     # for more information.
    36     include /etc/nginx/conf.d/*.conf;
    37 
    38     server {
    39         listen       80 default_server;
    40         listen       [::]:80 default_server;
    41         server_name  _;
    42         root         /usr/share/nginx/html;
    43 
    44         # Load configuration files for the default server block.
    45         include /etc/nginx/default.d/*.conf;
    46 
    47         location / {
    48             include uwsgi_params;
    49             uwsgi_pass 127.0.0.1:8000;
    50         }
    51 
    52         error_page 404 /404.html;
    53             location = /40x.html {
    54         }
    55 
    56         error_page 500 502 503 504 /50x.html;
    57             location = /50x.html {
    58         }
    59     }
    60 
    61 # Settings for a TLS enabled server.
    62 #
    63 #    server {
    64 #        listen       443 ssl http2 default_server;
    65 #        listen       [::]:443 ssl http2 default_server;
    66 #        server_name  _;
    67 #        root         /usr/share/nginx/html;
    68 #
    69 #        ssl_certificate "/etc/pki/nginx/server.crt";
    70 #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
    71 #        ssl_session_cache shared:SSL:1m;
    72 #        ssl_session_timeout  10m;
    73 #        ssl_ciphers HIGH:!aNULL:!MD5;
    74 #        ssl_prefer_server_ciphers on;
    75 #
    76 #        # Load configuration files for the default server block.
    77 #        include /etc/nginx/default.d/*.conf;
    78 #
    79 #        location / {
    80 #        }
    81 #
    82 #        error_page 404 /404.html;
    83 #            location = /40x.html {
    84 #        }
    85 #
    86 #        error_page 500 502 503 504 /50x.html;
    87 #            location = /50x.html {
    88 #        }
    89 #    }
    90 }

    重启 Nginx

    /usr/sbin/nginx -s reload

    创建 uwsgi 配置文件

    请在 /data/mysite 目录下创建 uwsgi.ini,参考下面的内容。

    示例代码:/data/mysite/uwsgi.ini

    [uwsgi]
    socket = 127.0.0.1:8000
    chdir = /data/mysite
    wsgi-file = mysite/wsgi.py
    processes = 4
    threads = 2
    stats = 127.0.0.1:9191
    uid = nobody
    gid = nobody
    master = true
    harakiri = 30
    daemonize = /data/mysite/uwsgi.log
    plugins = python

    启动 uwsgi

    uwsgi uwsgi.ini

    测试

    访问链接 http://118.89.65.22/ 
    如果可以看到 Django 的界面,恭喜你,环境搭建成功

  • 相关阅读:
    [WPF VTK]三维图形开发基础(一)
    WP开发(一)
    [WPF VTK]三维图形开发基础(四)

    WIN8 下IE突然无法打开(管理员权限可打开)
    [WPF VTK]三维图形开发基础(三)
    堆排序、快排的坑
    双向链表之插入
    [WPF VTK]三维图形开发基础(二)
    [转载]Android界面设计学习日志(一)
  • 原文地址:https://www.cnblogs.com/jikexianfeng/p/8454674.html
Copyright © 2020-2023  润新知