环境安装
以下的环境版本
1、vagrant_2.1.5_x86_64.msi
2、VirtualBox-5.1.0-108711-Win.exe
3、centos-7.2.box
安装VirtualBox
版本:VirtualBox-5.1.0-108711-Win
1、
2、
3、
4、
5、
6、
7、
安装vagrant
版本:vagrant_2.1.0_x86_64
1、
2、
3、
4、
5、重启下电脑
6、验证
配置启动虚拟机
在F盘新建devops 文件夹,将centos-7.2.box 拷贝到F:\devops 中
1、打开cmd
2、初始化
F:\devops>vagrant init centos-7.2.box
会在F:\devops中生产配置文件Vagrantfile,修改端口转发,访问主机的8000端口会调到虚拟机的8000端口
config.vm.network "forwarded_port", guest: 8000, host: 8000
3、启动:启动过程可能比较长,耐心等待
F:\devops>vagrant up
自动挂载
4、更改配置文件Vagrantfile启动端口
config.vm.network "forwarded_port", guest: 8000, host: 8000
报错如下:
解决:安装Windows6.1-KB2506143-x64.msu Windows6.1-KB2819745-x64-MultiPkg.msu
6、再次运行vagrant up
7、可以不用打开VirtualBox ,直接通过xshell连接虚拟机
可以通过key连接,key在F:\devops.vagrant\machines\default\virtualbox,导入这个key
实现了共享目录
虚拟机的镜像文件存在以下位置
虚拟机安装环境
1、[root@localhost tools]# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
2、yum install net-tools *sz openssl-devel readline-devel unzip vim -y
3、tar xf Python-3.6.6.tgz
4、cd Python-3.6.6
5、./configure --prefix=/usr/local/python36
6、make && make install
7、导入pip源
tee /etc/pip.conf <
[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
[list]
format=columns
EOF
[root@localhost Python-3.6.6]# vi /etc/pip.conf
[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
[list]
format=columns
8、新建虚拟环境
用普通用户创建
安装:
[vagrant@localhost ~]$ sudo /usr/local/python36/bin/pip3 install virtualenv
创建:
[vagrant@localhost ~]$ /usr/local/python36/bin/virtualenv ./python36env
进入:
[vagrant@localhost ~]$ source ./python36env/bin/activate
(python36env) [vagrant@localhost ~]$ pip list
Package Version
pip 18.1
setuptools 40.4.3
wheel 0.32.1
安装django
(python36env) [vagrant@localhost ~]$ pip install "django>=1.11,<2.0"
安装数据库:
(python36env) [vagrant@localhost ~]$ sudo yum -y install mariadb mariadb-server mariadb-devel
编辑:
vi /etc/my.ini
mysqld的模块中
default-storage-engine = innodb
innodb_file_per_table
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8’
character-set-server = utf8
启动:
(python36env) [vagrant@localhost ~]$ sudo systemctl start mariadb
(python36env) [vagrant@localhost ~]$ sudo systemctl enable mariadb
安装数据库模块:
(python36env) [vagrant@localhost ~]$ pip install mysqlclient
mysql初始化:
(python36env) [vagrant@localhost ~]$ mysql_secure_installation
(python36env) [vagrant@localhost ~]$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password: 123456
Re-enter new password: 123456
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
创建数据库:
(python36env) [vagrant@localhost ~]$ mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database devops CHARACTER SET utf8;
Query OK, 1 row affected (0.01 sec)
创建django项目:
(python36env) [vagrant@localhost ~]$ cd /vagrant/
(python36env) [vagrant@localhost vagrant]$ django-admin startproject devops
在vagrant中创建是因为这个目录关联到了window目录,这样可以在windows上开发
pycharm设置:
1、在pycharm中打开F:\devops
2、添加:
3、设置关联linux
当前目录必须和Vagrantfile同一个目录才行
4、设置数据库:
在settings中设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'devops',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': 3306,
'OPTIONS':{
'init_command': 'SET default_storage_engine=INNODB;',
},
}
}
5、验证:
创建app
1、将python3加入到环境变量中
(python36env) [vagrant@localhost ~]$ vi .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/vagrant/python36env/bin
export PATH
2、(python36env) [vagrant@localhost ~]$ source .bash_profile
3、(python36env) [vagrant@localhost ~]$ cd /vagrant/devops/
4、(python36env) [vagrant@localhost devops]$ python manage.py startapp dashboard
5、设置urls路径:
6、添加app
7、
验证:
数据格式为字典或者列表:
练习个登录操作
get:获取
post:提交
delete:删除
1、安装模块
pip install requests
2、编写url
3、编写view
4、在settings.py中禁用csrf功能
3、验证:
同步数据库
1、使用django命令行工具同步数据库
python manage.py makemigrations
python manage.py migrate
python manage.py showmigrations
创建用户
1、创建普通用户
2、查看数据库
3、创建管理员用户
4、查看,1表示管理员,0表示普通用户
5、修改密码