使用Vagrant可以不必再麻烦的去配置宿主机与虚拟机之间的环境差异,也可直接使用宿主机ide编辑虚拟机文件。
这里使用win10作为宿主机,centos-7.2作为虚拟机,python3.6.4,搭建django开发环境。
Vagrant安装与环境配置
笔记环境为win10,以下链接下载Vargrant,无脑安装即可,Vargrant对virtualbox有依赖,安装前请安装virtualbox。
https://releases.hashicorp.com/vagrant/2.2.3/vagrant_2.2.3_x86_64.msi
以下链接下载Vagrant需要的vagrant-centos-7.2.box文件(官网提供其他系统版本的box文件)。
https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box
以上倆步完成后,cmd执行:
>vagrant -h #查看命令帮助
>vagrant box add yunwei vagrant-centos-7.2.box #用法vagrant box add [options] <name, url, or path>
>vagrant init yunwei #初始化一个新VM yunwei
>vagrant up #启动vm
设置宿主机Vagrantfile端口转发:
config.vm.network “forwarded_port”, guest: 8000, host: 8000
cmd执行重新读取配置:
vagrant reload
启动完成后就可以使用vagrant ssh对虚拟机进行ssh连接,这里我使用ssh工具进行连接,登录需要的相关信息会在‘vagrant up’后打印。
VM的环境配置
1、替换国内源,此处为centos7阿里源.
mkdir /etc/yum.repos.d/repobak && cp /etc/yum.repos.d/*repo . /etc/yum.repos.d/repobak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
2、安装python依赖环境,pip依赖openssl-devel,ipython自动提示依赖readline-devel
yum -y install openssl-devel readline-devel unzip
3、编译安装python
wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz#下载tgz文件
tar xvf Python-3.6.4.tgz#解压
cd Python-3.6.4/
./congure --prefix='安装路径'#编译
make && make install
4、修改pip配置
sudo tee /etc/pip.conf <<EOF
[global]
index-url =http://pypi.douban.com/simple
trusted-host=pypi.douban.com
[list]
format=columns
EOF
5、安装virtualenv并初始化虚拟环境
/usr/local/python36/bin/pip3 install virtualenv
/usr/local/python36/bin/virtualenv ./python36env#普通用户执行
安装mariadb 数据库和配置
sudo yum -y install mariadb mariadb-server mariadb-devel#安装
vi /etc/my.cnf#修改数据库配置文件如下配置
[mysql]
default-storage-engine = innodb
innodb_file_per_table
collation-server = utf8_general_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8
systemctl start mariadb#启动服务
systemctl enable mariadb#加入开机启动项
mysql_secure_installation#初始化数据库,密码默认为空,以下为打印信息,无脑y
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:
Re-enter new password:
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!
8、安装mysqlclient
pip install mysqlclient
创建数据库
mysql -u[user] -p[passwd] -e "create database django CHARACTER SET utf8"
安装django
进入虚拟环境
source ./python36env/bin/activate
pip install 'django>=1.11,<=2.0'#安装长期支持版本django>=1.11,<=2.0
初始化django环境
cd /vagrant/#进入映射目录,此目录文件可通过宿主机直接编辑
django-admin startproject devops #创建django项目
vi devops/seeting.py#修改数据库配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER': '[user]',
'PASSWORD': '[passwd]',
'HOST': '127.0.0.1',
'PORT': 3306,
'OPTIONS':{
'init_command': 'SET default_storage_engine=INNODB;',
},
}
}
python ./manage.py runserver 0.0.0.0:8000#启动服务
宿主机访问127.0.0.1:8000网页测试。
#pycharm直接访问环境box环境
file-setting-project interpreter-addremote-【选择vagrant,目录为映射目录下的虚拟环境中的python】
file-open-【选择django项目位置】
然后即可直接对文件进行编辑。