<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Shubham Lad]]></title><description><![CDATA[Self-taught full-stack developer with a passion for scalable systems. I build high-quality systems & share my knowledge through my blog & YouTube videos.]]></description><link>https://blog.shubhamlad.in</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1674412582170/-WTertPM2.png</url><title>Shubham Lad</title><link>https://blog.shubhamlad.in</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Apr 2026 10:07:30 GMT</lastBuildDate><atom:link href="https://blog.shubhamlad.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to set up Redis on AWS EC2?]]></title><description><![CDATA[Recently for one of the projects I needed to set up Redis, I thought of ElastiCache but I required it for the development purpose and didn't want to spend much on it so I decided to spin up a Redis on AWS EC2 instance.
Today I had like to share a qui...]]></description><link>https://blog.shubhamlad.in/how-to-set-up-redis-on-aws-ec2-shubhamlad</link><guid isPermaLink="true">https://blog.shubhamlad.in/how-to-set-up-redis-on-aws-ec2-shubhamlad</guid><category><![CDATA[Redis]]></category><category><![CDATA[AWS]]></category><category><![CDATA[ec2]]></category><category><![CDATA[Node.js]]></category><dc:creator><![CDATA[Shubham Lad]]></dc:creator><pubDate>Mon, 11 Jul 2022 07:48:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1657525669868/gi2JsDLza.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Recently for one of the projects I needed to set up Redis, I thought of ElastiCache but I required it for the development purpose and didn't want to spend much on it so I decided to spin up a Redis on AWS EC2 instance.</p>
<p>Today I had like to share a quick and simple way to set up Redis on a AWS EC2 instance, how to add a password to Redis, and also how you can connect to it. Although this is a secure setup still I don't recommend it for production use, for Production use I will share the details in a later blog post.</p>
<p>Before starting this blog I assume that you have a basic knowledge of AWS and are able to spin up a EC2 instance on your own and also able to connect it through SSH.</p>
<h3 id="heading-launch-ec2-instance">Launch EC2 instance.</h3>
<p>We are going to use Amazon Linux 2 AMI for this tutorial but you can also use Ubuntu.</p>
<p>If you want to know how to launch AWS EC2 instance and connect to it through SSH please watch this video.</p>
<p>Make sure that required ports are open and accessible through the internet.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.youtube.com/watch?v=7ekRDRmk4DE&amp;ab_channel=AmazonWebServices">https://www.youtube.com/watch?v=7ekRDRmk4DE&amp;ab_channel=AmazonWebServices</a></div>
<h3 id="heading-download-and-install-redis">Download and Install Redis.</h3>
<p>Run the following commands step-by-step to download and install Redis on the EC2 instance.</p>
<p><strong>Connect to your instance through SSH</strong></p>
<p><code>ssh -i /path/key-pair-name.pem instance-user-name@instance-public-dns-name</code></p>
<p><strong>Install dependencies</strong></p>
<pre><code>sudo yum -y <span class="hljs-keyword">update</span>
sudo yum groupinstall "Development Tools"
</code></pre><p><strong>Install Redis</strong></p>
<pre><code>cd <span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>local<span class="hljs-operator">/</span>src 
sudo wget http:<span class="hljs-comment">//download.redis.io/redis-stable.tar.gz</span>
sudo tar xvzf redis<span class="hljs-operator">-</span>stable.tar.gz
sudo rm <span class="hljs-operator">-</span>f redis<span class="hljs-operator">-</span>stable.tar.gz
</code></pre><p><strong>Recompile Redis</strong></p>
<p>Now we have downloaded and extracted the Redis, we need to recompile it.</p>
<pre><code>cd redis-stable
sudo <span class="hljs-built_in">make</span> distclean
sudo <span class="hljs-built_in">make</span>
</code></pre><p><strong>Run Tests</strong></p>
<p>This is not required but recommended to check that Redis is correctly installed. It will take 10 mins to run all the tests.</p>
<pre><code>sudo yum <span class="hljs-keyword">install</span> -y tcl
sudo make <span class="hljs-keyword">test</span>
</code></pre><p><strong>Make directories and copy files into proper places</strong></p>
<pre><code>sudo mkdir <span class="hljs-operator">-</span>p <span class="hljs-operator">/</span>etc<span class="hljs-operator">/</span>redis <span class="hljs-operator">/</span><span class="hljs-keyword">var</span><span class="hljs-operator">/</span>lib<span class="hljs-operator">/</span>redis <span class="hljs-operator">/</span><span class="hljs-keyword">var</span><span class="hljs-operator">/</span>redis<span class="hljs-operator">/</span><span class="hljs-number">6379</span>
sudo cp src<span class="hljs-operator">/</span>redis<span class="hljs-operator">-</span>server src<span class="hljs-operator">/</span>redis<span class="hljs-operator">-</span>cli <span class="hljs-operator">/</span>usr<span class="hljs-operator">/</span>local<span class="hljs-operator">/</span>bin
sudo cp redis.conf <span class="hljs-operator">/</span>etc<span class="hljs-operator">/</span>redis<span class="hljs-operator">/</span><span class="hljs-number">6379</span>.conf
</code></pre><p><strong>Edit Config file</strong></p>
<pre><code>sudo nano <span class="hljs-operator">/</span>etc<span class="hljs-operator">/</span>redis<span class="hljs-operator">/</span><span class="hljs-number">6379</span>.conf
</code></pre><p>Set values as below in config file</p>
<ul>
<li>Set <strong>bind</strong> to <strong>127.0.0.1</strong></li>
<li><strong>daemonize</strong> to <strong>yes</strong></li>
<li><strong>logfile</strong> to <em>/var/log/redis_6379.log</em></li>
<li><strong>dir</strong> to <em>/var/redis/6379 </em></li>
</ul>
<p><strong>Download and install init script</strong></p>
<pre><code>sudo wget https:<span class="hljs-comment">//raw.githubusercontent.com/saxenap/install-redis-amazon-linux-centos/master/redis-server</span>
sudo mv redis<span class="hljs-operator">-</span>server <span class="hljs-operator">/</span>etc<span class="hljs-operator">/</span>init.d
sudo chmod <span class="hljs-number">755</span> <span class="hljs-operator">/</span>etc<span class="hljs-operator">/</span>init.d/redis<span class="hljs-operator">-</span>server
</code></pre><p><strong>Edit the Redis init script as below</strong></p>
<pre><code>sudo nano <span class="hljs-operator">/</span>etc<span class="hljs-operator">/</span>init.d/redis<span class="hljs-operator">-</span>server
</code></pre><ul>
<li>REDIS_CONF_FILE="/etc/redis/6379.conf"</li>
</ul>
<p>Finally, add new Redis init script to all the default runlevels for CentOS and start the Redis server.</p>
<pre><code>sudo chkconfig –<span class="hljs-keyword">add</span> redis-<span class="hljs-keyword">server</span>
sudo chkconfig –<span class="hljs-keyword">level</span> <span class="hljs-number">345</span> redis-<span class="hljs-keyword">server</span> <span class="hljs-keyword">on</span>
sudo service redis-<span class="hljs-keyword">server</span> <span class="hljs-keyword">start</span>
</code></pre><p>Also to ensure background saves and fix low-memory issues you need to create a new file <strong>systctl.conf</strong> in <em>/etc</em>.</p>
<p><code>sudo nano /etc/systctl.conf</code></p>
<p>add the below lines to the file and save it.</p>
<pre><code>vm.overcommit_memory <span class="hljs-operator">=</span> <span class="hljs-number">1</span>
systctl vm.overcommit_memory=<span class="hljs-number">1</span>
</code></pre><p>Congratulations! You are done, make sure to test the server by pinging your instance with redis-cli. It should give you a response PONG.</p>
<h3 id="heading-set-password-for-redis-server">Set password for Redis Server</h3>
<p>We need to set the password using redis-cli. From Redis 6 ACL is introduced so we can also set username and also define access for users but we will talk about ACL in detail in another article. 
Let's see how we can set the password for the default user.</p>
<p>1) Connect to the Redis command-line interface:</p>
<pre><code>redis<span class="hljs-operator">-</span>cli
</code></pre><p>2) Set password</p>
<p>Change <code>mypassword</code> to your choice of password.</p>
<pre><code><span class="hljs-attribute">127</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span>:<span class="hljs-number">6379</span>&gt; CONFIG SET requirepass <span class="hljs-string">"mypassword"</span>
</code></pre><p>3) Check if the password is set.</p>
<pre><code><span class="hljs-attribute">127</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span>:<span class="hljs-number">6379</span>&gt; AUTH <span class="hljs-string">"mypassword"</span>
</code></pre><p>If Password Matches then Server Response will be</p>
<p><code>OK</code></p>
<p>Else if Incorrect</p>
<p><code>(error) ERR invalid password</code></p>
<p>And you are done with the Redis setup on AWS EC2 instance.</p>
<h3 id="heading-connect-to-the-redis-server-using-nodejs">Connect to the Redis server using NodeJS.</h3>
<p>As I already told you that the default PORT of the Redis server is 6379 so your connection URL becomes 
<code>"redis://your_instance_public_ip_address:6379"</code>.</p>
<p>so by using <code>redis</code> client you can connect to the Redis server in following way.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>);
<span class="hljs-keyword">const</span> redis = <span class="hljs-built_in">require</span>(<span class="hljs-string">"redis"</span>);

<span class="hljs-keyword">const</span> app = express();

<span class="hljs-keyword">const</span> url = <span class="hljs-string">"redis://your_instance_public_ip_address:6379"</span>;

<span class="hljs-keyword">const</span> redisClient = redis.createClient({
  url,
  <span class="hljs-attr">password</span>: <span class="hljs-string">"mypassword"</span>,
});

<span class="hljs-keyword">const</span> connectRedis = <span class="hljs-keyword">async</span> () =&gt; {
  <span class="hljs-keyword">await</span> redisClient.connect();

  <span class="hljs-keyword">await</span> redisClient.set(<span class="hljs-string">"foo"</span>, <span class="hljs-string">"bar"</span>);
  <span class="hljs-keyword">let</span> data = <span class="hljs-keyword">await</span> redisClient.get(<span class="hljs-string">"foo"</span>);
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"DATA "</span>, data);

  app.listen(<span class="hljs-number">5000</span>, <span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Server is running on port 5000"</span>);
  });
};

connectRedis();
</code></pre>
<h3 id="heading-references">References:</h3>
<ul>
<li>https://redis.io/docs/getting-started/</li>
<li>https://redis.io/docs/manual/security/acl/</li>
</ul>
]]></content:encoded></item></channel></rss>