From 14dd29920fb6e14a0a7e6b319d45e9a19d7f9704 Mon Sep 17 00:00:00 2001 From: CSnap Date: Sun, 19 Nov 2023 09:01:57 +0000 Subject: [PATCH] From LEMP to FEMP. README updated. --- BASH.md | 442 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ FEMP.md | 276 ++++++++++++++++++++++++++++++++++ README.md | 30 ++-- 3 files changed, 734 insertions(+), 14 deletions(-) create mode 100644 BASH.md create mode 100644 FEMP.md diff --git a/BASH.md b/BASH.md new file mode 100644 index 0000000..662e4a7 --- /dev/null +++ b/BASH.md @@ -0,0 +1,442 @@ +# Comprehensive BASH Cheat Sheet + +## Table of Contents + +1. [Basic Commands](#basic-commands) +2. [Variables](#variables) +3. [Conditionals](#conditionals) +4. [Loops](#loops) +5. [Functions](#functions) +6. [File Operations](#file-operations) +7. [Text Processing](#text-processing) +8. [Pipelines](#pipelines) +9. [Redirection](#redirection) +10. [Processes](#processes) +11. [Environment Variables](#environment-variables) +12. [User Input](#user-input) +13. [Error Handling](#error-handling) +14. [Scripting](#bash-scripting-examples) +15. [Functions with Parameters](#functions-with-parameters) +16. [Arrays](#arrays) +17. [Error Handling and Logging](#error-handling-and-logging) +18. [Reading from Files](#reading-from-files) +19. [Command-Line Arguments](#command-line-arguments) +20. [Regular Expressions](#regular-expressions) +21. [Math Operations](#math-operations) +22. [Advanced I/O Redirection](#advanced-io-redirection) +23. [Advanced Control Structures](#advanced-control-structures) +24. [Job Control](#job-control) +25. [Signals and Traps](#signals-and-traps) +26. [Advanced Process Management](#advanced-process-management) + +## Basic Commands + +### Echo + +```bash +echo "Hello, World!" +``` + +### Print Working Directory + +```bash +pwd +``` + +### List Files + +```bash +ls +``` + +### Change Directory + +```bash +cd /path/to/directory +``` + +## Variables + +### Assigning Variables + +```bash +variable_name="value" +``` + +### Accessing Variables + +```bash +echo $variable_name +``` + +## Conditionals + +### If Statement + +```bash +if [ condition ]; then + # Commands +fi +``` + +### If-Else Statement + +```bash +if [ condition ]; then + # Commands for true condition +else + # Commands for false condition +fi +``` + +### Case Statement + +```bash +case "$variable" in + pattern1) + # Commands + ;; + pattern2) + # Commands + ;; + *) + # Default case + ;; +esac +``` + +## Loops + +### For Loop + +```bash +for item in list; do + # Commands +done +``` + +### While Loop + +```bash +while [ condition ]; do + # Commands +done +``` + +### Until Loop + +```bash +until [ condition ]; do + # Commands +done +``` + +## Functions + +```bash +function_name() { + # Commands +} + +# Call the function +function_name +``` + +## File Operations + +### Create a File + +```bash +touch filename +``` + +### Create a Directory + +```bash +mkdir directory_name +``` + +### Copy File + +```bash +cp source_file destination +``` + +### Move/Rename File + +```bash +mv old_filename new_filename +``` + +### Remove/Delete File + +```bash +rm filename +``` + +## Text Processing + +### Concatenate Files + +```bash +cat file1 file2 +``` + +### Count Lines in a File + +```bash +wc -l filename +``` + +### Grep (Search) + +```bash +grep "pattern" filename +``` + +### Sed (Stream Editor) + +```bash +sed 's/old_pattern/new_pattern/' filename +``` + +## Pipelines + +### Pipe Operator + +```bash +command1 | command2 +``` + +### Command Substitution + +```bash +result=$(command) +``` + +## Redirection + +### Standard Output (stdout) + +```bash +command > output.txt +``` + +### Standard Error (stderr) + +```bash +command 2> error.txt +``` + +### Input Redirection + +```bash +command < input.txt +``` + +## Processes + +### List Running Processes + +```bash +ps +``` + +### Kill Process + +```bash +kill process_id +``` + +## Environment Variables + +### Display All Variables + +```bash +printenv +``` + +### Set Environment Variable + +```bash +export MY_VARIABLE="value" +``` + +## User Input + +### Read User Input + +```bash +read -p "Enter your name: " name +``` + +## Error Handling + +### Exit with Error Code + +```bash +exit 1 +``` + +### Trap Command + +```bash +trap "echo 'Error occurred!'" ERR +``` + +## BASH Scripting Examples + +[Back to Top](#table-of-contents) + +1. [Functions with Parameters](#functions-with-parameters) +2. [Arrays](#arrays) +3. [Error Handling and Logging](#error-handling-and-logging) +4. [Reading from Files](#reading-from-files) +5. [Command-Line Arguments](#command-line-arguments) +6. [Regular Expressions](#regular-expressions) +7. [Math Operations](#math-operations) +8. [Advanced I/O Redirection](#advanced-io-redirection) +9. [Advanced Control Structures](#advanced-control-structures) +10. [Job Control](#job-control) +11. [Signals and Traps](#signals-and-traps) +12. [Advanced Process Management](#advanced-process-management) + +## Functions with Parameters + +```bash +function greet() { + echo "Hello, $1!" +} + +greet "John" +``` + +## Arrays + +```bash +# Declare an array +my_array=("apple" "banana" "cherry") + +# Access an element +echo ${my_array[1]} + +# Iterate over array elements +for item in "${my_array[@]}"; do + echo $item +done +``` + +## Error Handling and Logging + +```bash +# Redirect stdout and stderr to a log file +command > log.txt 2>&1 + +# Check the exit status of a command +if [ $? -eq 0 ]; then + echo "Command succeeded." +else + echo "Command failed." +fi +``` + +## Reading from Files + +```bash +# Read lines from a file +while IFS= read -r line; do + echo "$line" +done < "filename.txt" +``` + +## Command-Line Arguments + +```bash +# Access command-line arguments +echo "Script name: $0" +echo "First argument: $1" +echo "Second argument: $2" +``` + +## Regular Expressions + +```bash +# Check if a string matches a pattern +if [[ "string" =~ ^[0-9]+$ ]]; then + echo "String is a number." +fi +``` + +## Math Operations + +```bash +# Perform arithmetic operations +result=$((5 + 3)) +echo "Result: $result" +``` + +## Advanced I/O Redirection + +```bash +# Redirect stdin from a file +command < input.txt + +# Append to a file +echo "New content" >> output.txt +``` + +## Advanced Control Structures + +```bash +# Select Case statement +fruit="apple" +case $fruit in + "apple") + echo "It's an apple." + ;; + "banana") + echo "It's a banana." + ;; + *) + echo "Unknown fruit." + ;; +esac +``` + +## Job Control + +```bash +# Run a command in the background +command & + +# List background jobs +jobs + +# Bring a background job to the foreground +fg %1 +``` + +## Signals and Traps + +```bash +# Trap a signal +trap 'echo "Signal received"; cleanup_function' INT TERM + +# Send a signal to a process +kill -TERM process_id +``` + +## Advanced Process Management + +```bash +# Get the process ID of a running command +pid=$(pgrep process_name) + +# Check if a process is running +if ps -p $pid > /dev/null; then + echo "Process is running." +else + echo "Process is not running." +fi +``` + diff --git a/FEMP.md b/FEMP.md new file mode 100644 index 0000000..f26e7b5 --- /dev/null +++ b/FEMP.md @@ -0,0 +1,276 @@ +# Fedora based LEMP Setup List + +*Inspired by the version permutation nightmares caused by sites like [this one](https://www.howtoforge.com/how-to-install-nginx-with-php-and-mariadb-lemp-stack-on-fedora-32/*)* + +## Install packages + +### Massive swiss-army knife setup + +``` + +dnf install certbot certbot-nginx cockpit htop iftop iptraf nano openssh-server net-tools nginx* rsync screen vim wget && dnf groupinstall "Development Tools" "Web Server" "Mysql" "php" + +``` + +### Or Less Extra + +`dnf install certbot certbot-nginx nginx ` + +`dnf install vim nano rsync screen vim wget net-tools htop iftop iptraf openssh-server bash-completion` + +`dnf groupinstall "Development Tools" "Web Server" "Mysql" "php"` + + +### More butter Rocky variant + +`dnf install epel-release` + +`dnf install git vim nano rsync screen vim wget net-tools htop iftop iptraf openssh-server bash-completion mariadb mariadb-server certbot python3-certbot-nginx nginx php-fpm` + +`dnf groupinstall "Development Tools"` + +## Add non-root administrator + +`adduser user` + +`usermod -aG wheel user` + +`passwd user` + +`vi /etc/sudoers` + +`sudo -i -u user` + +## Configure SSH + +`ssh-keygen -t rsa -b 4096` + +### Change port and root login settings + +`vi /etc/ssh/sshd_config` + +### Add keys ( also see `ssh-copy-id` ) + +`vi .ssh/authorized_keys` + +## Firewall settings + +``` +systemctl enable firewalld +systemctl start firewalld +systemctl stop firewalld +systemctl restart firewalld +firewall-cmd --state +firewall-cmd --set-default-zone=public +firewall-cmd --zone=public --permanent --list-services +firewall-cmd --zone=public --permanent --add-service=http +firewall-cmd --zone=public --permanent --add-service=https +firewall-cmd --add-port 20022/tcp +firewall-cmd --permanent --add-port 20022/tcp +firewall-cmd --permanent --add-port YOUR_PORT_HERE/tcp +firewall-cmd --remove-service ssh --permanent +firewall-cmd --reload +systemctl reload firewalld + +``` + +## MariaDB +``` +systemctl enable mariadb +systemctl start mariadb +mysql_secure_installation # Y-N-Y-Y-Y-Y +mysql -u root -p +CREATE USER 'user1'@localhost IDENTIFIED BY 'password1'; +CREATE USER 'namenode'@localhost IDENTIFIED BY ':passwd'; +GRANT ALL PRIVILEGES ON *.* TO 'user1'@localhost IDENTIFIED BY 'password1'; +GRANT ALL PRIVILEGES ON *.* TO 'user2'@localhost IDENTIFIED BY 'passwd2'; +FLUSH PRIVILEGES; +SHOW GRANTS FOR 'user1'@localhost; +SHOW GRANTS FOR 'user2'@localhost; +CREATE DATABASE 'yourDB'; +SHOW DATABASES; +DROP USER 'user1'@localhost; # Just for example to show how to delete a user +``` + +## Redis Setup + +`dnf install redis php-redis` + +`sudo systemctl enable --now redis` + +`vi /etc/redis/redis.conf` + +Change bind (0.0.0.0), `requirepass`, `port (2*)`, `maxmemory` (256mb), and `maxmemory-policy allkeys-lru`. + +`systemctl restart redis` + +``` +firewall-cmd --zone=public --permanent --add-port=26379/tcp +firewall-cmd --reload +``` + +## NGINX + +### Important working directories: +``` +/usr/share/nginx/ + +/etc/nginx/ + +``` +### Create user working directory for custom configuration files: +``` +mkdir /etc/nginx/sites-available # Create a directory for nginx.conf files + +mkdir /usr/share/nginx/example.com/html -p # Create new webroot with specified structure +``` + +### Now we can create a new config file to start with: + +`vi /etc/nginx/sites-available/example.com.conf` + + +### Link it to active conf directory + +`ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/conf.d/` + +### If it is required at some point, removing that symlink is as easy as: + +`rm /etc/nginx/conf.d/example.com.conf` + + +### Now we edit the nginx.conf + +`vi /etc/nginx/nginx.conf` + + +### Set the following lines after the line "include /etc/nginx/conf.d/*.conf" (if not already set): +``` +server_names_hash_bucket_size 64; # Should already exist in recent versions + +types_hash_max_size 4096; ## Should already be set + +``` +### Comment out the root location directive (Can uncomment after setup so as not to confuse cache while testing?) + +**To test and reload the configuration:** + +`nginx -t` +`systemctl reload nginx` + +### Simple recap moving forward: +``` +systemctl start nginx +systemctl restart nginx +systemctl enable nginx +systemctl status nginx +systemctl reload nginx +nginx -t +mkdir /etc/nginx/sites-available +mkdir /usr/share/nginx/example.com/html -p +vi /etc/nginx/sites-available/example.com.conf +ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/conf.d/ +vi /etc/nginx/nginx.com # comment out the root in default server block (troubleshooting) +systemctl reload nginx +``` + +## PHP-FPM setup + +### Change user in configuration (nginx): + +`vi /etc/php-fpm.d/www.conf` + +`systemctl enable php-fpm` + +`systemctl restart php-fpm` + +### PHP-OPCache setup + +`vi /etc/php.d/10-opcache.ini` + +``` +opcache.enable_cli=1 +opcache.memory_consumption=128 +opcache.interned_strings_buffer=8 +opcache.max_accelerated_files=4000 +opcache.revalidate_freq=60 +``` + +`systemctl restart php-fpm` + +`systemctl reload nginx` + +### phpMyAdmin setup + +`dnf install phpmyadmin ` + +`ln -s /usr/share/phpMyAdmin/ /usr/share/nginx/hosting.namenode.xyz/dbpma` + +`chown -R nginx:nginx /var/lib/php/session` + +`chown -R nginx:nginx /var/lib/phpMyAdmin` + +`chown -R nginx:nginx /etc/phpMyAdmin` + +`vi /etc/phpMyAdmin/config.inc.php` + +``` +$cfg['Servers'][$i]['AllowNoPassword'] = false; +$cfg['Servers'][$i]['AllowRoot'] = false; + +$cfg['TempDir'] = '/var/lib/phpMyAdmin/temp'; + +``` +`systemctl reload php-fpm` + +`systemctl reload nginx` + + +### Securing phpMyAdmin further +``` +vi pass-infile ## make a password for openssl to encrypt - one line no spaces +``` +``` +openssl passwd -in pass-infile ## Copy the output (your encrypted password) +``` +``` +vi /etc/nginx/pma_pass # Create a user/pass pair for the authentication gateway. +``` +### Format: +``` +user:p@s$w0Rd # one line +``` +### Add the required "dbpma" section + +`vi /etc/nginx/sites-available/example.com.conf` + +`systemctl reload nginx` + +[Install and secure PMA with NGINX Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-an-ubuntu-18-04-server) + + +## Cockpit Setup + +`vi /etc/cockpit/cockpit.conf` + +`vi /etc/nginx/sites-available/example.com.conf` + +[Proxying Cockpit over NGINX](https://github.com/cockpit-project/cockpit/wiki/Proxying-Cockpit-over-nginx) + +[Reverse proxy Cockpit over NGINX](https://www.freesoftwareservers.com/display/FREES/Reverse+Proxy+Cockpit+over+NGinX) + + +## Certbot setup (Examples) +``` +certbot --nginx -d example.com -d www.example.com + +certbot --nginx --agree-tos -d example.com -d www.example.com --email your-email-address + +certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --must-staple -d example.com -d www.example.com --email your-email-address +``` + +`$ EDITOR=vim crontab -e` + +``` +25 2 * * 0 /usr/bin/certbot renew --quiet # Every Sunday 2:25am +``` diff --git a/README.md b/README.md index fd122ac..e0a220f 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,21 @@ -# CheetSheetz +# **CheetSheetz** -## A compendium of various technical "cheat sheets". +#### ***A compendium of various technical information.*** -### Cheat Sheets -- [CSS](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/CSS.md) -- [HTML](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/HTML.md) -- [JavaScript](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/javascript.md) -- [JavaScript Object Notation](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/JSON.md) -- [Markdown](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/markdown.md) -- [Perl](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/perl.md) -- [PHP](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/PHP.md) -- [Python](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/python.md) -- [SQL](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/SQL.md) -### Guides -- [LEMP Guide](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/LEMP.md) +## Cheat Sheets +- [BASH - The Bourne Again Shell](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/BASH.md) +- [CSS - Cascading Style Sheets](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/CSS.md) +- [HTML - The HyperText Markup Language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/HTML.md) +- [JavaScript - But we all really mean ECMAScript](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/javascript.md) +- [JSON - JavaScript Object Notation](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/JSON.md) +- [Markdown - Markup slimmed Down](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/markdown.md) +- [Perl - Practical Extraction and Reporting Language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/perl.md) +- [PHP - Proper Hypertext Pre-processor](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/PHP.md) +- [Python - High-level, general-purpose programming language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/python.md) +- [SQL - Structured Query Language](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/SQL.md) + +## Guides +- [FEMP Guide - Fedora, Nginx, MariaDB and PHP](https://git.namenode.xyz/CSnap/cheetsheetz/src/branch/main/FEMP.md) ![CheetSheetz](https://namenode.xyz/img/badges/cheetsheetzbadge.png) ![Formatted using Markdown](https://namenode.xyz/img/badges/markdownbadge.png)