---
# System prepended metadata

title: Untitled

---

## Server Setup IoT Kompor

### 1. Require Sistem*



| Item | Detail |
| -------- | -------- |
| Sistem Operasi     | Ubuntu v18.04 / latest     |
| Application     | Nodejs v16.13.0 / latest   |
| Database     | Mongodb v5.0 / latest   |
| backend & frontend     | Express.js   |

### 2. Instalasi DataBase

#### 2.1 Add Mongodb Repository
```shell=1
$ sudo apt-get update
$ wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
```
> for ubuntu 18.04

```shell=3
$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
```

> for ubuntu 20.04

```shell=3
$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
```

#### 2.2 Installation mongodb
```shell=4
$ sudo apt-get update
$ sudo apt-get install -y mongodb-org=5.0.2 mongodb-org-database=5.0.2 mongodb-org-server=5.0.2 mongodb-org-shell=5.0.2 mongodb-org-mongos=5.0.2 mongodb-org-tools=5.0.2
$ sudo systemctl start mongod
```

### 3. Setup Mongodb
> Cek mongodb
```sh
$ systemctl status mongod
```

> Output
```sh
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-12-07 09:06:35 WIB; 2 months 3 days ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 23516 (mongod)
     Memory: 54.8M
     CGroup: /system.slice/mongod.service
             └─23516 /usr/bin/mongod --config /etc/mongod.conf

Dec 07 09:06:35 vmi624299.contaboserver.net systemd[1]: Started MongoDB Database Server.
```

#### 3.1 Create new User

> masuk ke mongodb
```sh 
$ mongo
```

> membuat akun sebagai superuser:
* masuk ke database admin
```sh 
> use admin
```
`*Note : untuk database bisa dibuat sesuai kebutuhan, disini default superuser/admin biasanya berada pada database admin
`

> membuat user admin baru
```sh
> db.createUser({
    user: "admin",
    pwd: "123456789"
    roles: [
        role: "root",
        db: "admin"
    ]
})
```

* membuat user member baru
```sh
> use antares
> db.createUser({
    user: "antares",
    pwd: "aTr12345"
    roles: [
        role: "readWrite",
        db: "antares"
    ]
})
```


`*Note : untuk keseluruhan role mongodb bisa baca di` [sini](https://docs.mongodb.com/manual/reference/built-in-roles/)

* keluar dari mongodb
```sh 
> exit
```

#### 3.2 Setup Authentication Mongodb
> buka `/etc/mongod.conf` dengan code editor ubuntu
```sh
$ nano /etc/mongod.conf
```

> tambahkan baris baru dengan isi
```sh
security:
    authorization: enabled
```

> restart mongodb
```sh
$ sudo service mongodb restart
```

> login ke mongodb
```sh
$ mongo -u [user] -p [passsword]
```

`>> Jika login berhasil, setup sudah berhasil. Jika terdapat error pastikan semua tahapan setup sudah benar`


### 4. Install NodeJs
> install wget
```sh
$ sudo apt install wget
```
> jalankan perintah berikut
```sh
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
```
> beri akses bash ke user
```sh
$ source ~/.profile
```
> menampilkan semua list versi nodejs
```sh
$ nvm ls-remote
```
> install nodejs
```sh
$ nvm install 16.13.0
```
> melihat versi node js jika sudah ter-install
```sh
$ node -v
```

### 5. Setup backend dan frontend
#### 5.1 penyiapan folder
`untuk penyimpanan dapat dibuat didalam folder /var/www`
#### 5.2 clone data backend dan frontend 
> clone data
```sh
$ git clone https://github.com/Alis-Dev-idn/xirka-api-meter.git
```
`*Note : jika diminta username dan password bisa hubungi kami untuk dapat mengakses repository tersebut`

#### 5.3 Setup backend dan frontend
> masuk kedalam folder yang sudah di clone
```sh
$ cd xirka-api-meter
```

> menginstall library yang dibutuhkan
```sh
$ npm i
```
> update file .env
```sh
$ nano .env
```
pada file .env terdapat 3 item yaitu


| Item | Keterangan |
| -------- | -------- |
| base_url     | sebagai host ip yang akan diakses     |
| port     | sebagai port dari host yang dapat diakses     |
| db_url     | untuk akses kedalam mongodb |

> contoh pengisian
```sh
base_url = http://[ip/domain]
port = [port]
db_url = mongodb://[username]:[password]@[ip]/[database]
```
`*Note : pengisian tidak disertai tanda []`
> menjalankan program
```sh
$ node app.js
```
`*Note : Jika tidak terdapat error maka pengaturan telah berhasil`
> exit program
```
tekan kombinasi ctrl+c 
```
> install pm2
```sh
$ sudo npm install pm2@latest -g
```
> menjalankan server
```sh
$ pm2 start app.js --name [name]
```
> melihat status
```sh
$ pm2 list
```
> setup pm2 agar berjalan dibackground
```sh
$ pm2 startup systemd
```
* Output
```shell=
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure

ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service → /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd
```
> jalankan perintah
```shell=    
$ pm2 save
$ systemctl enable pm2-root
```
`*Note : pm2-root didapat dari output pada baris ke 33`
* jika terdapat output seperti ini
```shell=
[PM2] Init System found: systemd
sammy
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
```
> jalankan perintah
```sh
$ sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
```
`*Note : nama sammy didapat dari output`

> start service
```sh
$ sudo systemctl start pm2-sammy
```
> cek status service pm2
```sh
$ systemctl status pm2-sammy
```
> output
```sh
● pm2-root.service - PM2 process manager
     Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: https://pm2.keymetrics.io/
```
`Jika output sama seperti diatas maka setup pm2 telah berhasil`

> monitoring pm2
```sh
$ pm2 monit
```

`*Note : pada tahapan ini instalasi server sudah selesai silakan akses frontend dengan ip dan port yang sudah di atur sebelumnya. Jika server tidak dapat di akses pastikan port tidak diblokir oleh firewall.`