Skip to content

Instantly share code, notes, and snippets.

@khavari
Last active May 9, 2023 11:16
Show Gist options
  • Save khavari/f449bebabcf1449c04df64f45fb8ec31 to your computer and use it in GitHub Desktop.
Save khavari/f449bebabcf1449c04df64f45fb8ec31 to your computer and use it in GitHub Desktop.

Deploy Next.js app on Ubuntu

1- Server Initial Setup

set server timezone to Asia/Tehran:

dpkg-reconfigure tzdata

update server:

apt update -y
apt upgrade -y
apt dist-upgrade -y
apt autoremove -y

install the required tools:

apt install curl rsync screen cron supervisor iputils-ping dnsutils telnet net-tools ufw htop vim nano git tree bash-completion build-essential unzip apg -y

2- Install Node.js and NPM and PM2

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

apt install nodejs

node -v 
npm -v
npm install pm2 -g

3- Install and Configure Nginx

apt install nginx
sudo nano /etc/nginx/sites-available/default
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;
    }
}

3- Add ssh-keys and clone repositories

ssh-keygen -t rsa
cat /root/.ssh/id_rsa.pub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment