<?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[Notes from Production]]></title><description><![CDATA[Notes from Production]]></description><link>https://sakib412.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 18 Nov 2026 14:34:53 GMT</lastBuildDate><atom:link href="https://sakib412.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Shipping It, Part 1: From localhost to a real domain]]></title><description><![CDATA[Deploy a Node.js app by hand: a public IP, PM2, nginx, a custom domain and HTTPS, and the pain points that push you toward automation]]></description><link>https://sakib412.hashnode.dev/shipping-it-part-1-from-localhost-to-a-real-domain</link><guid isPermaLink="true">https://sakib412.hashnode.dev/shipping-it-part-1-from-localhost-to-a-real-domain</guid><category><![CDATA[Devops]]></category><category><![CDATA[Node.js]]></category><category><![CDATA[nginx]]></category><category><![CDATA[deployment]]></category><category><![CDATA[Linux]]></category><dc:creator><![CDATA[Najmus Sakib]]></dc:creator><pubDate>Tue, 03 Nov 2026 20:28:21 GMT</pubDate><content:encoded><![CDATA[<h2>About this series</h2>
<p>Every deployment system at a large company started as one person typing commands into a server. This series walks that whole road, one step at a time: from a solo developer deploying by hand, to a small team with CI/CD, to the progressive, self-healing pipelines enterprises run today.</p>
<p>Each part solves the pain the previous part leaves behind. Nothing is introduced before you need it, so you can stop at whatever stage fits your team.</p>
<table>
<thead>
<tr>
<th>Part</th>
<th>Topic</th>
<th>Team size</th>
<th>What you add</th>
</tr>
</thead>
<tbody><tr>
<td>1</td>
<td>Your first deploy, by hand</td>
<td>Solo</td>
<td>A server, a process manager, a reverse proxy, HTTPS</td>
</tr>
<tr>
<td>2</td>
<td>Scripting the deploy</td>
<td>Solo</td>
<td>One-command deploys, release folders, instant rollback</td>
</tr>
<tr>
<td>3</td>
<td>Containers</td>
<td>Solo to small</td>
<td>A reproducible artifact that runs the same everywhere</td>
</tr>
<tr>
<td>4</td>
<td>CI/CD</td>
<td>2 to 5 engineers</td>
<td>Tests on every PR, build once, deploy on merge</td>
</tr>
<tr>
<td>5</td>
<td>Environments and team workflow</td>
<td>Small team</td>
<td>Staging, preview environments, secrets, safe DB migrations</td>
</tr>
<tr>
<td>6</td>
<td>Infrastructure as code and orchestration</td>
<td>Growing team</td>
<td>Terraform, load balancers, rolling deploys, health checks</td>
</tr>
<tr>
<td>7</td>
<td>Rollback strategies</td>
<td>Growing team</td>
<td>Blue/green, version pinning, rolling forward vs back</td>
</tr>
<tr>
<td>8</td>
<td>Feature flags and A/B testing</td>
<td>Product + engineering</td>
<td>Separating deploy from release, experiments</td>
</tr>
<tr>
<td>9</td>
<td>Canary releases and progressive delivery</td>
<td>Multiple teams</td>
<td>Small-percentage rollouts, metric gates, waves</td>
</tr>
<tr>
<td>10</td>
<td>GitOps and platform engineering</td>
<td>Enterprise</td>
<td>Desired state in Git, golden paths, audit trails</td>
</tr>
<tr>
<td>11</td>
<td>Autonomous deployment</td>
<td>Enterprise</td>
<td>Automated analysis, self-rollback, systems that ship themselves</td>
</tr>
</tbody></table>
<p>We start where everyone starts: one server, one SSH session, and a lot of trust in yourself.</p>
<hr />
<h2>Part 1: From localhost to a real domain</h2>
<p>Every deployment system, from a weekend project to Netflix, starts with the same question: how do I let other people use the thing running on my computer? In this first part we answer it the simplest way possible. One app, one server, a real domain and HTTPS. No Docker, no CI, no magic.</p>
<p>Along the way, watch for the <strong>Pain now</strong> notes. Each one is a problem the current setup cannot solve, and each one is the reason the next step exists.</p>
<h2>The problem: localhost only works for you</h2>
<p>When you run an app on your laptop, it listens on something like <code>http://localhost:3000</code>. Open that in your browser and it works. Send the same link to a friend and it fails.</p>
<p>That is because <code>localhost</code> always means <em>this machine</em>. On your friend's laptop, <code>localhost</code> points to their own computer, where your app is not running.</p>
<p>To be reachable, your app needs an address that the whole internet can route to: a <strong>public IP address</strong>. Your laptop usually does not have one. At home it sits behind your router (and often behind your ISP's network too), so incoming connections from the internet never reach it.</p>
<h2>The demo application</h2>
<p>The app is not the focus of this series. Serving it to anyone is. So we use a tiny TypeScript API with three routes:</p>
<table>
<thead>
<tr>
<th>Route</th>
<th>What it does</th>
</tr>
</thead>
<tbody><tr>
<td><code>/</code></td>
<td>A simple HTML page showing which server answered</td>
</tr>
<tr>
<td><code>/health</code></td>
<td>JSON status, used to check the app is alive</td>
</tr>
<tr>
<td><code>/crash</code></td>
<td>Kills the process on purpose, so we can test auto-restart later</td>
</tr>
</tbody></table>
<pre><code class="language-plaintext">hello-deploy/
├── src/
│   └── server.ts
├── .env.example
├── .gitignore
├── package.json
└── tsconfig.json
</code></pre>
<p><strong>src/server.ts</strong></p>
<pre><code class="language-typescript">import express from "express";
import os from "node:os";

const app = express();

// Configuration comes from environment variables, never from the code.
const PORT = Number(process.env.PORT ?? 3000);
const HOST = process.env.HOST ?? "0.0.0.0";
const APP_NAME = process.env.APP_NAME ?? "hello-deploy";
const startedAt = new Date();

// A human-friendly page, so you can see it in a browser.
app.get("/", (_req, res) =&gt; {
  res.send(`
    &lt;h1&gt;Hello from ${APP_NAME}&lt;/h1&gt;
    &lt;p&gt;Served by &lt;b&gt;${os.hostname()}&lt;/b&gt;&lt;/p&gt;
    &lt;p&gt;Running since ${startedAt.toISOString()}&lt;/p&gt;
  `);
});

// A health endpoint for you, your scripts and (later) your load balancer.
app.get("/health", (_req, res) =&gt; {
  res.json({
    status: "ok",
    app: APP_NAME,
    host: os.hostname(),
    uptimeSeconds: Math.round(process.uptime()),
  });
});

// A deliberate crash, to prove the process manager restarts the app.
app.get("/crash", () =&gt; {
  console.error("Crashing on purpose");
  process.exit(1);
});

app.listen(PORT, HOST, () =&gt; {
  console.log(`${APP_NAME} listening on http://${HOST}:${PORT}`);
});
</code></pre>
<p><strong>package.json</strong></p>
<pre><code class="language-json">{
  "name": "hello-deploy",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "main": "dist/server.js",
  "engines": { "node": "&gt;=26" },
  "scripts": {
    "dev": "tsx watch --env-file=.env src/server.ts",
    "build": "tsc",
    "start": "node --env-file=.env dist/server.js"
  },
  "dependencies": {
    "express": "^5.3.0"
  },
  "devDependencies": {
    "@types/express": "^5.0.6",
    "@types/node": "^26.6.6",
    "tsx": "^4.23.15",
    "typescript": "^7.0.2"
  }
}
</code></pre>
<p><strong>tsconfig.json</strong></p>
<pre><code class="language-json">{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "outDir": "dist",
    "rootDir": "src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}
</code></pre>
<p><strong>.env.example</strong></p>
<pre><code class="language-bash"># .env.example — copy to .env and fill in real values
APP_NAME=hello-deploy
PORT=3000
HOST=0.0.0.0
</code></pre>
<p><strong>.gitignore</strong></p>
<pre><code class="language-plaintext">node_modules/
dist/
.env
</code></pre>
<p>Run it on your laptop first:</p>
<pre><code class="language-bash">npm install
cp .env.example .env
npm run build
npm start
# hello-deploy listening on http://0.0.0.0:3000

curl http://localhost:3000/health
# {"status":"ok","app":"hello-deploy","host":"my-laptop","uptimeSeconds":3}
</code></pre>
<p>Push the project to a GitHub repository. We will pull it onto the server from there.</p>
<h2>Why not use your own machine?</h2>
<p>You <em>can</em> serve the internet from your laptop. The easiest way is a <strong>tunnel</strong> (such as ngrok or Cloudflare Tunnel), which gives your local port a public URL. It is great for showing a demo to a client for ten minutes.</p>
<p>We will not do that here. A laptop sleeps, changes networks and shuts down when you close the lid. A real app needs a machine that is always on, always connected and has its own public IP.</p>
<h2>Step 1: Rent a server with a public IP</h2>
<p>Any cloud provider works: DigitalOcean, AWS EC2 or Lightsail, Vultr, Hetzner, Linode. For a small app, <strong>1 vCPU and 1 to 2 GB of RAM</strong> is more than enough. Choose a current <strong>Ubuntu LTS</strong> image and add your SSH public key while creating it.</p>
<p>When it is ready, the provider shows its public IP. We will use <code>203.0.113.10</code> as an example; replace it with yours.</p>
<pre><code class="language-bash">ssh root@203.0.113.10
</code></pre>
<p>Do not run your app as <code>root</code>. If the app is ever compromised, the attacker gets the whole server. Create a normal user once, then use it from now on:</p>
<pre><code class="language-bash">adduser deploy
usermod -aG sudo deploy
rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy
exit

ssh deploy@203.0.113.10
</code></pre>
<h2>Step 2: Get the code onto the server</h2>
<p>There are two ways to do this, and the difference matters.</p>
<h3>Option A: Build locally, copy with scp</h3>
<p><code>scp</code> copies files from your machine to the server over SSH. You build on your laptop and upload the output:</p>
<pre><code class="language-bash"># on your laptop
npm run build
scp -r dist package.json package-lock.json deploy@203.0.113.10:~/hello-deploy/

# on the server
cd ~/hello-deploy &amp;&amp; npm ci --omit=dev
</code></pre>
<p>It works, until it does not. Your laptop and your server are different machines:</p>
<ul>
<li><strong>Different CPU architecture.</strong> A MacBook is ARM; most servers are x86. Packages with native code (<code>bcrypt</code>, <code>sharp</code>, database drivers) compile for the machine they are installed on.</li>
<li><strong>Different Node.js version.</strong> Code built and tested on Node 26 can behave differently on an older Node 22 server.</li>
<li><strong>Different OS libraries.</strong> macOS and Ubuntu ship different system libraries.</li>
</ul>
<p>The result is the classic bug: <em>it works on my machine</em> and breaks on the server.</p>
<h3>Option B: Build on the server (what we will do)</h3>
<p>Building on the server means everything is compiled for the machine that runs it. The cost is that the server now needs the source code and the build tools.</p>
<p>Install Git and Node.js. We use nvm, so you can pin the exact Node version your app was developed with:</p>
<pre><code class="language-bash">sudo apt update &amp;&amp; sudo apt upgrade -y
sudo apt install -y git

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
source ~/.bashrc
nvm install 26
node --version
</code></pre>
<p>Clone the repository and build:</p>
<pre><code class="language-bash">git clone https://github.com/&lt;your-username&gt;/hello-deploy.git
cd hello-deploy

npm ci          # installs exactly what package-lock.json says
npm run build   # compiles src/ into dist/
ls dist
# server.js
</code></pre>
<p>Two details worth knowing:</p>
<ul>
<li>Use <code>npm ci</code>, not <code>npm install</code>. It installs the exact versions from <code>package-lock.json</code>, the ones you tested. <code>npm install</code> may quietly pick newer ones.</li>
<li>The build needs <code>typescript</code>, a dev dependency. Do not set <code>NODE_ENV=production</code> before <code>npm ci</code>, or npm skips dev dependencies and the build fails.</li>
</ul>
<p>For a private repository, create a read-only <strong>deploy key</strong> on the server (<code>ssh-keygen -t ed25519</code>) and add its public key in the repository settings. It can pull code and nothing else.</p>
<h2>Step 3: Run it and open it to the world</h2>
<p>Start the app:</p>
<pre><code class="language-bash">npm start
# node: .env: not found
</code></pre>
<p>It fails, and that is a good lesson. <code>.env</code> is in <code>.gitignore</code>, so it never reached the server. Configuration and secrets do not travel with the code; each server needs its own copy.</p>
<p>Create it on the server and lock it down so only your user can read it:</p>
<pre><code class="language-bash">cp .env.example .env
nano .env        # set real values; in a real app, DATABASE_URL, API keys and so on
chmod 600 .env

npm start
# hello-deploy listening on http://0.0.0.0:3000
</code></pre>
<p>One more door to open. Most servers block incoming traffic by default. Allow port 3000 in the server's firewall:</p>
<pre><code class="language-bash">sudo ufw allow OpenSSH
sudo ufw allow 3000
sudo ufw enable
</code></pre>
<p>On AWS, Google Cloud or Azure there is a second firewall outside the server: the <strong>security group</strong> (or firewall rules). Add an inbound rule for TCP port 3000 there too, or traffic never reaches your machine.</p>
<p>Now open <code>http://203.0.113.10:3000</code> from your phone, or send it to a friend.</p>
<p><strong>Congratulations, you just deployed an application to the internet.</strong> Anyone in the world can reach it.</p>
<blockquote>
<p><strong>Cannot reach it?</strong> Check in this order: is the app running (<code>curl localhost:3000/health</code> on the server)? Is it listening on <code>0.0.0.0</code>, not <code>127.0.0.1</code>? Does <code>sudo ufw status</code> allow 3000? Does the cloud security group allow 3000?</p>
</blockquote>
<h2>The first pain: it dies with your terminal</h2>
<p>Try three things:</p>
<ol>
<li>Close your SSH session. The app stops, because it was a child of your terminal.</li>
<li>Visit <code>/crash</code>. The process exits and nothing brings it back.</li>
<li>Run <code>sudo reboot</code>. When the server comes back up, nothing starts your app.</li>
</ol>
<p>Each time, you have to SSH in and run <code>npm start</code> again by hand. Your users will find out before you do.</p>
<blockquote>
<p><strong>Pain now:</strong> the app is only alive while you are watching it. We need something that starts it in the background, restarts it when it crashes, and starts it again after the server reboots.</p>
</blockquote>
<p>That something is a <strong>process manager</strong>. On Linux you could use <code>systemd</code>, which is built in. For Node.js apps, the most popular choice is <strong>PM2</strong>, so we use it here.</p>
<h2>Step 4: Keep it alive with PM2</h2>
<p>Stop the app with <code>Ctrl+C</code> and install PM2 globally:</p>
<pre><code class="language-bash">npm install -g pm2
</code></pre>
<p>Start the app through PM2 instead of <code>npm start</code>. The <code>--node-args</code> flag passes <code>--env-file=.env</code> to Node, so the app still gets its configuration:</p>
<pre><code class="language-bash">cd ~/hello-deploy
pm2 start dist/server.js --name hello-deploy --node-args="--env-file=.env"
</code></pre>
<p>PM2 now runs the app in the background. Close your SSH session and it keeps running. Test the crash case:</p>
<pre><code class="language-bash">curl http://localhost:3000/crash   # the process exits
curl http://localhost:3000/health  # ...and PM2 has already restarted it
pm2 status
# │ hello-deploy │ fork │ ↺ 1 │ online │
</code></pre>
<p>The <code>↺</code> column counts restarts. A number that keeps growing means your app is crash-looping, which is worth investigating.</p>
<p>PM2 handles crashes, but not a server reboot yet. Two more commands fix that:</p>
<pre><code class="language-bash">pm2 startup   # prints a command starting with sudo; copy it and run it
pm2 save      # saves the current app list, restored on every boot
</code></pre>
<p>Now test it the honest way: run <code>sudo reboot</code>, wait a minute, and open <code>http://203.0.113.10:3000</code> again. If it loads without you touching anything, your app survives both crashes and restarts.</p>
<p>Commands you will use every day:</p>
<table>
<thead>
<tr>
<th>Command</th>
<th>What it does</th>
</tr>
</thead>
<tbody><tr>
<td><code>pm2 status</code></td>
<td>Is it running, how many restarts, memory use</td>
</tr>
<tr>
<td><code>pm2 logs hello-deploy</code></td>
<td>Live logs (Ctrl+C to exit)</td>
</tr>
<tr>
<td><code>pm2 restart hello-deploy</code></td>
<td>Restart after deploying new code</td>
</tr>
<tr>
<td><code>pm2 stop hello-deploy</code></td>
<td>Stop it, but keep it in the list</td>
</tr>
<tr>
<td><code>pm2 monit</code></td>
<td>Live CPU and memory dashboard</td>
</tr>
</tbody></table>
<blockquote>
<p><strong>Pain now:</strong> look at the link you are sharing: <code>http://203.0.113.10:3000</code>. Nobody types a port number. Worse, your Node process faces the internet directly. Every scanner, bot and malformed request hits your application code with nothing in between to filter, buffer or encrypt it.</p>
</blockquote>
<h2>Step 5: Put nginx in front, on port 80</h2>
<p>Browsers use port <strong>80</strong> for <code>http://</code> and port <strong>443</strong> for <code>https://</code> automatically. That is why you never type <code>:80</code>. So why not run Node on port 80 directly?</p>
<ul>
<li>Ports below 1024 need root privileges on Linux, and we just stopped running as root.</li>
<li>Node is good at running your app logic. It is not the best tool for handling TLS certificates, slow clients, huge uploads, compression or thousands of idle connections.</li>
<li>Later you will want several apps, or several copies of one app, behind the same IP.</li>
</ul>
<p>The standard answer is a <strong>reverse proxy</strong>: a web server that accepts every public request and forwards it to your app. The most common one is <strong>nginx</strong>.</p>
<pre><code class="language-plaintext">Browser ──▶ nginx :80 (public) ──▶ Node :3000 (private, 127.0.0.1)
</code></pre>
<p>Install it:</p>
<pre><code class="language-bash">sudo apt install -y nginx
</code></pre>
<blockquote>
<p><strong>Where nginx configs live.</strong> Older tutorials put sites in <code>sites-available</code> and symlink them into <code>sites-enabled</code>. That layout exists only in Debian and Ubuntu's own package; the official <a href="http://nginx.org">nginx.org</a> packages have no such folders. Both packages load every file in <code>/etc/nginx/conf.d/*.conf</code>, so we put our config there: it works on either, with no symlink step.</p>
</blockquote>
<p>Create a site config at <code>/etc/nginx/conf.d/hello-deploy.conf</code>:</p>
<pre><code class="language-nginx">server {
    listen 80;
    server_name _;          # any hostname for now; we set the domain in step 6

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;

        # Tell the app who the real client is
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
</code></pre>
<p>Remove the default site so it does not answer requests meant for your app, then test the config and reload:</p>
<pre><code class="language-bash"># Ubuntu's package ships its default site here:
sudo rm -f /etc/nginx/sites-enabled/default
# the nginx.org package ships it here instead:
sudo rm -f /etc/nginx/conf.d/default.conf

sudo nginx -t                # always test before reloading
sudo systemctl reload nginx
</code></pre>
<p>Now close the side door. The app should only be reachable through nginx, so make it listen on the loopback address instead of every interface. In <code>.env</code>:</p>
<pre><code class="language-bash">HOST=127.0.0.1
</code></pre>
<p>Restart the app and update the firewall: allow web traffic, remove port 3000:</p>
<pre><code class="language-bash">pm2 restart hello-deploy --update-env

sudo ufw allow 'Nginx Full'   # ports 80 and 443
sudo ufw delete allow 3000
sudo ufw status
</code></pre>
<p>If you added port 3000 to a cloud security group, replace it with ports 80 and 443 there too.</p>
<p>Open <code>http://203.0.113.10</code> with no port. It works, and <code>http://203.0.113.10:3000</code> no longer does. Your app is now hidden behind nginx.</p>
<blockquote>
<p><strong>Pain now:</strong> nobody remembers <code>203.0.113.10</code>. And if you ever rebuild or move the server, the IP changes and every link you have shared breaks.</p>
</blockquote>
<h2>Step 6: Give it a real domain</h2>
<p>A domain is a human-friendly name that points to your IP. The system that translates names to IPs is <strong>DNS</strong>, the internet's phone book. When someone opens <code>hello.example.com</code>, their browser asks DNS for the IP, then connects to it.</p>
<p>Buy a domain from any registrar (Namecheap, Cloudflare, Porkbun, GoDaddy). We will use <code>example.com</code>; replace it with yours.</p>
<h3>Add a DNS record</h3>
<p>In your registrar's or DNS provider's dashboard, add an <strong>A record</strong>. An A record maps a name to an IPv4 address.</p>
<table>
<thead>
<tr>
<th>Type</th>
<th>Name</th>
<th>Value</th>
<th>TTL</th>
</tr>
</thead>
<tbody><tr>
<td>A</td>
<td><code>hello</code></td>
<td><code>203.0.113.10</code></td>
<td>300</td>
</tr>
</tbody></table>
<p>This makes <code>hello.example.com</code> point to your server. Use <code>@</code> as the name if you want the bare <code>example.com</code>.</p>
<p><strong>TTL</strong> (time to live) is how many seconds other DNS servers may cache the answer. A short TTL like 300 (5 minutes) means changes spread quickly, which is what you want while setting things up.</p>
<blockquote>
<p>If you use Cloudflare for DNS, set the record to <strong>DNS only</strong> (grey cloud) for now. Its proxy mode is useful, but it changes how HTTPS works and gets in the way of the next step.</p>
</blockquote>
<h3>Check that it resolves</h3>
<p>DNS changes take from a few seconds to several minutes. Check from your laptop:</p>
<pre><code class="language-bash">dig +short hello.example.com
# 203.0.113.10

# no dig? this works on every OS
nslookup hello.example.com
</code></pre>
<p>When it returns your server's IP, DNS is done.</p>
<h3>Tell nginx its name</h3>
<p>Replace <code>server_name _;</code> in <code>/etc/nginx/conf.d/hello-deploy.conf</code> with your domain:</p>
<pre><code class="language-nginx">server_name hello.example.com;
</code></pre>
<pre><code class="language-bash">sudo nginx -t &amp;&amp; sudo systemctl reload nginx
</code></pre>
<p>Open <code>http://hello.example.com</code>. Your app, on a real name.</p>
<p>The name also protects you from IP changes. If you move to a new server, you update one DNS record and every link keeps working.</p>
<blockquote>
<p><strong>Pain now:</strong> the browser shows <strong>Not secure</strong> next to your domain. Everything travels as plain text, so anyone on the same café Wi-Fi, or anywhere along the route, can read passwords, cookies and API tokens. Modern browser features (secure cookies, HTTP/2, geolocation, service workers) also refuse to work without HTTPS.</p>
</blockquote>
<h2>Step 7: Turn on HTTPS</h2>
<p>HTTPS is HTTP inside an encrypted <strong>TLS</strong> connection. To set one up, your server needs a <strong>certificate</strong>: a file, signed by a trusted certificate authority, that proves <em>this server really is</em> <a href="http://hello.example.com"><em>hello.example.com</em></a>. The browser checks that signature before it sends anything.</p>
<p>That is why the domain had to come first. A certificate is issued for a name, not for an IP, and the authority checks that you control the name before issuing it.</p>
<p><strong>Let's Encrypt</strong> issues certificates for free, and <strong>certbot</strong> automates the whole process, including the nginx changes.</p>
<pre><code class="language-bash">sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d hello.example.com
</code></pre>
<p>Certbot asks for an email (for expiry warnings) and asks you to accept the terms. Then it:</p>
<ol>
<li>Proves to Let's Encrypt that you control the domain, by serving a temporary file on port 80.</li>
<li>Downloads the certificate to <code>/etc/letsencrypt/live/hello.example.com/</code>.</li>
<li>Updates your nginx site to listen on 443 with that certificate.</li>
<li>Adds a redirect so <code>http://</code> sends visitors to <code>https://</code>.</li>
</ol>
<p>Your nginx config now looks roughly like this (certbot wrote the parts marked <code>managed by Certbot</code>):</p>
<pre><code class="language-nginx">server {
    server_name hello.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/hello.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/hello.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    listen 80;
    server_name hello.example.com;
    return 301 https://$host$request_uri; # managed by Certbot
}
</code></pre>
<p>Let's Encrypt certificates expire after a short period by design, so renewal must be automatic. Certbot installs a timer that renews them before they expire. Confirm the timer works:</p>
<pre><code class="language-bash">sudo certbot renew --dry-run
systemctl list-timers | grep certbot
</code></pre>
<p>Check from your laptop:</p>
<pre><code class="language-bash">curl -I http://hello.example.com
# HTTP/1.1 301 Moved Permanently
# Location: https://hello.example.com/

curl https://hello.example.com/health
# {"status":"ok","app":"hello-deploy","host":"hello-server","uptimeSeconds":842}
</code></pre>
<p>Open <code>https://hello.example.com</code> in a browser. The padlock is there. You now have a real, secure, always-on deployment.</p>
<h2>Step 8: Shipping an update</h2>
<p>The first deploy is the hard one. After that, every release is the same routine. Change some code on your laptop, push it, then:</p>
<pre><code class="language-bash">ssh deploy@203.0.113.10
cd ~/hello-deploy

git pull origin main
npm ci
npm run build
pm2 restart hello-deploy

curl https://hello.example.com/health
</code></pre>
<p>About a minute of work. For a side project with a few users, this is genuinely fine. But look closely at what happens during that minute.</p>
<blockquote>
<p><strong>Pain now: every deploy causes downtime.</strong> <code>pm2 restart</code> kills the running process before the new one is ready. For a second or two nothing listens on port 3000, and nginx answers every request with <code>502 Bad Gateway</code>.</p>
</blockquote>
<blockquote>
<p><strong>Pain now: you build on the live server.</strong> <code>npm ci</code> deletes <code>node_modules</code> while the old process is still running from it, and <code>npm run build</code> overwrites <code>dist/</code> in place. If either fails halfway, production is left half old and half new.</p>
</blockquote>
<blockquote>
<p><strong>Pain now: rolling back is slow.</strong> If the new version is broken, you have to find the last good commit, check it out, reinstall, rebuild and restart. That is the same slow minute again, under pressure, while users see errors.</p>
</blockquote>
<blockquote>
<p><strong>Pain now: it all lives in your head.</strong> The steps exist only as commands you remember. Miss one, or let a teammate try it, and production breaks in a new way.</p>
</blockquote>
<h2>What you built</h2>
<img src="https://raw.githubusercontent.com/sakib412/sakib412.github.io/main/blog/images/shipping-it-part-1-architecture.png" alt="Every request crosses DNS, the firewall, nginx and PM2 before it reaches the Node app" style="display:block;margin:0 auto" />

<p>A visitor's browser looks up your domain in DNS, connects to your server's IP, passes the firewall, and reaches nginx over HTTPS. nginx forwards the request to your Node app on a private port, and PM2 keeps that app alive. You still reach the same server over SSH to deploy, which is exactly what Part 2 starts to change.</p>
<h2>Standards check</h2>
<p>Big companies do not invent their deployment rules from scratch; they follow well-known practices. Here is how our single server measures up after Part 1.</p>
<table>
<thead>
<tr>
<th>Practice</th>
<th>Where it comes from</th>
<th>Status after Part 1</th>
</tr>
</thead>
<tbody><tr>
<td>Config in the environment, not in code</td>
<td>The Twelve-Factor App</td>
<td>Following: <code>.env</code>, never committed</td>
</tr>
<tr>
<td>The app binds to a port; something else exposes it</td>
<td>The Twelve-Factor App</td>
<td>Following: Node on <code>127.0.0.1:3000</code>, nginx in front</td>
</tr>
<tr>
<td>Least privilege</td>
<td>CIS Benchmarks</td>
<td>Partly: non-root user and firewall; SSH password login still enabled</td>
</tr>
<tr>
<td>Encryption in transit</td>
<td>Industry baseline</td>
<td>Following: HTTPS with automatic renewal and redirect</td>
</tr>
<tr>
<td>Reproducible builds</td>
<td>The Twelve-Factor App (build, release, run)</td>
<td>Breaking: we build on the live server</td>
</tr>
<tr>
<td>Zero-downtime deploys and fast rollback</td>
<td>Continuous delivery practice</td>
<td>Breaking: every restart drops requests; rollback means rebuilding</td>
</tr>
<tr>
<td>Dev/prod parity</td>
<td>The Twelve-Factor App</td>
<td>Breaking: the server is set up by hand and will drift from your laptop</td>
</tr>
</tbody></table>
<p>Before anyone else depends on this server, finish hardening SSH and run through the checklist:</p>
<pre><code class="language-bash"># /etc/ssh/sshd_config: keys only, no root login
PasswordAuthentication no
PermitRootLogin no

sudo systemctl restart ssh   # test a new SSH session before closing the old one
</code></pre>
<ul>
<li><input disabled type="checkbox" /> SSH password and root login disabled</li>
<li><input disabled type="checkbox" /> App survives <code>/crash</code> and <code>sudo reboot</code></li>
<li><input disabled type="checkbox" /> Port 3000 closed in both <code>ufw</code> and the cloud security group</li>
<li><input disabled type="checkbox" /> <code>sudo certbot renew --dry-run</code> passes</li>
<li><input disabled type="checkbox" /> <code>.env</code> has <code>chmod 600</code> and is not in Git</li>
<li><input disabled type="checkbox" /> The update steps are written down somewhere other than your head</li>
</ul>
<h2>Next: Part 2, scripting the deploy</h2>
<p>The deploy works, but it is a manual ritual with downtime and no undo. In Part 2 we fix all four pains from Step 8 without adding any new infrastructure:</p>
<ul>
<li>One command deploys from your laptop.</li>
<li>Each release builds in its own timestamped folder, so the live app is never touched mid-build.</li>
<li>An atomic symlink switch plus <code>pm2 reload</code> gives zero-downtime releases.</li>
<li>Rolling back to the previous version takes one command and about a second.</li>
</ul>
<p>Same server, same app, far less fear when you press deploy.</p>
]]></content:encoded></item></channel></rss>