Hosting a NodeJS Web App on Ubuntu Server 22.04

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

alt text

Next step on our adventure: saddle up those dependencies! ๐Ÿš€ Run this command in your project directory:

npm install

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

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

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

If UFW is not active, enable it with:

sudo ufw enable

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

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

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
    
    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! ๐Ÿ”ฅ

More to read