Common commands

To release terminal or run command in background:

command &

or

command > /dev/null 2>&1 &
docker-compose up > /dev/null 2>&1 &

Administration

Add user:

sudo adduser michael

To add the sudo rights:

sudo usermod -aG sudo michael

To login as a michael:

su michael

System Info

Get info about memory usage:

free
cat /proc/meminfo
htop

Run script as a service

Step 1: Create a Systemd Unit File

sudo nano /lib/systemd/system/shellscript.service

Edit it:

[Unit]
Description=My Shell Script

[Service]
ExecStart=/usr/bin/script.sh
User=michael

[Install]
WantedBy=multi-user.target

Step 2: reload demon

sudo systemctl daemon-reload

Step 3: Enable service

sudo systemctl enable shellscript.service 
sudo systemctl start shellscript.service 

You can check status:

sudo systemctl status shellscript.service 

Useful tips:

How to archive directory:

tar -czvf result_file.tar.gz target_dir

How to execute several commands:

A; B    # Run A and then B, regardless of success of A
A && B  # Run B if and only if A succeeded
A || B  # Run B if and only if A failed
A &     # Run A in background.

Installation Web Service

NodeJS 18 installation:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

Then

sudo apt-get install nodejs

PM2 Installation as root:

sudo npm install pm2@latest -g

Start PM2 process as specific user

pm2 start build/index.js --name appname

To see the ports of processes (one of the two following commands):

sudo netstat -tnlp
ss -tnlp | grep "node /"

Install NGINX as a reverse proxy

sudo nano /etc/nginx/sites-enabled/site-name

Use the following config:

map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

server {
    listen              443 ssl;

    ssl_certificate     /etc/ssl/site-name/site.crt;
    ssl_certificate_key /etc/ssl/site-name/ssl-paletter.key;

    server_name         site.com;
    access_log          /var/log/nginx/nginx.vhost.access.log;
    error_log           /var/log/nginx/nginx.vhost.error.log;

    location / {
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection $connection_upgrade;
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://localhost:3000;
    }
}