Hosting a NodeJS Web App on Ubuntu Server 22.04

eneaslari

EneasLari

Posted on April 29, 2024

Hosting a NodeJS Web App on Ubuntu Server 22.04

Let's dive into setting up and running a Node.js server on a Linux Ubuntu machine, and guess what? We're not starting from scratch! 🚀 We'll be using the super sleek one-file server I crafted in a previous article. If you missed it, don't worry—you can catch up right here. Get ready to turbocharge your server setup and have some fun along the way! 🎉👨‍💻

Ready to bring your Node.js project to life on your local server? 🌟 Let's roll up our sleeves and jump right in!

First up, let’s snatch the project from GitHub. Go ahead and clone the repository with this slick command:

git clone https://github.com/EneasLari/singlefilenodejs.git
Enter fullscreen mode Exit fullscreen mode

alt text

Next step on our adventure: saddle up those dependencies! 🚀 Run this command in your project directory:

npm install
Enter fullscreen mode Exit fullscreen mode

alt text

Don’t have MongoDB installed yet? No stress! Follow these easy-peasy instructions right here to get set up.

Now, let's fire up that server with:

npm run dev
Enter fullscreen mode Exit fullscreen mode

And watch the magic happen! 🧙‍♂️

alt text

Want to show off your server to the world? 🌍 Let's open up port 5000! Here’s how to make it available using UFW, the Uncomplicated Firewall:

sudo ufw allow 5000/tcp
Enter fullscreen mode Exit fullscreen mode

And voilà! Your local server is now ready to welcome visitors from anywhere, just like a party host opening the gates to an awesome backyard bash! 🎉

Now, pull up your browser and take your freshly served Node.js app for a spin! 🎢 Happy coding!

Here’s how to do it:

Step 1: Check UFW Status
First, check if UFW is enabled on your system:

sudo ufw status verbose
Enter fullscreen mode Exit fullscreen mode

If UFW is not active, enable it with:

sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

This command will start the firewall and ensure it is set to start automatically when your system boots.

Step 2: Allow Port 5000
Specify if the rule applies to TCP or UDP traffic. If your application requires both protocols, you should enable both. Here's how to do it:

sudo ufw allow 5000/tcp
sudo ufw allow 5000/udp
Enter fullscreen mode Exit fullscreen mode

If you only need it for one protocol, you can just run the respective command.

Step 3: Verify the Changes
After adding the rules, it's important to verify that they have been implemented correctly:

sudo ufw status
Enter fullscreen mode Exit fullscreen mode

This command will list all active rules, including the newly added entries for port 5000, confirming that it has been opened successfully.

Additional Notes:

  • Purpose of Port 5000: Port 5000 is commonly used for web server development (e.g., Flask and other frameworks often use this port by default) as well as some media services. Ensure that opening this port is safe within your network environment and aligns with your security policies.
  • Logging and Monitoring: For security, consider enabling logging for UFW:
  sudo ufw logging on
Enter fullscreen mode Exit fullscreen mode

This helps in keeping track of attempted connections, aiding in security audits and troubleshooting.

Alright! 🌟 Now you're all set up and ready to roll! Just point your browser to your server's IP and the magic port 5000. Here's an example, but remember, your server IP might be different:

🌐 Check out: http://192.168.72.128:5000/

alt text

And just like that, you've got your Node.js server humming along on your local setup! 🎉

Stay tuned! In our next article, we're going to level up and introduce PM2 to keep your server running smooth like butter, even when you close your terminal. Think of it as hiring a little digital assistant to keep the lights on 24/7! 🤖✨

So, pat yourself on the back, grab a cup of your favorite brew, and take a moment to revel in your success! 🔥

💖 💪 🙅 🚩
eneaslari
EneasLari

Posted on April 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related