Skip to content

Instantly share code, notes, and snippets.

@Qblack
Created September 8, 2014 03:24
Show Gist options
  • Save Qblack/794020f38dbd29599d67 to your computer and use it in GitHub Desktop.
Save Qblack/794020f38dbd29599d67 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<title>How to Vagrant with flask</title>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="jumbotron"><h2>How I finally managed to combine, vagrant and flask</h2><br>Note this should work but not guarenteed </div>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h2>Step 1: Make virtual environment with virutalenv-wrapper, we are using python 2.7 for this</h2>
<pre>
mkvirtualenv -p 'C:\Python27\python.exe' FlaskPHI2.7
cd FlaskPHI2.7</pre>
<h2>Step 2: Make a basic folder, this will soon be a git repository</h2>
<pre>
mkdir flask_app
cd flask_app</pre>
<h2>Step 3: Setup vagrant</h2>
<pre>vagrant init avenuefactory/lamp</pre>
<h3>Edit the vagrant file</h3>
<p>
<h4> Establish a private network</h4>
<pre>config.vm.network "private_network", ip: "192.168.33.10"</pre>
<h4>Make the shared folder belong to www-data so that the app will not give permission errors</h4>
<pre>config.vm.synced_folder "./", "/vagrant", :owner=> 'vagrant', :group=>'www-data', :mount_options => ['dmode=775', 'fmode=664']</pre>
<h4>Create a file called <b>bootstrap.sh</b> this will install pip, flask, update, and mod_wsgi</h4>
It also makes a symbolic link to public_html from html in order for the site to work on port 80
<pre>
#!/usr/bin/env bash
sudo apt-get update
sudo apt-get install libapache2-mod-wsgi
sudo apt-get install python-pip
sudo pip install flask
sudo ln -fs /vagrant/public_html /var/www/html</pre>
<h4>Call the script from the VagrantFile</h4>
<pre>config.vm.provision :shell, path: "bootstrap.sh"</pre>
</p>
<h3>Install the vbguest plugin, this allows for proper sharing</h3>
<pre>vagrant plugin install vagrant-vbguest</pre>
<h3> Launch the vagrant environment</h3>
<pre>vagrant up --provision</pre>
<h3>Get the ssh details for your environment</h3>
<pre>vagrant ssh</pre>
<p>
Use putty gen to convert the given key to one that putty can use.<br>
Use putty to ssh into your virtual box.
</p>
<h3>One last step for sharing, run this command</h3>
<pre>sudo ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions'</pre>
Vagrant should now be ready to be played with.
<h2>Step 4: A basic flask app</h2>
<p>
<h3>Setup the folder structure in /vagrant</h3>
<pre>
cd ~
wget https://beagle.whoi.edu/redmine/attachments/download/579/flask_deployment_starter.tar.gz
tar zxvf flask_deployment_starter.tar.gz
tar zxvf flask_deployment_starter.tar.gz</pre>
You can also use your own but will need to change files as needed.
<h3>Now add the wsgi files</h3>
<pre>
cd wsgi
nano flasktest1.wsgi</pre>
<b>flasktest1.wsgi</b>
<pre>
import sys
sys.path.insert(0,'/vagrant/public_html/apps/flasktest')
from flasktest1 import app as application</pre>
<pre>nano flasktest2.wsgi</pre>
<b>flasktest2.wsgi</b>
<pre>
import sys
sys.path.insert(0,'/vagrant/public_html/apps/flasktest')
from flasktest2 import app as application</pre>
</p>
<h2>Step 5: Configure Apache</h2>
<h3>Add a virtual host to a new conf file. Make sure to change the email, server name, and that it points to the desired application</h3>
<pre>
cd /etc/apache2/sites-available
sudo nano flask-test.conf</pre>
<b>flask-test.conf</b>
<pre>
&#60VirtualHost *:80>
# ---- Configure VirtualHost Defaults ----
ServerName 192.168.33.10
ServerAdmin tech.phi@gmail.com
DocumentRoot /var/www/html/http
&#60Directory />
Options FollowSymLinks
AllowOverride None
&#60/Directory>
&#60Directory /var/www/html/http/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
&#60/Directory>
# ---- Configure WSGI Listener(s) ----
WSGIDaemonProcess flaskapp1Public user=www-data group=www-data threads=5
WSGIScriptAlias / /var/www/html/wsgi/flasktest1.wsgi
&#60Directory /var/www/html/http/>
WSGIProcessGroup flaskapp1Public
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
&#60/Directory>
# ---- Configure Logging ----
ErrorLog /var/www/html/logs/error.log
LogLevel warn
CustomLog /var/www/html/logs/access.log combined
&#60/VirtualHost>
</pre>
<h3>Now make the server listen for the port and virtual host</h3>
<pre>sudo nano /etc/apache2/ports.conf</pre>
<b>Add to /etc/apache2/ports.conf</b>
<pre>
NameVirtualHost *:80
Listen 80</pre>
<h3>Register the site with apache</h3>
<pre>sudo a2ensite flask-test</pre>
<h3>Reload the apache server</h3>
<pre>sudo /etc/init.d/apache2 reload</pre>
<h2>Step 6: Go to the ip in your browser and make sure it works</h2>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>Sources and References, may or may not be applicable</h1>
<h4>
<ul>
<li><a href="https://docs.vagrantup.com/v2/">Vagrant</a></li>
<li><a href="https://vagrantcloud.com/">VagrantCloud</a></li>
<li><a href="https://beagle.whoi.edu/redmine/projects/ibt/wiki/Deploying_Flask_Apps_with_Apache_and_Mod_WSGI">Deploying Flask Apps with Apache and Mod WSGI</a></li>
<li><a href="http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/">mod_wsgi (Apache) Flask Doc</a></li>
<li><a href="http://serverfault.com/questions/6895/whats-the-best-way-of-handling-permissions-for-apache2s-user-www-data-in-var">Add group, change mass permissions</a></li>
<li><a href="http://stackoverflow.com/questions/13169154/cannot-change-permissions-of-folders-within-vagrant-home-folder">Making stuff share</a></li>
<li><a href="https://github.com/mitchellh/vagrant/issues/3341">Fix Step by step Vbguest install</a></li>
<li><a href="https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps">Smaller App</a></li>
</ul>
</h4>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment