Centos 6.x python3.6 Django 环境设置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiegh2014/article/details/78883498

Django 概述
Django 中提供了开发网站经常用到的模块,常见的代码都为你写好了,通过减少重复的代码,Django 使你能够专注于 web 应用上有 趣的关键性的东西。为了达到这个目标,Django 提供了通用Web开发模式的高度抽象,提供了频繁进行的编程作业的快速解决方法,以及为“如何解决问题”提供了清晰明了的约定。Django的理念是DRY(Don't Repeat Yourself)来鼓励快速开发!

Django全貌 
urls.py
网址入口,关联到对应的views.py中的一个函数(或者generic类),访问网址就对应一个函数。

views.py
处理用户发出的请求,从urls.py中对应过来, 通过渲染templates中的网页可以将显示内容,比如登陆后的用户名,用户请求的数据,输出到网页。

models.py
与数据库操作相关,存入或读取数据时用到这个,当然用不到数据库的时候 你可以不使用。

forms.py
表单,用户在浏览器上输入数据提交,对数据的验证工作以及输入框的生成等工作,当然你也可以不使用。

templates 文件夹
views.py 中的函数渲染templates中的Html模板,得到动态内容的网页,当然可以用缓存来提高速度。

admin.py
后台,可以用很少量的代码就拥有一个强大的后台。

settings.py
Django 的设置,配置文件,比如 DEBUG 的开关,静态文件的位置等。

一、系统环境设置
1、修改系统字符集
echo 'LANG="en_GB.utf8"' > /etc/sysconfig/i18n && cat /etc/sysconfig/i18n

2、修改YUM源
参考
http://blog.csdn.net/xiegh2014/article/details/53031894
二、python安装
1、安装python相关依赖
yum -y install openssl-devel readline-devel unzip
yum install gcc

2、下载python源码包
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz

3、编译安装python
tar -xvf Python-3.6.1.tgz 
cd Python-3.6.1
./configure --prefix=/usr/local/python36
make && make install

查看所安装的工具
ll /usr/local/python36/bin/
总用量 19496
lrwxrwxrwx 1 root root       8 12月 24 09:55 2to3 -> 2to3-3.6
-rwxr-xr-x 1 root root     110 12月 24 09:55 2to3-3.6
-rwxr-xr-x 1 root root     251 12月 24 09:55 easy_install-3.6
lrwxrwxrwx 1 root root       7 12月 24 09:55 idle3 -> idle3.6
-rwxr-xr-x 1 root root     108 12月 24 09:55 idle3.6
-rwxr-xr-x 1 root root     223 12月 24 09:55 pip3
-rwxr-xr-x 1 root root     223 12月 24 09:55 pip3.6
lrwxrwxrwx 1 root root       8 12月 24 09:55 pydoc3 -> pydoc3.6
-rwxr-xr-x 1 root root      93 12月 24 09:55 pydoc3.6
lrwxrwxrwx 1 root root       9 12月 24 09:55 python3 -> python3.6
-rwxr-xr-x 2 root root 9961691 12月 24 09:54 python3.6
lrwxrwxrwx 1 root root      17 12月 24 09:55 python3.6-config -> python3.6m-config
-rwxr-xr-x 2 root root 9961691 12月 24 09:54 python3.6m
-rwxr-xr-x 1 root root    3092 12月 24 09:55 python3.6m-config
lrwxrwxrwx 1 root root      16 12月 24 09:55 python3-config -> python3.6-config
lrwxrwxrwx 1 root root      10 12月 24 09:55 pyvenv -> pyvenv-3.6
-rwxr-xr-x 1 root root     450 12月 24 09:55 pyvenv-3.6

4、配置pip                                  
vim /etc/pip.conf
[global]
index-url = http://pypi.douban.com/simple/ 
trusted-host=pypi.douban.com
[list]
format=columns

/usr/local/python36/bin/pip3 list
Package    Version
---------- -------
pip        9.0.1  
setuptools 28.8.0 

5、安装virtualenv
/usr/local/python36/bin/pip3 install virtualenv

执行结果
Collecting virtualenv
  Downloading http://pypi.doubanio.com/packages/6f/86/3dc328ee7b1a6419ebfac7896d882fba83c48e3561d22ddddf38294d3e83/virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 983kB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0

三、安装django
1、初始化env环境

创建一个普通用户
useradd devops
passwd devops

切换到普通devops
su - devops
/usr/local/python36/bin/virtualenv ./python36env

Using base prefix '/usr/local/python36'
New python executable in /home/devops/python36env/bin/python3.6
Also creating executable in /home/devops/python36env/bin/python
Installing setuptools, pip, wheel...done.

进入虚拟环境
source python36env/bin/activate
(python36env) [devops@devops-python36 ~]$ pip list
Package    Version
---------- -------
pip        9.0.1  
setuptools 38.2.4 
wheel      0.30.0 

2、django版本选择
Django 1.5.x 支持 Python 2.6.5 Python 2.7, Python 3.2 和 3.3.
Django 1.6.x 支持 Python 2.6.X, 2.7.X, 3.2.X 和 3.3.X
Django 1.7.x 支持 Python 2.7, 3.2, 3.3, 和 3.4 (注意:Python 2.6 不支持了)
Django 1.8.x 支持 Python 2.7, 3.2, 3.3, 3.4 和 3.5.  (长期支持版本 LTS)
Django 1.9.x 支持 Python 2.7, 3.4 和 3.5. 不支持 3.3 了
Django 1.10.x 支持 Python 2.7, 3.4 和 3.5. 
Django 1.11.x 支持 Python 2.7, 3.4, 3.5 和 3.6(长期支持版本 LTS) 最后一个支持 Python 2.7 的版本
Django 2.0.x 支持 Python 3.4, 3.5 和 3.6 (注意,不再支持 Python 2)

3、安装django
pip install "django>=1.11

(python36env) [devops@devops-python36 ~]$ pip install "django>=1.11,<=1.12"
Collecting django<=1.12,>=1.11
  Downloading http://pypi.doubanio.com/packages/7e/36/5266e0c51ee9b953d60ea8ea1fea10e268b1368f9c0ad08e2ff76ee9c1b5/Django-1.11.8-py2.py3-none-any.whl (6.9MB)
    100% |████████████████████████████████| 7.0MB 1.9MB/s 
Collecting pytz (from django<=1.12,>=1.11)
  Downloading http://pypi.doubanio.com/packages/a3/7f/e7d1acbd433b929168a4fb4182a2ff3c33653717195a26c1de099ad1ef29/pytz-2017.3-py2.py3-none-any.whl (511kB)
    100% |████████████████████████████████| 512kB 1.4MB/s 
Installing collected packages: pytz, django
Successfully installed django-1.11.8 pytz-2017.3

4、mysql安装
可以参考http://blog.csdn.net/xiegh2014/article/details/72860951
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
rpm -ivh mysql-community-release-el6-5.noarch.rpm
yum repolist all | grep mysql

yum install mysql mysql-server mysql-devel zlib-devel -y
pip install pymysql

(python36env) [devops@devops-python36 ~]$ pip install pymysql
Collecting pymysql
  Downloading http://pypi.doubanio.com/packages/c6/42/c54c280d8418039bd2f61284f99cb6d9e0eae80383fc72ceb6eac67855fe/PyMySQL-0.7.11-py2.py3-none-any.whl (78kB)
    100% |████████████████████████████████| 81kB 2.6MB/s 
Installing collected packages: pymysql
Successfully installed pymysql-0.7.11

(python36env) [devops@devops-python36 ~]$ pip list
Package    Version
---------- -------
Django     1.11.8 
pip        9.0.1  
PyMySQL    0.7.11 
pytz       2017.3 
setuptools 38.2.4 
wheel      0.30.0 

4.1、修改配置
default-storage-engine = innodb
innodb_file_per_table
collation-server=utf8_general_ci
init_connect='SET NAMES utf8'
character-set-server=utf8

4.2、启动服务
/etc/init.d/mysqld start

4.3、设置密码
/usr/bin/mysqladmin -u root password '123456'

[root@devops-python36 ~]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

4.4、设置为开机启动
chkconfig --list | grep mysqld
chkconfig mysqld on

4.5、创建数据库
mysql -uroot -p123456 -e "create database django CHARACTER SET utf8;"


四、配置vim
yum -y install vim


vim ~/.vimrc
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set fileformat=unix 
set nobomb
set ff=unix
set ambiwidth=double 
set fileencodings=utf-8,ucs-bom,cp936
syntax on
filetype plugin on 
set nocompatible
set completeopt=preview
set ai 
set hls
set nu

猜你喜欢

转载自blog.csdn.net/xiegh2014/article/details/78883498