Skip to content
WONGCW 網誌
  • 首頁
  • 論壇
  • 微博
  • 壁紙下載
  • 免費圖床
  • 視頻下載
  • 聊天室
  • SEO工具
  • 支援中心
  • 表格製作
  • More
    • 在線名片
    • 網頁搜索
    • 天氣預報
    • 二維碼生成器
    • WordPress 插件及主題下載
  • Search Icon

WONGCW 網誌

記錄生活經驗與點滴

基于 CentOS7.2 的 Django 环境搭建

基于 CentOS7.2 的 Django 环境搭建

2018-09-29 Comments 0 Comment

安装 Django

任务时间:5min ~ 10min

先安装 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

任务时间:10min ~ 15min

安装并启动 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,这里假设密码被设置为 test
mysql -uroot -ptest

创建一个数据库

create database mysite;

成功后,输入 exit 命令退出 db

exit

创建 Django 项目

任务时间:10min ~ 15min

创建 mysite 项目

  • 在 /data/ 目录下,创建一个名为 mysite 的 Django 项目
cd /data/
django-admin startproject mysite

修改配置文件,与 Mysql 数据库相关联

  • 备注:SECRET_KEY 配置项无需修改
  • 编辑 /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/'

创建 Django 数据库

cd /data/mysite
python manage.py migrate

启动 Django

python manage.py runserver
  • 如果没有报错,就说明 Django 已经安装成功了,并且跟 Mysql 的连接正常

退出 Django

按 ctrl+c 退出 Django 服务

安装 Nginx

任务时间:5min ~ 10min

通过 yum 安装 Nginx

yum install nginx -y

启动 Nginx 服务

systemctl start nginx
  • 访问下面的链接,可以看到 nginx 的欢迎界面
    http://<您的 CVM IP 地址>/

安装 uwsgi

任务时间:5min ~ 10min

使用 yum 命令安装 uwsgi

yum install uwsgi uwsgi-plugin-python -y

让 Nginx,uwsgi,Django 协同工作

任务时间:5min ~ 15min

修改 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

创建 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://<您的 CVM IP 地址>/
如果可以看到 Django 的界面,恭喜你,环境搭建成功

分享此文:

  • 分享到 Twitter(在新視窗中開啟)
  • 按一下以分享至 Facebook(在新視窗中開啟)
  • 分享到 WhatsApp(在新視窗中開啟)
  • 按一下以分享到 Telegram(在新視窗中開啟)
  • 分享到 Pinterest(在新視窗中開啟)
  • 分享到 Reddit(在新視窗中開啟)
  • 按一下即可分享至 Skype(在新視窗中開啟)
  • 按一下即可以電子郵件傳送連結給朋友(在新視窗中開啟)
  • 點這裡列印(在新視窗中開啟)

相關


教學資源

Post navigation

PREVIOUS
基于 CentOS 搭建 Python 的 Django 环境
NEXT
Linux 下部署 Django 环境

發表迴響 取消回覆

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料。

More results...

Generic filters
Exact matches only
Search in title
Search in content
Search in excerpt
Filter by 分類
網站公告
Featured
限時免費
ESET NOD32
WINDOWS 10 &11 INSIDER PREVIEW
Windows 軟件下載
系統軟件
辦公軟件
圖像處理
影音媒體
網絡軟件
應用軟件
Mac 軟件下載
安卓軟件下載
網絡資訊
Mac資訊
Linux資訊
VPS資訊
NASA資訊
金融資訊
WhatsApp Stickers教學
WordPress資訊
WeChat資訊
PHP資訊
Plesk資訊
TensorFlow
教學資源
開源程序
網頁工具
SEO工具
醫療健康
旅遊及消閒
其他資訊
Content from
Content to
2018 年 9 月
一 二 三 四 五 六 日
 12
3456789
10111213141516
17181920212223
24252627282930
« 8 月   10 月 »

分類

  • 網站公告
  • 限時免費
  • ESET NOD32
  • WINDOWS 10 &11 INSIDER PREVIEW
  • Windows 軟件下載
  • 系統軟件
  • 辦公軟件
  • 圖像處理
  • 影音媒體
  • 網絡軟件
  • 應用軟件
  • Mac 軟件下載
  • 安卓軟件下載
  • 網絡資訊
  • Mac資訊
  • Linux資訊
  • VPS資訊
  • NASA資訊
  • WhatsApp Stickers教學
  • WordPress資訊
  • WeChat資訊
  • PHP資訊
  • Plesk資訊
  • TensorFlow
  • 教學資源
  • 開源程序
  • 網頁工具
  • SEO工具
  • 醫療健康
  • 旅遊及消閒
  • 其他資訊

彙整

近期文章

  • 體驗了微軟的ChatGPT後我覺得谷歌、百度麻煩了 2023-02-09
  • 新版必應比ChatGPT牛?實測:更有人情味兒 2023-02-09
  • 蘋果任命首位首席人力官管理16萬員工大軍 2023-02-09
  • 迪士尼宣布裁員7000人股價盤後大漲 2023-02-09
  • SpaceX明天將嘗試進行創紀錄的火箭測試33個引擎一齊點火 2023-02-09
  • 任天堂:當前沒計劃對硬件和軟件降價 2023-02-09
  • 可用陽光激活的”絲瓜水凝膠”在淨化水方面表現出色 2023-02-09
  • NASA成功完成RS-25火箭發動機的全長熱火測試 2023-02-09
  • 一種新的鋰-空氣電池設計有望實現前所未有的能量密度 2023-02-09
  • 現在Twitter Blue用戶可以寫4000字的推文 2023-02-09

熱門文章與頁面︰

  • DP vs HDMI 誰才是遊戲玩家最佳選擇?
  • ESET NOD32 LICENSE KEY (UPDATED 2023-01-17)
  • Explorer Patcher:讓Windows 11恢復Windows 10的行為特徵
  • 打車叫到特斯拉不會開門很尷尬?官方介紹開關門方法
  • 舊機福音:極限精簡Windows 10系統Tiny10升級C盤僅佔4.3GB
  • Autodesk AutoCAD 2021 正式版註冊版-簡體/繁體中文/英文版
  • 微軟強化Game Bar:可顯示Xbox手柄剩餘電量
  • Google Chrome瀏覽器意外出現Status_Access_Violation錯誤而崩潰
  • 移動版RTX 3050與3050 Ti跑分曝光較RTX 1650 Ti提升顯著
  • Office 2013-2021 C2R Install v7.3.1 正式版-Office 2013/2016/2019/2021自定義組件安裝工具

投遞稿件

歡迎各界人士投遞稿件到admin@wongcw.com

請提供以下資料:

1.你的名字

2.你的電郵

3.分類目錄

4.文章標題

5.文章摘要

6.文章內容

7.文章來源

 

聯繫我們

查詢,投稿,商務合作:
​admin@wongcw.com
​技術支援:
​support@wongcw.com
​客户服務:
​cs@wongcw.com

QQ群:833641851

快帆

MALUS

極度掃描

DMCA.com Protection Status

WONGCW 網誌

  • 免責聲明
  • 捐助我們
  • ThemeNcode PDF Viewer
  • ThemeNcode PDF Viewer SC
  • Events

服務器提供

本站使用之服務器由ikoula提供。

聯繫我們

查詢,投稿,商務合作:
​admin@wongcw.com
​技術支援:
​support@wongcw.com
​客户服務:
​cs@wongcw.com

QQ群:833641851

© 2023   All Rights Reserved.
X