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 的界面,恭喜你,環境搭建成功