How to Host a MEAN Stack Application on a VPS?

How to Host a MEAN Stack Application on a VPS

To host a MEAN stack application on a VPS, install Node.js and MongoDB, deploy your Angular build, run the Express server with PM2, proxy it through Nginx, and secure it with SSL. You can finish the whole process of MEAN host deployment in one afternoon. 

Here is each step in order.

What Is a MEAN Stack Application?

A MEAN stack application uses four JavaScript tools:

  • MongoDB stores the data.
  • Express handles routing.
  • Angular builds the front end.
  • Node.js runs the server code.

The whole stack uses one language. A small team can share code and review each other’s work without switching languages.

Why Host a MEAN Stack Application on a VPS?

A VPS gives a team dedicated resources, full server control, and a predictable monthly cost. That suits startups that need solid performance while the product finds its audience. Teams that want zero server work can look at managed deployment platforms instead.

Here is what you get:

  • Reserved resources. CPU and memory belong to your app. Load times stay steady when neighboring sites get busy.
  • Version control. Root access lets you pin Node.js and MongoDB to the versions your project needs.
  • Easy scaling. If launch day brings a traffic surge, you upgrade your plan. You don’t rebuild anything.
  • Wide support. In the 2024 Stack Overflow Developer Survey, about 41% of developers said they work with Node.js. Help is easy to find.

If traffic keeps growing, it helps to know when a dedicated server makes sense instead.

What You Need Before You Host a MEAN Stack Application

Prepare these items before you log in to your server:

  • A VPS running Ubuntu 22.04 or newer with at least 2 GB of RAM
  • A registered domain name
  • An SSH key pair
  • Your project in a Git repository

Not sure where to keep it? Compare Git hosting options first.

With these ready, you can send code to the server in one step over an encrypted connection.

Check your project too. The Angular build must finish. The Express server must start without errors. Then move every secret into environment variables. That includes database credentials and API keys.

If you need a host, MilesWeb VPS plans come with Ubuntu pre-installed, root access, and upgrade options. That covers the server requirements on this list.

How to Install Node.js and MongoDB on Your VPS

Your MEAN stack application needs both runtimes on the server. Install them first.

  1. Connect over SSH.
  2. Run sudo apt update to refresh the package list.
  3. Install Node.js from the NodeSource repository.
  4. Add MongoDB from its official source.
  5. Start the database and set it to launch at boot.
  6. Create a dedicated database user with a strong password.

Give the app its own credentials. If one account is ever compromised, the damage stays limited.

How to Deploy Your MEAN Stack Application Code

Deployment means copying your code to the server and building it for production.

  1. Clone the repo to /var/www/app.
  2. Run npm ci. It installs exactly what your lock file lists, so no package changes version between machines.
  3. Run ng build with the production configuration.

Angular writes its output to the dist folder. Express serves those files to every visitor. If you want a shortcut, there is also a simpler way to deploy an app without touching the server.

Next, create a .env file for the database URI, the port, and the JWT secret. Add it to .gitignore so it stays out of your repo. Keeping configuration out of the codebase also makes later changes easier.

How to Keep Your Node.js App Running with PM2

PM2 restarts your MEAN stack application if the Node.js process crashes or the server reboots.

  1. Start the app with pm2 start server.js.
  2. Run pm2 save.
  3. Run pm2 startup.

The app now launches again after every reboot. You don’t need to start it by hand.

How to Set Up Nginx as a Reverse Proxy for Express

Nginx sits in front of your Express server and passes visitors to it.

  1. Run sudo apt install nginx.
  2. Configure it as a reverse proxy.
  3. Forward requests on port 80 to Express. In most setups, Express listens on port 3000.

Visitors get a clean address. Nginx also handles incoming connections well.

How to Connect a Domain to Your VPS

Your domain must point at the VPS before people can reach your MEAN stack application by name.

  1. Go to your registrar.
  2. Create an A record that points the domain at your VPS IP address.
  3. Add the domain to your Nginx server block.
  4. Run nginx -t to check the syntax.

DNS changes can take a few hours to spread. Use that time for steps 3 and 4. Once the record resolves, visitors can type your domain instead of a string of numbers.

How to Secure a MEAN Stack Application on a VPS

Every public server needs a security baseline. Apply these four steps to your VPS:

StepWhat it does
Install CertbotIssues a free SSL certificate and gives you HTTPS
Enable UFWOpens only the ports for SSH, HTTP, and HTTPS
Bind MongoDB to localhostBlocks outside access to your database
Disable root login over SSHStops attackers from targeting the root account

Each step blocks a common attack path. Together, they give any public website a solid baseline.

Certbot also renews your certificate on its own. The HTTPS padlock stays valid without any action from you.

How to Test and Maintain Your Hosted MEAN Stack Application

Test the live site before you share the link.

  • Click through every route, form, and API call.
  • Check the site on a phone. Many first visits happen on mobile.
  • Confirm the HTTPS padlock shows on every page.
  • Run pm2 logs to spot errors early.
  • Run a speed test to see how fast pages load.

Then keep up a simple routine:

  • Update packages once a month.
  • Archive old logs.
  • Track memory with pm2 monit.
  • Enable Gzip in Nginx.
  • Add indexes to the MongoDB fields you filter on most.
  • Set up uptime monitoring, so you get an alert the moment the site goes down.
  • Keep a short changelog of server updates. It helps you trace a problem back to its source.

Final Word on How to Host MEAN Stack Application

The routine is the same each time: install the runtime, deploy the build, run it under PM2, secure the connection, and test everything. After a few rounds, new releases take minutes instead of hours. Your team spends its time on features, not server chores.

MilesWeb VPS plans give you root access, a choice of Ubuntu versions, and room to upgrade as traffic grows. The server side of this setup is covered. Share your link and watch the first visitors arrive.

FAQs About Hosting a MEAN Stack Application

Q#1. How much RAM do I need to host a MEAN stack application on a VPS?
Use at least 2 GB of RAM on Ubuntu 22.04 or newer.

Q#2. Why use PM2 for a Node.js app?
PM2 restarts your app after a crash or a server reboot. Run pm2 save and pm2 startup to make this permanent.

Q#3. Why put Nginx in front of Express?
Nginx forwards requests on port 80 to your Express server, which usually listens on port 3000. Visitors get a clean address.

Q#3. How long do DNS changes take?
They can take a few hours. Check your Nginx syntax with nginx -t while you wait.

Q#4. How often should I update my VPS?
Update packages once a month. Review logs and memory use at the same time.

Facebook
X
LinkedIn
Scroll to Top