centos + anaconda + django + uwsgi + nginx 배치

6144 단어
# info
# centos 7
# anaconda 2019.03
# python 3.6
# django 2.1
# uwsgi 2.0.18
# nginx 1.16.1

1. 기본 패키지 설치
# yum groupinstall "Development tools"
# yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
# yum install  pcre pcre-devel pcre-static
# yum install python3-devel
# yum install uwsgi-plugin-python36*

2. 프로젝트 디 렉 터 리
  • Liux 에서 새 사용 자 를 만 듭 니 다. 사용자 이름 은 cjc
    # useradd cjc
    
  • 입 니 다.
  • 사용자 비밀번호 수정
    # passwd cjc
    
  • 사용자 집 디 렉 터 리 에 디 렉 터 리 만 들 기 Downloads, Project
    $ mkdir ~/Downloads/
    $ mkdir ~/Project/
    
  • 3. Anaconda
  • 설치 wget
    # yum install wget
    
  • 사용 wgetAnaconda 다운로드 ~/Downloads/
    $ wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2019.03-Linux-x86_64.sh
    
  • 설치 Anaconda
    $ bash ~/Downloads/Anaconda3-2019.03-Linux-x86_64.sh
    
  • 환경 쇄신
    $ source ~/.bashrc
    
  • 4. 프로젝트 환경
  • 사용 conda 가상 환경 만 들 기 My_Django, 지정 python 버 전 3.6
    $ conda create -n My_Django python==3.6
    
  • 5. django
  • 가상 환경 활성화 My_Django
    $ conda activate My_Django
    
  • 설치 django
    $ pip install django==2.1
    
  • ~/Project/ 디 렉 터 리 에서 항목 생 성 Myweb
    $ django-admin startproject Myweb
    
  • 생 성 응용 Myapp
    $ python manage.py startapp Myapp
    
  • django 의 간단 한 설정 Myweb/Myweb/settings.py
    ALLOWED_HOSTS = ['*',]
    INSTALLED_APPS = [    
    'django.contrib.admin',    
    'django.contrib.auth',    
    'django.contrib.contenttypes',    
    'django.contrib.sessions',    
    'django.contrib.messages',    
    'django.contrib.staticfiles',    
    'Myapp',]
    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',            
    ],       
    },   
    },
    ]
    LANGUAGE_CODE = 'zh-hans'
    TIME_ZONE = 'Asia/Shanghai'
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
    STATIC_ROOT = '/home/cjc/static/'
    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    
  • 6. django 홈 페이지
  • Bootstrap 다운로드 후 파일 압축 풀기 Myweb/static/https://v3.bootcss.com/getting-started/#download
  • Myweb/templates/Myapp/index.html
  • Myweb/Myapp/views.py
    def index(request):    
        return render(request, 'Myapp/index.html')
    
  • Myweb/Myweb/urls.py
    from django.contrib import adminfrom 
    from django.urls import pathfrom 
    from Myapp.views import index as Myapp_index
    urlpatterns = [
    path('admin/', admin.site.urls),    
    path('', Myapp_index),]
    

  • 7. uwsgi
  • 설치 uwsgi
    $ pip install uwsgi
    
  • 에서 ~/Project/Myweb/ 파일 만 들 기 uwsgi.iniHow to use Django with uWSGI
    [uwsgi]
    socket = :8000
    chdir = /home/cjc/Project/Myweb
    module = Myweb.wsgi
    master = true
    processes = 5
    threads = 5
    vacuum = true
    pidfile = /tmp/Myweb-master.pid
    daemonize = /var/log/uwsgi/Myweb.log
    
    socket 소켓 연결 chdir 프로젝트 경 로 를 사용 합 니 다. 즉, my site 패키지 가 포 함 된 디 렉 터 리 module 가 사용 할 wgi 모듈 master 이 주 스 레 드 processes 시작 프로 세 스 수 threads 시작 스 레 드 수 vacuum 서버 가 물 러 날 때 pid pidfile 가 지정 한 시작 시의 pid daemonize uwsgi 로그 경로
  • 를 자동 으로 삭제 합 니 다.
  • 시동 uwsgi
    $ uwsgi --ini uwsgi.ini
    
  • 기타
    #     
    $ uwsgi --reload /tmp/Myweb-master.pid
    #   
    $ uwsgi --stop /tmp/Myweb-master.pid
    
  • 8. Nginx
  • 설치 nginx
    # yum install nginx
    
  • 설정 nginx, /etc/nginx/nginx.conf
    user cjc;
       
           server {
            listen  80;
            server_name  127.0.0.1;
    
            location / {
                include uwsgi_params;
                uwsgi_pass 127.0.0.1:8000;
                uwsgi_param UWSGI_SCRIPT Myweb.wsgi;
                uwsgi_param UWSGI_CHDIR /home/cjc/Project/Myweb;
                client_max_body_size 35m;
            }
    
            location /static {
                alias /home/cjc/static;
            }
    
        }
    
  • 서비스 시작 nginx
    # systemctl start nginx
    
  • 기타
    #     
    # systemctl restart nginx
    #   
    # systemctl stop nginx
    
  • 9. 닥 칠 수 있 는 문제
  • uwsgi 설치 필요 gcc, pcre
    # log
    !!! no internal routing support, rebuild with pcre support !!!
    
  • nginx 잘못 보고 502 닫 기 firewall
    # systemctl stop firewalld
    
    닫 기 selinux
    #     
    setenforce 0
    #   selinux  
    getenforce
    #     (      )
    vim /etc/selinux/config
    SELINUX=disabled
    
  • 정적 파일 접근 설정 STATIC_ROOT 을 사용 하고 python manage.py collectstatic 정적 파일 을 수집 합 니 다. 설정 nginx/static, 설정 nginx 중의 user
  • 을 수집 합 니 다.

    좋은 웹페이지 즐겨찾기