# Deploy anvil app on Nipa Cloud ## Create nipa cloud instance OS: Ubuntu 20.04 Cloud Firewall: Ping, SSH, Http-Https Authentication: Key-Pair ([see how to generate public key](https://hackmd.io/OC2Sz57ERc6Z7MJuBfWaTg?both#Generate-SSH-Keys)) ## SSH into cloud instance  ``` \> ssh nc-user@<server_ip_address> ``` ## Update cloud instance ```bash sudo apt update && sudo apt upgrade ``` ## Setup Server #### อนุญาติให้ใช้งาน port 433 On many Linux systems unprivileged users cannot create services on port 443. This is a somewhat archaic restriction, and there are a few ways around it. The simplest is to turn it off, with the commands: ``` sudo -i sudo echo 'net.ipv4.ip_unprivileged_port_start=0' > /etc/sysctl.d/50-unprivileged-ports.conf sudo sysctl --system exit ``` #### Install Required Packages ```bash sudo apt install python3-pip openjdk-8-jdk ``` #### Install virtualenv ```bash sudo pip3 install virtualenv ``` #### สร้าง virtualenv ใน `first_app` We are going to start by creating a virtual environment for the Anvil Standalone Runtime to be contained in. The directory I’m creating my environment in is my users [home directory](https://help.ubuntu.com/community/HomeFolder). To navigate there we can run: #### สร้าง folder สำหรับเก็บโปรเจ็ค anvil ชื่อ `first_app` ```bash cd ~ mkdir first_app ``` #### สร้าง Virtualenv ```bash cd first_app virtualenv env ``` #### Activate virtualenv ```bash source env/bin/activate ``` #### Install anvil-app-server ลงใน virtualenv ที่เราสร้างในขั้นตอนก่อนหน้า ```bash (env) pip install anvil-app-server ``` เท่านี้ server ของเราก็พร้อมที่จะใช้งานเป็น anvil-app-server แล้ว ## Install anvil app #### สร้างโปรเจ็คใหม่ใน anvil ชื่อ first_app ... Go build something ... #### ทำการ commit code ในเมนู Version History  #### Clone code จาก anvil มายัง server  จากนั้นเอาคำสั่ง git clone ..... ไปรันใน server ของเรา **ข้อสังเกต** 1. มีการเปิดใช้งาน env 2. รันคำสั่งนี้ใน folder `~/first_app` ```bash (env) nc-user@tanin-1258:~/first_app$ git clone ssh://dobybot%40gmail.com@anvil.works:2222/ZDO4CIWZBE3JISFL.git first_app ----- ----------- ^ ^ 1 2 ``` #### Run anvil app ```bash (env) nc-user@tanin-1258:~/first_app$ anvil-app-server --app first_app --origin http://<your_server_ip> ``` หรือ ```bash (env) nc-user@tanin-1258:~/first_app$ anvil-app-server --app first_app --origin https://yourwebsite.com ``` เปิด web browser ไปที่ `http://<your_server_ip>` หรือ `https://yourwebsite.com` #### การอัพเดตโค๊ด 1. แก้ไขโค๊ดใน anvil.works 2. ทำการ commit code ในเมนู Version History ([วิธีการ](https://hackmd.io/OC2Sz57ERc6Z7MJuBfWaTg?both#%E0%B8%97%E0%B8%B3%E0%B8%81%E0%B8%B2%E0%B8%A3-commit-code-%E0%B9%83%E0%B8%99%E0%B9%80%E0%B8%A1%E0%B8%99%E0%B8%B9-Version-History)) 3. ใช้คำสั่ง git pull เพื่ออัพเดตโค๊ดล่าสุดจาก anvils.work ```bash cd ~/first_app/first_app git pull ``` ## Run anvil server ในโหมด daemon ด้วย systemd ปัญหาหนึ่งของขั้นตอนก่อนหน้าคือ เมื่อเราปิด terminal ไป server จะหยุดทำงานไปด้วย เราสามารถแก้ไขปัญหานี้ได้โดยการสั่งให้ anvil app รันใน daemon mode หรือ background process ```bash cd /etc/systemd/system sudo nano first_app.service ``` #### first_app.service ```= [Unit] Description=My First Anvil app server [Service] User=nc-user WorkingDirectory=/home/nc-user/first_app ExecStart=/home/nc-user/first_app/env/bin/anvil-app-server --app first_app --origin https://yourwebsite.com Restart=always [Install] WantedBy=multi-user.target ``` ออกจาก nano โดยการกด `ctrl+x` >>> `y` >>> `enter` เมื่อทำการแก้ไขไฟล์ .service ต้องสั่ง `sudo systemctl daemon-reload` ทุกครั้ง #### Reload the service files to include the new service. `sudo systemctl daemon-reload` #### Start your service `sudo systemctl start first_app.service` #### Restart your service `sudo systemctl restart first_app.service` #### Stop your service `sudo systemctl stop first_app.service` #### To check the status of your service `sudo systemctl status first_app.service` #### To enable your service on every reboot `sudo systemctl enable first_app.service` #### To disable your service on every reboot `sudo systemctl disable first_app.service` ## Logs ดู App logs (แก้เลข 80 ได้คือจำนวนแถว) ``` journalctl -u first_app.service -n 80 -f ``` ดู Error logs ``` cd ~/first_app/.anvil-data/ cat error.log ``` ## Reset Database ``` cd ~/first_app rm -rf .anvil-data ``` # Anvil Exras วิธีการลง Anvil Extras สำหรับ local host ให้ไปที่ Folder App (ชั้นแรก)  จากนั้นให้ git pull คำสั่งนี้ ``` git clone ssh://ict%40tsgco.co.th@anvil.works:2222/YFK2ZLQ7DWJ7KTPV.git anvil_extras ``` จะได้ folder ของ anvil_extras ปรากฏขึ้นมา  จากนั้นเข้าไปที่ systemcmd ```bash cd /etc/systemd/system sudo nano first_app.service ``` ให้เพิ่มคำสั่งนี้ลงไป ```--dep-id C6ZZPAPN4YYF5NVJ=anvil_extras```  เซฟและทำตามขั้นตอน restart service ตามปกติ # Basic Linux Commands - `ls` --- list files / folders - `ls -a` --- list all files and folders - `cd` --- change directory - `mkdir <folder-name>` --- make directory - `pwd` --- print working directory - `which <command>` --- print command full path - `nano <file-name>` --- edit file - `rm <file-name>` --- delete file - `rm -r <folder-name>` --- delete folder # Generate SSH Keys **public key** = แม่กุญแจ **private key** = ลูกกุญแจ สร้าง public / private key ``` ssh-keygen ``` ดู public / private key ``` cd ~/.ssh ls cat id_rsa cat id_rsa.pub ``` # Reference Articles - Setup and Secure a Server - https://www.linode.com/docs/guides/set-up-and-secure/ - Deploy anvil app on ubuntu server - https://anvil.works/docs/how-to/app-server/cloud-deployment-guides/linode-app-server-deployment - How to create a systemd service - https://www.shubhamdipt.com/blog/how-to-create-a-systemd-service-in-linux/ - How To Use Journalctl to View and Manipulate Systemd Logs - https://www.digitalocean.com/community/tutorials/how-to-use-journalctl-to-view-and-manipulate-systemd-logs
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up