Skip to content

Instantly share code, notes, and snippets.

@Nditah
Last active March 11, 2024 14:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nditah/6d619b2370d20f34949a7e249f4d6d94 to your computer and use it in GitHub Desktop.
Save Nditah/6d619b2370d20f34949a7e249f4d6d94 to your computer and use it in GitHub Desktop.
How to Setup Nodejs Mongodb in Production server on Ubuntu 18.04 in AWS, GCP, Azure, Digital Ocean Cloud Instance or Locally
#!/usr/bin/env bash
# Steps to write and execute a script
# Open the terminal. Go to the directory where you want to create your script.
# Create a file with . sh extension.
# Write the script in the file using an editor.
# Make the script executable with command chmod +x <fileName>.
# Run the script using ./<fileName>.
echo "
----------------------
Adding a New User to the System 'Sammy'
----------------------
"
adduser sammy
# enter all the prompted info
# Step 3 — Adding the User to the sudo Group
usermod -aG sudo sammy
# Testing sudo Access
su - sammy
sudo ls -la /root
echo "
----------------------
NODE & NPM
----------------------
"
## You may also need development tools to build native addons:
sudo apt-get install -y git curl gcc g++ make
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
nvm ls-remote
nvm install 18
nvm alias default 18
echo "
----------------------
MONGODB
----------------------
"
# import mongodb 4.0 public gpg key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
# create the /etc/apt/sources.list.d/mongodb-org-4.0.list file for mongodb
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
# reload local package database
sudo apt-get update
# install the latest version of mongodb
sudo apt-get install -y mongodb-org
# start mongodb
sudo systemctl start mongod
# stop mongodb
sudo systemctl stop mongod
# Make a directory as root user
sudo mkdir -p /data/db
# Provide access to the directory
sudo chown -R $USER /data/db
# set mongodb to start automatically on system startup
sudo systemctl enable mongod
# stop mongodb to start automatically on system startup
sudo systemctl disable mongod
# install local replication-set driver for nodejs
sudo npm install --unsafe-perm --verbose -g run-rs -f
# start mongodb replica set
# run-rs --mongod --keep --shell --dbpath /home/user/data"
# start mongod as a background process
mongod --fork --syslog
echo "
----------------------
PM2
----------------------
"
# install pm2 with npm
npm install -g pm2 yarn
# set pm2 to start automatically on system startup
pm2 startup systemd
# make current user the owner of the pm2 log home dir
sudo chown -R $(whoami):$(whoami) /home/ubuntu/.pm2
# create a shell script replica.sh
$ nano replica.sh
#!/bin/bash
run-rs --mongod --keep --shell --dbpath /data/db
$ pm2 run replica.sh
echo "
----------------------
NGINX
----------------------
"
# install nginx
sudo apt-get install -y nginx
# You can make the currrent $USER the owner of that directory
sudo chown -R $(whoami):$(whoami) /var/www
# set the appropriate permissions
chmod 755 -R /var/www
echo "
----------------------
UFW (FIREWALL)
----------------------
"
# allow ssh connections through firewall
# sudo ufw allow OpenSSH
# allow http & https through firewall
# sudo ufw allow 'Nginx Full'
# enable firewall
# sudo ufw --force enable
echo "
----------------------
NETWORK TESTING TOOL
----------------------
"
# curl tool
sudo apt install httpie -y
sudo apt update
sudo apt install redis-server -y
# # comment out `supervised no` and set `supervised systemd`
sudo nano /etc/redis/redis.conf
# > supervised systemd
# restart redis server
sudo systemctl restart redis.service
echo "
----------------------
SET UP LETS-ENCRYPT
----------------------
"
# Instal CertBot
# curl -o- https://raw.githubusercontent.com/vinyll/certbot-install/master/install.sh | bash
sudo apt-get install python3-certbot-nginx
# Open the server block file for your domain using nano or your favorite text editor:
sudo nano /etc/nginx/sites-available/example.com
#server_name example.com www.example.com;
# test and restart nginx
sudo nginx -t
sudo systemctl reload nginx
# create the nginx default configuration
nano default
# paste the content below
```conf
# website server
server {
server_name example.com www.example.com;
root /var/www/html/web/build;
index index.html;
location / {
try_files $uri$args $uri$args/ /index.html;
}
}
# admin console server
server {
server_name admin.example.com;
root /var/www/html/admin/dist;
index index.html;
location / {
try_files $uri$args $uri$args/ /index.html;
}
}
# demo or documentation server
server {
server_name developers.example.com;
root /var/www/html/backend/doc;
index index.html;
location / {
try_files $uri$args $uri$args/ /index.html;
}
}
# backend api server
server {
server_name api.example.com;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://localhost:5000;
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;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
}
```
sudo rm /etc/nginx/sites-available/default
sudo mv default /etc/nginx/sites-available/default
# Set up Certbot to obtain SSL certificates
sudo certbot --nginx -d example.com -d www.example.com -d api.example.com -d dev.example.com -d developers.example.com -d admin.example.com
# To test the renewal process, you can do a dry run with certbot:
sudo certbot renew --dry-run
# git config credential.helper store
echo "
--------------------------------------------
Clone and Deploy from Github or Bitbucket
-------------------------------------------
1. From the terminal of Cloud Console (Digital Ocean Droplet, AWS EC2), enter ssh-keygen at the command line.
The command prompts you for a file to save the key in:
$ ssh-keygen
1. From Bitbucket, choose Personal settings from your avatar in the lower left.
2. Click *SSH keys*. If you've already added keys, you'll see them on this page.
3. In your terminal window, copy the contents of your public key file. If you renamed the key, replace id_rsa.pub with the public key file name.
On Linux, you can cat the contents:
$ cat ~/.ssh/id_rsa.pub | xclip -sel clip
On macOS, the following command copies the output to the clipboard:
$ pbcopy < ~/.ssh/id_rsa.pub
4. Select and copy the key output in the clipboard.
If you have problems with copy and paste, you can open the file directly with Notepad. Select the contents of the file (just avoid selecting the end-of-file characters).
5. From Bitbucket, click `Add key`.
6. Enter a Label for your new key, for example, Default public key.
7. Paste the copied public key into the SSH Key field.
You may see an email address on the last line when you paste. It doesn't matter whether or not you include the email address in the Key.
8. Click Save.
Bitbucket sends you an email to confirm the addition of the key."
## Install Docker
# Next, install a few prerequisite packages which let apt use packages over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
# Then add the GPG key for the official Docker repository to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Add the Docker repository to APT sources:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
# This will also update our package database with the Docker packages from the newly added repo.
# Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:
apt-cache policy docker-ce
# You’ll see output like this, although the version number for Docker may be different:
# Finally, install Docker:
sudo apt install docker-ce
# Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:
sudo systemctl status docker
## Instaaling Docker-Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
## Next, set the correct permissions so that the docker-compose command is executable:
sudo chmod +x /usr/local/bin/docker-compose
## To verify that the installation was successful, you can run:
docker-compose --version
@Nditah
Copy link
Author

Nditah commented Aug 28, 2023

How to Solve Digital Ocean Error "Could not establish connection to droplet: Permission denied (publickey)."

On your Local Machine, e.g. MacOS and copy the code

cat ~/.ssh/id_ed25519.pub

On the cloud droplet, open the Auth key files and paste what you copied.

nano ~/.ssh/authorized_keys

@Nditah
Copy link
Author

Nditah commented Sep 2, 2023

@Nditah
Copy link
Author

Nditah commented Mar 11, 2024

To create the docker group and add your user:

  1. Create the docker group.

sudo groupadd docker

  1. Add your user to the docker group.

sudo usermod -aG docker $USER

  1. Log out and log back in so that your group membership is re-evaluated.

    If you're running Linux in a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.

You can also run the following command to activate the changes to groups:

newgrp docker

  1. Verify that you can run docker commands without sudo.

docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.

To fix this problem, either remove the ~/.docker/ directory (it's recreated automatically, but any custom settings are lost), or change its ownership and permissions using the following commands:

 sudo chown "$USER":"$USER" /home/"$USER"/.docker -R

 sudo chmod g+rwx "$HOME/.docker" -R

Configure Docker to start on boot with systemd

 sudo systemctl enable docker.service

 sudo systemctl enable containerd.service

To stop this behavior, use disable instead.

 sudo systemctl disable docker.service

 sudo systemctl disable containerd.service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment