Linux部署Django环境

一、安装python3可能使用的依赖

[root@instance-xu9puhyr ~]# yum -y install gcc
[root@instance-xu9puhyr ~]# yum -y groupinstall 'Development Tools'
[root@instance-xu9puhyr ~]# yum install -y ncurses-libs zlib-devel mysql-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

二、进入tmp目录

[root@instance-xu9puhyr ~]# cd /
[root@instance-xu9puhyr /]# cd tmp/
下载到tmp目录 python3.6.7
[root@instance-xu9puhyr tmp]# wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tgz
解压python3.6.7
[root@instance-xu9puhyr tmp]# tar xf Python-3.6.7.tgz
进入python目录
[root@instance-xu9puhyr tmp]# cd Python-3.6.7
使用gcc编译python3.6.7
[root@instance-xu9puhyr Python-3.6.7]# ./configure
安装python3.6.7 安装目录:/usr/local/bin/python3.6
[root@instance-xu9puhyr Python-3.6.7]# make
[root@instance-xu9puhyr Python-3.6.7]# make intsall
备份系统自带python2.7 系统自带命令python目录:/usr/bin/python  更改为/usr/bin/python-2.7.backup
[root@instance-xu9puhyr ~]# mv /usr/bin/python /usr/bin/python-2.7.backup
创建系统命令(软链接)将系统命令python指定目录到/usr/local/bin/python3.6 以及pip3
[root@instance-xu9puhyr /]# ln -s /usr/local/bin/python3.6 /usr/bin/python
[root@instance-xu9puhyr /]# ln -s /usr/local/bin/pip3 /usr/bin/pip3
 
由于yum依赖于python2,系统命令被修改所以需要告诉yum命令指定python2  使用i修改文件 esc退出修改 :wq强制保存文件修改
[root@instance-xu9puhyr /]# vi /usr/bin/yum
#!/usr/bin/python
打开yum文件修改头部===查看目录/usr/bin/中python2对应的文件
#!/usr/bin/python2.6
看系统有没有这个文件有则修改头部 目录:/usr/libexec/urlgrabber-ext-down
[root@instance-xu9puhyr /]# vi /usr/libexec/urlgrabber-ext-down
#!/usr/bin/python
打开yum文件修改头部===查看目录/usr/bin/中python2对应的文件
#!/usr/bin/python2.6
使用yum -v检查是否yum还存在报错

三、根目录创建python3.6虚拟环境

[root@instance-xu9puhyr /]# python -m venv /myblog/
进入虚拟环境的bin目录
[root@instance-xu9puhyr /]# cd myblog/bin
进入虚拟环境
[root@instance-xu9puhyr bin]# source activate
查看环境里的模块
(myblog) [root@instance-xu9puhyr bin]# pip freeze
安装django2.1.0版本
(myblog) [root@instance-xu9puhyr bin]# pip install django==2.1.0
安装Gunicorn“绿色独角兽”是一个被广泛使用的高性能的Python WSGI UNIX HTTP服务器
(myblog) [root@instance-xu9puhyr bin]# pip install gunicorn
(myblog) [root@instance-xu9puhyr bin]# pip install pymysql

四、安装nginx环境

(myblog) [root@instance-xu9puhyr bin]# yum -y install nginx
(myblog) [root@instance-xu9puhyr bin]# service nginx start
(myblog) [root@instance-xu9puhyr bin]# systemctl start nginx.service
(myblog) [root@instance-xu9puhyr bin]# systemctl enable nginx.service

五、修改django项目settings文件

 
import os
 
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
 
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6m1wv5553o(vc1^_wr_gq5^=ma6_@a5ax8tsh#lx$jyya8)x5)'
 
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
 
ALLOWED_HOSTS = ["*"]
 
 
# Application definition
 
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'DB',
    'pubadmin',
    'student',
    'Sale',
 
]
 
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
 
ROOT_URLCONF = 'ProjectCRM.urls'
 
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'),
                 os.path.join(BASE_DIR, 'pubadmin/templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
 
WSGI_APPLICATION = 'ProjectCRM.wsgi.application'
 
 
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
 
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
 
        'NAME': 'xxx',    #你的数据库名称
 
        'USER': 'xxx',   #你的数据库用户名
 
        'PASSWORD': 'xxx', #你的数据库密码
 
        'HOST': 'xxxx', #你的数据库主机,留空默认为localhost
 
        'PORT': 'xxx', #你的数据库端口
    }
}
 
 
# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
 
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]
 
 
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
 
LANGUAGE_CODE = 'zh-Hans'
 
TIME_ZONE = 'Asia/Shanghai'
 
USE_I18N = True
 
USE_L10N = True
 
USE_TZ = True
 
 
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
 
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR,'static'),
    os.path.join(BASE_DIR, 'pubadmin/static'),
)
 
# MEDIA_URL = '/media/'
# MEDIAFILES_DIRS = (
#     os.path.join(BASE_DIR,'media'),
#     os.path.join(BASE_DIR, 'pubadmin/media'),
# )
 
LOGIN_URL = '/pubadmin/login/'
 

六、修改ngnix文件

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
 
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
 
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
 
events {
    worker_connections 1024;
}
 
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;
 
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /mysite/ProjectCRM;
 
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
 
        location /static/ {
        alias /mysite/ProjectCRM/static/;
        }
 
        location /media/ {
        alias /mysite/ProjectCRM/media/;
        }
 
        location / {
            root /mysite/ProjectCRM;
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
 
        error_page 404 /404.html;
            location = /40x.html {
        }
 
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
 
# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
 
}

七、重启ngnix服务

(mysite) [root@instance-xu9puhyr bin]# systemctl restart nginx.service
进入项目目录
(mysite) [root@instance-xu9puhyr mysite]# cd ProjectCRM/
启动项目
(mysite) [root@instance-xu9puhyr ProjectCRM]# gunicorn ProjectCRM.wsgi:application -b 127.0.0.1:8000 -D

八、查看gunicorn进程

(mysite) [root@instance-xu9puhyr ProjectCRM]# pstree -ap|grep gunicorn
(mysite) [root@instance-xu9puhyr ProjectCRM]# kill -9 进程号
kill -HUP 进程号
posted @ 2019-06-18 18:45  我在地球凑人数的日子  阅读(465)  评论(0编辑  收藏  举报