Vagrant is a tool that you can use to set up, configure, and access a VM through the command line. This is a life changer. I love it. In this post I’ll walk through setting up an OEL6 virtual machine, installing a non-ancient version of Python, and configuring the port forwarding so that you can use it for backend web development. (The port forwarding is not obvious on RHEL/OEL.)
Install VirtualBox and Vagrant
First, install VirtualBox, and then install Vagrant. Make a new directory somewhere for a virtual machine. I put a “boxes/
” directory on my Desktop with subdirectories for different VMs.
mkdir -p Desktop/boxes/OEL6 cd Desktop/boxes/OEL6
Set OEL6 with Port Forwarding
First, you’ll need to get the Vagrant box that you want. Boxes can be found at Vagrant’s box atlas. I’ll be using an Oracle Enterprise Linux v6 box, so I’ll run the following:
vagrant box add elastic/oel-6-x86_64
Next, open an editor and create a Vagrantfile. This will be used to configure your VM. My Vagrantfile looks like this:
Vagrant.configure("2") do |config| config.vm.box = "elastic/oel-6-x86_64" config.vm.provision :shell, path: "bootstrap.sh" config.vm.network "forwarded_port", guest: 5000, host: 3000 end
The second line tells Vagrant which box to use.
The third line specifies a shell script that should be run once the VM is up and running. That shell script is listed in the next section.
In the fourth line, I have connected the 127.0.0.1:5000
and 0.0.0.0:5000
ports on my guest VM to the localhost:3000
port on my host machine. In order to host my web app on my guest VM and make it available on my host machine, I bind the app to the 0.0.0.0
address, instead of the 127.0.0.1
address. The 5000
port number is the default value chosen by Flask. This can be changed in your Flask server on the app.run(...)
line, but I opted to stick with the default. I picked 3000
on my host machine for no other reason than the fact that that is the default port of the Python SimpleHTTPServer.
bootstrap.sh
This is the bootstrap.sh
shell script mentioned in the Vagrantfile above.
#!/bin/sh # prerequisites sudo yum -y groupinstall "Development tools" sudo yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel # install python 2.7.9 at /usr/local/bin/python2,7 wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz tar -xvf Python-2.7.9.tgz cd Python-2.7.9 ./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" make sudo make altinstall # install setuptools and pip wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py sudo /usr/local/bin/python2.7 ez_setup.py sudo /usr/local/bin/easy_install-2.7 pip # install flask sudo /usr/local/bin/pip2.7 install flask # turn off iptables sudo service iptables stop
Get Up and Running
Finally, kick things off by saying:
vagrant up --provision
And then access your VM by saying:
vagrant ssh
From here you can open Vim and write the following to /vagrant/hello.py
:
#!/usr/local/bin/python2.7 from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run( host="0.0.0.0", port=5000 )
And then run it from within vagrant as:
/usr/local/bin/python2.7 /vagrant/hello.py
When you are done, you can exit by saying, “exit
“. If you want to reload vagrant, say, “vagrant reload
“, or if you want to destroy a VM and start over, then say, “vagrant destroy
“.
The “/vagrant/
” directory in the VM is the same as the directory on your host machine that holds you Vagrantfile. That means you don’t have to monkey around with setting up a local directory in the VM, it’s just there in a nice predictable place, ready to go.
Once your server is up and running in your new VM, you can access the server by saying, “curl localhost:3000
” on your host machine, and you should see a “Hello World!
” message.