Report this

What is the reason for this report?

how to deploy MEAN Separeted apps on Ubuntu 18?

Posted on February 19, 2020

Hi, I am new here to DigitalOcean and Ubuntu. I have MEAN Application and I want to Deploy my both applications separetely i-e Angluar and NodeJS. Please give me some useful direction. I shall be very Thankfull.

Rehan Shah



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

Deploying a MEAN (MongoDB, Express.js, AngularJS, Node.js) stack application on an Ubuntu Droplet involves several steps.

Firstly, you need to ensure that all necessary software is installed on your Ubuntu server. Then, you’ll need to deploy both the backend (Express.js/Node.js) and the frontend (AngularJS) applications.

Here are some high-level steps on how you can accomplish this:

Step 1: Set up the Ubuntu Server

  1. Update the package list on the server:

    sudo apt-get update
    
  2. Install curl and some other necessary software:

    sudo apt-get install curl gnupg build-essential
    

Step 2: Install Node.js

  1. Install Node.js via the NodeSource repository:

    curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
    sudo apt-get install -y nodejs
    

    For mode details on how to install Node.js I would suggest this tutorial here.

  2. Check the installed Node.js and npm versions:

    node -v 
    npm -v
    

Step 3: Install MongoDB

To install MongoDB, follow the steps from this tutorial here

# How To Install MongoDB on Ubuntu

Or alternatively, you can use the managed MongoDB database clusters provided by DigitalOcean:

https://docs.digitalocean.com/products/databases/mongodb/

Step 4: Deploy the Backend (Express.js/Node.js) App

  1. Clone your application from your Git repository (replace <your-git-repo> with your repository URL):

    git clone <your-git-repo>
    
  2. Navigate into your project folder and install dependencies:

    cd <your-project-folder>
    npm install
    
  3. Start your Express.js/Node.js app (assuming your main file is server.js):

    node server.js
    

    Alternatively, I would recommend using pm2 to run your Node.js service so that you don’t have to worry about the process stopping when you close your terminal:

    How To Set Up a Node.js Application for Production on Ubuntu 20.04

Step 5: Install Nginx

  1. Update the package list:

    sudo apt-get update
    
  2. Install Nginx:

    sudo apt-get install nginx
    

Step 6: Deploy the Frontend (AngularJS) App

  1. Build your AngularJS app on your local machine:

    ng build --prod
    
  2. Copy the dist/ folder (which was created by the build command) to your server. You can use a tool like scp for this.

  3. On your server, move the contents of the dist/ folder to Nginx’s html directory (replace <angular-project-name> with the name of your Angular project):

    sudo mv dist/<angular-project-name>/* /var/www/html/
    

Step 7: Configure Nginx

  1. Open the Nginx configuration file:

    sudo nano /etc/nginx/sites-available/default
    
  2. Replace the contents of the file with the following (replace <your-node-app-location> with the path to your Node.js app’s server.js file):

    server {
        listen 80;
        server_name _;
        location / {
            proxy_pass http://localhost:3000; # assuming your Express app runs on port 3000
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
    
  3. Restart Nginx to apply the changes:

    sudo systemctl restart nginx
    

Your MEAN stack application should now be deployed on your Ubuntu server. Users should be able to access your AngularJS app by navigating to your server’s IP address or domain name in their web browser. The AngularJS app will interact with the Express.js/Node.js app as necessary.

A few remarks to keep in mind:

  • These instructions assume that your Express.js/Node.js app runs on port 3000. If it runs on a different port, adjust the proxy_pass line in the Nginx configuration file accordingly.

  • As mentioned above, for a production deployment, it’s recommended to use a process manager like PM2 to keep your Node.js app running even after the server restarts or the app crashes.

  • You should also secure your application by installing an SSL certificate. Let’s Encrypt provides free SSL certificates and is compatible with Nginx. How To Secure Nginx with Let’s Encrypt on Ubuntu 20.04 will walk you through the process.

  • Depending on the size and requirements of your application, it might be more appropriate to use a cloud-based database service, such as a Managed MongoDB instance, instead of running MongoDB directly on your server.

Hope that this helps!

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.