apache 2.4 virtual host template

Alias /PMA /var/www/vhosts/PMA
<VirtualHost *:80>
        ServerName stage.sappersuite.com
        Redirect / https://stage.sappersuite.com
        ServerAdmin webmaster@localhost
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>
 ServerName stage.sappersuite.com
 DocumentRoot  /var/www/vhosts/stage.sappersuite.com/html/
 SSLEngine on
 SSLCertificateFile  /etc/ssl/certs/stage.sappersuite.com.crt
 SSLCertificateKeyFile /etc/ssl/private/stage.sappersuite.com.key
ErrorLog ${APACHE_LOG_DIR}/stage.sappersuite.com.error.log
CustomLog ${APACHE_LOG_DIR}/stage.sappersuite.com.access.log combined
<Directory /var/www/vhosts/stage.sappersuite.com/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
</VirtualHost>

phpmyadmin and amazon RDS

on the phpmyadmin folder
vim config.inc.php

#at end of file paste below
$i++;
$cfg['Servers'][$i]['host']          = 'myskdwklji.us-west-2.rds.amazonaws.com';
$cfg['Servers'][$i]['port']          = '3306';
$cfg['Servers'][$i]['socket']        = '';
$cfg['Servers'][$i]['connect_type']  = 'tcp';
$cfg['Servers'][$i]['extension']     = 'mysql';
$cfg['Servers'][$i]['compress']      = TRUE;
$cfg['Servers'][$i]['auth_type']     = 'cookie';
$cfg['Servers'][$i]['user']          = '';
$cfg['Servers'][$i]['password']      = '';

gpg cheat sheet

Filenames are italicized (loosely, some aren't, sorry), so if you see something italicized, think "put my filename there."
I've used User Name as being the name associated with the key. Sorry that isn't very imaginative. I *think* gpg is pretty wide in it's user assignments, ie. the name for my private key is Charles Lockhart, but I can reference that by just putting in Lockhart. That doesn't make any sense, sorry.
to create a key:
gpg --gen-key
generally you can select the defaults.
to export a public key into file public.key:
gpg --export -a "User Name" > public.key

This will create a file called public.key with the ascii representation of the public key for User Name. This is a variation on:
gpg --export
which by itself is basically going to print out a bunch of crap to your screen. I recommend against doing this.
gpg --export -a "User Name"
prints out the public key for User Name to the command line, which is only semi-useful
to export a private key:
gpg --export-secret-key -a "User Name" > private.key

This will create a file called private.key with the ascii representation of the private key for User Name.
It's pretty much like exporting a public key, but you have to override some default protections. There's a note (*) at the bottom explaining why you may want to do this.
to import a public key:
gpg --import public.key

This adds the public key in the file "public.key" to your public key ring.
to import a private key:
NOTE: I've been informed that the manpage indicates that "this is an obsolete option and is not used anywhere." So this may no longer work.
gpg --allow-secret-key-import --import private.key

This adds the private key in the file "private.key" to your private key ring. There's a note (*) at the bottom explaining why you may want to do this.
to delete a public key (from your public key ring):
gpg --delete-key "User Name"
This removes the public key from your public key ring.
NOTE! If there is a private key on your private key ring associated with this public key, you will get an error! You must delete your private key for this key pair from your private key ring first.
to delete an private key (a key on your private key ring):
gpg --delete-secret-key "User Name"
This deletes the secret key from your secret key ring.
To list the keys in your public key ring:
gpg --list-keys

To list the keys in your secret key ring:
gpg --list-secret-keys

To generate a short list of numbers that you can use via an alternative method to verify a public key, use:
gpg --fingerprint > fingerprint
This creates the file fingerprint with your fingerprint info.
To encrypt data, use:
gpg -e -u "Sender User Name" -r "Receiver User Name" somefile

There are some useful options here, such as -u to specify the secret key to be used, and -r to specify the public key of the recipient.
As an example: gpg -e -u "Charles Lockhart" -r "A Friend" mydata.tar
This should create a file called "mydata.tar.gpg" that contains the encrypted data. I think you specify the senders username so that the recipient can verify that the contents are from that person (using the fingerprint?).
NOTE!: mydata.tar is not removed, you end up with two files, so if you want to have only the encrypted file in existance, you probably have to delete mydata.tar yourself.
An interesting side note, I encrypted the preemptive kernel patch, a file of 55,247 bytes, and ended up with an encrypted file of 15,276 bytes.

To decrypt data, use:
gpg -d mydata.tar.gpg
If you have multiple secret keys, it'll choose the correct one, or output an error if the correct one doesn't exist. You'll be prompted to enter your passphrase. Afterwards there will exist the file "mydata.tar", and the encrypted "original," mydata.tar.gpg.
NOTE: when I originally wrote this cheat sheet, that's how it worked on my system, however it looks now like "gpg -d mydata.tar.gpg" dumps the file contents to standard output. The working alternative (worked on my system, anyway) would be to use "gpg -o outputfile -d encryptedfile.gpg", or using mydata.tar.gpg as an example, I'd run "gpg -o mydata.tar -d mydata.tar.gpg". Alternatively you could run something like "gpg -d mydata.tar.gpg > mydata.tar" and just push the output into a file. Seemed to work either way.
Ok, so what if you're a paranoid bastard and want to encrypt some of your own files, so nobody can break into your computer and get them? Simply encrypt them using yourself as the recipient.
I haven't used the commands:
gpg --edit-key
gpg --gen-revoke

  • --gen-revoke creates a revocation certificate, which when distributed to people and keyservers tells them that your key is no longer valid, see http://www.gnupg.org/gph/en/manual/r721.html
  • --edit-key allows you do do an assortment of key tasks, see http://www.gnupg.org/gph/en/manual/r899.html


Sharing Secret Keys

NOTE!: the following use cases indicate why the secret-key import/export commands exist, or at least a couple ideas of what you could do with them. HOWEVER, there's some logistics required for sharing that secret-key. How do you get it from one computer to another? I guess encrypting it and sending it by email would probably be ok, but I wouldn't send it unencrypted with email, that'd be DANGEROUS.
Use Case *.1 : Mentioned above were the commands for exporting and importing secret keys, and I want to explain one reason of why maybe you'd want to do this. Basically if you want one key-pair for all of your computers (assuming you have multiple computers), then this allows you export that key-pair from the original computer and import it to your other computers.
Use Case *.2 : Mentioned above were the commands for exporting and importing secret keys, and I want to explain one reason of why maybe you'd want to do this. Basically, if you belonged to a group, and wanted to create a single key-pair for that group, one person would create the key-pair, then export the public and private keys, give them to the other members of the group, and they would all import that key-pair. Then a member of the group or someone outside could use the group public key, encrypt the message and/or data, and send it to members of the group, and all of them would be able to access the message and/or data. Basically you could create a simplified system where only one public key was needed to send encrypted stuffs to muliple recipients.

Install docker on ubuntu 14/04

# sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

# apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual

# add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"

# apt-get install docker-ce -y

mysql repo for linux

Mysql repository:-

https://dev.mysql.com/downloads/repo/

https://dev.mysql.com/downloads/repo/yum/     for centos

https://dev.mysql.com/downloads/repo/apt/       for ubuntu

Attach digital ocean block storage volume to containers

Configure your volume
To configure your volume, run the following commands on your Droplet.

# SSH into your droplet
$ ssh root@192.168.1.12 
To format the volume, copy and paste the text below:
# Format the volume with ext4 Warning: This will erase all data on the volume. Only run this command on a volume with no existing data.
$ sudo mkfs.ext4 -F /dev/disk/by-id/scsi-0DO_Volume_db2vol-nyc1Copy 
To mount the volume, copy and paste the text below:
# Create a mount point under /mnt
$ sudo mkdir -p /mnt/db2vol-nyc1

# Mount the volume
$ sudo mount -o discard,defaults /dev/disk/by-id/scsi-0DO_Volume_db2vol-nyc1 /mnt/db2vol-nyc1

# Change fstab so the volume will be mounted after a reboot
$ echo '/dev/disk/by-id/scsi-0DO_Volume_db2vol-nyc1 /mnt/db2vol-nyc1 ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstabCopy 
The volume is now accessible at /mnt/db2vol-nyc1, and you are able to write files and store other data. This data will persist if you detach the volume, and will continue to be available when the volume is reattached to another Droplet. If you want you can customize the commands above, please refer to the following articles:

How To Partition and Format DigitalOcean Block Storage Volumes in Linux
How To Partition and Format Storage Devices in Linux

mysql enabling the query cache performance impact

MySQL query cache has its own downsides too. Well, let’s think about this: If you are frequently updating the table, you are then invalidating the query cache for ALL queries cached for that table. So really, anytime you have a “frequently updated table” means you’re probably not going to get any sort of good usage from the MySQL query cache. See the below example.

query cache status

SHOW STATUS LIKE "qcache%";
Qcache_lowmem_prunes is the status variable which indicates how many times MySQL had to clear/prune some data from the Qcache to make space for the outputs of other queries. We need to observe the Qcache_lowmem_prunes  status variable and try to increase/adjust the size of the cache till we get a very low value ratio for the variable.
It is also undesirable to keep the query cache relatively high value at 256 MB as the Qcache invalidation becomes costly. 

TMP_TABLE_SIZE AND MAX_HEAP_TABLE_SIZE

https://www.saotn.org/mysql-innodb-performance-improvement/

The actual limit is determined from whichever of the values of tmp_table_size and max_heap_table_size is smaller. If an in-memory temporary table exceeds the limit, MySQL automatically converts it to an on-disk MyISAM table. Increase the value of tmp_table_size (and max_heap_table_size if necessary) if you do many advanced GROUP BY queries and you have lots of memory.

You can compare the number of internal on-disk temporary tables created to the total number of internal temporary tables created by comparing the values of the Created_tmp_disk_tables and Created_tmp_tables variables.


circumstances which could create temporary tables using MEMORY but that can go to MyISAM (so disk) if too large:
  • If there is an ORDER BY clause and a different GROUP BY clause, or if the ORDER BY or GROUP BY contains columns from tables other than the first table in the join queue;
  • DISTINCT combined with ORDER BY may require a temporary table;
  • In the case of the SQL_SMALL_RESULT option, MySQL uses an in-memory temporary table, unless the query also contains elements (described later) that require on-disk storage.
There are some conditions which will force the temporary table to use MyISAM :
  • Presence of a BLOB or TEXT column in the table;
  • Presence of any column in a GROUP BY or DISTINCT clause larger than 512 bytes;
  • Presence of any column larger than 512 bytes in the SELECT list, if UNION or UNION ALL;
tmp_table_size      = 16M
max_heap_table_size = 32M
max_connections = 1000

Value of tmp_table_size OR max_heap_table_size which ever is lower is taken
memory used if all connections are made 1000x16M=1600M  

innodb_flush_log_at_trx_commit log buffer

innodb_flush_log_at_trx_commit=1
The default value of 1 is required for full ACID compliance. The contents of the InnoDB 
log buffer are written out to the log file at each transaction commit and the log file is flushed to disk.

0 = log buffer are written to the log file approximately once per second and the log file is flushed to disk
2 = log buffer are written to the log file after transaction commit and the log file is flushed to disk approximately once per second


logbuffer-->logfiles[ib_logfileN] --> redolog [ib_logfile0 , ib_logfile1] size limit is 512G (default-48M)

log buffer
The memory area that holds data to be written to the log files that make up the redo log. It is controlled by the innodb_log_buffer_size configuration option.

redo log

A disk-based data structure used during crash recovery, to correct data written by incomplete transactions. During normal operation, it encodes requests to change InnoDB table data, which result from SQL statements or low-level API calls through NoSQL interfaces. Modifications that did not finish updating the data files before an unexpected shutdown are replayed automatically.

The redo log is physically represented as a set of files, typically named ib_logfile0 and ib_logfile1. The data in the redo log is encoded in terms of records affected; this data is collectively referred to as redo. The passage of data through the redo logs is represented by the ever-increasing LSN value. The original 4GB limit on maximum size for the redo log is raised to 512GB in MySQL 5.6.3.

The disk layout of the redo log is influenced by the configuration options innodb_log_file_size, innodb_log_group_home_dir, and (rarely)innodb_log_files_in_group. The performance of redo log operations is also affected by the log buffer, which is controlled by the innodb_log_buffer_size configuration option.


ansible cheat sheet


Friday, August 12, 2016

How To - Ansible adhoc commands (One Liners)

Make a note of below assumptions for one liners explained on this page are as below,

User root - the superhero, superuser
User normaluser - user that has root sudo privilege and all ansible required connectivity in place User user1 - user that has root sudo privilege

Understanding basic differences in commands using various users with various rights

To run command on all servers to check reach ability from ansible point of view with 25 parallel forks with current user (i.e. root or normaluser)
# ansible all -m ping -f 25
To run command on all servers to check reach ability from ansible point of view with 25 parallel forks when you logged in as normal user (user1) and using ansible privilege of another normal user (normaluser)
# sudo -u normaluser ansible all -m ping -f 25
To run a command on list of servers (IP addresses) specified in a text file with 25 parallel forks with current user (i.e. root or normaluser)
# ansible all -i /tmp/my-inventory -m ping -f 25
To run a command on list of servers (IP addresses) specified in a text file with 25 parallel forks when you logged in as normal user (user1) and using ansible privilege of another normal user (normaluser)
# sudo -u normaluser ansible all -i /tmp/my-inventory -m ping -f 25
To run command on all servers in a group with 25 parallel forks with current user (i.e. root or normaluser)
# ansible all -a "df -h" -f 25
To run command on all servers in a group with 25 parallel forks when you logged in as normal user (user1) and using ansible privilege of another normal user (normaluser)
# sudo -u normaluser ansible all -a "df -h" -f 25
To run command which requires super user rights on all servers in a group with 25 parallel forks with current user (i.e. root)
# ansible all -a "/etc/init.d/snmpd restart" -f 25
To run command which requires super user rights on all servers in a group with 25 parallel forks with current user (i.e. normaluser)
# ansible all -a "/etc/init.d/snmpd restart" -f 25 --sudo 
To run command which requires super user rights on all servers in a group with 25 parallel forks when you logged in as normal user (user1) and using ansible privilege of another normal user (normaluser)
# sudo -u normaluser ansible all -a "/etc/init.d/snmpd restart " -f 25 --sudo 

Ping Module

To limit ping for random first 10 servers from a group of hosts defined in /etc/ansible/hosts file
# sudo -u normaluser all dev -m ping --limit allvmhosts[0-10]
To limit Ping some servers from "api" group of hosts defined in /etc/ansible/hosts file i.e. [11 to 20]
# sudo -u normaluser ansible all -m ping --limit allvmhosts[11-20]
In case of JSON Error "Error: ansible requires a json module, none found!" Try your command in raw mode as below
# sudo -u normaluser ansible all -m ping  -m raw

Service Module

To install a service on all servers in a group using service module
# ansible all -m service -a "name=snmpd state=installed"
To start a service on all servers in a group using service module
# ansible all -m service -a "name=snmpd state=started"
To stop a service on all servers in a group using service module
# ansible all -m service -a "name=snmpd state=stopped"
To restart a service on all servers in a group using service module
# ansible all -m service -a "name=snmpd state=restarted"
To reload a service on all servers in a group using service module
# ansible all -m service -a "name=snmpd state=reloaded"
To enable a service on all servers in a group using service module to get started in OS startup/bootup
# ansible all -m service -a "name=snmpd enabled=yes"
To disable a service on all servers in a group using service module to get started in OS startup/bootup
# ansible all -m service -a "name=snmpd enabled=no"

User Module

To add a user 'sam' and assign shell as /bin/bash, secondary group as wheel to it on a group on all servers in a group
# sudo -u normaluser ansible all -m user -a "name=sam shell=/bin/bash groups=wheel"
To add the user 'sam' with a specific uid and a primary group of 'admin' on all servers in a group
# sudo -u normaluser ansible all -m user -a "name=sam comment="Samual Pol" uid=1040 group=admin"
To add the user 'sam' with a bash shell, appending the group 'admins' and 'developers' to the user's groups on all servers in a group
# sudo -u normaluser ansible all -m user -a "name=sam shell=/bin/bash groups=admins,developers append=yes"
To Remove the user 'sam' from all servers in a group
# sudo -u normaluser ansible all -m user -a "name=sam state=absent remove=yes"

Shell Module

To create a backup file from command line with sudo on a group
# sudo -u normaluser ansible all -m shell -a "cp /etc/sudoers /etc/sudoer_DDMMYYYY" --sudo
To replace content of a file with special characters with sudo on a group
# sudo -u normaluser ansible all -m shell -a "sed -i 's|\# %wheel\tALL=(ALL)\tALL|%wheel\tALL=(ALL)\tALL|g' /etc/sudoers" --sudo
To change primary group of a user on a group
# sudo -u normaluser ansible all -m shell -a "usermod -aG wheel idcadm"
To execute the command in remote shell by specifying stdout going to the specified file on all remote servers in a group
# sudo -u normaluser ansible all -m shell -a "somescript.sh >> somelog.txt"
To change the working directory to somedir/ before executing the command
# sudo -u normaluser ansible all -m shell -a "somescript.sh >> somelog.txt chdir=somedir/"

File Module =

To change permissions of a file on all servers in a group
# sudo -u normaluser ansible all -m file  -a "path=/usr/bin/sudo  state=touch mode='4755' " --sudo 
To change file ownership, group and mode using octal numbers (Note: first digit should always be 0 whenever octal used)
# sudo -u normaluser ansible all -m file  -a "path=/etc/ntp.conf owner=ntp group=ntp mode=0644"
To create symbolic link
# sudo -u normaluser ansible all -m file  -a "src=/file/to/link/to dest=/path/to/symlink owner=sam group=sam state=link"
To touch a file, using symbolic modes to set the permissions (equivalent to 0644)
# sudo -u normaluser ansible all -m file  -a "path=/etc/test.conf state=touch mode='u=rw,g=r,o=r'"


Replace Module

To replace a particular line in a file on a group
# sudo -u normaluser ansible all -m replace -a "dest=/root/test.sh regexp='^(\sread -p \"gateway recover after nodes \" gateway_recover_after_nodes)$' replace='XXXXXXXXX'"

lineinfile Module

To append a line in file use lineinfilemodule on a group
# sudo -u normaluser ansible all -m lineinfile  -a "dest=/etc/sudoers_07May2015 line='# Sudo access for running passwd command'"