<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Web Server on MangoDriod</title><link>https://md.eknath.dev/tags/web-server/</link><description>Recent content in Web Server on MangoDriod</description><generator>Hugo -- 0.141.0</generator><language>en-us</language><lastBuildDate>Sat, 13 Sep 2025 12:00:00 +0530</lastBuildDate><atom:link href="https://md.eknath.dev/tags/web-server/index.xml" rel="self" type="application/rss+xml"/><item><title>Introduction to Nginx: Hosting Your First Website</title><link>https://md.eknath.dev/posts/software-development/introduction-to-nginx/</link><pubDate>Sat, 13 Sep 2025 12:00:00 +0530</pubDate><guid>https://md.eknath.dev/posts/software-development/introduction-to-nginx/</guid><description>&lt;p>So you have a server, and now you want to share your project with the world. To do that, you need a web server. Nginx (pronounced &amp;ldquo;Engine-X&amp;rdquo;) is one of the most popular, powerful, and efficient web servers available. It&amp;rsquo;s famous for its high performance and its ability to also act as a reverse proxy, but at its core, it&amp;rsquo;s brilliant at serving web pages.&lt;/p>
&lt;p>This guide will walk you through the basics of Nginx, explaining its structure and showing you how to set up your very first website.&lt;/p></description><content:encoded><![CDATA[<p>So you have a server, and now you want to share your project with the world. To do that, you need a web server. Nginx (pronounced &ldquo;Engine-X&rdquo;) is one of the most popular, powerful, and efficient web servers available. It&rsquo;s famous for its high performance and its ability to also act as a reverse proxy, but at its core, it&rsquo;s brilliant at serving web pages.</p>
<p>This guide will walk you through the basics of Nginx, explaining its structure and showing you how to set up your very first website.</p>
<h2 id="1-installation">1. Installation</h2>
<p>If you haven&rsquo;t already, you can install Nginx on any Debian-based system (like Ubuntu) with a single command. It&rsquo;s also good practice to ensure your firewall allows web traffic.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># Install Nginx</span>
</span></span><span style="display:flex;"><span>sudo apt update
</span></span><span style="display:flex;"><span>sudo apt install nginx -y
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Allow Nginx through the firewall</span>
</span></span><span style="display:flex;"><span>sudo ufw allow <span style="color:#e6db74">&#39;Nginx Full&#39;</span>
</span></span></code></pre></div><p>To check that it&rsquo;s running, you can use <code>systemctl</code>:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo systemctl status nginx
</span></span></code></pre></div><p>If it&rsquo;s active, you can visit your server&rsquo;s IP address in a web browser, and you should see the default &ldquo;Welcome to Nginx!&rdquo; page.</p>
<h2 id="2-understanding-the-nginx-directory-structure">2. Understanding the Nginx Directory Structure</h2>
<p>Nginx&rsquo;s configuration can seem intimidating, but it&rsquo;s very logical. All the important files live in <code>/etc/nginx/</code>.</p>
<ul>
<li><code>/etc/nginx/nginx.conf</code>: The main configuration file. You will rarely need to edit this file directly.</li>
<li><code>/etc/nginx/sites-available/</code>: This is where you will store the configuration files for each of your websites. Think of it as a library of all possible sites you <em>could</em> host.</li>
<li><code>/etc/nginx/sites-enabled/</code>: This directory contains symbolic links (shortcuts) to the files in <code>sites-available</code>. Nginx only reads the configurations in this <code>sites-enabled</code> directory. This setup allows you to easily turn websites on and off without deleting their configuration.</li>
</ul>
<h2 id="3-setting-up-your-first-site-a-server-block">3. Setting Up Your First Site (A Server Block)</h2>
<p>Let&rsquo;s host a website for a domain you own, <code>your-domain.com</code>. First, ensure you have pointed your domain to your server&rsquo;s IP address using an <code>A</code> record at your domain registrar.</p>
<h3 id="step-1-create-a-home-for-your-website">Step 1: Create a Home for Your Website</h3>
<p>It&rsquo;s standard practice to store website files in the <code>/var/www/</code> directory.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># Create a directory for your domain</span>
</span></span><span style="display:flex;"><span>sudo mkdir -p /var/www/your-domain.com/html
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Assign ownership to your current user so you can edit files easily</span>
</span></span><span style="display:flex;"><span>sudo chown -R $USER:$USER /var/www/your-domain.com/html
</span></span></code></pre></div><h3 id="step-2-create-a-sample-page">Step 2: Create a Sample Page</h3>
<p>Let&rsquo;s create a simple <code>index.html</code> file for Nginx to serve.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>echo <span style="color:#e6db74">&#34;&lt;h1&gt;Success! The your-domain.com server block is working!&lt;/h1&gt;&#34;</span> &gt; /var/www/your-domain.com/html/index.html
</span></span></code></pre></div><h3 id="step-3-create-the-nginx-configuration-file">Step 3: Create the Nginx Configuration File</h3>
<p>Nginx calls the configuration for a single site a &ldquo;server block&rdquo;. We will create a new file in <code>sites-available</code> for our domain.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo nano /etc/nginx/sites-available/your-domain.com
</span></span></code></pre></div><p>Paste in the following configuration. Each line is explained by the comments.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-nginx" data-lang="nginx"><span style="display:flex;"><span><span style="color:#75715e"># This defines a server
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">server</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># Listen on port 80 (standard HTTP) for both IPv4 and IPv6
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">listen</span> <span style="color:#ae81ff">80</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">listen</span> <span style="color:#e6db74">[::]:80</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># The root directory where the website files are stored
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">root</span> <span style="color:#e6db74">/var/www/your-domain.com/html</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># The order of files to look for when a request comes in
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">index</span> <span style="color:#e6db74">index.html</span> <span style="color:#e6db74">index.htm</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># The domain names this server block should respond to
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">server_name</span> <span style="color:#e6db74">your-domain.com</span> <span style="color:#e6db74">www.your-domain.com</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># This block handles how to find files
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#f92672">location</span> <span style="color:#e6db74">/</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#75715e"># Try to serve the requested file, then a directory, or else show a 404 error
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#f92672">try_files</span> $uri $uri/ =<span style="color:#ae81ff">404</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h3 id="step-4-enable-your-site">Step 4: Enable Your Site</h3>
<p>Now, we need to tell Nginx to actually use this configuration. We do this by creating a symbolic link to the file in the <code>sites-enabled</code> directory.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo ln -s /etc/nginx/sites-available/your-domain.com /etc/nginx/sites-enabled/
</span></span></code></pre></div><h3 id="step-5-test-and-restart-nginx">Step 5: Test and Restart Nginx</h3>
<p>It&rsquo;s very important to test your configuration file for syntax errors before restarting Nginx.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo nginx -t
</span></span></code></pre></div><p>If you see <code>syntax is ok</code> and <code>test is successful</code>, you are good to go. Restart Nginx to apply the changes.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo systemctl restart nginx
</span></span></code></pre></div><p>That&rsquo;s it! If you now visit <code>http://your-domain.com</code> in your browser, you will see your &ldquo;Success!&rdquo; message instead of the default Nginx welcome page.</p>
<h2 id="4-whats-next">4. What&rsquo;s Next?</h2>
<p>You have successfully configured Nginx to host a basic website. This is the foundation for hosting any web project. From here, you can:</p>
<ul>
<li><strong>Host multiple websites</strong> on the same server by creating a new directory and a new server block file for each domain.</li>
<li><strong>Set up a reverse proxy</strong> to pass traffic to applications running on different ports (like Node.js or Python apps).</li>
<li><strong>Secure your sites with SSL/TLS</strong> using a free tool like Certbot to enable HTTPS.</li>
</ul>
<p>These more advanced topics are covered in our &ldquo;Advanced VPS Guide: Mastering Nginx, Subdomains, and Reverse Proxies&rdquo;.</p>
]]></content:encoded></item></channel></rss>