Vagrant及Ubuntu环境搭建

step 1. 安装Vagrant 1.9.5

step 2. 本地创建Django项目, 使用PyCharm 2017.1.4 创建CMS_Service并进入该目录

1
cd CMS_Service

step 3. 初始化Vagrantfile

1
vagrant init

step 4. 安装Ubuntu 16.04.2 LTS

1
vim Vagrantfile

替换第15内容config.vm.box = “base”为

1
config.vm.box = "ubuntu/trusty64"

打开第34行的注释,

1
config.vm.network "private_network", ip: "192.168.33.10"

保存Vagrantfile

step 5. 启动vagrant配置的Ubuntu系统

1
vagrant up

step 6. 进入vagrant配置好的Ubuntu系统

1
vagrant ssh

Python环境搭建

step 1. 备份系统镜像源

1
sudo mv  /etc/apt/sources.list  /etc/apt/sources.list.bak

step 2. 新建国内快速镜像源

1
sudo vim /etc/apt/sources.list

粘贴以下内容到文件中并保存

1
2
3
4
5
6
7
8
9
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse

step 3. 更新镜像源并安装必要的系统包

1
2
3
sudo apt-get update
sudo apt-get install git libpq-dev
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev

step 4. 安装Pyenv

1
curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

step 5. 配置Pyenv环境变量

1
sudo vim ~/.bashrc

添加以下内容并保存:

1
2
3
4
export PATH="/home/vagrant/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
source .bashrc

step 6. 安装Python 3.4.3

1
2
3
wget http://mirrors.sohu.com/python/3.4.3/Python-3.4.3.tar.xz -P ~/.pyenv/cache/;pyenv install 3.4.3 
pyenv local 3.4.3
pyenv global 3.4.3

step 7. 进入/vagrant, 安装virtualenv

1
pip install virtualenv

step 8. 创建虚拟环境并激活

1
2
pyenv virtualenv pycmsenv
pyenv activate pycmsenv

安装并创建Django

step 1. 安装Django

1
pip install Django==1.11.3

查看Django版本号

1
python -m django --version

step 2. 修改settings.py中的

1
ALLOWED_HOSTS = []

1
ALLOWED_HOSTS = ['*']

step 3. 启动项目

1
./manage.py runserver 0.0.0.0:8080

在本机通过http://192.168.33.10:8080/访问, 看到It worked!则成功了