Skip to content

Instantly share code, notes, and snippets.

@paulera
Last active June 14, 2023 17:50
Show Gist options
  • Save paulera/bb755df9ec772e75bea1d692873c6204 to your computer and use it in GitHub Desktop.
Save paulera/bb755df9ec772e75bea1d692873c6204 to your computer and use it in GitHub Desktop.
Script to install python3 + virtualenv + flask, and setup passenger, for Dreamhost shared hosting.
#!/bin/bash
# ---------- SETUP --------------
SITE="unforgivenexception.com"
APP_FOLDER="app"
VIRTUALENV_FOLDER="env"
PYTHON_VERSION="3.5.6"
# ----------------------------------------------
#
# What does this script do?
#
# TL;DR; Installs a flask site in a blank shared hosting in Dreamhost
#
# Lng version:
# 1) Download of python version: $PYTHON_VERSION to ~/tmp
# 2) Install python in ~/opt/python-$PYTHON_VERSION
# 3) Add this python installation to the $PATH
# 4) Install virtualenv
# 5) Setup virtualenv inside the project, in the dir $VIRTUALENV_FOLDER
# 6) Activate the virtualenv and install Flask
# 7) Create required files passenger_wsgi.py and .htaccess
# 8) Create a sample Flask entrypoint to have something to start with
# 9) Create some handy scripts (see below)
#
# Site structure:
#
# ~/yoursite.com - Project root.
# ~/yoursite.com/$VIRTUALENV_FOLDER - virtualenv folder.
# ~/yoursite.com/$APP_FOLDER - Project app code. This is where index.py lives.
# ~/yoursite.com/$APP_FOLDER/index.py - Sample flask entrypoint
# ~/yoursite.com/public - Public assets (CSS, JS, images, static html)
# ~/yoursite.com/tmp - create a file restart.txt in there to restart the server in the next request
#
# Scritps:
#
# ~/yoursite.com/restart - Script to do the above for you (the restart.txt thing)
# ~/yoursite.com/activate - Executable symlink to start virtualenv
# ~/yoursite.com/monitor - Monitors the $APP_FOLDER dir and restart the server when there are source code changes
#
# You find info about what you have to do in Dreamhost's panel here:
# https://inventwithpython.com/blog/2017/03/14/how-to-install-django-1106-and-python-360-on-dreamhost-shared-hosting/
#
# References:
# * https://mattcarrier.com/flask-dreamhost-setup/
# * http://www.sureshjoshi.com/development/python-web-framework-on-dreamhost/
# * https://stackoverflow.com/questions/17778894/flask-passenger-wsgi-on-dreamhost
# * https://inventwithpython.com/blog/2017/03/14/how-to-install-django-1106-and-python-360-on-dreamhost-shared-hosting/
#
# -----------------------------------------------
USER=$(whoami)
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_TYPE=en_US.UTF-8
mkdir -p ~/tmp
cd ~/tmp
wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz
tar zxvf Python-$PYTHON_VERSION.tgz
cd Python-$PYTHON_VERSION
./configure --prefix=$HOME/opt/python-$PYTHON_VERSION
make
make install
BASH_PROFILE_LINE="export PATH=$HOME/opt/python-$PYTHON_VERSION/bin:$PATH"
if [ "$(grep "$BASH_PROFILE_LINE" ~/.bash_profile)" == "" ]; then
echo $BASH_PROFILE_LINE >> ~/.bash_profile
echo
echo "LINE ADDED TO ~/.bash_profile"
echo
fi
$BASH_PROFILE_LINE
pip3 install virtualenv
mkdir -p ~/$SITE/$APP_FOLDER
cd $HOME/$SITE
virtualenv -p $HOME/opt/python-$PYTHON_VERSION/bin/python3 $VIRTUALENV_FOLDER
chmod u+x,g+x $HOME/$SITE/$VIRTUALENV_FOLDER/bin/activate
ln -s $HOME/$SITE/$VIRTUALENV_FOLDER/bin/activate activate
echo "touch \$( dirname \"\${BASH_SOURCE[0]}\" )/tmp/restart.txt" > $HOME/$SITE/restart
chmod u+x,g+x $HOME/$SITE/restart
. activate
pip3 install Flask
deactivate
echo -e "DH_WORKING_DIR = '/home/$USER/$SITE/'
import sys,os
cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(os.path.join(DH_WORKING_DIR, '$APP_FOLDER'))
sys.path.append(os.path.join(DH_WORKING_DIR, 'env/bin'))
sys.path.append(os.path.join(DH_WORKING_DIR, 'env/lib/python3.5/site-packages'))
from index import *" > $HOME/$SITE/passenger_wsgi.py
echo 'PassengerPython "/home/'$USER'/'$SITE'/'$VIRTUALENV_FOLDER'/bin/python"' > $HOME/$SITE/.htaccess
INDEX="from flask import Flask
application = Flask(__name__)
@application.route('/')
def index():
return 'Hello YoLo!'"
mkdir -p $HOME/$SITE/$APP_FOLDER
INDEX_FILE="$HOME/$SITE/$APP_FOLDER/index.py"
INDEX_FILE_2="$HOME/$SITE/$APP_FOLDER/index.sample.py"
if [ ! -f "$INDEX_FILE" ]; then
echo "$INDEX" > $INDEX_FILE
echo
echo "CREATED SAMPLE FILE $INDEX_FILE"
echo
else
echo
echo "I WAS GOING TO CREATE A SAMPLE $INDEX_FILE FILE, BUT THERE IS ONE ALREADY!"
if [ ! -f "$INDEX_FILE_2" ]; then
echo "I WILL SAVE IT TO $INDEX_FILE_2"
echo "$INDEX" > $INDEX_FILE_2
else
echo "$INDEX_FILE_2 ALSO EXISTS."
echo "NOW YOU ARE ON YOUR OWN."
echo
echo "---------- [index.py] ----------"
echo "$INDEX"
echo "--------------------------------"
echo
fi
fi
echo "
#!/bin/bash
ROOT_FOLDER=\$( dirname \"\${BASH_SOURCE[0]}\" )
APP_FOLDER=\$ROOT_FOLDER/$APP_FOLDER
echo \"I will restart the server for you when files change in \$APP_FOLDER\"
getlast() {
echo \"\$(find \$APP_FOLDER -path '*/__pycache__' -prune -o -type f -printf '%T@ %TY-%Tm-%Td %TH:%TM:%.2TS %P\n' | sort -nr | head -n 1 | cut -f2- -d' ')\"
}
restart() {
touch \$ROOT_FOLDER/tmp/restart.txt
}
LAST_CHANGE=\$(getlast)
CURRENT_CHANGE=\"\"
restart
while :; do
CURRENT_CHANGE=\$(getlast)
if [ \"\$CURRENT_CHANGE\" != \"\$LAST_CHANGE\" ]; then
echo \$CURRENT_CHANGE
LAST_CHANGE=\$CURRENT_CHANGE
restart
fi
sleep 1
done
" > $HOME/$SITE/monitor
chmod u+x,g+x $HOME/$SITE/monitor
mkdir -p $HOME/$SITE/tmp
touch $HOME/$SITE/tmp/restart.txt
@jcouturier
Copy link

This is incredibly helpful!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment