Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ArafatOSDev/6aecb912f463b347154393bbd8c58a6e to your computer and use it in GitHub Desktop.
Save ArafatOSDev/6aecb912f463b347154393bbd8c58a6e to your computer and use it in GitHub Desktop.

Revisions

  1. @oelbaga oelbaga revised this gist Jul 3, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ubuntu-nextjs-terminal-commands
    Original file line number Diff line number Diff line change
    @@ -47,7 +47,7 @@ touch name_of_app
    nano name_of_app

    [SEE OTHER GIST FOR CONFIG FILE CONTENTS]
    #https://gist.github.com/oelbaga/5019647715e68815c602ff05cff2416e#file-nginx-config-file
    #https://gist.github.com/oelbaga/5019647715e68815c602ff05cff2416e#file-ubuntu-nextjs-nginx-config-file

    #Option1 Syslink the file in sites-enabled
    sudo ln -s /etc/nginx/sites-available/name_of_app /etc/nginx/sites-enabled/name_of_app
  2. @oelbaga oelbaga revised this gist Jul 3, 2022. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  3. @oelbaga oelbaga revised this gist Jul 3, 2022. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion terminal-commands
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,8 @@ cd /etc/nginx/sites-available
    touch name_of_app
    nano name_of_app

    [SEE OTHER GIST FOR CONFIG FILE CONTENTS: https://gist.github.com/oelbaga/5019647715e68815c602ff05cff2416e#file-nginx-config-file]
    [SEE OTHER GIST FOR CONFIG FILE CONTENTS]
    #https://gist.github.com/oelbaga/5019647715e68815c602ff05cff2416e#file-nginx-config-file

    #Option1 Syslink the file in sites-enabled
    sudo ln -s /etc/nginx/sites-available/name_of_app /etc/nginx/sites-enabled/name_of_app
  4. @oelbaga oelbaga revised this gist Jul 3, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion terminal-commands
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,7 @@ cd /etc/nginx/sites-available
    touch name_of_app
    nano name_of_app

    [SEE OTHER GIST FOR CONFIG FILE CONTENTS]
    [SEE OTHER GIST FOR CONFIG FILE CONTENTS: https://gist.github.com/oelbaga/5019647715e68815c602ff05cff2416e#file-nginx-config-file]

    #Option1 Syslink the file in sites-enabled
    sudo ln -s /etc/nginx/sites-available/name_of_app /etc/nginx/sites-enabled/name_of_app
  5. @oelbaga oelbaga created this gist Jul 3, 2022.
    28 changes: 28 additions & 0 deletions nginx-config-file
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #nginx config file for Nextjs App
    #place in /etc/nginx/sites-available/name_of_config_file
    server {
    listen 80;
    server_name domainname.com;

    gzip on;
    gzip_proxied any;
    gzip_types application/javascript application/x-javascript text/css text/javascript;
    gzip_comp_level 5;
    gzip_buffers 16 8k;
    gzip_min_length 256;

    location /_next/static/ {
    alias /var/www/name_of_app/.next/static/;
    expires 365d;
    access_log off;
    }

    location / {
    proxy_pass http://127.0.0.1:3000; #change to 3001 for second app, but make sure second nextjs app starts on new port in packages.json "start": "next start -p 3001",
    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;
    }
    }
    84 changes: 84 additions & 0 deletions terminal-commands
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    #Setup NextJS on Digital Ocean Ubuntu server Terminal Commands
    #based on my YouTube video

    #login to server
    ssh root@ip_address

    #Upgrade Server
    sudo apt update && sudo apt upgrade

    #Install NGINX and Certbot
    sudo apt install nginx certbot python3-certbot-nginx

    #Allow Firewall Access
    sudo ufw allow "Nginx Full"
    ufw allow OpenSSH
    ufw enable

    #Install NPM
    apt install npm

    #Install pm2
    npm install -g pm2

    #Check pm2 is working
    pm2 status

    #go to www root
    cd /var/www

    #install nvm and nodejs
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    exec $SHELL
    nvm install --lts

    #Create NextJS App or clone here
    npx create-next-app@latest name_of_app

    #Go inside new app directory
    cd name_of_app

    #Build it
    npm run build

    #Create NGINX config file and edit it
    cd /etc/nginx/sites-available
    touch name_of_app
    nano name_of_app

    [SEE OTHER GIST FOR CONFIG FILE CONTENTS]

    #Option1 Syslink the file in sites-enabled
    sudo ln -s /etc/nginx/sites-available/name_of_app /etc/nginx/sites-enabled/name_of_app

    #Option 2 No need to use sites-enabled
    nano /etc/nginx/nginx.conf
    change include /etc/nginx/sites-enabled/*; to include /etc/nginx/sites-available/*;

    #make Sure NGINX file is good
    nginx -t

    #remove the default config files
    cd /etc/nginx/sites-available
    rm default
    cd /etc/nginx/sites-enabled
    rm default

    #restart NGINX to reload config files
    systemctl restart nginx

    #Go to site directory and launch it with pm2
    cd /var/www/name_of_app

    #launch app with pm2
    pm2 start npm --name name_of_app -- start

    #Create SSL with letsencryot
    sudo certbot --nginx -d domainname.com



    ————— helpful commands ————
    pm2 start npm --name name_of_app -- start (make sure you're inside the site's directory first)
    systemctl restart nginx (restart NGINX)
    sudo certbot --nginx -d domainname.com (Add SSL)