Hosting a NodeJS Web App on Ubuntu Server 22.04
Tags: node, ubuntu
Published: 2024-04-29 20:20:32

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](https://eneaslari.com/articleview/render/creating-a-single-file-node.js-api-with-express-mongodb-and-ejs-integration-654b70d76105d6905a4fe0ca "single file server"). 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:
```bash
git clone https://github.com/EneasLari/singlefilenodejs.git
```
![alt text](https://eneaslari.com/f5887e68-b67d-4f95-b5b0-9aebb8dbd5da.PNG "git clone")

Next step on our adventure: saddle up those dependencies! 🚀 Run this command in your project directory:
```bash
npm install
```
![alt text](https://eneaslari.com/a6f20171-06ad-4f8b-8ea0-cb7c45998deb.PNG "npm install")

Don’t have MongoDB installed yet? No stress! Follow these easy-peasy instructions [right here](https://www.cherryservers.com/blog/install-mongodb-ubuntu-22-04 "Install MongoDB") to get set up.

Now, let's fire up that server with:
```bash
npm run dev
```
And watch the magic happen! 🧙‍♂️

![alt text](https://eneaslari.com/650ea153-aa2d-4a66-a9c8-6ac197f0a426.PNG "npm run dev")

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:
```bash
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:
```bash
sudo ufw status verbose
```
If UFW is not active, enable it with:
```bash
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:
```bash
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:
```bash
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:
  ```bash
  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/](http://192.168.72.128:5000/)

![alt text](https://eneaslari.com/755306bf-87bf-4db8-a6b2-09ae6a9c8acd.PNG "server running")

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! 🔥