阿裏雲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 build9 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) [[email protected] mysite]# cd /usr/local/nginx/sbin/ (pyweb) [[email protected] 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) [[email protected]-server sbin]# nginx -s reload
通過域名或者局域網ip就可以訪問了
阿裏雲CentOS7下部署Django+uwsgi+pip3+pytz+python3