# การติดตั้ง

## ติดตั้ง Nodejs

ติดตั้งผ่าน nvm เพื่อให้ง่ายต่อการ upgrade

{% embed url="<https://github.com/nvm-sh/nvm>" %}
<https://github.com/nvm-sh/nvm>
{% endembed %}

### ใช้ Window (powershell and Run as admin)&#x20;

{% embed url="<https://github.com/coreybutler/nvm-windows>" %}
<https://github.com/coreybutler/nvm-windows>
{% endembed %}

### ใช้ Debian

```
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
```

### ติดตั้ง Nodejs LTS ผ่าน nvm (20.10.0)

```
//install nodejs lts
nvm install lts

//nodejs list
nvm ls

//use node
nvm use lts
```

### ติดตั้ง yarn

```
// use new yarn in nodejs
corepack enable

//yarn version 4.0.2
yarn set version stable

//check version
yarn -v
```

ติดตั้ง Mongodb ด้วย docker

```
// /home/mongodb/mongodb/docker-compose.yml
version: "3.8"

services:
  mongo1:
    image: mongo:5
    container_name: mongo1
    # command: ["--replSet", "so-replica-set", "--bind_ip_all", "--port", "30001"]
    command: ["--bind_ip_all", "--port", "30001"]
    restart: always
    volumes:
      - ./mongodb_data_container/mongo-1:/data/db
      - /mnt/sda3/backup/mongodb:/home
    ports:
      - 30001:30001
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: qIsVzM5cZgHwqqIIVxGH3NE

```

```
// mongodb@mongodb:~/mongodb$ 
sudo docker compose -f docker-compose.yml up -d --build

```

## Download จาก git

```
// Some code
git clone ...
```

## ทดลองบน Local

### Frontend

```
// frontend
cd tph-smart-office/frontend

//install dependency
yarn install

// run dev env
yarn dev
```

### Backend

```
// api
cd tph-smart-office/api

//install dependency
yarn install

// run dev env
yarn dev
```

## Server

### ตำแหน่ง upload ไฟล์

```
//project path
so.tph.go.th@HosBak: ~/public_html/smart-office

//upload file path
so.tph.go.th@HosBak: ~/public_html/smart-office/api/uploads
```

### Deploy ขึ้น server&#x20;

<pre><code><strong>cd smart-office
</strong><strong>
</strong><strong>//update code
</strong><strong>git pull
</strong><strong>
</strong>
// down docker
docker-compose -f docker-compose.yml down

// up docker
docker-compose -f docker-compose.yml up -d --build
</code></pre>

<pre><code><strong>// root@HosBak:/etc/nginx/sites-available/so.tph.go.th.vhost
</strong><strong>server {
</strong>        listen 80;
        server_name so.tph.go.th;
        return 301 https://$host$request_uri;
}

// root@HosBak:/etc/nginx/sites-available/api.tph.go.th.vhost
server {
        client_max_body_size 64M;
        listen 80;
        server_name api.tph.go.th;

        location / {
                proxy_pass             http://127.0.0.1:19072;
                proxy_read_timeout     60;
                proxy_connect_timeout  60;
                proxy_redirect         off;

                # Allow the use of websockets
                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;
        }
}

</code></pre>

### backup script

file upload folder backup

<pre><code><strong>// local upload folder
</strong>so.tph.go.th@HosBak: ~/public_html/smart-office/api/uploads

//remote backup folder
mongodb@mongodb: /mnt/sda3/backup/upload$
<strong>
</strong><strong>// rsync command
</strong><strong>rsync -az -e "ssh -i /home/so.tph.go.th/.ssh/id_rsa_backup" /home/so.tph.go.th/public_html/smart-office/api/uploads/ mongodb@192.168.10.245:/mnt/sda3/backup/upload/
</strong>
//crontab
crontab -e
20 12 * * * /home/so.tph.go.th/public_html/backup-so.sh
10 18 * * * /home/so.tph.go.th/public_html/backup-so.sh
</code></pre>
