<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bryan Helmig &#187; ubuntu</title>
	<atom:link href="http://bryanhelmig.com/tag/ubuntu/feed/" rel="self" type="application/rss+xml" />
	<link>http://bryanhelmig.com</link>
	<description>...does nerdy things.</description>
	<lastBuildDate>Thu, 29 Mar 2012 16:18:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Setting up Ubuntu 10.04 with Apache, memcached, ufw, MySQL, and Django 1.2 on Linode</title>
		<link>http://bryanhelmig.com/setting-up-ubuntu-10-04-with-apache-memcached-ufw-mysql-and-django-1-2-on-linode/</link>
		<comments>http://bryanhelmig.com/setting-up-ubuntu-10-04-with-apache-memcached-ufw-mysql-and-django-1-2-on-linode/#comments</comments>
		<pubDate>Mon, 24 May 2010 08:40:03 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Work]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[linode]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[ufw]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=286</guid>
		<description><![CDATA[Call me a sucker, but I love a good server setup as much as the next guy, I just have a little trouble setting it up sometimes. So I thought I&#8217;d walk through my process for setting up all this Django goodness on what is basically a LAMP setup (where &#8220;P&#8221; stands for Python!) with [...]]]></description>
			<content:encoded><![CDATA[<p>Call me a sucker, but I love a good server setup as much as the next guy, I just have a little trouble setting it up sometimes. So I thought I&#8217;d walk through my process for setting up all this Django goodness on what is basically a LAMP setup (where &#8220;P&#8221; stands for Python!) with a few extras like memcached and ufw. We&#8217;ll get to a level of general security, but not prefect security.</p>
<p>Also, we&#8217;ll keep this well within the 360 megabytes allotted for the cheapest Linode. Before we get underway, just create a new Linode with Ubuntu 10.04 (32-bit), set your password. Alright, let&#8217;s get going!</p>
<p><strong>Initial Setup</strong><br />
First thing to do is log in via the standard SSH on your Linode&#8217;s IP, port 22, and with <em>root</em> as your username. We&#8217;ll change the login user away from root, but for now, this will do (plus we can skip all that sudo stuff). First, let&#8217;s update and upgrade the system.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> update <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">apt-get</span> upgrade</pre></div></div>

<p>Awesome, you should be 100% up-to-date. Now its time to get to the fun part, let&#8217;s <strong>install</strong> some of the software we&#8217;ll be using! Below are the commands to install Apache, MySQL, mod_wsgi and the python MySQL bindings, as well as memcached and ufw. If you have any prompts for passwords, you know what to do! Just remember what you set them as.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> build-essentials
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> apache2 apache2.2-common apache2-mpm-worker apache2-threaded-dev libapache2-mod-wsgi python-dev python-setuptools
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> mysql-server python-mysqldb
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> memcached libmemcache-dev
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> ufw</pre></div></div>

<p>Really quickly, let&#8217;s get <strong>python-memcached</strong> installed (alternatively, if you need some raw speed, look up cmemcached or python-libmemcached). This will take a few steps&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># create and enter a temp dir in /home</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> downloads <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #7a0874; font-weight: bold;">cd</span> downloads
<span style="color: #666666; font-style: italic;"># get django 1.2 tar and untar it</span>
<span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> pymem.tar.gz <span style="color: #c20cb9; font-weight: bold;">ftp</span>:<span style="color: #000000; font-weight: bold;">//</span>ftp.tummy.com<span style="color: #000000; font-weight: bold;">/</span>pub<span style="color: #000000; font-weight: bold;">/</span>python-memcached<span style="color: #000000; font-weight: bold;">/</span>python-memcached-<span style="color: #000000;">1.47</span>.tar.gz <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvf</span> pymem.tar.gz
<span style="color: #666666; font-style: italic;"># enter the directory and install</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> python-memcached-<span style="color: #000000;">1.47</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Now its time for Django! <strong>Django 1.1.1</strong> is a lot easier to install than Django 1.2.4:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># install django 1.1.1</span>
<span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> python-django</pre></div></div>

<p>But let&#8217;s say we need <strong>Django 1.2.4</strong>: its gonna take a few more commands to make this happen. Watch out for that last command, anytime you use <code>rm -r</code> you can run into real trouble if you mistype (but we&#8217;re not production yet so no worries, right?).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># enter the downloads directory in /home</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>downloads
<span style="color: #666666; font-style: italic;"># get django 1.2 tar and untar it</span>
<span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #660033;">-O</span> django124.tar.gz http:<span style="color: #000000; font-weight: bold;">//</span>www.djangoproject.com<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>1.2.4<span style="color: #000000; font-weight: bold;">/</span>tarball<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zxvf</span> django124.tar.gz
<span style="color: #666666; font-style: italic;"># enter the directory and install django 1.2</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> Django-1.2.4 <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span>
<span style="color: #666666; font-style: italic;"># strictly optional delete of downloads directory, be careful with rm -r</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home <span style="color: #000000; font-weight: bold;">&amp;</span>amp;<span style="color: #000000; font-weight: bold;">&amp;</span>amp; <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-r</span> downloads</pre></div></div>

<p><strong>Actual Configuration</strong><br />
Let&#8217;s work backwards, we&#8217;ll start with the easy stuff and work our way to the more complicated things. Let&#8217;s get <strong>ufw</strong> and the <strong>SSH port</strong> out of the way first. Go ahead and pick a number between 1024-8000ish for the port we will eventually; I chose <strong>5555</strong> but you <del>can</del> should use something else.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># turn on ufw</span>
ufw <span style="color: #7a0874; font-weight: bold;">enable</span>
<span style="color: #666666; font-style: italic;"># log all activity (you'll be glad you have this later)</span>
ufw logging on
<span style="color: #666666; font-style: italic;"># allow port 80 for tcp (web stuff)</span>
ufw allow <span style="color: #000000;">80</span><span style="color: #000000; font-weight: bold;">/</span>tcp
<span style="color: #666666; font-style: italic;"># allow our ssh port</span>
ufw allow <span style="color: #000000;">5555</span>
<span style="color: #666666; font-style: italic;"># deny everything else</span>
ufw default deny
<span style="color: #666666; font-style: italic;"># open the ssh config file and edit the port number from 22 to 5555, ctrl-x to exit</span>
<span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ssh<span style="color: #000000; font-weight: bold;">/</span>sshd_config
<span style="color: #666666; font-style: italic;"># restart ssh (don't forget to ssh with port 5555, not 22 from now on)</span>
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">ssh</span> reload</pre></div></div>

<p>Now that you have ufw and SSH locked down, its time to move onto setting up <strong>memcached</strong> (which is super easy). We&#8217;ll just run it as root and be done with it (you will need to repeat this command on each boot):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># replace 24 with however many megabytes of cache is appropriate</span>
memcached <span style="color: #660033;">-u</span> root <span style="color: #660033;">-d</span> <span style="color: #660033;">-m</span> <span style="color: #000000;">24</span> <span style="color: #660033;">-l</span> 127.0.0.1 <span style="color: #660033;">-p</span> <span style="color: #000000;">11211</span></pre></div></div>

<p>Alright, with that out of the way, let&#8217;s get MySQL nice and tight. The standard install of MySQL can suck up a lot of memory, so we&#8217;ll suggest a few ways to lighten the load:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># open mysql conf and set these settings:</span>
<span style="color: #666666; font-style: italic;">#    key_buffer = 16k</span>
<span style="color: #666666; font-style: italic;">#    max_allowed_packet = 1M</span>
<span style="color: #666666; font-style: italic;">#    thread_stack = 64K</span>
<span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>my.cnf
<span style="color: #666666; font-style: italic;"># restart mysql</span>
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>mysql restart</pre></div></div>

<p>Now let&#8217;s get a new user setup and leave behind this <strong>root</strong> nonsense for safety&#8217;s sake. Your username is going to be <strong>bobby</strong> for this example. Replace <strong>bobby</strong> everywhere if you want something different.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># create bobby, you'll be asked to set the password and such</span>
adduser bobby
<span style="color: #666666; font-style: italic;"># edit the ssh file and add the line: AllowUsers bobby</span>
<span style="color: #666666; font-style: italic;"># ctrl-x to exit and save</span>
<span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>ssh<span style="color: #000000; font-weight: bold;">/</span>sshd_config
<span style="color: #666666; font-style: italic;"># restart ssh</span>
<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">ssh</span> reload
<span style="color: #666666; font-style: italic;"># log out and login as bobby from now on!</span></pre></div></div>

<p>It&#8217;s time for the nitty gritty stuff: setting up <strong>Apache</strong> and <strong>mod_wsgi</strong> with Django for the domain you own called <em>examplesite.com</em> (creative, I know). We need to make a <strong>public_html</strong> folder in bobby&#8217;s <strong>home</strong> folder and place a folder called <strong>examplesite.com</strong> (as well as a few more). We&#8217;ll do that first.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>bobby<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> public_html
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> public_html<span style="color: #000000; font-weight: bold;">/</span>examplesite.com
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> public_html<span style="color: #000000; font-weight: bold;">/</span>examplesite.com<span style="color: #000000; font-weight: bold;">/</span>logs
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> public_html<span style="color: #000000; font-weight: bold;">/</span>examplesite.com<span style="color: #000000; font-weight: bold;">/</span>private</pre></div></div>

<p>Right now you should place your Django project into the <strong>public_html/examplesite.com</strong> folder. For example, if the project is housed in <strong>demoproject</strong> (eg: demoproject/manage.py, demoproject/urls.py, etc.) you&#8217;ll want it placed ALA <strong>public_html/examplesite.com/demoproject</strong>. Time to get the Apache config files up and running. Here we go!</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>bobby<span style="color: #000000; font-weight: bold;">/</span>public_html<span style="color: #000000; font-weight: bold;">/</span>examplesite.com<span style="color: #000000; font-weight: bold;">/</span>demoproject<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> apache
<span style="color: #c20cb9; font-weight: bold;">nano</span> apache<span style="color: #000000; font-weight: bold;">/</span>demoproject.wsgi</pre></div></div>

<p>First, in the <strong>demoproject.wsgi</strong> file you should paste and save:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">import os, sys
&nbsp;
<span style="color: #000099;">apache_configuration</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> os.path.dirname<span style="">&#40;</span>__file__<span style="">&#41;</span></span>
<span style="color: #000099;">project</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> os.path.dirname<span style="">&#40;</span>apache_configuration<span style="">&#41;</span></span>
<span style="color: #000099;">workspace</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> os.path.dirname<span style="">&#40;</span>project<span style="">&#41;</span></span>
sys.path.append<span style="">&#40;</span>workspace<span style="">&#41;</span>
&nbsp;
sys.path.append<span style="">&#40;</span>'/usr/lib/python2.5/site-packages/django/'<span style="">&#41;</span>
sys.path.append<span style="">&#40;</span>'/home/bobby/public_html/examplesite.com/demoproject'<span style="">&#41;</span>
&nbsp;
os.environ<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>'DJANGO_SETTINGS_MODULE'<span style="">&#93;</span></span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 'demoproject.settings'</span>
import django.core.handlers.wsgi
<span style="color: #000099;">application</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> django.core.handlers.wsgi.WSGIHandler<span style="">&#40;</span><span style="">&#41;</span></span></pre></div></div>

<p>We&#8217;re so close, let&#8217;s get the other Apache files setup. Oh, and don&#8217;t worry about that <strong>www-data</strong> thing just yet, we&#8217;ll get to that in a second.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># give the www-data user/group permission on public_html</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chown</span> <span style="color: #660033;">-R</span> www-data:www-data <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>bobby<span style="color: #000000; font-weight: bold;">/</span>public_html
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">nano</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>apache2<span style="color: #000000; font-weight: bold;">/</span>sites-available<span style="color: #000000; font-weight: bold;">/</span>examplesite.com</pre></div></div>

<p>Place the text below in the <strong>examplesite.com</strong> config file (remember the www-data part from the last command?). You can modify the threads and processes numbers to suit your moods and load.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">    #Basic setup
    ServerAdmin your@email.com
    ServerName www.examplesite.com
    ServerAlias examplesite.com
&nbsp;
        Order deny,allow
        Allow from all
&nbsp;
    LogLevel warn
    ErrorLog  /home/bobby/public_html/examplesite.com/logs/apache_error.log
    CustomLog /home/bobby/public_html/examplesite.com/logs/apache_access.log combined
&nbsp;
    WSGIDaemonProcess examplesite.com user<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">www-data group=www-data threads=20 processes=2</span>
    WSGIProcessGroup examplesite.com
&nbsp;
    WSGIScriptAlias / /home/bobby/public_html/examplesite.com/demoproject/apache/demoproject.wsgi</pre></div></div>

<p>A few more final things:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> a2ensite examplesite.com
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p><strong>Time to admire your handiwork.</strong></p>
<p>Congrats! You&#8217;re all set up and ready to roll! Nothing can stop you now! Here&#8217;s a neat command to measure your memory usage as mine rarely gets over 160mb. This gives lots of room for growth as you can always increase the memcached size, MySQL settings, and Apache/mod_wsgi instances/threads.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># measure your memory usage in kb</span>
<span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $3&quot;\t&quot;$6&quot;\t&quot;$11;sum+=$6;cpu+=$3} END {print &quot;Total RSS&quot;, sum, &quot;\nTotal CPU&quot;, cpu}'</span></pre></div></div>

<p>Also, under Ubuntu 10.04 (Lucid), Python&#8217;s site-packages is now called dist-packages. Just a FYI.</p>
]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/setting-up-ubuntu-10-04-with-apache-memcached-ufw-mysql-and-django-1-2-on-linode/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

