Blog

  • Backup Webite to Amazon S3

    Create file

    mkdir /usr/serverok/
    vi /usr/serverok/backup-daily.sh
    

    Add

    #!/bin/bash
     
    BACKUP_FILE="backup-`date +%Y-%m-%d`.tgz"
    /bin/tar --exclude-tag-under=no-backup -zcvhf /backup/$BACKUP_FILE /var/www/html/
    
    aws s3 cp /backup/$BACKUP_FILE s3://S3_BUCKET_NAME/
    rm -f /backup/$BACKUP_FILE
    

    Make the file executable

    chmod 755 /usr/serverok/backup-daily.sh
    

    Set it run everyday with cronjob

    @daily /usr/serverok/backup-daily.sh  > /var/log/backup-daily.log 2>&1
    

    To backup MySQL database, use

    https://serverok.in/mysqldump-backup

    See Backup

  • ModSecurity Web Application Firewall

    ModSecurity is a Web Application Firewall. Protect sites from SQL injection and Application level hacking.

    To install ModSecurity on Ubuntu/Debian with Apache, run

    apt install libapache2-mod-security2 -y

    verify Apache module is installed with

    apachectl -M | grep security

    Enable config file

    mv /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf

    Update config

    sed -i "s/SecRuleEngine DetectionOnly/SecRuleEngine On/" /etc/modsecurity/modsecurity.conf
    sed -i "s/SecResponseBodyAccess On/SecResponseBodyAccess Off/" /etc/modsecurity/modsecurity.conf

    Restart Apache

    systemctl restart apache2
  • Docker detach container

    Docker container stop once the main program it started with stop running. If you start a docker container with console using -ti option, it stop when you disconnect.

    To avoid process getting stopped, you can detach from the console.

    This is done by pressing keys

    CTRL + P
    

    Followed by

    CTRL + Q
    

    You can just press CTRL key, then press “p”, then “q”.

  • exiqgrep

    exiqgrep allows you to search the exim mail queue.

    To search for a particular recipient, run

    exiqgrep -r EMAIL_ADDR_HERE

    To search for all emails from a particular email account, use

    exiqgrep -f EMAIL_ADDR_HERE

    You can use -i option to list only message id. This is useful when you need to delete mails from a particular account.

    To delete all email sent from a particular email account, run

    exiqgrep -f EMAIL_ADDR_HERE -i | xargs exim -Mrm

    To delete all frozen emails from queue, run

    exiqgrep -iz | xargs exim -Mrm

    If you have too many emails, then use xargs -P option to specify how many mails to delete at a time

    exiqgrep -iz | xargs -P20 exim -Mrm

    This command will delete 20 mails at a time.

    Delete all mails in the queue

    To delete all emails in the queue, run

    exiqgrep -i | xargs -P20 exim -Mrm

    exim

  • FreeBSD ports Sucks

    During 2004, i used to use FreeBSD 4 and 5 servers. During this time Cpanel Control panel supported FreeBSD. After cpanel stopped supporting FreeBSD, all the servers slowly got converted to Linux.

    I was FreeBSD free for several years. Today i decided to try FreeBSD again.

    I tried to install node.js from ports to see how ports work.

    cd /usr/ports/www/node
    make
    

    It started with a dialogue box, where i select some of the options, that made me happy that i have freedom what to install. Then it started downloading software, compiling it. It is good to see where all the software coming.

    After some time, it popup another dialogue box, it wanted me select some options. I done it.

    It repeated 3 or 4 times. I lost my patience and pressed CTRL+C, that stopped the installation.

    It is impossible to watch the screen during such long installs and select options as it pops up. If you are only doing it for one software and only update once in a while, then it is fine, if that is the case, why ports even there. I can’t imaging how many popups i will get if i am installing Apache + PHP and all its module from ports.

    I remember long back i had web sites break after doing ports upgrade, php web sites just show source code, then i have to shutdown apache to prevent disclosure of database credentials stored in PHP files and reinstall PHP from ports to get it working.

    I ended up installing node.js from pkg, that worked perfectly fine.

    pkg install node
    
  • Installing Software with FreeBSD ports

    FreeBSD ports allows installing software from source.

    Update the tree. This will create /usr/ports if missing.

    portsnap fetch update
    portsnap extract
    

    Updating Index

    Before you can use ports, you need to fetch index. To do this, run

    cd /usr/ports
    make fetchindex
    

    Find Software

    To find software, use

    cd /usr/ports
    make search name=php
    

    Install a Software

    To install a software, for example node.js, run

    cd /usr/ports/www/node
    make
    make install
    
  • Installing Software in FreeBSD with pkg

    pkg command is used to install software in FreeBSD.

    To update package repo, run

    pkg update -f
    

    Example

    root@ok-vm:~ # pkg update -f
    Updating FreeBSD repository catalogue...
    pkg: Repository FreeBSD has a wrong packagesite, need to re-create database
    Fetching meta.txz: 100%    940 B   0.9kB/s    00:01    
    Fetching packagesite.txz: 100%    6 MiB 878.0kB/s    00:07    
    Processing entries: 100%
    FreeBSD repository update completed. 28776 packages processed.
    All repositories are up to date.
    root@ok-vm:~ # 
    

    To install postfix mail server, run

    pkg install postfix
    

    To auto start postfix, you need to run

    sysrc postfix_enable="YES"
    

    You can also add postfix_enable=”YES” to /etc/rc.conf manually.

    root@ok-vm:~ # cat /etc/rc.conf
    hostname="ok-vm"
    ifconfig_hn0="DHCP"
    ifconfig_hn0_ipv6="inet6 accept_rtadv"
    sshd_enable="YES"
    # Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
    dumpdev="AUTO"
    waagent_enable="YES"
    root@ok-vm:~ # sysrc postfix_enable="YES"
    postfix_enable:  -> YES
    root@ok-vm:~ # cat /etc/rc.conf
    hostname="ok-vm"
    ifconfig_hn0="DHCP"
    ifconfig_hn0_ipv6="inet6 accept_rtadv"
    sshd_enable="YES"
    # Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
    dumpdev="AUTO"
    waagent_enable="YES"
    postfix_enable="YES"
    root@ok-vm:~ # 
    

    Finding Software

    To find package that provides a software, you can run

    pkg search -o SOFTWARE_NAME
    

    Example

    root@ok-vm:~ # pkg search -o nginx
    www/nginx                      Robust and small WWW server
    www/nginx-devel                Robust and small WWW server
    www/nginx-full                 Robust and small WWW server (full package)
    www/nginx-lite                 Robust and small WWW server (lite package)
    www/nginx-naxsi                Robust and small WWW server (plus NAXSI)
    www/p5-Nginx-ReadBody          Nginx embeded perl module to read and evaluate a request body
    www/p5-Nginx-Simple            Perl 5 module for easy to use interface for Nginx Perl Module
    www/p5-Test-Nginx              Testing modules for Nginx C module development
    root@ok-vm:~ # 
    

    Delete a software

    pkg delete PACKAGE_NAME
    

    Upgrade all installed software

    pkg upgrade
    
  • exiwhat

    exiwhat commands shows what exim mail server is doing.

    [root@server78 ~]# exiwhat
     6415 delivering 1emCxq-0001f7-61: waiting for a remote delivery subprocess to finish
     6428 delivering 1emCxq-0001f7-61 to aspmx3.googlemail.com [209.85.202.26] (billing@hostonnet.com)
     6437 delivering 1emCxq-0001fd-TA: waiting for a remote delivery subprocess to finish
     6452 delivering 1emCxq-0001fd-TA to aspmx3.googlemail.com [209.85.202.26] (billing@hostonnet.com)
     8185 delivering 1emCyw-00021q-MM: waiting for a remote delivery subprocess to finish
     8258 delivering 1emCyw-00021q-MM to alt3.gmail-smtp-in.l.google.com [108.177.15.27] (wllimosine@gmail.com)
     9037 delivering 1emCzn-0002LS-NT: waiting for a remote delivery subprocess to finish
     9041 delivering 1emCzn-0002LS-NT to aspmx5.googlemail.com [173.194.79.26] (billing@hostonnet.com)
    11772 delivering 1emD1k-00033V-JM: waiting for a remote delivery subprocess to finish
    11787 delivering 1emD1k-00033V-JM to alt2.aspmx.l.google.com [209.85.203.26] (billing@hostonnet.com)
    11825 delivering 1emCOM-0004kF-WE: waiting for a remote delivery subprocess to finish
    11829 delivering 1emCOM-0004kF-WE to alt2.gmail-smtp-in.l.google.com [209.85.202.26] (nitsaholidays@gmail.com)
    11967 handling TLS incoming connection from (telkomsa.net) [185.62.188.116]:51246
    12631 handling TLS incoming connection from (telkomsa.net) [185.62.188.116]:51247
    12800 handling TLS incoming connection from (telkomsa.net) [185.62.188.116]:51248
    13564 handling TLS incoming connection from (telkomsa.net) [185.62.188.116]:51249
    13667 handling TLS incoming connection from (telkomsa.net) [185.62.188.116]:51250
    14009 handling TLS incoming connection from (telkomsa.net) [185.62.188.116]:51251
    14326 handling TLS incoming connection from (telkomsa.net) [185.62.188.116]:51252
    14446 handling TLS incoming connection from (telkomsa.net) [185.62.188.116]:51253
    14463 delivering 1emD3X-0003lD-2o: waiting for a remote delivery subprocess to finish
    14469 delivering 1emD3X-0003lD-2o to aspmx.l.google.com [74.125.29.26] (billing@hostonnet.com)
    14672 handling TLS incoming connection from (telkomsa.net) [185.62.188.116]:51254
    16096 handling TLS incoming connection from mail-ua0-f187.google.com [209.85.217.187]:39394
    16175 handling TLS incoming connection from (telkomsa.net) [185.62.188.116]:51255
    16556 tidying up after delivering 1emD4s-000371-T9
    16559 accepting a local non-SMTP message from <>
    16622 tidying up after delivering 1emD4u-0003oe-EZ
    16626 accepting a local non-SMTP message from <>
    16663 tidying up after delivering 1emD4v-0003Hj-Ca
    16666 accepting a local non-SMTP message from <>
    16680 handling incoming connection from [74.208.175.54]:52747
    16704 tidying up after delivering 1emD4x-0003YR-On
    16707 accepting a local non-SMTP message from <>
    16709 tidying up after delivering 1emD4y-0003l0-8X
    16714 accepting a local non-SMTP message from <>
    16744 tidying up after delivering 1emD4z-0003dx-AV
    16749 accepting a local non-SMTP message from <>
    16760 tidying up after delivering 1emD4z-0003KS-Kk
    16764 accepting a local non-SMTP message from <>
    16776 tidying up after delivering 1emD50-000371-Ge
    16782 accepting a local non-SMTP message from <>
    16802 tidying up after delivering 1emD54-0003Wm-Sr
    16805 accepting a local non-SMTP message from <>
    16816 tidying up after delivering 1emD55-0003oe-8h
    16824 accepting a local non-SMTP message from <>
    16830 tidying up after delivering 1emD56-0004Ct-0r
    16833 accepting a local non-SMTP message from <>
    16842 handling incoming connection from [212.237.45.48]:50484
    16845 handling incoming connection from [212.237.45.48]:58446
    16854 delivering 1emD5F-0004Nf-6l: waiting for a remote delivery subprocess to finish
    16855 tidying up after delivering 1emD57-0003Hj-Hq
    16860 accepting a local non-SMTP message from <>
    16864 delivering 1emD5F-0004Nf-6l to aspmx.l.google.com [74.125.192.27] (billing@hostonnet.com)
    16870 tidying up after delivering 1emD4y-0004IX-Sd
    16871 tidying up after delivering 1emD57-0003j4-Sf
    16876 accepting a local non-SMTP message from <>
    16890 handling incoming connection from [185.156.173.137]:58672
    23465 running queue: waiting for 1emCOM-0004kF-WE (11825)
    24554 daemon(4.89_1): -q1h, listening for SMTP on port 25 (IPv6 and IPv4) port 587 (IPv6 and IPv4) and for SMTPS on port 465 (IPv6 and IPv4)
    [root@server78 ~]# 
    

    exim

  • Find all MySQL databases using InnoDB engine

    To find all MySQL database using InnoDB MySQL engine, run

    mysql -N mysql -e "SELECT table_schema FROM INFORMATION_SCHEMA.TABLES WHERE engine = 'innodb';" | cut -d" " -f2 | sort | uniq
    

    MySQL

  • Install Mail-in-a-Box

    Mail-In-A-Box is a complete mail server, that included webmail, smtp, imap, pop3.

    https://mailinabox.email

    Installation

    You need Ubuntu 14.04 to install Mail-In-A-Box.

    Before you start installation, make sure you have server hostname set properly.

    To start installation, run

    curl -s https://mailinabox.email/setup.sh | sudo bash
    

    During installation, it ask you to set password for admin email account – me@your-host-name

    Once installation finished, you will be able to login at

    https://YOUR-SERVER-IP/admin

    mail-in-a-box