Wednesday, 8 January 2020

nginx config for node.js app

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;
        server_name _;
    location / {
        root /data/frontend;
        try_files $uri $uri/ /index.html;
    }
        location /api {
        proxy_pass http://localhost:5000;
    }
    location /socket.io {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://localhost:5000;
    }
}

Thursday, 18 April 2019

Linux Hard Disk Format Command







source


Q. I’ve installed a new 250GB SATA hard disk on our office CentOS Linux server. How do I format a hard disk under Linux operating system from a shell prompt?

A.. There are total 4 steps involved for hard disk upgrade and installation procedure:
  😱
 Step #1 : Partition the new disk using fdisk command
Following command will list all detected hard disks:
# fdisk -l | grep '^Disk'
Output:
Disk /dev/sda: 251.0 GB, 251000193024 bytes
Disk /dev/sdb: 251.0 GB, 251000193024 bytes
A device name refers to the entire hard disk. For more information see Linux partition naming convention and IDE drive mappings.
To partition the disk – /dev/sdb, enter:
# fdisk /dev/sdb
The basic fdisk commands you need are:
  • m – print help
  • p – print the partition table
  • n – create a new partition
  • d – delete a partition
  • q – quit without saving changes
  • – write the new partition table and exit
Then press n, choose default primary and  then press  w to write to disk

Step#2 : Format the new disk using mkfs.ext3 command

To format Linux partitions using ext2fs on the new disk:
# mkfs.ext4 /dev/sdb1

Step#3 : Mount the new disk using mount command

First create a mount point /disk1 and use mount command to mount /dev/sdb1, enter:
# mkdir /disk1
# mount /dev/sdb1 /disk1
# df -H

Step#4 : Update /etc/fstab file

Open /etc/fstab file, enter:
# vi /etc/fstab
Append as follows:
/dev/sdb1               /disk1           ext3    defaults        1 2
Save and close the file.

Task: Label the partition

You can label the partition using e2label. For example, if you want to label the new partition /backup, enter
# e2label /dev/sdb1 /backup
You can use label name insted of partition name to mount disk using /etc/fstab:
LABEL=/backup /disk1 ext3 defaults 1 2

Note: for disks > 2TB the above method does not work, so  follow this:


parted with gpt label

Create a partition using GPT format. I've chosen to use just 1 large partition that uses the whole disk.
root@turtle:~# parted /dev/sdb
GNU Parted 2.3
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Error: /dev/sdb: unrecognised disk label
(parted) mklabel gpt
(parted) mkpart
Partition name?  []?
File system type?  [ext2]? ext3
Start? 0%
End? 100%
(parted) print
Model: ATA ST3000DM001-1CH1 (scsi)
Disk /dev/sdb: 3001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  3001GB  3001GB

(parted) quit
Information: You may need to update /etc/fstab.

Saturday, 13 April 2019

How to run gunicorn script using systemd in Debian

Since upstart is deprecated, it is not available in Debian 9, so I had to use systemd which turned out to be quite easy:

nano  /etc/systemd/system/gunicorn.service

Create the file:

[Unit]
Description=Gunicorn Daemon
#After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
User= appuser
Group=www-data
ExecStart=/path/to/project/gunicorn.sh
#Restart=always
#RestartSec=1
#Restart=on-failure
# Configures the time to wait before service is stopped forcefully.
TimeoutStopSec=300
[Install]
WantedBy=multi-user.target
You don't need to restart the server to enable the service:

systemctl enable gunicorn
Start it:
systemctl start gunicorn

Watch it:
systemctl status gunicorn
If you made changes to gunicorn.service you need to run:

systemctl daemon-reload

before restarting the daemon with

systemctl restart gunicorn

Sunday, 6 January 2019

How to compress scanned PDF?

One problem with scanned pdfs is that the size is so bloated. One good solution to compress such files is this one script:

#!/bin/sh

gs  -q -dNOPAUSE -dBATCH -dSAFER \
    -sDEVICE=pdfwrite \
    -dCompatibilityLevel=1.3 \
    -dPDFSETTINGS=/screen \
    -dEmbedAllFonts=true \
    -dSubsetFonts=true \
    -dColorImageDownsampleType=/Bicubic \
    -dColorImageResolution=120 \
    -dGrayImageDownsampleType=/Bicubic \
    -dGrayImageResolution=72 \
    -dMonoImageDownsampleType=/Bicubic \
    -dMonoImageResolution=120 \
    -sOutputFile=out.pdf \
     $1


Here the compression rate can be changed by tweaking resolution values. I found the above gives a good compression without sacrificing the text quality.


Friday, 2 November 2018

How to cut mp4 using ffmpeg

Tested on Ubuntu 16.04

-ss is the starting time
-t is the duration

ffmpeg -i input.mp4 -ss 00:12:20 -t 00:2:00 -async 1 -strict -2  cut.mp4

Friday, 13 July 2018

How to Install Go 1.10 on Ubuntu16.04


Go is an open source programming language developed by a team at Google. It provides easy to build simple, reliable, and efficient software. This language is designed for writing servers, that’s why it is using widely these days. Go has released latest version 1.10. This tutorial will help you to install Go 1.10 on your Ubuntu 18.04 LTS, 16.04 LTS, and 14.04 LTS systems.

Step 1 – Install Go Language

Login to your Ubuntu system using ssh and upgrade to apply latest security updates there.
sudo apt-get update
sudo apt-get -y upgrade
Now download the Go language binary archive file using following link. To find and download latest version available or 32 bit version go to official download page.
wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
Now extract the downloaded archive and install it to the desired location on the system. For this tutorial, I am installing it under /usr/local directory. You can also put this under the home directory (for shared hosting) or other location.
sudo tar -xvf go1.10.3.linux-amd64.tar.gz
sudo mv go /usr/local

Step 2 – Setup Go Environment

Now you need to setup Go language environment variables for your project. Commonly you need to set 3 environment variables as GOROOTGOPATH and PATH.
GOROOT is the location where Go package is installed on your system.
export GOROOT=/usr/local/go
GOPATH is the location of your work directory. For example my project directory is ~/Projects/Proj1 .
export GOPATH=$HOME/Projects/Proj1
Now set the PATH variable to access go binary system wide.
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
All above environment will be set for your current session only. To make it permanent add above commands in ~/.profile file.

Step 3 – Verify Installation

At this step, you have successfully installed and configured go language on your system. First, use the following command to check Go version.
go version

go version go1.10.3 linux/amd64
Now also verify all configured environment variables using following command.
go env

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/Projects/Proj1"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
...
...

Wednesday, 27 June 2018

Deploy celery on Debian 8 using upstart

Make an upstart conf file:

nano /etc/init/celery.conf

Insert in it:

description "Celery application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid john
setgid www-data
chdir /path/to/django/proj
exec bash celery.sh
Put `celery.sh` in /path/to/django/proj


CELERY_BIN="/home/john/.projenv/bin/celery"


# App instance to use
CELERY_APP="proj"
# Where to chdir at start.
CELERYD_CHDIR="/path/to/django/proj"
# Extra command-line arguments to the worker
CELERYD_OPTS="--time-limit=300 --concurrency=1"
# %n will be replaced with the first part of the nodename.
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_PID_FILE="/var/run/celery/%n.pid"
# Workers should run as an unprivileged user.
#   You need to create this user manually (or you can choose
#   a user/group combination that already exists (e.g., nobody).
CELERYD_USER="john"
CELERYD_GROUP="www-data"
# If enabled pid and log directories will be created if missing,
# and owned by the userid/group configured.
CELERY_CREATE_DIRS=1
export SECRET_KEY="somekey"

Start the deamon:

service celery start

That's it!‌

Wednesday, 20 June 2018

How to concatenate mp4 videos using ffmpeg on Ubuntu

for f in $(ls *.mp4); do
    ffmpeg -i $f -c copy -bsf:v h264_mp4toannexb -f mpegts $f.ts
done

CONCAT=$(echo $(ls *.ts) | sed -e "s/ /|/g")

ffmpeg -i "concat:$CONCAT" -c copy -bsf:a aac_adtstoasc output.mp4

rm *.ts


Source

Friday, 29 December 2017

How to install Node.js 8/9 in Ubuntu/Xubuntu


nodejs-logo


  • Simply go to the official website of Node.js or click here to download th latest version of node.js.
  • After you downloaded the package, extract it using the command :
    tar -xf name_of_package
  • Rename the folder just made after extraction of the package to nodejs. Command is this :mv name_of_package nodejs
  • Copy the folder nodejs inside the ‘bin‘ folder in root directory using the command:
    sudo cp -r nodejs /bin
  • At this point, you need to add the nodejs binaries in the PATH variable so that they can be executed from anywhere.
    There are two ways to do this. I am using the less popular way to keep things easy. What you have to do is that copy the files inside nodejs/bin
    Use this command : sudo cp /bin/nodejs/bin/*/usr/local/bin/
  • Okay all done. Check the version of Node.js and NPM using the following commands :
    • node -v
    • npm -v
  • If you face any error while checking the version of npm, one like this “Error: Cannot find module ‘../lib/utils/unsupported.js’ “ do the following :
    • Remember the ‘nodejs’ folder in the ‘Downloads’?
    • Go to Downloads folder, and give the following series of commands :
      • Delete the ‘npm’ in usr/local/bin
        Use this command: sudo rm usr/local/bin/npm
      • cd ~/Downloads [Go to the Downloads Folder]
      • cd nodejs/bin [Go inside the binaries folder of node.js]
      • Execute npm from inside the node binaries file to install latest version of NPM globally use the following command:
        sudo ./npm install npm@latest -g
      • Done !
  • Now finally you can use the latest NPM v5.0.1 and Node.js v8/9
  • Suggestions/corrections are welcome
  • Thanks ! Happy coding

Monday, 11 December 2017

Monero CPU mining on Debian 8 using xmr-stak-cpu

Monero Mining Is For Everyone!

Unlike Etherum, you can mine Monero with about anything, even your old laptop CPU. That won’t yield much profit, but it is a possibility. On top of that, GPU mining does not offer a substantial increase in mining power as in Ethereum. Here, a dedicated GPU can offer only 2 to 3 times greater hashrate than a new CPU, instead of Ethereum’s 10 to 20 times. This means you’re better off setting all your old laptops to mine than purchasing a new GPU, unless of course, you intend to set up a whole farm.
You can also mine with a CPU and a GPU at the same time. Bear in mind that this probably has greater power draw, and you ought to calculate whether what you’re doing is profitable. Let’s see how to mine using just a CPU.

How Intall XMR-STAK-CPU - Monero Mining Tool

I recommend using xmr-stak-cpu, a very fast, cost-effective, open source solution. First off, we will have to install dependencies of the program. We will also have to compile it, so we need software for that, too. Run:
sudo apt install libmicrohttpd-dev libssl-dev cmake build-essential libhwloc-dev
Then, download the miner:
wget https://github.com/fireice-uk/xmr-stak-cpu/archive/master.zip
Unzip it:
unzip master.zip
Enter the new directory:
cd xmr-stak-cpu-master
Run CMake:
cmake .
After you’re done, install the miner:
make install
It should create a „bin” folder. Enter the folder:
cd bin

How to Configure

(notice we are dealing with config text in bin folder, not the one in the root)

And edit the configuration file. Usually, all you need to do is specify your pool address, wallet address and „pool password”, which for the pool we’re using is just a machine name and email. Here is an example.
nano config.txt
"pool_address" : "pool.supportxmr.com:3333",
"wallet_address" : "yourmonerowalletaddresshere",
"pool_password" : "worker:name@email.com",
You can find other pool addresses here under ` Pick a server and port`.

Once you set the configuration file, run
./xmr-stak-cpu
And your machine will start mining with your CPU!

If you want to start the process in the background run this:

nohup ./xmr-stak-cpu &

You might see nasty messages like this:

[2017-07-09 12:04:02] : MEMORY ALLOC FAILED: mmap failed

This means that you can get around a 20% hashrate boost by enabling large pages.

Large pages on Linux

Firstly stop the miner (if it's running), run the following commands to enable large pages and then start the miner as root:
sudo sysctl -w vm.nr_hugepages=128

----------------------------

Disclaimer:

This is a slight modification of a tutorial originally written by Maciej Borkowski for Ubuntu 16.04 but I tested for Debian 8 and it works flawlessly. So thought it worth copying with slight midifications for future reference and maybe help others too, as I had difficulty with similar tutorials for Ubuntu on Debian.