Apache Tips & Tricks

Virtual Hosts

Full walk-through is available here.  These notes are what my brain needs to trigger off of.

In /etc/apache2/apache2.conf

-IncludeOptional sites-enabled/*.conf
+IncludeOptional sites-available/*.conf

Create Your Virtual Hosts in /etc/apache2/sites-available/

root@wildweb:~# cat /etc/apache2/sites-available/data.pjakey.local.conf 
<VirtualHost *:80>
Alias /old-data "/var/www/html/"

# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName data.pjakey.local
ServerAlias data

ServerAdmin me@pjakey.com
DocumentRoot /data

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf

<Directory "/data">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
</VirtualHost>

Grep Basics – But, Are You Sure You Want to Use Grep?

Keep in mind: many things you want to do in grep you can do in awk.  Or, maybe there is just a better way.  I learned grep early on and have been forcing it to do things it wasn’t really designed to do, so here I will actually be listing solutions using other tools – tools that may be better designed for the task at hand.

Display Line Number of a Match

awk:

awk '/textstring/ {print FNR}' textfile

sed:

sed -n '/pattern/=' filename

Working With Files in Shell

Inserting Text Before a Certain Line

(taken from SO)

$ cat animals
dog
cat
dolphin
cat

$ sed "/cat/ { N; s/cat\n/giraffe\n&/ }" animals
dog
giraffe
cat
dolphin
cat

sed explained:

  1. match a line with /cat/
  2. continue on next line (N)
  3. substitute matched pattern with the insertion and the matched string where & represents the matched string

Ruby

Strings

There is no substring method in Ruby.  We must rely on ranges and expressions.

Example:
# Index 0 = c
# Index 1 = r
# Index 2 = u
# Index 3 = s
# Index 4 = t

value = "crust"

# Get substring at indexes 0 through 3.
sub1 = value[0..3]

# Get substring at indexes 3 through 4.
sub2 = value[2..3]

# Get substring past index three through end of string.
sub3 = value[3..-1]

puts sub1
puts sub2
puts sub3

# Output

crus
us
st

HEREDOC

Write to File

Append
cat << EOF >> /tmp/myfile.txt
Contents that are added to the file at the end
        You can indent files by spacing them appropriately
EOF

 

Noteworthy

  1. Make sure the last EOF (called the LimitString) is actually the very first item on the line. Do not indent, do not do anything other than make EOF begin at position #1.  If you wish to indent your LimitString, you can use <<- to disable leading tabs.
  2. If you do not want to interpret variables in the text, use single quotes around the first LimitString: cat << 'EOF' > /tmp/myfile.txt
  3. To write the heredoc to a file using sudo: cat << 'EOF' | sudo tee /etc/http.d/my_ssl_config

Logical Volume Manager (LVM) on Linux

xfs_repair on LVM

Search for LVM information on all available hard drives and create all the needed entries in /dev so you can actually use them.

vgscan -v --mknodes

All recognizable LVM information were created under /dev/mapper  You’ll probably see something like this:

$ ls -al /dev/mapper
total 0
drwxr-xr-x. 2 root root 100 Apr 13 13:49 .
drwxr-xr-x. 20 root root 3100 Apr 13 13:49 ..
lrwxrwxrwx. 1 root root 7 Apr 13 13:49 centos-root -> ../dm-0
lrwxrwxrwx. 1 root root 7 Apr 13 13:49 centos-swap -> ../dm-1
crw-------. 1 root root 10, 236 Apr 13 13:49 control

The volume named centos-root is the logical volume that is mounted as the root partition.

Activate your volumes:

vgchange -a y

Repair the XFS filesystem:

xfs_repair /dev/mapper/centos-root

You may receive an error stating there is an unwritten (dirty, corrupt) log.  As this note from RedHat states, you may have data loss if you clear the log.

The xfs_repair utility cannot repair an XFS file system with a dirty log. To clear the log, mount and unmount the XFS file system. If the log is corrupt and cannot be replayed, use the -L option (“force log zeroing”) to clear the log, that is, xfs_repair -L /dev/device. Be aware that this may result in further corruption or data loss.

If you can’t mount the volume and clear your log, you will have to use -L and risk data loss.

xfs_repair messageI haven’t experienced any serious data loss after using -L, or if I did, it wasn’t apparent to me.  ** Use at your own risk **

Git Merge With Conflict Resolution

Merging can sometimes be difficult, primarily when you have conflicts!  Yikes!

Prerequisites: Add, commit, and push to your feature branch; then, attempt a merge into master.  You’re left with conflicts.  You are here!

First: From the command line, checkout your feature branch and attempt a merge so you can get a fresh copy of the merge conflicts.

$ git fetch origin
$ git checkout -b property-create origin/property-create
$ git merge master
Auto-merging set_properties.py
CONFLICT (content): Merge conflict in set_properties.py
Auto-merging get_properties.py
CONFLICT (content): Merge conflict in get_properties.py
Auto-merging deploy.py
CONFLICT (content): Merge conflict in deploy.py
Automatic merge failed; fix conflicts and then commit the result.

Next: Open your favorite editor and visit one of those files.

Decide which changes you want to keep, and if need be, keep a little bit of both.  Oftentimes, it’s one or the other…at least in my experience.  When you’re finished with one file, save your changes and move to the next.  Try to keep your changes specific to what is minimally needed to resolve the merge conflicts.

Finally: When you’re ready to try again:

[feature]$ git add deploy.py get_properties.py
[feature]$ git commit -m 'fixed merge conflicts'
[feature]$ git merge master
Already up-to-date.
[feature]$ $ git checkout master
Switched to branch 'master'
[master]$ git merge --no-ff property-create
Merge made by the 'recursive' strategy.
build_promoter.py | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
set_properties.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------
2 files changed, 126 insertions(+), 77 deletions(-)
[master]$ git push origin master
Counting objects: 8, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 1.02 KiB | 0 bytes/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 2 local objects.
To git@github.com:build/pck-build.git
2ebfbe4..9115acf master -> master

 

Debian Networking

Using a Bridge

$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
iface eno1 inet manual

auto vmbr0
iface vmbr0 inet static
  address 172.16.16.111
  netmask 255.255.255.0
  gateway 172.16.16.1
  dns-domain mydomain.local
  dns-nameservers 8.8.4.4 8.8.8.8
  bridge_ports eno1
  bridge_stp off
  bridge_fd 0