<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Olashina</title>
    <description>The latest articles on DEV Community by Olashina (@olashina).</description>
    <link>https://dev.to/olashina</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F666871%2Fd834f139-bb8d-44ef-8675-3782fa8476aa.jpeg</url>
      <title>DEV Community: Olashina</title>
      <link>https://dev.to/olashina</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/olashina"/>
    <language>en</language>
    <item>
      <title>How to Deploy a NestJS Application to a VPS Server: A Complete Guide.</title>
      <dc:creator>Olashina</dc:creator>
      <pubDate>Thu, 24 Oct 2024 06:25:54 +0000</pubDate>
      <link>https://dev.to/olashina/how-to-deploy-a-nestjs-application-to-a-vps-server-a-complete-guide-44a</link>
      <guid>https://dev.to/olashina/how-to-deploy-a-nestjs-application-to-a-vps-server-a-complete-guide-44a</guid>
      <description>&lt;p&gt;This guide documents the process of deploying a &lt;strong&gt;NestJS&lt;/strong&gt; application to a &lt;strong&gt;VPS server&lt;/strong&gt; for the first time, after having worked extensively with platforms like &lt;strong&gt;Docker&lt;/strong&gt;, &lt;strong&gt;DigitalOcean&lt;/strong&gt;, &lt;strong&gt;Heroku&lt;/strong&gt;, &lt;strong&gt;AWS (EC2)&lt;/strong&gt;, and &lt;strong&gt;Google Cloud Service&lt;/strong&gt;. It serves as a detailed reference for those transitioning from managed platforms to the more hands-on approach of VPS deployment.&lt;/p&gt;

&lt;p&gt;Deploying a NestJS application to a VPS involves setting up the server, transferring your application, configuring Nginx as a reverse proxy, managing the application with PM2, and securing it with HTTPS via Let's Encrypt. This article explains each step and the reasoning behind it, making it a useful resource for both newcomers and those needing a refresher.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Prerequisites&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;NestJS&lt;/strong&gt; application on your local machine.&lt;/li&gt;
&lt;li&gt;A VPS server (in this case, Contabo VPS).&lt;/li&gt;
&lt;li&gt;A registered domain (e.g., from Namecheap).&lt;/li&gt;
&lt;li&gt;SSH access to the VPS server.&lt;/li&gt;
&lt;li&gt;A Linux distribution (or your preferred OS) installed on the VPS.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;1. Setting Up the VPS and SSH Access&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before deploying your app, you need secure access to the VPS server. SSH is the standard protocol to remotely connect to servers.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Steps Taken&lt;/strong&gt;:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Access the VPS via SSH&lt;/strong&gt;:
First, ensure that SSH is enabled and configured correctly on your VPS. If you face issues like &lt;em&gt;"Permission denied"&lt;/em&gt;, ensure you are using the correct username and password, and that SSH keys (if configured) are set up correctly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Command&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   ssh root@&amp;lt;your-vps-ip&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Using VNC as an Alternative&lt;/strong&gt;:
If SSH fails, some VPS providers (like Contabo) offer a web-based or VNC console to access the server directly. This helps resolve initial connection issues.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tools&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RealVNC was used in this case to access the server when SSH wasn’t working.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;2. Transferring the NestJS Application to the VPS&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You need to transfer your NestJS project files from your local machine to the VPS server so they can be run and served to users.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Steps Taken&lt;/strong&gt;:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Using Git or SCP to Transfer Files&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If Git is installed on your server, you can clone your project directly from your repository:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt; git clone https://github.com/your-repo/your-nestjs-app.git
&lt;/code&gt;&lt;/pre&gt;



&lt;ul&gt;
&lt;li&gt;Alternatively, you can use &lt;code&gt;scp&lt;/code&gt; to securely copy files from your local machine to the VPS:
&lt;/li&gt;
&lt;/ul&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt; scp &lt;span class="nt"&gt;-r&lt;/span&gt; /path-to-your-project root@your-server-ip:/var/www/your-app
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;3. Installing Dependencies and Building the Project&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Your NestJS app likely has dependencies that need to be installed before it can run on the server. Building the project ensures that all necessary files (such as compiled JavaScript) are ready to be served.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Steps Taken&lt;/strong&gt;:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Navigate to the Project Directory&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;cd&lt;/span&gt; /var/www/your-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Install Dependencies&lt;/strong&gt;:
Install all required dependencies from the &lt;code&gt;package.json&lt;/code&gt; file:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Build the Application&lt;/strong&gt;:
Build the app for production, generating the &lt;code&gt;dist/&lt;/code&gt; folder containing the compiled code:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;4. Running the Application with PM2&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;PM2 is a process manager that allows you to keep your NestJS app running in the background and automatically restart it if it crashes or after deployments.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Steps Taken&lt;/strong&gt;:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Install PM2&lt;/strong&gt;:
If PM2 is not already installed, install it globally:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; pm2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Start the Application&lt;/strong&gt;:
Start your NestJS app using PM2:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   pm2 start dist/main.js &lt;span class="nt"&gt;--name&lt;/span&gt; your-app-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Set Up Auto-Restart&lt;/strong&gt;:
Configure PM2 to automatically start your application on server reboots:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   pm2 startup
   pm2 save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;5. Configuring Nginx as a Reverse Proxy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Nginx acts as a reverse proxy to forward incoming traffic from port 80 (HTTP) or 443 (HTTPS) to your NestJS application, which usually runs on an internal port like 3000. This setup is necessary to route web traffic properly.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Steps Taken&lt;/strong&gt;:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Install Nginx&lt;/strong&gt;:
If Nginx isn’t installed, install it using:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Configure Nginx&lt;/strong&gt;:
Create a new Nginx configuration file for your app:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/nginx/sites-available/your-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following configuration to proxy traffic to your NestJS app running on port 3000:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;   &lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="kn"&gt;server_name&lt;/span&gt; &lt;span class="s"&gt;yourdomain.com&lt;/span&gt; &lt;span class="s"&gt;www.yourdomain.com&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

       &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="kn"&gt;proxy_pass&lt;/span&gt; &lt;span class="s"&gt;http://localhost:3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="kn"&gt;proxy_http_version&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;Upgrade&lt;/span&gt; &lt;span class="nv"&gt;$http_upgrade&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;Connection&lt;/span&gt; &lt;span class="s"&gt;'upgrade'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;Host&lt;/span&gt; &lt;span class="nv"&gt;$host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
           &lt;span class="kn"&gt;proxy_cache_bypass&lt;/span&gt; &lt;span class="nv"&gt;$http_upgrade&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Enable the Configuration&lt;/strong&gt;:
Create a symbolic link to enable the site:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /etc/nginx/sites-available/your-app /etc/nginx/sites-enabled/
   &lt;span class="nb"&gt;sudo &lt;/span&gt;nginx &lt;span class="nt"&gt;-t&lt;/span&gt;
   &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  &lt;strong&gt;6. Configuring DNS on Namecheap&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To access your app via a custom domain, you need to configure DNS records on your domain provider (e.g., Namecheap) to point your domain to the VPS’s IP address.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Steps Taken&lt;/strong&gt;:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Add an A Record&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log in to Namecheap and go to &lt;strong&gt;Domain List&lt;/strong&gt; &amp;gt; &lt;strong&gt;Manage&lt;/strong&gt; for your domain.&lt;/li&gt;
&lt;li&gt;Under the &lt;strong&gt;Advanced DNS&lt;/strong&gt; tab, add an A Record:

&lt;ul&gt;
&lt;li&gt;Host: &lt;code&gt;@&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Value: &lt;code&gt;&amp;lt;your-vps-ip&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Add a Subdomain&lt;/strong&gt;:&lt;br&gt;
If you’re also using a subdomain (like &lt;code&gt;www.yourdomain.com&lt;/code&gt;), add another A Record:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Host: &lt;code&gt;www&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Value: &lt;code&gt;&amp;lt;your-vps-ip&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  &lt;strong&gt;7. Securing Your App with HTTPS (Let's Encrypt)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Securing your app with HTTPS ensures that traffic between the client and the server is encrypted, adding an extra layer of security.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Steps Taken&lt;/strong&gt;:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Install Certbot&lt;/strong&gt;:
Install Certbot, a tool used to generate SSL certificates from Let’s Encrypt:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;certbot python3-certbot-nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Generate an SSL Certificate&lt;/strong&gt;:
Use Certbot to obtain an SSL certificate for your domain:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;certbot &lt;span class="nt"&gt;--nginx&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; yourdomain.com &lt;span class="nt"&gt;-d&lt;/span&gt; www.yourdomain.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Certbot automatically configures Nginx to use the SSL certificate.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Renew Certificates Automatically&lt;/strong&gt;:
Set up a cron job to auto-renew the certificates:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;crontab &lt;span class="nt"&gt;-e&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Add the following line to renew the certificates automatically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   0 0 &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt; /usr/bin/certbot renew &lt;span class="nt"&gt;--quiet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By following these steps, you’ve successfully deployed a NestJS application to a VPS, configured Nginx as a reverse proxy, secured your app with HTTPS, and ensured it remains running with PM2. This guide not only helps you understand the deployment process but also serves as a reference for future VPS projects.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>node</category>
      <category>tutorial</category>
      <category>softwareengineering</category>
    </item>
  </channel>
</rss>
