<?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>Equivalence &#187; Amazon EC2</title>
	<atom:link href="http://equivalence.co.uk/archives/category/amazon-ec2/feed" rel="self" type="application/rss+xml" />
	<link>http://equivalence.co.uk</link>
	<description>Technical Blog for Software Developers</description>
	<lastBuildDate>Mon, 21 Nov 2011 22:58:35 +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>idiot’s guide to linux on amazon ec2 – part 2</title>
		<link>http://equivalence.co.uk/archives/1578</link>
		<comments>http://equivalence.co.uk/archives/1578#comments</comments>
		<pubDate>Wed, 13 Jan 2010 00:34:26 +0000</pubDate>
		<dc:creator>Gregg</dc:creator>
				<category><![CDATA[Amazon EC2]]></category>
		<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[thin]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=1578</guid>
		<description><![CDATA[In Part 1 I covered how to remove the root login, create a new user, and add this user to the list of sudoers on an linux ec2 instance. In this section I will cover how I got Ruby on Rails, MySQL, Nginx and Thin working together on the Ubuntu instance. First up, I think [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.equivalence.co.uk/archives/1521">Part 1</a> I covered how to remove the root login, create a new user, and add this user to the list of sudoers on an linux ec2 instance. In this section I will cover how I got <a href="http://rubyonrails.org/">Ruby on Rails</a>, <a href="http://mysql.com">MySQL</a>, <a href="http://nginx.org">Nginx</a> and <a href="http://code.macournoyer.com/thin/">Thin</a> working together on the <a href="http://www.ubuntu.com/">Ubuntu</a> instance.</p>
<p>First up, I think it&#8217;s worth taking a moment to explain what Nginx and Thin actually are, as they are maybe not as well known as the others.</p>
<p>Nginx is a very fast web/proxy server developed by <a href="http://sysoev.ru/en/">Igor Sysoev</a>.  According to wikipedia it currently runs, amongst others, the WordPress and Github websites.</p>
<p>Thin is a ruby web server that &#8220;glues together 3 of the best Ruby libraries in web history&#8221;[1]:</p>
<blockquote><ol>
<li>the Mongrel parser, the root of Mongrel speed and security</li>
<li>Event Machine, a network I/O library with extremely high scalability, performance and stability</li>
<li>Rack, a minimal interface between webservers and Ruby frameworks</li>
</ol>
</blockquote>
<p>Right on to the job at hand and first up was getting <code>apt-get</code> working!</p>
<p>To my surprise (but probably widely known) the Ubuntu ec2 instance did not come with <a href="http://en.wikipedia.org/wiki/Advanced_Packaging_Tool"><code>apt</code></a> pre-configured &#8211; unlike <code>yum</code> on a Fedora instance I had previously used. Instead you first have to run <code>apt-get update</code> to download the list of package locations. Now that we&#8217;ve done this we can get to work installing the other bit of software required.</p>
<p><strong>MySQL</strong><br />
The first thing we need to install are the MySQL client and server. To do this run the commands:</p>
<p><code>sudo apt-get install mysql-server</code><br />
<code>sudo apt-get install mysql-client</code></p>
<p>Then you need to make sure that the root password for MySQL is set to something secure. This can be done using:</p>
<p><code>sudo mysqladmin -u root a_good_secure_password</code></p>
<p><strong>Ruby</strong><br />
Now it&#8217;s time to install Ruby on Rails. First we need to install ruby, rake, rubygems, and a couple of other useful packages. The following commands should add the required binaries to your path:</p>
<p><code>sudo apt-get install rubygems</code><br />
<code>sudo apt-get install build-essential</code><br />
<code>sudo apt-get install rake</code><br />
<code>sudo apt-get install ruby-full</code></p>
<p>We can now use <code>gem</code> to install rails:</p>
<p><code>sudo gem install rails</code></p>
<p>As we will be using MySQL you probably also want to install the MySQL client development library in order to get the ruby gem to build/install correctly. This can be done by running:</p>
<p><code>sudo apt-get install libmysqlclient15-dev</code></p>
<p>Obviously the version of the libmysqlclient will depend on the MySQL version that you are using. Finally we can install the mysql gem by running:</p>
<p><code>sudo gem install mysql</code></p>
<p><strong>Nginx and Thin</strong><br />
To install the nginx package we run the command:</p>
<p><code>sudo apt-get install nginx</code></p>
<p>Nginx then needs to be started so we run:</p>
<p><code>sudo /etc/init.d/nginx start</code></p>
<p>By default the package should also add the entries required to restart nginx if the instance is rebooted &#8211; you can always check by looking in the /etc/rcX.d directory (where X is the run-level number).</p>
<p>Now it&#8217;s time to install thin:</p>
<p><code>sudo apt-get install thin</code></p>
<p><strong>Creating application config files for Thin and Nginx</strong><br />
It is a good idea to create config files that can be used to restart your thin clusters. To do this we use the <code>thin config</code> command. Now, let&#8217;s assume the app is called <code>myapp</code> and so we run the following command:</p>
<p><code>sudo thin config -C /etc/thin/myapp.yaml -c ~user/www/myapp --servers 3 -e production</code></p>
<p>This creates a thin config file /etc/thin/myapp.yaml that starts 3 instances of the rails application found in ~user/www/myapp using the production environment. By default it will start the first server on port 3000 and the next on 3001, and so on. Should you wish to specify the port you can supply it with the -p option, i.e. -p 6666.</p>
<p>You can now start your thin clients using:</p>
<p><code>sudo /etc/init.d/thin start -C myapp.yaml</code></p>
<p>It&#8217;s worth noting that if you don&#8217;t specify the -C option <code>thin</code> will use the config files found in <code>/etc/thin</code> and start the thin clients for each config file found in this directory.</p>
<p>As we want to use nginx as a proxy to our thin client instances we must create a nginx config file for our application. An example of such a config file is shown below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">upstream myapp <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    server 127.0.0.1:<span style="color: #000000;">3000</span>;
    server 127.0.0.1:<span style="color: #000000;">3001</span>;
    server 127.0.0.1:<span style="color: #000000;">3002</span>;
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
server <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    listen   <span style="color: #000000;">80</span> default;
    server_name example.co.uk;
&nbsp;
    access_log <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>myapp<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>access.log;
    error_log <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>myapp<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>error.log;
&nbsp;
    root   <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>myapp<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>;
    index  index.html;
&nbsp;
    location <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #666666; font-style: italic;">#auth_basic &quot;Please supply login details&quot;;</span>
        <span style="color: #666666; font-style: italic;">#auth_basic_user_file /home/user/www/myapp/public/protect.passwd;</span>
        proxy_set_header  X-Real-IP  <span style="color: #007800;">$remote_addr</span>;
        proxy_set_header  X-Forwarded-For <span style="color: #007800;">$proxy_add_x_forwarded_for</span>;
        proxy_set_header Host <span style="color: #007800;">$http_host</span>;
        proxy_redirect off;
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>-f <span style="color: #007800;">$request_filename</span><span style="color: #000000; font-weight: bold;">/</span>index.html<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            rewrite <span style="color: #7a0874; font-weight: bold;">&#40;</span>.<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">$1</span><span style="color: #000000; font-weight: bold;">/</span>index.html;
            <span style="color: #7a0874; font-weight: bold;">break</span>;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>-f <span style="color: #007800;">$request_filename</span>.html<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            rewrite <span style="color: #7a0874; font-weight: bold;">&#40;</span>.<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">$1</span>.html;
            <span style="color: #7a0874; font-weight: bold;">break</span>;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">!</span>-f <span style="color: #007800;">$request_filename</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
            proxy_pass http:<span style="color: #000000; font-weight: bold;">//</span>myapp;
            <span style="color: #7a0874; font-weight: bold;">break</span>;
        <span style="color: #7a0874; font-weight: bold;">&#125;</span>
    <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Lines 1-5 set up the proxy for our thin clients that we started on ports 3000-3002. The values that you include here obviously depend on the number of clients that you started and the ports they are running on. The rest of the file is dedicated to setting up the web server with the majority of settings being pretty self explanatory, so I&#8217;ll only highlight the important bits.</p>
<p>First, we see that the server waits for requests on port 80 and the domain used for this site is example.co.uk (lines 8-9). It&#8217;s worth noting that hosting a subdomain, say subdomain.example.co.uk, is as easy as replacing example.co.uk in line 9 with subdomain.example.co.uk.  Lines 20-23 take care of things like forwarding the real IP address to rails as well as some other set up required for https. Finally the remaining lines in the file check to see if an index.html file is available at the url specified and if so displays displays it (lines 25-28), serve static files straight up (lines 30-33), and finally if the file specified by the url does not exit on the file system it sets headers and proxies for our thin clients and passes it on.</p>
<p>As a side note, lines 18 and 19 that are commented out enable basic http authentication in nginx. You can uncomment out these lines if you require this feature. The password file for http auth can be generated using the apache util <code>htpasswd</code> &#8211; you will need to install the package that contains the htpasswd utility.</p>
<p>The config file (let&#8217;s call it <code>myapp</code>) is placed in <code>/etc/nginx/sites-available</code>, and finally a sim link is set up between the <code>sites-available</code> directory to the <code>sites-enabled</code> directory to enable the website:</p>
<p><code>sudo ln -s sites-available/myapp sites-enabled/myapp</code></p>
<p>That&#8217;s it. All we need to do now is restart nginx (<code>/etc/init.d/nginx restart</code>) and assuming your config is ok the site should now be up and running. (If nginx is already running and you want to parse the config without restarting you can always get the pid of the nginx process, <code>ps aux | egrep '(PID|nginx)'</code>, and run <code>sudo kill -HUP PID</code> &#8211; in fact this is all you actually need to do to get your site up and running)</p>
<p>[1] The Thin homepage &#8211; <a href="http://code.macournoyer.com/thin/">http://code.macournoyer.com/thin/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://equivalence.co.uk/archives/1578/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>idiot&#8217;s guide to linux on amazon ec2 &#8211; part 1</title>
		<link>http://equivalence.co.uk/archives/1521</link>
		<comments>http://equivalence.co.uk/archives/1521#comments</comments>
		<pubDate>Tue, 15 Dec 2009 01:30:31 +0000</pubDate>
		<dc:creator>Gregg</dc:creator>
				<category><![CDATA[Amazon EC2]]></category>
		<category><![CDATA[Cloud Computing]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[rabbitmq]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.equivalence.co.uk/?p=1521</guid>
		<description><![CDATA[Recently I&#8217;ve had the opportunity of setting up a Linux instance on Amazon EC2 for use with Ruby on Rails, MySQL, Nginx and Rabbit MQ. I suspect much of what I will document is obvious to many but hopefully some of you may find it useful, especially, if like me, you are totally inexperienced with [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve had the opportunity of setting up a Linux instance on <a href="http://aws.amazon.com/ec2">Amazon EC2</a> for use with <a href="http://rubyonrails.org">Ruby on Rails</a>, <a href="http://www.mysql.com">MySQL</a>, <a href="http://nginx.org">Nginx</a> and <a href="http://www.rabbitmq.com">Rabbit MQ</a>. I suspect much of what I will document is obvious to many but hopefully some of you may find it useful, especially, if like me, you are totally inexperienced with setting up a Linux server.</p>
<p>As it turns out I&#8217;ll probably document this over a couple of posts as it took up a bit more time and space than I first anticipated. In this first part I will cover, logging in as the root user, adding a new user, generating their ssh key, adding the user to the list of sudoers, and finally disabling root login via ssh. I&#8217;ll update this article with links to the other parts as I create them (<a href="http://www.equivalence.co.uk/archives/1521">Part 2</a>).</p>
<p>Right, first things first, some background info. Rightly or wrongly we required the server to do more than one thing, hence the list of items to install. So to reduce this number I picked an image with RabbitMQ pre-installed &#8211; as setup of this was uncharted territory for me. A consequence of this choice was that it pushed us down the path of <a href="http://www.ubuntu.com">Ubuntu</a> and the latest version which is currently 9.10. So let&#8217;s get to it.</p>
<p>The goal here is to disable remote root login, and in doing so we need to create a new user, and give him the ability to <a href="http://en.wikipedia.org/wiki/Sudo">sudo</a> commands. To do that we first need to login to our new EC2 image &#8211; which took me a little time to figure out! This can be done from Windows using <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html">putty</a>. However, we must first use <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html">puttygen</a> to generate a putty ssh auth key (putty doesn&#8217;t understand the key generated by Amazon) from your Amazon keypair which can be found in the AWS Management Console under Key Pairs.  Check out <a href="http://docs.amazonwebservices.com/AWSEC2/latest/GettingStartedGuide">this link</a> for further information.</p>
<p>Now on to the real work.</p>
<p><strong>Adding a user and generating their ssh key</strong><br />
Follow the process below to add a new user and generate an ssh key for this user.</p>
<ol>
<li>Login as root using method described above</li>
<li>Run <code>adduser webuser</code> &#8211; where <code>webuser</code> is the name of the user we are adding. Fill in the details including the password of this user.</li>
<li>Type <code>su webuser</code> &#8211; to run a shell as this user without logging out</li>
<li>Execute <code>ssh-keygen -t dsa</code> from this users home directory</li>
<li>Rename the file <code>~/.ssh/id_dsa.pub</code> to <code>~/.ssh/authorized_keys</code></li>
<li>Take a copy of the generated private key (should be in <code>~/.ssh/id_dsa</code>) and copy it to your local machine</li>
<li>Now use puttygen to generate the ssh key from <code>id_dsa</code></li>
<li>Finally login using putty and the new key &#8211; you should only have to specify your username when logging in.</li>
</ol>
<p><strong>Adding your new user to the list of sudoers</strong><br />
This is a very basic sudoers setup as we are only adding a single sudo user to the <code>/etc/sudoers</code> file. I know you can do way more complicated things with this but what is documented here was sufficient for our needs. So let&#8217;s get on with it.</p>
<ol>
<li>Login as <code>root</code></li>
<li>Run <code>visudo</code> &#8211; this is an editor for the sudoers file to stop multiple people editing the file at the same time</li>
<li>Locate the lines below in the editor<br />
<blockquote><p><code># User privilege specification<br />
root    ALL=(ALL) ALL</code></p></blockquote>
<p>and change this to </p>
<blockquote><p><code># User privilege specification<br />
root    ALL=(ALL) ALL<br />
webuser    ALL=(ALL) ALL</code></p></blockquote>
</li>
<li>If you would like to allow the user to sudo without having to supply a password then you need to add the following line as well:<br />
<blockquote><p><code>webuser ALL=NOPASSWD: ALL</code></p></blockquote>
</li>
<li>Now save the file and exit &#8211; ensure that the changes are saved to <code>/etc/sudoers</code></li>
</ol>
<p><strong>Disabling root login</strong></p>
<ol>
<li>Login as <code>webuser</code></li>
<li>Run <code>sudo vi /etc/ssh/sshd_config</code> &#8211; you can replace <code>vi</code> with another editor if you please, I&#8217;ve heard <code>nano</code> might be a little more friendly to windows users!</li>
<li>Find the line <code>PermitRootLogin </code>and change it to:<br />
<blockquote><p><code>PermitRootLogin no</code></p></blockquote>
<p>If I remember correctly in the instance I was using there was more than one line with <code>PermitRootLogin</code> so it may be worth check for this yourself.
</li>
<li>As a side note, should you wish to allow login using passwords rather than using a ssh key (this maybe what users familiar with shared hosting are used to) you can enable this by changing the relevant line in <code>sshd_config</code> to:<br />
<blockquote><p><code>PasswordAuthentication yes</code> </p></blockquote>
</li>
<li>Finally, restart <code>sshd</code> by running <code>sudo /etc/init.d/ssh restart</code> </li>
</ol>
<p>You should now be able to login in using <code>webuser</code>, and <code>sudo</code> commands as <code>webuser</code> that require to be run as <code>root</code>. Additionally, <code>root</code> login from a remote server has also been disabled. </p>
<p>There may be better ways to do the above, but what I&#8217;ve documented works. I may also be missing stuff, if so, let me know and I will update this. Well, that&#8217;s it for now. Check back soon for Part 2 which will be on it&#8217;s way shortly.</p>
<p><strong>Update</strong>: <a href="http://www.equivalence.co.uk/archives/1578">idiot&#8217;s guide to linux on amazon ec2 &#8211; part 2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://equivalence.co.uk/archives/1521/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

