Here I am going to show you how to get a server set up on EC2 running mysql and django from the ground up.I often find these kinds of guides difficult to follow because they tend to focus only on one component of the process and use setups that maybe incompatible with the tools you’re working with. To simplify the process I am going to show a complete setup from start to finish. There are some finer details I won’t go into like securing the server and setting up iptables, but you can head over to Slicehost for some great articles on the subject. Ok, let’s do it.
Amazon EC2
The first step you need to take is get an Amazon AWS account. Once you’ve done that, sign into the management console and select the EC2 tab. Along the left you’ll see four headlines: Instances, Images, Elastic Block Store, and Networking & Security.
For now we’ll concern ourselves with the first two: instances and images. The images tab has a sub-option: AMIs. These Amazon Machine Images are disk images of operating systems with some useful utilities that you can launch as an instance. An instance is just a machine image running in the elastic cloud.
Go ahead and select AMIs. On that page from the ‘Viewing’ box select ‘EBS Images’ and filter to the platform ‘Ubuntu’. We definitely want to set up an Ubuntu server and we chose EBS (Elastic Block Storage) because Amazon allows you to run really small instances, micro instances, that are virtually free. Let’s select the latest Ubuntu Hardy image and launch it as instance.
Select “micro” from the Instance Details modal dialog that pops up and hit Continue.
Nice. You can hit continue on the next couple of dialogs. These settings can be configured later and aren’t important for our purposes now. When you get to the ‘Create Key Pair’ dialog go ahead and create and download a new key. This pair gives you a private key that you can use to authenticate over ssh to your server the first time without a password. I’ll call mine eric:
We need to add rules to our security group that allow access to SSH and HTTP. The ones defined in the image below allow ssh and http access from anywhere.
On the last tab we can review the changes and then hit Launch. Wooo. Now we are getting somewhere.
It will take a minute for the new instance to launch, but once up it will show in your instances pane. This page shows the details we just configured for the instance as well as a public DNS we can use to access it. Let’s go ahead and fire up an ssh session to our server. Navigate to wherever you downloaded the private key and initialize the session like so:
> ssh -i eric.pem ubuntu@ec2-##-##-##-##.compute-1.amazonaws.com
Cool. Now that we have a session we’ll want to first make sure aptitude is up to date:
> sudo aptitude update
Once we’ve done that let’s get cracking and install some utilities:
> sudo aptitude install python-django
> sudo aptitude install mysql-server
> sudo aptitude install python-mysqldb
> sudo aptitude install libapache2-mod-wsgi
Create a Django Application
Run django-admin to create a new project in a directory under your home directory:
Run django-admin to create a new project in a directory under your home directory:
> mkdir srv
> cd srv
> django-admin startproject mysite
You’ll need to configure your django settings to suit your database and application needs.
Configuring mod_wsgi
Alright now that we have a starter django app going let’s configure mod_wsgi to serve our application. We need to enable a site on apache to do this so let’s create a virtual host.
Alright now that we have a starter django app going let’s configure mod_wsgi to serve our application. We need to enable a site on apache to do this so let’s create a virtual host.
> sudo nano /etc/apache2/sites-available/mysite.com
Copy the following into the new file:
<VirtualHost *:80>
ServerName mysite.com
ServerAlias www.mysite.com
WSGIScriptAlias / /home/demo/public_html/mysite.com/mysite.wsgi
</VirtualHost>
Now that we’ve defined a wsgi script to serve our app we need to create that script,
> nano /home/demo/public_html/mysite.com/mysite.wsgi
Copy the following configuration to that file
import os
import sys
sys.path.append('/home/demo/public_html/mysite.com/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Finally we need to enable the site and reload the apache configuration
> sudo a2ensite mysite.com
> sudo /etc/init.d/apache2 reload
That’s it! You should now see the django start page when you navigate to your site.
No comments:
Post a Comment