安裝 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
安裝并啟動(dòng) mariadb
因?yàn)?CentOS 7 之后的版本都不在提供 Mysql 安裝源,這里我們使用 mariadb 代替 mysql,依次執(zhí)行下列命令
yum install mariadb mariadb-server -y
yum install MySQL-python -y
systemctl start mariadb
對 mariadb 進(jìn)行初始化設(shè)置
執(zhí)行下面命令,根據(jù)提示操作
設(shè)置新密碼為 test
默認(rèn)密碼為空,直接回車即可
mysql_secure_installation
使用設(shè)置的密碼登陸 mariadb
登陸 db闯团,這里假設(shè)密碼被設(shè)置為 test
mysql -uroot -ptest
創(chuàng)建一個(gè)數(shù)據(jù)庫
create database mysite;
成功后,輸入 exit 命令退出 db
exit
創(chuàng)建 Django 項(xiàng)目
創(chuàng)建 mysite 項(xiàng)目
在 /data/ 目錄下仙粱,創(chuàng)建一個(gè)名為 mysite 的 Django 項(xiàng)目
cd /data/
django-admin startproject mysite
修改配置文件房交,與 Mysql 數(shù)據(jù)庫相關(guān)聯(lián)
備注:SECRET_KEY 配置項(xiàng)無需修改
編輯 /data/mysite/mysite/settings.py
示例代碼:/data/mysite/mysite/settings.py
"""
Django settings for mysite project.
Generated by 'django-admin startproject' using Django 1.11.7.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/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/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'm4@g1=hz^08y(9d)v5l!8^*0wbla=oe15s@u8@5^pw=llfz48%'
# 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',
]
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': [],
? ? ? ? '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/1.11/ref/settings/#databases
DATABASES = {
? ? 'default': {
? ? ? ? 'ENGINE': 'django.db.backends.mysql',
? ? ? ? 'NAME': 'mysite',
? ? ? ? 'PASSWORD':'test',
? ? ? ? 'USER': 'root',
? ? ? ? 'HOST':'127.0.0.1',
? ? ? ? 'PORT':'3306',
? ? }
}
# Password validation
# https://docs.djangoproject.com/en/1.11/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/1.11/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/1.11/howto/static-files/
STATIC_URL = '/static/'
創(chuàng)建 Django 數(shù)據(jù)庫
cd /data/mysite
python manage.py migrate
啟動(dòng) Django
python manage.py runserver
如果沒有報(bào)錯(cuò),就說明 Django 已經(jīng)安裝成功了伐割,并且跟 Mysql 的連接正常
退出 Django
按 ctrl+c 退出 Django 服務(wù)
安裝 Nginx
通過 yum 安裝 Nginx
yum install nginx -y
啟動(dòng) Nginx 服務(wù)
systemctl start nginx
訪問下面的鏈接候味,可以看到 nginx 的歡迎界面
http://<您的 CVM IP 地址>/
安裝 uwsgi
任務(wù)時(shí)間:5min ~ 10min
使用 yum 命令安裝 uwsgi
yum install uwsgi uwsgi-plugin-python -y
讓 Nginx,uwsgi隔心,Django 協(xié)同工作
修改 Nginx 配置文件
編輯 /etc/nginx/nginx.conf
示例代碼:/etc/nginx/nginx.conf
# 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? ? ? ? /usr/share/nginx/html;
? ? ? ? # Load configuration files for the default server block.
? ? ? ? include /etc/nginx/default.d/*.conf;
? ? ? ? location / {
? ? ? ? ? ? include uwsgi_params;
? ? ? ? ? ? uwsgi_pass 127.0.0.1:8000;
? ? ? ? }
? ? ? ? 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 {
#? ? ? ? }
#? ? }
}
重啟 Nginx
/usr/sbin/nginx -s reload
創(chuàng)建 uwsgi 配置文件
請?jiān)?/data/mysite?目錄下創(chuàng)建 uwsgi.ini白群,參考下面的內(nèi)容。
示例代碼:/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
啟動(dòng) uwsgi
uwsgi uwsgi.ini
測試
訪問鏈接 http://<您的? IP 地址>/?
如果可以看到 Django 的界面硬霍,恭喜你帜慢,環(huán)境搭建成功