1 of 42

A Noob Speaks to Noobs:�

Your first site in the cloud

Katie Cunningham

Cox Media Group

2 of 42

Who am I?

3 of 42

Why learn server basics?

Story time!

4 of 42

Ground Rules

  • Do not mess around on the server

  • Do not test on production

  • Do not push from your local

  • Change things one at a time

5 of 42

Tools we'll be using

  • Fabric

  • Local *nix box

  • Git + Github

6 of 42

Picking an OS

DO NOT:

  • Be proud

  • Pick the one your SO / work / neighbor / Twitter friends / favorite sites use

Do use the one that works for you

7 of 42

I'm using Ubuntu

8 of 42

Picking a provider

Look for:

  • Your OS
  • SSH access
  • Root access
  • Ability to clone your instance

9 of 42

Some providers!

10 of 42

I'm using

11 of 42

Before we get going…

How are we going to serve the site?

So many choices!

  • Apache!
  • Gunicorn!
  • Lighttp!
  • Ngnx!

12 of 42

I’m using

Apache + mod_wsgi

13 of 42

Setting up Apache

File: mysite.wsgi

import os, sys

sys.path.append('/var/www/django/django_projects/')

os.environ['DJANGO_SETTINGS_MODULE'] = ’mysite.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

14 of 42

And the virtual host…

File: mysite.net

<VirtualHost *:80>

ServerAdmin whoever@mysite.com

ServerName mysite.com

ServerAlias www.mysite.com

DocumentRoot /var/www/django/django_projects/mysite

LogLevel warn

WSGIDaemonProcess myuser processes=2 maximum-requests=500 threads=1

WSGIProcessGroup myuser

WSGIScriptAlias / /var/www/mydjango/apache/mysite.wsgi

Alias /media /var/www/mydjango/media/mysite/

Alias /admin_media /var/www/mydjango/admin_media

</VirtualHost>

15 of 42

Get a server

Done through the web interface

16 of 42

Get a server

Add a server

17 of 42

Get a server

Pick your OS

18 of 42

Get a server

Pick what size you want

19 of 42

Get a server

Seriously, save the password.

20 of 42

Get a server

YAY! It’s done!

21 of 42

Get a server

You now have a server!

22 of 42

Get a server

Some words about this server…

23 of 42

Server setup!

  • You have a shiny new server!

  • Let's log in! Let’s install things!

NO

We are starting this right.

24 of 42

Fabric

  • Fabric will SAVE YOU

  • Does lots of things...

  • I only use 'sudo', 'local', and 'put'

  • Install on your local

25 of 42

Running the fabric script

fab -f new_server.py -H XX.XX.XX.XX –u root deploy

26 of 42

Server setup!

Tasks:

  • Update the server
  • Add a user
  • Install some packages
  • Install pip
  • Install our framework
  • Create a structure for our files
  • Place our files

27 of 42

Server setup!

We need some packages…

PACKAGES=('apache2',

'subversion',

'sqlite3',

'python-setuptools',

'python-pip',

'git-core',

'gitosis',

'libapache2-mod-wsgi',)

28 of 42

Server setup!

Let’s get our server ready!

def setup_server():

sudo('apt-get update')

for p in PACKAGES:

sudo('apt-get -y install %s' % p)

29 of 42

Server setup!

Adding a user…

def add_user(user):

local('touch passwords')

sudo('useradd -m %s' % (user))

sudo('echo "%s ALL=(ALL) ALL" >> /etc/sudoers' % user)

password = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(8))

sudo('echo "%s:%s" | chpasswd' % (user, password))

local('echo "%s:%s" >> passwords' % (user, password))

30 of 42

Server setup!

Install our pip things!

PIP_PACKAGES = (‘django’,)

def pip_install():

for package in PIP_PACKAGES:

sudo('pip install %s' % package)

31 of 42

Setup is NOT DONE?!

Moving some files…

def put_files():

sudo('mkdir -p /tmp/deploy/')

sudo('chown kcunning /tmp/deploy')

local('rm -rf mysite')

local('git clone git@github.com:yourgitname/gitrepo')

local('tar cvf deploy-files.tar gitrepo/*')

put('deploy-files.tar', '/tmp/deploy/')

sudo('tar xvf /tmp/deploy/deploy-files.tar -C /tmp/deploy')

local('rm -rf gitrepo')

32 of 42

Argh MORE SETUP

def place_files():

sudo('mv /tmp/deploy/The-Real-Katie/config/therealkatie.net /etc/apache2/sites-available/')

sudo('mv /tmp/deploy/The-Real-Katie/config/trk.wsgi /var/www/mydjango/apache/’)

33 of 42

Almost there…

def services():

sudo('a2ensite mysite.com')

sudo('service apache2 restart')

34 of 42

We’re done!

Huzzah!

35 of 42

DO IT AGAIN

36 of 42

Updates

You’ll eventually want to update things…

Clone your server

37 of 42

Updates

Go get your image…

38 of 42

Updates

Create a new image…

39 of 42

Updates

Now make a new server!

40 of 42

Updates

Choose your image and size…

41 of 42

Updates

  • An update fabfile

Include:

  • Update templates and CSS
  • Install a new library
  • Update your settings file
  • Restart services

42 of 42

Next steps

  • Try swapping out elements

  • Caching!

  • SSL?

  • Attach a domain to your server