1、virtualbox安装路径需要写到环境变量
否则会出现下面报错:
Vagrant could not detect VirtualBox! Make sure VirtualBox is properly installed.
Vagrant uses the VBoxManage
binary that ships with VirtualBox, and requires this to be available on the PATH.
If VirtualBox is installed, please find the VBoxManage
binary and add it to the PATH environmental variable.
D:\devops\data>vagrant box add centos7.2 vagrant-centos-7.2.box
D:\devops\data>vagrant box list
centos7.2 (virtualbox, 0)
D:\devops\data>vagrant init centos7.2
生成一个Vagrantfile文件:
vagrant up (启动虚拟机)
vagrant halt (关闭虚拟机——对应就是关机)
vagrant suspend (暂停虚拟机——只是暂停,虚拟机内存等信息将以状态文件的方式保存在本地,可以执行恢复操作后继续使用)
vagrant resume (恢复虚拟机 —— 与前面的暂停相对应)
vagrant box remove centos6.6 (移除box,其中centos6.6是box名)
vagrant destroy (删除虚拟机,删除后在当前虚拟机所做进行的除开Vagrantfile中的配置都不会保留)
Vagrant连接:
D:\devops\data>vagrant ssh
[[email protected] ~]$
注意关机之前都要关闭下虚拟机。如果不关闭的话 明天早上起来就发现启动不了
在.Vagrantfile文件中设置:
# 设置用户名以及密码:
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
#设置虚拟网络
config.vm.network "private_network", ip: "192.168.33.10"
Vagrant.configure("2") do |config|
config.vm.box = "centos7.2"
config.vm.network "forwarded_port", guest: 8000, host: 8000
end
使用连接:
ssh 127.0.0.1:2222 用户名vagrant 密码vagrant
环境准备:django 1.11 python3.6
#安装python:
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
#安装依赖环境:
yum install openssl-devel readline-devel unzip -y
tar -zxf Python-3.6.6.tgz
cd Python-3.6.6
./configure --prefix=/usr/local/python36
make && make install
配置pip源:
tee /etc/pip.conf <<EOF
[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
[list]
format=columns
EOF
========================================================
安装虚拟环境:
将Linux里面的/home/vagrant 映射到 win的D:\devops-8\data了
报错:/sbin/mount.vboxsf: mounting failed with the error: No such device
解决办法:
yum clean all
yum update
yum install kernel kernel-devel kernel-headers gcc make -y
reboot
cd /opt/VBoxGuestAdditions-*/init
./vboxadd setup
reboot
[[email protected] ~]$ df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 8.9G 5.4G 3.5G 61% /
devtmpfs devtmpfs 305M 0 305M 0% /dev
tmpfs tmpfs 322M 0 322M 0% /dev/shm
tmpfs tmpfs 322M 5.1M 317M 2% /run
tmpfs tmpfs 322M 0 322M 0% /sys/fs/cgroup
/dev/sda1 xfs 521M 221M 301M 43% /boot
none vboxsf 149G 49G 101G 33% /media/sf_data
tmpfs tmpfs 65M 0 65M 0% /run/user/1000
[[email protected] ~]$ sudo ln -s /media/sf_data /home/vagrant
===========================================================
出现报错:
[[email protected] vagrant]# /usr/local/python36/bin/virtualenv ./python36env
Using base prefix '/usr/local/python36'
New python executable in /vagrant/python36env/bin/python3.6
Also creating executable in /vagrant/python36env/bin/python
Traceback (most recent call last):
File "/usr/local/python36/bin/virtualenv", line 11, in <module>
sys.exit(main())
File "/usr/local/python36/lib/python3.6/site-packages/virtualenv.py", line 762, in main
symlink=options.symlink,
File "/usr/local/python36/lib/python3.6/site-packages/virtualenv.py", line 998, in create_environment
install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages=site_packages, clear=clear, symlink=symlink)
File "/usr/local/python36/lib/python3.6/site-packages/virtualenv.py", line 1459, in install_python
os.symlink(py_executable_base, full_pth)
OSError: [Errno 71] Protocol error: 'python3.6' -> '/vagrant/python36env/bin/python'
解决方法:
export PIPENV_VENV_IN_PROJECT = 1
export PIPENV_IGNORE_VIRTUALENVS = 1
export VIRTUALENV_ALWAYS_COPY = 1
/usr/local/python36/bin/virtualenv --always-copy ./python36env
=====================================================================
[[email protected] ~]$ sudo /usr/local/python36/bin/pip3 install virtualenv
[[email protected] ~]$ /usr/local/python36/bin/virtualenv ./python36env
[[email protected] ~]$ source ./python36env/bin/activate
(python36env) [[email protected] ~]$ pip list
Package Version
---------- -------
pip 18.1
setuptools 40.6.2
wheel 0.32.3
(python36env) [[email protected] ~]$ pip install "django>=1.11,<2.0"
Successfully installed django-1.11.17 pytz-2018.7
安装数据库
(python36env) [[email protected] ~]$ sudo yum -y install mariadb mariadb-server mariadb-devel
(python36env) [[email protected] ~]$ sudo vim /etc/my.cnf
[mysqld]
default-storage-engine = innodb
innodb_file_per_table
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8
(python36env) [[email protected] ~]$ sudo systemctl start mariadb
(python36env) [[email protected] ~]$ sudo systemctl enable mariadb
(python36env) [[email protected] ~]$ pip install mysqlclient
初始化数据库
(python36env) [[email protected] ~]$ mysql_secure_installation
(python36env) [[email protected] ~]$ mysql -uroot -pAbcd.1234
MariaDB [(none)]> create database devops CHARACTER SET utf8;
创建Django项目:
(python36env) [[email protected] ~]$ django-admin startproject devops
配置pycharm选编译器 setting -> Project:devops -> Project Interpreter
连接mysql数据库:
devops/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'devops',
'USER': 'root',
'PASSWORD': 'Abcd.1234',
'HOST': '127.0.0.1',
'PORT': 3306,
'OPTIONS':{
'init_command':'SET default_storage_engine=INNODB;',
},
}
}
启动服务:
方法一:
(python36env) [[email protected] devops]$ python manage.py runserver 0.0.0.0:8000
方法二:
在pycharm的manage.py点击run的Edit Configurations,添加参数,然后再运行manage.py
(python36env) [[email protected] devops]$ tree
.
├── devops
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── settings.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── wsgi.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
2 directories, 9 files
最外层的devops/目录是项目的一个容器
manage.py一个实用的命令行工具,可让你以各种方式与该Django项目进行交付;
内层的devops/目录是项目中的实际Python包,通过它可以导入它里面的任何东西;
devops/init.py:一个空文件,告诉Python该目录是一个Python包;
devops/setting.py:该Django项目的配置文件;
devops/urls.py:该Django项目的URL声明;
devops/wsgi.py:一个WSGI兼容的Web服务器入口;
使用APPS
步骤一:新建Django app名为dashboard
(python36env) [[email protected] devops]$ python manage.py startapp dashboard
(python36env) [[email protected] devops]$ cd dashboard/
(python36env) [[email protected] dashboard]$ tree
.
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│ └── __init__.py
├── models.py
├── tests.py
└── views.py
1 directory, 7 files
步骤二:让项目调用apps,配置URL:
devops/urls.py
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^dashboard/',include("dashboard.urls")),
]
步骤三:**APP:
devops/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dashboard',
]
步骤四:编写视图函数
devops/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello World!")
步骤五:配置URL
dashoard/urls.py
from django.conf.urls import url,include
from .views import index
urlpatterns = [
url(r'^$', index, name='index'),
]