By bitmonxtech
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!
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
Update the package list on the server:
sudo apt-get update
Install curl and some other necessary software:
sudo apt-get install curl gnupg build-essential
Step 2: Install Node.js
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.
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
Or alternatively, you can use the managed MongoDB database clusters provided by DigitalOcean:
Step 4: Deploy the Backend (Express.js/Node.js) App
Clone your application from your Git repository (replace <your-git-repo> with your repository URL):
git clone <your-git-repo>
Navigate into your project folder and install dependencies:
cd <your-project-folder>
npm install
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
Update the package list:
sudo apt-get update
Install Nginx:
sudo apt-get install nginx
Step 6: Deploy the Frontend (AngularJS) App
Build your AngularJS app on your local machine:
ng build --prod
Copy the dist/ folder (which was created by the build command) to your server. You can use a tool like scp for this.
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
Open the Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
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;
}
}
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.