Skip to content

Instantly share code, notes, and snippets.

@amit08255
Last active June 20, 2023 17:15
Show Gist options
  • Save amit08255/9b7fc9baec17dab786963ffb39a6e15d to your computer and use it in GitHub Desktop.
Save amit08255/9b7fc9baec17dab786963ffb39a6e15d to your computer and use it in GitHub Desktop.
DigitalOcean Cheatsheet

DigitalOcean Cheatsheet

Building Node.js Application

To build Node.js without interrupting run below command:

nohup yarn build &

Install Node.js

cd ~
curl -sL https://deb.nodesource.com/setup_20.x -o /tmp/nodesource_setup.sh

Check and validate script file using command:

nano /tmp/nodesource_setup.sh

Install using commands:

sudo bash /tmp/nodesource_setup.sh
sudo apt-get install -y nodejs

To install yarn package manager use:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

Verify installation using command:

node -v

Installing PostgreSQL

sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql.service

To open postgreql in terminal use command:

sudo -i -u postgres
psql -h localhost -U postgres

Set postgres user password using below SQL:

ALTER USER postgres WITH PASSWORD 'postgres';

Check port on which server is running use SQL:

SELECT * FROM pg_settings WHERE name = 'port';

Create database using SQL:

create database todoapp;

Login to database (username: postgres):

psql -h localhost -U postgres

Running Node.js app in background with pm2

Install pm2 using below command:

yarn global add pm2

or

sudo npm install pm2@latest -g

Switch to your Node.js application directory and build your project using command like:

npm run build

or

yarn build

Run below command to run start script:

pm2 start npm --name "nextapp" -- start

Check status of running app process using command:

pm2 status

Stop process using command:

pm2 stop nextapp

To update app, rebuild application and restart process using command:

pm2 restart nextapp

To list running processes use command:

pm2 ls

Hosting Node.js (under pm2) with Nginx

Install Nginx using command:

sudo apt-get update
sudo apt-get install nginx

Setup your site by editing file:

sudo vim /etc/nginx/sites-available/default

Paste below config in file (change 3000 port with your node.js application port):

server {
        listen 80;
        server_name example.com www.example.com;
        root /var/www/html;
        index index.html index.htm;

        location / {
                proxy_pass             http://127.0.0.1:3000;
                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;
        }
}

Restart nginx using command:

sudo service nginx restart

Copy local files to server using SSH

 scp -r full_path_of_source_folder username@server_ip:~/path_to_files/

Example:

 scp -r C:/Users/username/Documents/bedrock root@server_ip:~/bedrock/

Destination path should be a valid existing path.

Fix npm install getting killed

Run below command in ssh terminal:

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl vm.vfs_cache_pressure=50
echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf

Kill process

Run below command in ssh terminal:

To list running process:

ps -aux

To kill by process ID

sudo kill <pid>

Or to kill process by name

sudo killall <process-name>

List process running on a port

sudo lsof -n -i :PORT_NUMBER | grep LISTEN

Create new user

adduser username
usermod -aG sudo username

Use password login:

Go to dashboard -> project -> access. Enter username and launch console. Type below command:

sudo nano /etc/ssh/sshd_config
sudo service ssh restart

change PasswordAuthentication from no to yes save

Deleting user

deluser username

Delete user with it's home:

deluser --remove-home userame

Run Python Jupyter Server

If python pip command is not installed use below command:

sudo apt install -y python3-pip

Installing jupyter using below command:

python3 -m pip install jupyter

To access jupyter you need to use SSH tunneling. Run below command in your local computer:

ssh username@server_ip -L 8888:localhost:8888 -t "jupyter notebook --no-browser --port 8888"

If you are using root user then use below command:

ssh [email protected] -L 8888:localhost:8888 -t "jupyter notebook --no-browser --port 8888 --allow-root"

Open http://localhost:8888/ in your browser. It will ask for login token, copy from console and paste.

Second method [prevents killing of server]

Instead of running above ssh command run below command:

 ssh -N -f -L 8888:localhost:8888 username@server_ip

This command will run a background port forwarding using ssh.

Now open digitalocean dashboard and go to your project. Click on: Access. Now enter your username and Launch Droplet console. Run below command under the terminal:

jupyter notebook

Open http://localhost:8888/ in your browser. It will ask for login token, copy from console and paste.

Using jupyter in virtual environment:

Install python virtual environment:

sudo apt install -y python3-venv

Setup path for environments:

mkdir environments
cd environments

Create environment:

python3 -m venv my_env

Check if environment created:

ls my_env

Activating environment:

source my_env/bin/activate

To leave the environment, type the command deactivate and you will return to your original directory.

Install jupyter kernel for virtual environment

ipython kernel install --user --name=my_env

Now when you run the jupyter notebook you will be able to select the kernel to run in virtual environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment