Skip to content

Instantly share code, notes, and snippets.

View mttjohnson's full-sized avatar

Matt Johnson mttjohnson

View GitHub Profile
@mttjohnson
mttjohnson / read_config_xml.sh
Created January 6, 2018 01:15
Bash Structured Data Parameters as XML
#!/usr/bin/env bash
set -eu
# This script is an example of utilizing an XML file or string to pass into a bash
# shell script parameters for the script. By using a structured data set this allows
# for the configuration file to have optional values and nested structures like lists
# of options all in a single place. Separating the variables from the script provides
# a separation of the values that change from the functionality of a script so that it
# can be executed on multiple different saved sets of data.
@mttjohnson
mttjohnson / phpfpm_reload_and_watch.md
Created March 30, 2023 20:19
Reloading PHP-FPM and Watching Processes

Monitor php-fpm processes

This shows the master processes and 1 child from each pool and refreshes every 5 seconds

WATCH_COMMAND=$(cat <<'COMMAND_CONTENT'
sudo ps afxo pid,user,command --sort command | perl -ne 'print if /^\s*$/ || /^\s*\d+(.+)$/ && ! $SEEN{$1}++' | grep -i '[p]hp-fpm'
COMMAND_CONTENT
)
watch -n 5 "${WATCH_COMMAND}"
@mttjohnson
mttjohnson / redis_admin.sh
Last active March 30, 2023 06:41
Redis Administration
# Get redis info
redis-cli -p 6379 info
# watch changes in redis info every 5 seconds
watch -d -n 5 redis-cli -p 6381 info memory
# get the maxmemory value currently set on the instance
redis-cli -p 6379 config get maxmemory
# set the maxmemory value on the instance without restarting
@mttjohnson
mttjohnson / mysql-performance-tuning.sql
Last active March 6, 2023 09:17
MySQL Performance Tuning
-- Query the database to calculate a recommended innodb_buffer_pool_size
-- and get the currently configured value
-- The rollup as the bottom row gives the total for all DBs on the server, where each other row is recommendations per DB.
SELECT
TABLE_SCHEMA,
CONCAT(CEILING(RIBPS/POWER(1024,pw)),SUBSTR(' KMGT',pw+1,1))
Recommended_InnoDB_Buffer_Pool_Size,
(
SELECT CONCAT(CEILING(variable_value/POWER(1024,FLOOR(LOG(variable_value)/LOG(1024)))),SUBSTR(' KMGT',FLOOR(LOG(variable_value)/LOG(1024))+1,1))
@mttjohnson
mttjohnson / .env-1password
Last active March 5, 2023 07:02
Load Secret Environment Variables from 1Password
# Load environment secrets with with 1Password CLI Injection
#
# Used with load_secrets.sh
# source load_secrets.sh
# AWS Service Account Credentials
AWS_ACCESS_KEY_ID={{ op://$OP_VAULT/$INF_PROJECT.aws_access_key/username }}
AWS_SECRET_ACCESS_KEY={{ op://$OP_VAULT/$INF_PROJECT.aws_access_key/password }}
# Project Secrets
@mttjohnson
mttjohnson / validate_ssh_key.sh
Last active February 1, 2023 05:49
Validating ssh keys
# When you SSH into a server for the first time it prompts you if you trust the remote server's host key
# To validate that they key you received is the same as the server you just logged into you can check
# the fingerprint of the host key on the remote server itself.
# Output fingerprint of system's host key
ssh-keygen -l -f /etc/ssh/ssh_host_ecdsa_key.pub
# You can fingerprint both the private and public key from a file
# They should both produce the same fingerprint value.
ssh-keygen -l -f ~/.ssh/id_ed25519.pub
@mttjohnson
mttjohnson / color_code_files.sh
Last active January 28, 2023 22:40
Colorize code files on shell
# Install Pygments (https://pygments.org/)
pip install Pygments
brew install pygments
# XML
cat myfile.xml | xmllint --format - | pygmentize -l xml
# JSON
cat myfile.json | jq . | pygmentize -l json
@mttjohnson
mttjohnson / nginx_phpfpm_status_override_with_location.php
Created August 2, 2018 21:12
Overriding HTTP Status Code when Location Header Exists with Nginx and PHP-FPM
<?php
$status_code = "200";
$reason_phrase = "OK";
# Status of responses including a Location header: 201, 301, 302, 303, 307, 308.
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Location
# https://tools.ietf.org/html/rfc2616#section-14.30
# Nginx interprets CGI response headers differently than Apache and if a location
# header is found in a response not containing a standard status code it will override
@mttjohnson
mttjohnson / shell_prompt_config.sh
Last active October 27, 2022 21:27
shell prompt config (zsh, bash)
# ZSH - ~/.zshrc
# Luke's config for the Zoomer Shell (https://gist.github.com/LukeSmithxyz/e62f26e55ea8b0ed41a65912fbebbe52)
autoload -U colors && colors
PS1="%B%{$fg[red]%}[%{$fg[yellow]%}%n%{$fg[green]%}@%{$fg[blue]%}%M %{$fg[magenta]%}%~%{$fg[red]%}]%{$reset_color%}$%b "
PS1='$("$POWERLINE_COMMAND" $=POWERLINE_COMMAND_ARGS shell aboveleft -r .zsh --last-exit-code=$? --last-pipe-status="$pipestatus" --renderer-arg="client_id=$$" --renderer-arg="shortened_path=${(%):-%~}" --jobnum=$_POWERLINE_JOBNUM --renderer-arg="mode=$_POWERLINE_MODE" --renderer-arg="default_mode=$_POWERLINE_DEFAULT_MODE" --width=$(( ${COLUMNS:-$(_powerline_columns_fallback)} - ${ZLE_RPROMPT_INDENT:-1} )))'
# BASH - ~/.bash_profile
PS1="\[\033[0;5m\]\u@\h\[\033[0m\]:\@:\[\033[0;31m\]\w\[\033[0m\]#"
PS1="\[\033[0;36m\]\u@\h\[\033[0m\]:\@:\[\033[0;37m\]\w\[\033[0m\]$"
@mttjohnson
mttjohnson / nginx_multi-conditional.conf
Created August 20, 2019 16:14
Nginx multi-conditional for whitelist of IP or user agent at a specific path
# Set the $unauthorized_admin_ip flag if remote_addr is not on list of authorized IPs
map $remote_addr $unauthorized_admin_ip {
# All IPs are considered unauthorized by default
default 1;
"127.0.0.1" 0;
}
map $request_uri $unauthorized_admin_path {
# All paths are considered unauthorized by default and require some other
# means of authorization in order for the request to be accepted.