Basic server for MEAN stack
Run NodeJS Server and AngularJS application in single ubuntu server
Here, we will have two parts:
- Installation
- Setup
Will be installing following;
- Nginx
Nginx works as a front end server, which in this case proxies the requests to a node.js server.
- NodeJS
Cross-platform JavaScript run-time environment that executes JavaScript code server-side
- Yarn
Fast, reliable, and secure dependency management.
- Pm2
Advanced process manager for production Node.js applications
- MongoDB
MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need
- Git
Git (/ɡɪt/) is a version control system
INSTALLATION PART
1. Install Nginx
sudo apt-get install nginx
2. Install Node
Will be installing node using nvm, so first we have to install nvm;
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
export NVM_DIR=”$HOME/.nvm”
[ -s “$NVM_DIR/nvm.sh” ] && \. “$NVM_DIR/nvm.sh” # This loads nvmInstall Node:
nvm install — lts
3. Install Yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo “deb https://dl.yarnpkg.com/debian/ stable main” | sudo tee /etc/apt/sources.list.d/yarn.list
4. Install PM2
sudo npm install pm2 -g
5. Install Git
sudo apt-get install git
6. Install MongoDB
sudo apt-key adv — keyserver hkp://keyserver.ubuntu.com:80 — recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod
Adding an Administrative User
mongo
use admin
db.createUser({user: “peacenepal”,pwd: “peacezone@123”, roles:[{role:”dbAdminAnyDatabase”, db: “admin”}]})
Type ‘exit’ and press ENTER or use CTRL+C to leave the client.
sudo nano /etc/mongod.conf
SETUP PART
NodeJs Server
- Run the server using PM2
Go to the project folder and run the following command
pm2 start app.js -i 2 — name api-server
Note: Command depends on your project
After that api would be accessible
eg: http://<SERVER_ADDRESS>:<PORT>
SERVER_ADDRESS ; IP address or Domain name used for server access
PORT; used to start the NodeJs server
Angular App setup
- First add the project files in the server in desired location
- Configure the Nginx to server our app files
Create Nginx configuration
Create new file in the following folder;
/etc/nginx/sites-enabled/
Add following details;
server {
listen 80;
listen [::]:80;
root <PROJECT_FOLDER_PATH>;
index index.html index.htm index.nginx-debian.html;
server_name <SERVER_ADDRESS>;
location / {
try_files $uri $uri/ =404;
}
}
Then reload the Nginx server with the following command to use the new server block.
sudo systemctl reload nginx
sudo systemctl restart nginx
What am I missing here? Let me know in the comments and I’ll add it in!