Skip to content

Instantly share code, notes, and snippets.

@ScottEvil
Last active December 9, 2015 19:11
Show Gist options
  • Save ScottEvil/ea11db3a748866bff6e2 to your computer and use it in GitHub Desktop.
Save ScottEvil/ea11db3a748866bff6e2 to your computer and use it in GitHub Desktop.
Pre-Install steps for Sahana Eden for Ubuntu 14.04
#!/bin/sh
# Script to turn a generic Debian Squeeze box into an Eden server
# with Apache & MySQL
# Update system
apt-get update
#apt-get upgrade -y
# Install Admin Tools
apt-get install -y unzip
# Install Git
apt-get install -y git-core
# Email
apt-get -y install exim4-config exim4-daemon-light
########
# MySQL
########
apt-get -y install mysql-server python-mysqldb phpmyadmin mytop
# If you run above manually, a UI pops up asking for mysql root pw and to choose the web server to automatically run phpmyadmin. I chose apache
# phpadmin then asks if you want to install default DB. I selected yes, following this post: http://www.liquidweb.com/kb/how-to-install-and-configure-phpmyadmin-on-ubuntu-14-04/
#Question to ask: In a production environment would we want phpmyadmin exposed to the outside world?
sudo nano /etc/apache2/apache2.conf
# Edit the file to add the following lines
# phpMyAdmin Configuration
Include /etc/phpmyadmin/apache.conf
service apache2 restart
# Tune for smaller RAM setups
sed -i 's|query_cache_size = 16M|query_cache_size = 1M|' /etc/mysql/my.cnf
sed -i 's|key_buffer = 16M|key_buffer = 1M|' /etc/mysql/my.cnf
sed -i 's|max_allowed_packet = 16M|max_allowed_packet = 1M|' /etc/mysql/my.cnf
/etc/init.d/mysql restart
#########
# Apache
#########
apt-get -y install libapache2-mod-wsgi
a2enmod rewrite
a2enmod deflate
a2enmod headers
a2enmod expires
# Enable Basic Authentication for WebServices
sed -i 's|</IfModule>|WSGIPassAuthorization On|' /etc/apache2/mods-enabled/wsgi.conf
echo "</IfModule>" >> /etc/apache2/mods-enabled/wsgi.conf
# Prevent Memory leaks from killing servers
sed -i 's|MaxRequestsPerChild 0|MaxRequestsPerChild 300|' /etc/apache2/apache2.conf
# Tune for smaller RAM setups
sed -i 's|MinSpareServers 5|MinSpareServers 3|' /etc/apache2/apache2.conf
sed -i 's|MaxSpareServers 10|MaxSpareServers 6|' /etc/apache2/apache2.conf
apache2ctl restart
# Holding Page for Maintenance windows
cat << EOF > "/var/www/maintenance.html"
<html><body><h1>Site Maintenance</h1>Please try again later...</body></html>
EOF
#########
# Python
#########
# Install Libraries
apt-get -y install libgeos-c1
# Install Python
#apt-get -y install python2.6
#apt-get -y install python-dev
# 100 Mb of diskspace due to deps, so only if you want an advanced shell
#apt-get -y install ipython
apt-get -y install python-lxml python-setuptools python-dateutil
#apt-get -y install python-serial
apt-get -y install python-imaging python-reportlab
apt-get -y install python-matplotlib
apt-get -y install python-requests
apt-get -y install python-xlwt python-xlrd
# Upgrade Shapely for Simplify enhancements
#apt-get remove -y python-shapely
apt-get -y install libgeos-dev
wget --no-check-certificate http://pypi.python.org/packages/source/S/Shapely/Shapely-1.5.6.tar.gz
tar zxvf Shapely-1.5.6.tar.gz
cd Shapely-1.5.6
python setup.py install
cd ..
#########
# Web2Py
#########
apt-get -y install libodbc1
# Install Web2Py
adduser --system --disabled-password web2py
addgroup web2py
cd /home
# @ToDo: Stable release once 2.0 released
#wget http://www.web2py.com/examples/static/web2py_src.zip
#unzip web2py_src.zip
git clone --recursive git://github.com/web2py/web2py.git
ln -s /home/web2py ~
cp -f /home/web2py/handlers/wsgihandler.py /home/web2py
cat << EOF > "/home/web2py/routes.py"
#!/usr/bin/python
#ended up using nano to do this
default_application = 'eden'
default_controller = 'default'
default_function = 'index'
routes_onerror = [
('eden/400', '!'),
('eden/401', '!'),
('eden/509', '!'),
('eden/*', '/eden/errors/index'),
('*/*', '/eden/errors/index'),
]
EOF
# Configure Matplotlib
mkdir /home/web2py/.matplotlib
chown web2py /home/web2py/.matplotlib
echo "os.environ['MPLCONFIGDIR'] = '/home/web2py/.matplotlib'" >> /home/web2py/wsgihandler.py
sed -i 's|TkAgg|Agg|' /etc/matplotlibrc
##############
# Sahana Eden
##############
# Install Sahana Eden
cd web2py
cd applications
# @ToDo: Stable branch
git clone git://github.com/flavour/eden.git
# Fix permissions
#NOTE: had to add folders so that the install doesn't error out
chown web2py ~web2py
mkdir -p ~web2py/applications/admin/cache
chown web2py ~web2py/applications/admin/cache
chown web2py ~web2py/applications/admin/cron
mkdir -p ~web2py/applications/admin/databases
chown web2py ~web2py/applications/admin/databases
mkdir -p ~web2py/applications/admin/errors
chown web2py ~web2py/applications/admin/errors
mkdir -p ~web2py/applications/admin/sessions
chown web2py ~web2py/applications/admin/sessions
chown web2py ~web2py/applications/eden
mkdir -p ~web2py/applications/eden/cache
chown web2py ~web2py/applications/eden/cache
chown web2py ~web2py/applications/eden/cron
mkdir -p ~web2py/applications/eden/databases
chown web2py ~web2py/applications/eden/databases
mkdir -p ~web2py/applications/eden/errors
chown web2py ~web2py/applications/eden/errors
chown web2py ~web2py/applications/eden/models
mkdir -p ~web2py/applications/eden/sessions
chown web2py ~web2py/applications/eden/sessions
chown web2py ~web2py/applications/eden/static/fonts
chown web2py ~web2py/applications/eden/static/img/markers
mkdir -p ~web2py/applications/eden/static/cache/chart
chown web2py -R ~web2py/applications/eden/static/cache
mkdir -p ~web2py/applications/eden/uploads
chown web2py ~web2py/applications/eden/uploads
mkdir -p ~web2py/applications/eden/uploads/gis_cache
mkdir -p ~web2py/applications/eden/uploads/images
mkdir -p ~web2py/applications/eden/uploads/tracks
chown web2py ~web2py/applications/eden/uploads/gis_cache
chown web2py ~web2py/applications/eden/uploads/images
chown web2py ~web2py/applications/eden/uploads/tracks
ln -s /home/web2py/applications/eden ~
#####################
# Management scripts
#####################
cat << EOF > "/usr/local/bin/backup"
#!/bin/sh
NOW=\$(date +"%Y-%m-%d")
mysqldump sahana > /root/backup-\$NOW.sql
gzip -9 /root/backup-\$NOW.sql
OLD=\$(date --date='7 day ago' +"%Y-%m-%d")
rm -f /root/backup-\$OLD.sql.gz
EOF
chmod +x /usr/local/bin/backup
cat << EOF > "/usr/local/bin/compile"
#!/bin/sh
cd ~web2py
python web2py.py -S eden -M -R applications/eden/static/scripts/tools/compile.py
apache2ctl restart
EOF
chmod +x /usr/local/bin/compile
cat << EOF > "/usr/local/bin/maintenance"
#!/bin/sh
# Script to activate/deactivate the maintenance site
# Can provide the option 'off' to disable the maintenance site
if [ "\$1" != "off" ]; then
# Stop the Scheduler
killall python
# Deactivate the Production Site
a2dissite production
# Activate the Maintenance Site
a2ensite maintenance
else
# Deactivate the Maintenance Site
a2dissite maintenance
# Activate the Production Site
a2ensite production
# Start the Scheduler
cd ~web2py && sudo -H -u web2py nohup python web2py.py -K eden -Q >/dev/null 2>&1 &
fi
apache2ctl restart
EOF
chmod +x /usr/local/bin/maintenance
cat << EOF > "/usr/local/bin/pull"
#!/bin/sh
cd ~web2py/applications/eden
sed -i 's/settings.base.migrate = False/settings.base.migrate = True/g' models/000_config.py
git pull
/usr/local/bin/maintenance
rm -rf compiled
cd ~web2py
sudo -H -u web2py python web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py
cd ~web2py/applications/eden
sed -i 's/settings.base.migrate = True/settings.base.migrate = False/g' models/000_config.py
/usr/local/bin/compile
/usr/local/bin/maintenance off
EOF
chmod +x /usr/local/bin/pull
# Change the value of prepopulate, if-necessary
cat << EOF > "/usr/local/bin/clean"
#!/bin/sh
/usr/local/bin/maintenance
cd ~web2py/applications/eden
rm -f databases/*
rm -rf errors
rm -rf sessions
rm -rf uploads
sed -i 's/settings.base.migrate = False/settings.base.migrate = True/g' models/000_config.py
sed -i 's/settings.base.prepopulate = 0/#settings.base.prepopulate = 0/g' models/000_config.py
rm -rf compiled
mysqladmin -f drop sahana
mysqladmin create sahana
cd ~web2py
sudo -H -u web2py python web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py
cd ~web2py/applications/eden
sed -i 's/settings.base.migrate = True/settings.base.migrate = False/g' models/000_config.py
sed -i 's/#settings.base.prepopulate = 0/settings.base.prepopulate = 0/g' models/000_config.py
/usr/local/bin/maintenance off
/usr/local/bin/compile
EOF
chmod +x /usr/local/bin/clean
cat << EOF > "/usr/local/bin/w2p"
#!/bin/sh
cd ~web2py
python web2py.py -S eden -M
EOF
chmod +x /usr/local/bin/w2p
# END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment