Universal-cms / Next js new server script

NVM

sudo apt install curl

curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh -o install_nvm.sh

bash install_nvm.sh

# close terminal and open a new one

nvm ls-remote  // list of versions

nvm install 10.15.3   //desired version

nvm use 10.15.3


KEEP CONNECTION ALIVE 

echo 'ClientAliveInterval 60' | sudo tee --append /etc/ssh/sshd_config

sudo service ssh restart


MONGO

sudo apt update

sudo apt install -y mongodb

sudo systemctl status mongodb

mongo --eval 'db.runCommand({ connectionStatus: 1 })'

sudo systemctl status mongodb

sudo ufw status


GIT

sudo apt install git

git config --global credential.helper store  // to save credentials


NGINX WITH SSL

https://www.digitalocean.com/community/tools/nginx#?0.domain=example.com.com&0.path=%2Funiversal%2Fcurrent%2Findex.js&0.document_root=%2Fstatic&0.cert_type=custom&0.php=false&0.proxy&0.root=false

sudo apt-get update

sudo apt-get install nginx

sudo ufw allow 'Nginx HTTP'

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


sudo certbot renew    // renewing licence

https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx    // add both www and non-www


Global redirect map - https://www.tendenci.com/help-files/nginx-redirect-maps/

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

// SSL + redirect http --> https + redirect non-www --> www

#Static domain as CDN cookie-free

server {

    server_name static.example.com;

    location / {

        proxy_pass http://localhost:3300/static/;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection "upgrade";

        proxy_set_header Host $host;

                if ($request_filename ~ "\.(css|js|jpg|jpeg|gif|ico|png|bmp|pict|csv|doc|pdf|pls|ppt|tif|tiff|eps|ejs|swf|midi|mid|ttf|eot|woff|otf|svg|s$                        add_header Pragma "public";

                        add_header Cache-Control "public, must-revalidate, proxy-revalidate";

                        add_header Access-Control-Allow-Origin *;

                        access_log off;

                        expires 1M; break;

                }

        }

  #  listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot

    listen 443 ssl http2; # managed by Certbot

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot

    ssl_certificate_key /etc/letsencrypt/live/example.com.com/privkey.pem; # managed by Certbot

    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server {

    server_name example.com.com www.example.com.com; # managed by Certbot

location / {

        proxy_pass http://localhost:3300/;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection "upgrade";

        proxy_set_header Host $host;

#Expire headers

if ($request_uri ~* ".(ico|css|js|gif|jpe?g|png)$") {

                                        expires 30d;

                                        access_log off;

                                        add_header Pragma public;

                                        add_header Cache-Control "public";

                                        break;

                 }


        }


    listen [::]:443 ssl ipv6only=on http2; # managed by Certbot  //with http2

    listen 443 ssl http2; # managed by Certbot

    ssl_certificate /etc/letsencrypt/live/example.com.com/fullchain.pem; # managed by Certb$    ssl_certificate_key /etc/letsencrypt/live/example.com.com/privkey.pem; # managed by Cer$    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server {

    if ($host = www.example.com.com) {

        return 301 https://example.com.com$request_uri;

    } # managed by Certbot


    if ($host = example.com.com) {

        return 301 https://example.com.com$request_uri;

    } # managed by Certbot


    listen 80 ;

    listen [::]:80 ;

    server_name example.com.com www.example.com.com;

    return 404; # managed by Certbot

}


#Rewriting if needed for specific routes


location / {

        proxy_pass http://localhost:5100/;

...      

rewrite ^/blog/2018/08/15/uterine-septum$ /uterineseptum permanent;

rewrite ^/blog/2018/08/15/uterine-septum-diagnosis-and-treatment$ /uterineseptum permanent;

rewrite ^/TLCintro.mp4 /static/images/tlcfertility/TLCintroheadDesk.webm permanent;

}


Nginx.conf

grep ^processor /proc/cpuinfo | wc -l

For 2 core

# One worker per CPU-core.

  • worker_processes  2;

events {

    worker_connections  4096;

    multi_accept        on;

    use                 epoll;

}

  • worker_rlimit_nofile 40000;

http {

    sendfile           on;

    tcp_nopush         on;

    tcp_nodelay        on;

    keepalive_timeout  25;

    # Your content here ..

}


  • access_log off;


##

# Gzip Settings

##


gzip on;

gzip_vary on;

gzip_min_length 10240;

gzip_proxied expired no-cache no-store private auth;

gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/rss+xml text/javascript image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype;

gzip_disable "MSIE [1-6]\.";

gzip_comp_level 6;


sudo nginx -t

sudo systemctl restart nginx


MIDNIGHT COMMANDER

sudo apt-get install mc


PM2 ECOSYSTEM 

cd ~

sudo nano .bashrc

//comment out following lines

# If not running interactively, don't do anything

case $- in

    *i*) ;;

      *) return;;

esac


npm install pm2 -g  //on local and on remote

pm2 generate // // on remote

pm2 startup //  // on remote add to path

pm2 deploy production setup // local to create current and clone on remote


./deploy.sh

chmod +x deploy.sh

.envExample  -> .env  //in /current on remote set ip SERVER_URL

next.config.js  // set ip/address

pm2 describe appname> /dev/null

RUNNING=$?

if [ "${RUNNING}" -ne 0 ]; then

 NODE_ENV=production PORT=3300 pm2 --name appname--update-env --env production start index.js

else

  pm2 restart appname --update-env

Fi;


package.json

    "deploy": "pm2 deploy ecosystem.config.js production"

ecosystem.config.js

module.exports = {

  apps: [{

    name: 'universal',

    script: './index.js',

  }],

  deploy: {

    production: {

      user: 'ubuntu',

      host: 'server ip',   // server

      key: 'universal.pem',

      ref: 'origin/master',

      repo: 'git hub link',

      path: '/home/ubuntu/universal',

      'post-deploy': 'git checkout . && npm install && ./deploy.sh'

    }

  }

}


DEPLOY PROCESS

npm run build

git add .

git commit -m "commit"

git push

npm run deploy