Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save okeken/cba0bb8e9159fd8f07eca60ebcfc42bb to your computer and use it in GitHub Desktop.
Save okeken/cba0bb8e9159fd8f07eca60ebcfc42bb to your computer and use it in GitHub Desktop.
Setup NextJS app on Digital Ocean Ubuntu server Full Terminal Commands Step by Step
server {
listen 80;
listen [::]:80;
root /var/www/your_domain/html;
index index.html index.htm index.nginx-debian.html;
server_name your_domain www.your_domain;
location / {
try_files $uri $uri/ =404;
}
}
#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;
}
}
#Setup Your App on Linux Based (Ubuntu/Centos etc) server Terminal Commands
#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
sudo 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 16
# create app folder
mkdir app-name
cd app-name
# install github runner For github actions CI/CD
curl -o actions-runner-linux-x64-2.304.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.304.0/actions-runner-linux-x64-2.304.0.tar.gz
# Verify file
echo "292e8770bdeafca135c2c06cd5426f9dda49a775568f45fcc25cc2b576afc12f actions-runner-linux-x64-2.304.0.tar.gz" | shasum -a 256 -c
# Extract file
tar xzf ./actions-runner-linux-x64-2.304.0.tar.gz
https://github.com/username/repo-name/settings/actions/runners/new
visit for step by step and get runner token
# configure runner
./config.sh --url https://github.com/okeken/safekeepv2 --token your-token
# install runner
sudo ./svc.sh install
# start runner
sudo ./svc.sh start
#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]
#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
#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
#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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment