Wednesday, December 17, 2014

Sending emails from Linux with ssmtp

Install SSMTP

apt-get install ssmtp

Configure the file

nano /etc/ssmtp/ssmtp.conf

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=thetnswe@gmail.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
#rewriteDomain=thetnswe@gmail.com

# The full hostname
hostname=thetnswe@gmail.com

UseSTARTTLS=YES
AuthUser=thetnswe@gmail.com
AuthPass=password

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

Add the user to allow to send emails

nano /etc/ssmtp/revaliases

# sSMTP aliases
#
# Format:       local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.

root:thetnswe@gmail.com:smtp.gmail.com:587
localusername:thetnswe@gmail.com:smtp.gmail.com:587

Sending email

sudo ssmtp thetnswe@gmail.com

You will then type your message, hit enter and ‘ctrl+d‘

Now that you have a simple outgoing email server setup, you can do all sorts of neat things:

Creating email Form

nano /emails/email_test1.txt
sudo ssmtp thetnswe@gmail.com < /emails/email_test1.txt

Email send form might looks like this

Subject: Terminal Email Send

Email Content line 1
Email Content line 2



Setting up CRON jobs on shell scripts in Linux (Ubutu)

Open the CRON job editor

crontab -e

After that you can format the job as below

*     *     *     *     *  YOURCOMMAND
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- Day in Week (0 to 7) (Sunday is 0 and 7)
|     |     |     +------- Month (1 to 12)
|     |     +--------- Day in Month (1 to 31)
|     +----------- Hour (0 to 23)
+------------- Minute (0 to 59)
There are some shorts, too (if you don´t want the *):
@reboot --> only once at startup
@daily ---> once a day
@midnight --> once a day at midnight
@hourly --> once a hour
@weekly --> once a week
@monthly --> once a month
@annually --> once a year
@yearly --> once a year
If you want to use the shorts as cron (because they don´t work or so):
@daily --> 0 0 * * *
@midnight --> 0 0 * * *
@hourly --> 0 * * * *
@weekly --> 0 0 * * 0
@monthly --> 0 0 1 * *
@annually --> 0 0 1 1 *
@yearly --> 0 0 1 1 *

# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

//Every 4 hours
This will run the job every 4 hours, on the hours of 00:00, 04:00, 08:00 12:00, 16:00, 20:00.

0 0,4,8,12,16,20 * * * /scripts/test.sh

More info @http://serverfault.com/questions/39004/what-is-the-correct-syntax-to-run-cron-every-4-hours

Start/Stop cron service


sudo service cron restart




Tuesday, December 16, 2014

Some useful linux commands

Give read/write permission to the specific folder

chmod -775 folder_name

//Give read/write permission to all the sub-folders
chmod -775 -R folder_name

//Check rails server
ps aux|grep rails

//Check Ubuntu version
lsb_release -a

Mongodb locations

//Config file
/etc/mongodb.conf

//Actual Storage location file
cd var/lib/mongodb/

/etc/init.d/mongod

Change the owner

sudo chown -R mongodb:mongodb /var/lib/mongodb/

Repair the mongodb is nothing can't be fixed

sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/

Check all ports

 netstat -tulpn

Show all running processes

ps aux | less

Checking free memory

free -m

Stop Bitnami mysql

sudo ./ctlscript.sh stop mysql

Start the default mysql server

/etc/init.d/mysql start
/etc/init.d#

Sometimes if the Bitnami stack is installed and local mysql is running, it can cause the problem

//End the default mysql
cd /etc/init.d
sudo service mysql stop

Check haproxy log

vim /var/log/haproxy.log

Use HAProxy to loadbalance the ruby processes

Installing HAProxy

apt-get install haproxy

We need to enable HAProxy to be started by the init script. 

nano /etc/default/haproxy

Set the ENABLED option to 1 

ENABLED=1

Test whether it's installed and enabled

sudo service haproxy status

Configuring HAProxy

We'll move the default configuration file and create our own one.

mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.backup
Create and edit a new configuration file:

nano /etc/haproxy/haproxy.cfg
Let us begin by adding configuration block by block to this file:

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon

defaults
        option http-server-close # for slowloris like attacks
        mode http
        timeout http-request 5s # for slowloris like attacks, wait only 5sec for                                                                                                                                                              header
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms

        log     global
        mode    http
        option  httplog
        option  dontlognull

frontend WebServer-In
    bind *:801
    default_backend Web-Servers

backend Web-Servers
    balance url_param user_id
    server web1 127.0.0.1:3000 maxconn 12 check
    server web2 127.0.0.1:3001 maxconn 12 check
    server web3 127.0.0.1:3002 maxconn 12 check

frontend API-In
    bind *:802
    default_backend API-Servers

backend API-Servers
    balance url_param user_id
    server api1 127.0.0.1:4567 maxconn 6 check
    server api2 127.0.0.1:4568 maxconn 6 check
    server api3 127.0.0.1:4569 maxconn 6 check

listen Stats ip_address:port
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats uri /
    stats auth username:password

#This include both frontend and backend
listen appname 0.0.0.0:80
    mode http
    stats enable
    stats uri /haproxy?stats
    stats realm Strictly\ Private
    stats auth A_Username:YourPassword
    stats auth Another_User:passwd
    balance roundrobin
    option httpclose
    option forwardfor
    server lamp1 10.0.0.1:80 check
    server lamp2 10.0.0.2:80 check

#Use subdomain and re-route the clusters
frontend http-in
        bind *:80

        # Define hosts
        acl host_bacon hdr(host) -i ilovebacon.com
        acl host_milkshakes hdr(host) -i bobsmilkshakes.com

        ## figure out which one to use
        use_backend bacon_cluster if host_bacon
        use_backend milshake_cluster if host_milkshakes

backend baconcluster
        balance leastconn
        option httpclose
        option forwardfor
        cookie JSESSIONID prefix
        server node1 10.0.0.1:8080 cookie A check
        server node1 10.0.0.2:8080 cookie A check
        server node1 10.0.0.3:8080 cookie A check


backend milshake_cluster
        balance leastconn
        option httpclose
        option forwardfor
        cookie JSESSIONID prefix
        server node1 10.0.0.4:8080 cookie A check
        server node1 10.0.0.5:8080 cookie A check
        server node1 10.0.0.6:8080 cookie A check

Save and close the file
sudo service haproxy start

multiple Subdomain setups

To keep performance at a maximum (avoiding a regex every hit) but still cleaning up the config, I'd use an external file for your ACLs here. For example let's say you had a file called /etc/haproxy/sub1urls, which was exactly this:

apple.gamma.com
banana.gamma.com
cherry.gamma.com
Then in your config the ACL could simply be:

acl is_sub1 hdr(host) -i -f /etc/haproxy/sub1urls
Putting the other hosts in a sub2urls file the same way reduces your config down to:

frontend http-in
    bind *:80

    acl alpha     hdr(host) -i alpha.com
    acl beta      hdr(host) -i beta.com
    acl is_sub1   hdr(host) -i -f /etc/haproxy/sub1urls
    acl is_sub2   hdr(host) -i -f /etc/haproxy/sub2urls
    acl gamma     hdr(host) -i gamma.com

    use_backend a if alpha
    use_backend b if beta
    use_backend sub1 if is_sub1
    use_backend sub2 if is_sub2
    use_backend g if gamma

    default_backend default
This makes it very easy to maintain those other files, since they're just lists of hosts. It opens up the list of who can edit them and exposes less risk as well. For example, we have people editing these ACL lists like this in puppet who don't have to know the HAProxy config syntax at all.