Difference between revisions of "Deploying node, react apps to ionos cloud"

From Logic Wiki
Jump to: navigation, search
(Create a site in Nginx)
 
(8 intermediate revisions by the same user not shown)
Line 39: Line 39:
 
  mkdir live
 
  mkdir live
 
  cd live
 
  cd live
 +
sudo -s
 
This command below is a runner cmd in github/repo/settings/action/runner and select OS
 
This command below is a runner cmd in github/repo/settings/action/runner and select OS
 
  curl -o actions-runner-linux-x64-2.316.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.316.1/actions-runner-linux-x64-2.316.1.tar.gz
 
  curl -o actions-runner-linux-x64-2.316.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.316.1/actions-runner-linux-x64-2.316.1.tar.gz
 
to run the next command in github you may see permission problems and to fix them run commands below
 
to run the next command in github you may see permission problems and to fix them run commands below
 +
 +
tar xzf ./actions-runner-linux-x64-2.316.1.tar.gz
 
  $ sudo -s
 
  $ sudo -s
 
  # chmod o+w .
 
  # chmod o+w .
 +
# exit
 
Github command next
 
Github command next
tar xzf ./actions-runner-linux-x64-2.316.1.tar.gz
+
  ./config.sh --url https://github.com/Aliiybar/logicmade_api --token ABKE7DQYNVTH3NWQYOP6BPTGIEZJK
 
fill the tags  
 
fill the tags  
 
+
To activate it
 +
sudo ./svc.sh install
 +
sudo ./svc.sh start
 +
==== Removing / uninstalling a runner ====
 +
* In the Github repo go to Settings/Actions/Runners and click three dots then click remove.
 +
* You'll get removing command (./config...)
 +
* Goto the folder where it's installed on the server and run this
 +
sudo ./svc.sh uninstall
 +
run the command you get from github above
 +
./config.sh remove --token <TOKEN PROVIDED BY GITHUB>
 
=== Create a site in Nginx ===
 
=== Create a site in Nginx ===
 +
For node sites it's expected to run the site in pm2 with a port number and redirect the port number of the site in nginx. But in react or static sites no need to run it in pm2 just point where the dist folder is.
  
 +
These definitions contains letsencrypt files to make the sites SSL ready
 +
 +
Create a file in /etc/nginx/sites-available with the name of the site
 +
For node site :
 +
<pre>
 +
server {
 +
        listen 80;
 +
        server_name api.logicmade.co.uk;
 +
        return 301 https://$server_name$request_uri;
 +
}
 +
 +
server{
 +
        listen 443 ssl;
 +
        server_name api.logicmade.co.uk;
 +
 +
        ssl_certificate /etc/letsencrypt/live/api.logicmade.co.uk/fullchain.pem;
 +
        ssl_certificate_key /etc/letsencrypt/live/api.logicmade.co.uk/privkey.pem;
 +
 +
        location / {
 +
                proxy_pass http://localhost:4000/;
 +
                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;
 +
 +
             
 +
        }
 +
}
 +
</pre>
 +
For React / static site
 +
<pre>
 +
server {
 +
 +
        root /var/www/admin.logicmade.co.uk/html/_work/logicmade_admin/logicmade_admin/dist/;
 +
        index index.html index.htm index.nginx-debian.html;
 +
 +
        server_name admin.logicmade.co.uk;
 +
 +
        location / {
 +
                try_files $uri /index.html$is_args$args $uri/ =404;
 +
        }
 +
 +
 +
    listen 443 ssl; # managed by Certbot
 +
    ssl_certificate /etc/letsencrypt/live/admin.logicmade.co.uk/fullchain.pem; # managed by Certbot
 +
    ssl_certificate_key /etc/letsencrypt/live/admin.logicmade.co.uk/privkey.pem; # managed by Certbot
 +
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
 +
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
 +
 +
 +
}
 +
server {
 +
    if ($host = admin.logicmade.co.uk) {
 +
        return 301 https://$host$request_uri;
 +
    } # managed by Certbot
 +
 +
 +
 +
        listen 80;
 +
        listen [::]:80;
 +
 +
        server_name admin.logicmade.co.uk;
 +
    return 404; # managed by Certbot
 +
}
 +
</pre>
 +
  sudo systemctl restart nginx
  
 
=== Install let's encrypt ===
 
=== Install let's encrypt ===
 +
 +
  apt-get update
 +
  sudo apt-get install certbot
 +
  apt-get install python3-certbot-nginx
 +
 +
  sudo nginx -t
 +
  sudo systemctl reload nginx
 +
 +
  sudo certbot --nginx -d api.logicmade.co.uk -d apitest.logicwiki.co.uk
 +
 +
sudo systemctl status certbot.timer
 +
sudo certbot renew --dry-run
 +
 +
 
[https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04 https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04]
 
[https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04 https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04]
 +
 +
=== Environment variables ===
 +
edit /etc/environment file in the server to set environment variables and activate it with source or "."
 +
sudo nano /etc/environment
 +
. /etc/environment

Latest revision as of 09:34, 17 May 2024


Plan

  • install node / npm
  • install nginx to cloud
  • create site in nginx
  • manually deploy typescript node app after build
  • install let's encrypt
  • Test node app in SSL
  • Create github action to deploy site from main branch
  • Create github action to deploy site to Azure WebApps from develop branch
  • Decide where to keep database
  • test deployment end to end
  • repat steps for the react app

Action

Installing node / npm

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

restart the terminal and to install

nvm install 22

Install Nginx

sudo apt update
sudo apt install nginx
sudo systemctl start nginx

Install PM2

sudo npm install -g pm2
pm2 startup systemd

Change Owner of a folder

sudo chown -R username path 

Create Self-Hosted Runners of Github

cd /var/www/api.logicmade.co.uk
mkdir live
cd live
sudo -s

This command below is a runner cmd in github/repo/settings/action/runner and select OS

curl -o actions-runner-linux-x64-2.316.1.tar.gz -L https://github.com/actions/runner/releases/download/v2.316.1/actions-runner-linux-x64-2.316.1.tar.gz

to run the next command in github you may see permission problems and to fix them run commands below

tar xzf ./actions-runner-linux-x64-2.316.1.tar.gz
$ sudo -s
# chmod o+w .
# exit

Github command next

 ./config.sh --url https://github.com/Aliiybar/logicmade_api --token ABKE7DQYNVTH3NWQYOP6BPTGIEZJK

fill the tags To activate it

sudo ./svc.sh install
sudo ./svc.sh start

Removing / uninstalling a runner

  • In the Github repo go to Settings/Actions/Runners and click three dots then click remove.
  • You'll get removing command (./config...)
  • Goto the folder where it's installed on the server and run this
sudo ./svc.sh uninstall

run the command you get from github above

./config.sh remove --token <TOKEN PROVIDED BY GITHUB>

Create a site in Nginx

For node sites it's expected to run the site in pm2 with a port number and redirect the port number of the site in nginx. But in react or static sites no need to run it in pm2 just point where the dist folder is.

These definitions contains letsencrypt files to make the sites SSL ready

Create a file in /etc/nginx/sites-available with the name of the site For node site :

server {
        listen 80;
        server_name api.logicmade.co.uk;
        return 301 https://$server_name$request_uri;
}

server{
        listen 443 ssl;
        server_name api.logicmade.co.uk;

        ssl_certificate /etc/letsencrypt/live/api.logicmade.co.uk/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/api.logicmade.co.uk/privkey.pem;

        location / {
                proxy_pass http://localhost:4000/;
                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;

              
        }
}

For React / static site

server {

        root /var/www/admin.logicmade.co.uk/html/_work/logicmade_admin/logicmade_admin/dist/;
        index index.html index.htm index.nginx-debian.html;

        server_name admin.logicmade.co.uk;

        location / {
                try_files $uri /index.html$is_args$args $uri/ =404;
        }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/admin.logicmade.co.uk/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/admin.logicmade.co.uk/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
    if ($host = admin.logicmade.co.uk) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

 

        listen 80;
        listen [::]:80;

        server_name admin.logicmade.co.uk;
    return 404; # managed by Certbot
}
 sudo systemctl restart nginx

Install let's encrypt

 apt-get update
 sudo apt-get install certbot
 apt-get install python3-certbot-nginx
 sudo nginx -t
 sudo systemctl reload nginx
 sudo certbot --nginx -d api.logicmade.co.uk -d apitest.logicwiki.co.uk
sudo systemctl status certbot.timer
sudo certbot renew --dry-run


https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

Environment variables

edit /etc/environment file in the server to set environment variables and activate it with source or "."

sudo nano /etc/environment
. /etc/environment