Skip to content

Instantly share code, notes, and snippets.

@danieltorscho
Last active January 18, 2024 17:18
Show Gist options
  • Save danieltorscho/a1bb9763969300b83b60837983735b7c to your computer and use it in GitHub Desktop.
Save danieltorscho/a1bb9763969300b83b60837983735b7c to your computer and use it in GitHub Desktop.
Ubuntu initial setup for Node.js

Initial Server Setup with Ubuntu 20.04

project: kizlyarsk username: amroll

(Optional) Enable SSH access for non root-users

  1. sudo vi /etc/ssh/sshd_config
  2. change PasswordAuthentication from no to yes
  3. save and exit
  4. restart ssh sudo systemctl restart ssh

Setup Firewall

  1. ufw allow OpenSSH
  2. ufw enable

Create a super-user

  1. ssh root@your_server_ip
  2. adduser amroll
  3. usermod -aG sudo amroll
  4. rsync --archive --chown=amroll:amroll ~/.ssh /home/amroll
  5. exit
  6. ssh amroll@your_server_ip

Install Node.js

  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
  2. source ~/.bashrc
  3. nvm install 14
  4. cd ~
  5. mkdir ~/.npm-global
  6. npm config set prefix '~/.npm-global'
  7. sudo vi ~/.profile and add export PATH=~/.npm-global/bin:$PATH
  8. source ~/.profile
  9. nvm use --delete-prefix v14.18.1 <-- check the WARNING message in console log to correct the version number
  10. source ~/.profile

Install Database (PostgreSQL)

  1. sudo apt update
  2. sudo apt install postgresql postgresql-contrib -y
  3. sudo -i -u postgres
  4. sudo -u postgres createuser --interactive
  5. sudo -u postgres createdb -O amroll kizlyarsk
  6. sudo -u postgres psql
  7. ALTER USER amroll PASSWORD 'password';
  8. \q
  9. sudo nano /etc/postgresql/12/main/postgresql.conf
  10. Change listen_addresses from localhost to *
  11. sudo nano /etc/postgresql/12/main/pg_hba.conf
  12. sudo ufw allow 5432/tcp
  13. sudo systemctl restart postgresql

Deploy from Github

  1. cd ~
  2. mkdir www
  3. cd www
  4. git clone [email protected]:danieltorscho/strapi kizlyarsk
  5. cd kizlyarsk
  6. npm install
  7. NODE_ENV=production npm run build
  8. sudo ufw allow 1337/tcp (later sudo ufw deny 1337)

Install PM2

  1. npm install pm2@latest -g
  2. pm2 init
  3. sudo vi ecosystem.config.js and replace with following:
module.exports = {
  apps: [
    {
      name: 'kizlyarsk',
      cwd: '/home/amroll/www/kizlyarsk',
      script: 'npm',
      args: 'start',
      env: {
        NODE_ENV: 'production',
        DATABASE_HOST: 'localhost', // database endpoint
        DATABASE_PORT: '5432',
        DATABASE_NAME: 'strapi', // DB name
        DATABASE_USERNAME: 'your-name', // your username for psql
        DATABASE_PASSWORD: 'password', // your password for psql
      },
    },
  ],
};
  1. pm2 start ecosystem.config.js
  2. pm2 startup systemd and copy the output as suggested in terminal
  3. sudo env PATH=$PATH:/home/amroll/.nvm/versions/node/v14.18.1/bin /home/amroll/.nvm/versions/node/v14.18.1/lib/node_modules/pm2/bin/pm2 startup systemd -u amroll --hp /home/amroll
  4. pm2 save

Install Nginx

  1. sudo apt install nginx -y
  2. sudo ufw allow 'Nginx HTTP'
  3. systemctl status nginx
  4. sudo systemctl stop nginx
  5. sudo nano /etc/nginx/sites-available/kizlyarsk
  6. sudo ln -s /etc/nginx/sites-available/kizlyarsk /etc/nginx/sites-enabled/
  7. sudo nano /etc/nginx/nginx.conf> server_names_hash_bucket_size 64;
  8. sudo nginx -t
  9. sudo systemctl restart nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment