Slide 1

Slide 1 text

HOSTING COMPLEX WEB APPLICATIONS ON WEBFACTION SERVERS Martin Brochhaus (@mbrochh) PyCon Singapore 2013 Saturday, 15 June, 13

Slide 2

Slide 2 text

Martin Brochhaus Founder of Bitmazk Pte. Ltd. @mbrochh [email protected] Saturday, 15 June, 13

Slide 3

Slide 3 text

WHY WEBFACTION? • https://www.webfaction.com/features • Very fast setup (< 1 hour) & affordable (102 USD / year) • Server centers in Texas, Amsterdam and Singapore • One click installers (Django, Git) • Don’t worry about Apache, Nginx setup • Great Control Panel, don’t worry about DNS setup • Insanely great support team / strong community Saturday, 15 June, 13

Slide 4

Slide 4 text

WHY NOT WEBFACTION? • Only 265 MB RAM (long running processes can be tricky) • Probably not the fastest CPUs? • No root access (obviously, because shared hosting) • Shared hosting (other users can slow down your instance) Saturday, 15 June, 13

Slide 5

Slide 5 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 6

Slide 6 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 7

Slide 7 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 8

Slide 8 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 9

Slide 9 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 10

Slide 10 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 11

Slide 11 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 12

Slide 12 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 13

Slide 13 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 14

Slide 14 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 15

Slide 15 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 16

Slide 16 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 17

Slide 17 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 18

Slide 18 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 19

Slide 19 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 20

Slide 20 text

WHAT IS POSSIBLE? Saturday, 15 June, 13

Slide 21

Slide 21 text

SIGNUP • https://www.webfaction.com/?affiliate=bitmazk • Server is usually ready in less than an hour • You will get an email with username and password • Let’s begin! Saturday, 15 June, 13

Slide 22

Slide 22 text

CHANGE PASSWORDS • https://my.webfaction.com/password • Change Control Panel password (for now:pyconsgpw) • Change SSH password (for now: pyconsgpw) Saturday, 15 June, 13

Slide 23

Slide 23 text

DEPLOY SSH KEY • Create your RSA-Key: ssh-keygen -t rsa • Deploy your RSA-Key ssh [email protected] mkdir .ssh touch .ssh/authorized_keys chmod 600 .ssh/authorized_keys chmod 700 .ssh/authorized_keys Saturday, 15 June, 13

Slide 24

Slide 24 text

WEBSITES, APPS & DOMAINS WEBSITE (i.e. myproject) DOMAIN (i.e. www.example.com) Django App (/) Static App (/static/) Saturday, 15 June, 13

Slide 25

Slide 25 text

SETUP YOUR DOMAINS • https://my.webfaction.com/domains • For our testing purpose just add www.username.webfactional.com Saturday, 15 June, 13

Slide 26

Slide 26 text

SETUP YOUR APPS • https://my.webfaction.com/applications • Delete htdocs • Add git (Latest git) • Add username_django (Latest Django) • Add username_static (Static / Static only / expires max) • Add username_media (Static / Static only / expires max) • Add username_www (Static / Static/CGI/PHP-5.4) • Add username_no_ssl (Static / Static/CGI/PHP-5.4) Saturday, 15 June, 13

Slide 27

Slide 27 text

SETUP YOUR WEBSITES • https://my.webfaction.com/websites • Edit the existing one • Enable HTTPS • Add django / media / static apps • Create new one (username_nossl) • Add all domains • Add no_ssl app • Create new one (username_www) • Enable HTTPS • Add www domains • Add www app Saturday, 15 June, 13

Slide 28

Slide 28 text

SETUP MAILBOX • https://my.webfaction.com/mailboxes • Name it (i.e. pyconsg001) • Note down the password (for now: emailpw) Saturday, 15 June, 13

Slide 29

Slide 29 text

SETUP DATABASE • https://my.webfaction.com/databases • Name it (i.e. pyconsg001) • Select PostgreSQL • Create User () • Note down the password (for now: dbpw) Saturday, 15 June, 13

Slide 30

Slide 30 text

BEFORE WE START... USE TMUX! • Start tmux with `tmux` • Create new window with `CTRL+a c` • Toggle window with `CTRL+a X` where X is the window ID • List all windows with `CTRL+a w` • List all tmux sessions with `tmux ls` • Re-attach a session with `tmux attach-session -t X` where X is the session ID Saturday, 15 June, 13

Slide 31

Slide 31 text

INITIAL SERVER SETUP • (on the server) git clone git://github.com/bitmazk/webfaction-dotfiles.git ./webfaction-dotfiles/bin/init.sh zsh source .zshrc git config --global user.email "[email protected]" git config --global user.name "Your Name" Saturday, 15 June, 13

Slide 32

Slide 32 text

PLEASE BE PATIENT... Picture: Pauline Mak Saturday, 15 June, 13

Slide 33

Slide 33 text

INITIATE GIT REPOSITORY • (on the server) cd webapps/git git init --bare repos/pyconsg001.git Saturday, 15 June, 13

Slide 34

Slide 34 text

INITIATE DJANGO PROJECT • (on the server) cd src git clone git://github.com/bitmazk/django-project-template.git cd vim init.sh # Change the project name to your username ./init.sh git remote add origin ~/webapps/git/repos/.git git push -u origin master cd mkvirtualenv pip install -r requirements.txt Saturday, 15 June, 13

Slide 35

Slide 35 text

PIP WILL TAKE A WHILE Picture: Pauline Mak Saturday, 15 June, 13

Slide 36

Slide 36 text

A WORD ABOUT VIRTUALENV Global packages: /usr/lib/python2.7/site- packages see Stackoverflow Local packages: $HOME/lib/python2.7/ Virtual Envs: $HOME/Envs/envname/lib/ python2.7/site-packages Saturday, 15 June, 13

Slide 37

Slide 37 text

PREPARE FIRST DEPLOYMENT • (on the server) cp -rf ~/src/ ~/webapps/_django/ cd ~/webapps/_django rm -rf myproject rm -rf lib/python2.7/* vim apache2/conf/httpd.conf • Replace “myproject” with “” • Replace .../python2.7 path with ~/Envs//lib/ python2.7/site-packages Saturday, 15 June, 13

Slide 38

Slide 38 text

CREATE LOCAL_SETTINGS.PY • (on the server) cd ~/webapps/_django//settings cp local_settings.py.sample local_settings.py • Add yourself to admins • Replace database password • Uncomment email settings and set email password Saturday, 15 June, 13

Slide 39

Slide 39 text

TRY COLLECTSTATIC • (on the server) cd ~/webapps/_django/ workon ./manage.py collectstatic Saturday, 15 June, 13

Slide 40

Slide 40 text

INITIALIZE DATABASE • (on the server) cd ~/webapps/_django/ workon ./manage.py syncdb --all ./manage.py migrate --fake Saturday, 15 June, 13

Slide 41

Slide 41 text

CREATE .PGPASS FILE • (on the server) cd $HOME cp src//scripts/.pgpass . chmod 600 .pgpass vim .pgpass • Replace the values with your database credentials Saturday, 15 June, 13

Slide 42

Slide 42 text

TEST SERVER SCRIPTS • (on the server) cd ~/bin ./pg-backup.sh ./locale-backup.sh ./media-backup.sh ./django-cleanup.sh ./django-retry-deferred.sh ./django-send-email.sh Saturday, 15 June, 13

Slide 43

Slide 43 text

ADD CRONTAB • (on the server) cp ~/src//scripts/crontab.txt $HOME vim crontab.txt * crontab crontab.txt rm crontab.txt • * Replace MAILFROM and MAILTO in crontab.txt Saturday, 15 June, 13

Slide 44

Slide 44 text

DEPLOY WEBSITE • (on the server) cd ~/src// workon fab prod run_deploy_website • Browse to https://.webfactional.com/p/home.html • See https://github.com/bitmazk/django-development-fabfile Saturday, 15 June, 13

Slide 45

Slide 45 text

TRY THE REDIRECTS • They are already copied into webapps/username_www and webapps/username_no_ssl • Open http://username.webfactional.com • Open https://www.username.webfactional.com • Open http://www.username.webfactional.com Saturday, 15 June, 13

Slide 46

Slide 46 text

TEST ADMIN HONEYPOT & EMAIL SENDING • Your admin url is set to `/admin-XXXX/` • Visit `/admin-1234/` and try to login • You should get an email warning • You should not be able to login :) Saturday, 15 June, 13

Slide 47

Slide 47 text

OTHER USEFUL THINGS Picture: Elvert Barnes Saturday, 15 June, 13

Slide 48

Slide 48 text

EMERGENCY DEBUGGING • django_settings.py has a file system logger defined • use it like so, somewhere in your code: import logging logger = logging.getLogger('debugger') logger.debug('Some debug output') • don’t forget restart-apache or touch wsgi.py Saturday, 15 June, 13

Slide 49

Slide 49 text

INSTALL SSL CERTIFICATE • Without SSL logging into your site is not secure • http://startssl.com/ • http://martinbrochhaus.com/ssl.html Saturday, 15 June, 13

Slide 50

Slide 50 text

SETUP LOCAL DEV ENVIRONMENT • (on local machine) git clone [email protected]:/ home/username/webapps/git/repos/username.git • cd username/username mkvirtualenv -p python2.7 username pip install -r requirements.txt # create your local_settings.py fab create_db ./manage.py syncdb --all ./manage.py migrate --fake ./manage.py runserver Saturday, 15 June, 13

Slide 51

Slide 51 text

THANK YOU! https://github.com/bitmazk/webfaction-dotfiles https://github.com/bitmazk/django-project-template https://github.com/bitmazk/django-development-fabfile/ @mbrochh Saturday, 15 June, 13