• 阿里云CentOS7下部署Django+uwsgi+pip3+pytz+python3


    环境:

    CentOS==7.6

    Nginx==1.14

    Python==3.6

    pip3==8.0.2

    Django==2.1.7

    pytz==2018.9

    uwsgi==2.0.18

    更新一次系统软件

    yum update -y

    依赖环境

    yum -y groupinstall "Development tools"
    yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc

     安装python3

    cd /usr/local
    wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
    tar -zxvf Python-3.6.6.tgz
    cd Python-3.6.6
    ./configure --prefix=/usr/local/python3 && make && make install
    ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
    ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3

     如果这个pip3安装报错不上,用下面的方法

     1 1.依赖
     2 yum install openssl-devel -y 
     3 yum install zlib-devel -y
     4 2.安装setuptools
     5 wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26 
     6 tar -zxvf setuptools-19.6.tar.gz 
     7 cd setuptools-19.6
     8 python setup.py build 
     9 python setup.py install
    10 3.安装pip3
    11 wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb 
    12 tar -zxvf pip-8.0.2.tar.gz 
    13 cd pip-8.0.2 
    14 python setup.py build 
    15 python setup.py install
    16 ln -s /usr/local/python3.6/bin/pip3 /usr/bin/pip3

    安装virtualenv,方便不同版本项目的管理

    1 pip3 install virtualenv
    2 ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
    3 mkdir -p /data/env    #存放env文件
    4 mkdir -p /data/wwwroot    #存放用户文件
    5 virtualenv --python=/usr/bin/python3 pyweb    #指定版本的虚拟环境

    启动虚拟环境

    source activate
    如果前面出现(pyweb)就是说明进入虚拟环境

     虚拟环境安装pip3,django,uwsgi

    pip3 install django (如果用于生产的话,则需要指定安装和你项目相同的版本)
    pip3 install uwsgi

    这个时候在系统里面也需要安装一次,并且创建软连接

    ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi

    然后开始创建一个新的django项目

    django-admin.py startproject mysite    #名字可以自定义

    创建完之后/data/wwwroot/下会有一个mysite文件夹,项目就在这个文件夹里面

    创建一个名叫blog的APP

    python3 manage.py startapp blog

    进入项目文件夹/data/wwwroot/mysite,添加static和templates,分别用于存放静态文件和模板文件。
    编辑项目里mysite/settings.py文件

    vim /data/wwwroot/mysite/mysite/settings.py

     在INSTALLED_APPS 列表里最后一行添加'blog',注意,后面要加上一个逗号','

    """
    Django settings for mysite project. Generated by 'django-admin startproject' using Django 2.1.7. For more information on this file, see https://docs.djangoproject.com/en/2.1/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.1/ref/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 = '******************************************' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['*']              #改动这里 # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog',              #改动这里 ] 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 = 'mysite.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, '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 = 'mysite.wsgi.application' # Database # https://docs.djangoproject.com/en/2.1/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Database # https://docs.djangoproject.com/en/2.1/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # 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 = 'en-us' TIME_ZONE = 'UTC' 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'), )

    :wq保存退出

    在我们刚才创建的templates下添加inde.html文件写一个网站

    vim /data/wwwroot/mystie/templates/index.html
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Django</title>
    </head>
    <body>
    <h1>Django website</h1>
    </body>
    </html>

    配置URL

    vim /data/wwwroot/mysite/mysite/urls.py
    """mysite URL Configuration
    
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/2.1/topics/http/urls/
    Examples:
    Function views
        1. Add an import:  from my_app import views
        2. Add a URL to urlpatterns:  path('', views.home, name='home')
    Class-based views
        1. Add an import:  from other_app.views import Home
        2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
    Including another URLconf
        1. Import the include() function: from django.urls import include, path
        2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
    """
    from django.contrib import admin
    from django.urls import path
    from blog import views          #导入模块   
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', views.index),          #添加
    ]
    ~                                                                                                                           
                     

    编辑BlogAPP下的views.py

    vim /data/wwwroot/mysite/blog/views.py
    def index(request):
        return render(request, 'index.html')

    变成这个样子

    from django.shortcuts import render
    def index(request):
        return render(request, 'index.html')
    # Create your views here.
    ~                                                                                                                                                                                                                                                                                                                                                                             
    ~                           

    启动项目

    启动目录
    /data/wwwroot/mysite
    启动命令
    python3 manage.py runserver

    出现报错不会有其他原因就是配置文件写错了,仔细检查,出现需要CONTROL-C打断就是启动成功了

    UWSGI

    Django正常运行来配置启动uwsgi

    网站项目路径是 /data/wwwroot/mysite/,在项目根目录下创建
    mysite.xml文件

    <uwsgi>    
       <socket>127.0.0.1:8997</socket>       <!-- 内部端口,自定义 --> 
       <chdir>/data/wwwroot/mysite/</chdir>     <!-- 项目路径 -->            
       <module>mysite.wsgi</module>        <!-- my site为wsgi.py所在目录名--> 
       <processes>4</processes>         <!-- 进程数 -->     
       <daemonize>uwsgi.log</daemonize>     <!-- 日志文件 -->
    </uwsgi>

    接下来配置Nginx就不详情写

    nginx的配置文件,路径要写对

    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        server {
            listen 80;
            server_name  0.0.0.0;   #有域名写域名,没有域名写ip:80
            charset utf-8;
            location / {
               include uwsgi_params;
               uwsgi_pass 127.0.0.1:8997;
               uwsgi_param UWSGI_SCRIPT mysite.wsgi;
               uwsgi_param UWSGI_CHDIR /data/wwwroot/templates/;
    
            }
            location /static/ {
            alias /data/wwwroot/mysite/static/;
            }
        } 
    }   

    修改配置文件

    vim /data/wwwroot/mysite/mysite/settings.py

    关闭DEBUG

    DEBUG = False  

    进入项目源码的目录

    cd /data/wwwroot/mysite/
    uwsgi -x mysite.xml

    然后进入nginx的sbin目录

    (pyweb) [root@Test-server mysite]# cd /usr/local/nginx/sbin/
    (pyweb) [root@Test-server sbin]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    (pyweb) [root@Test-server sbin]# nginx -s reload

    通过域名或者局域网ip就可以访问了

  • 相关阅读:
    STL hash_map使用
    STL的 string 类赋值
    STL map使用详解
    下面我使用vector容器为基础来构成一棵树
    MFC中CString.Format的详细用法
    error LNK2001: 无法解析的外部符号 "public: static class stdext::hash_map
    !!! STL的string类如何实现CString的Format功能 这是一个经典问题,记住
    STL map和STL set(转载)
    为什么提示此错误?RunTime Check Failure #2 Stack around the variable 'tch1'was corrupted.
    STL源码剖析
  • 原文地址:https://www.cnblogs.com/chenwz/p/10643775.html
Copyright © 2020-2023  润新知