AWS, GCP, Azure, Digital Ocean Cloud 인스턴스 또는 로컬에서 Ubuntu 18.04의 프로덕션 서버에 Nodejs Mongodb를 설정하는 방법

6895 단어 howdeploybackendcloud
인프라 자동화와 같은 DevOps 관행에 대해 말하면 대기업 애플리케이션을 위한 훌륭한 도구가 많이 있습니다. 그러나 작은 응용 프로그램의 경우 파리에 썰매 망치를 사용하는 것과 같이 과잉입니다. 그렇다면 Terraform 또는 구성 관리 시스템Chef , Ansible , Puppet 과 같은 인프라를 코드 시스템으로 사용하는 이유는 무엇입니까? 이 5분 설치 가이드로 간단하게 비행할 수 있을 때 😜 (농담입니다. 지금 배우고 있습니다 📚)

설치 단계를 정기적으로 업데이트하므로 최신 버전Github gist을 얻습니다.

📝 아직 기사 초안 작성 중

#!/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 "
----------------------
  GIT
----------------------
"
# install curl
sudo apt install curl -y

# install git
sudo apt-get install -y git


echo "
----------------------
  NODE & NPM
----------------------
"
## You may also need development tools to build native addons:
sudo apt-get install gcc g++ make -y

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

nvm ls-remote

nvm install 14

nvm alias default  14.15.0

# add nodejs 14 ppa (personal package archive) from nodesource
# curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

# install nodejs and npm
# sudo apt-get install -y nodejs


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

# 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

# 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

## start 📥 

# 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;
    }
}

## end 📤

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

좋은 웹페이지 즐겨찾기