Apache Cheat Sheet

The Apache HTTP Server is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0.

Apache General

Database normalization

FIRST NORMAL FORM
NO repeating columns containing same type of data
ALL columns contain a single value
Unique PRIMARY KEY for each row

SECOND NORMAL FORM
Table must be in First Normal Form
Create new tables for any data that is repeated in columns
Each new table needs a unique PRIMARY KEY for each row.
AUTO_INCREMENT
can do this.

THIRD NORMAL FORM
Table must be in First and Second Normal Form. This is usually all that is necessary.
Any data that is not dependent on a primary key, but is dependent on another value, should be removed to another table. This is a very strict use of normalization.

Database Access Information

Directory for include files
[docroot]/inc
MySQL Login file
db_conn.php
PHP include line
require_once 'inc/db_conn.php';
[docroot]/inc/db_conn.php
<?php

// credentials for connecting to database

$host = 'hostname';  //database hostname

$user = 'db_user';  //database username

$pw = 'db_user_pw';  //database user password

$db = 'database';  //mysql database name

?>

If you lock yourself out of phpmyadmin:

...by turning on no password IN phpmyadmin:
At the command line:
$ sudo mysql

mysql> SET PASSWORD FOR root@localhost=PASSWORD('letmein');

mysql> quit;

Now use username root and password letmein

Install phpmyadmin

- Download phpmyadmin
- Make new directory in the document root:
sudo mkdir phpmyadmin

- Make it writeable by owner (which could be root):
sudo chmod 755 phpmyadmin

- Unzip or unpack the zip file and copy to new directory
Also see: phpmyadmin setup doc
- From
localhost/phpmyadmin/setup
, create a new server connection by clicking New server
- Under Authentication tab, enter
root
as user and new password for root
Then click Save.
- Now navigate to
localhost/phpmyadmin

Install MySQL

- Download from https://dev.mysql.com/downloads/mysql
- Choose the version for your OS
- Click “No thanks, just start my download”
- Go through the download process
- Make sure you copy the password for root@localhost
- Start MySQL Server
- Add
/usr/local/mysql/bin
to path in
.bash_profile


To update password, see "If you lock yourself out of phpmyadmin"

Turn on PHP

At the terminal, type: $>
sudo vi /etc/apache2/httpd.conf

Uncomment:
#LoadModule php7_module libexec/apache2/libphp7.so

Save file then restart apache: $>
sudo apachectl restart


In the document root: $>
sudo vi index.php

Enter:
<?php

phpinfo();

?>

Save file then restart apache: $>
sudo apachectl restart

In a browser, type:
localhost/index.php

Apache General Information

Apache Doc Root Directory
/Library/WebServer/Documents
httpd.conf lives in:
/etc/apache2/httpd.conf
localhost web access
http://localhost/index.php
Other docs and dirs in Document Root
phpinfo.php, phpmyadmin, php.ini, inc/
bash alias
docroot='cd /Library/WebServer/Documents\'

Turn on apache

- At the terminal, type: >
sudo apachectl start

- In a browser type:
localhost
or
localhost/index.html

- If you see: It Works!, it works.

Directives

RewriteBase
specifies the URL prefix to be used for per-directory (htaccess)
RewriteRule
directives that substitute a relative path
RewriteCond
These directives may precede
RewriteRule
. For rule that matches URL it will trigger rewriting only if all it's conditions evaluate to true
RewriteEngine
Enable or disable rewriting
RewriteMap
Used for values mapping in substitutions of
RewriteRule
. Map values from files, databases, external functions etc.
RewriteOptions
Specifies rewriting options for server of directory
RewriteRule
Manch the URL, make substitution(s). This directive only used for matching against URL path. For matching port, scheme, query string use
RewriteCond

Logging

mod_rewrite
logging can be enabled in apache configuration file with
LogLevel
directive. Possible levels are from
trace1
to
trace8
. Log levels higher than 2 should be used only for debugging.
Config example:
LogLevel alert rewrite:trace3

RewriteRule flags

B
Escape non-alphanumerical characters before substitution
BNP
Escape space as %20 and not +
C
Chain the rules. It this rule fails, the chained one will be skipped
CO
Set a cookie
DPI
Discard path info of the URL
END
Stop all rewriting imeediately
F
Return 403 Forbidden
G
Return 410 Gone
H
Send to specific content handler
L
Stop rewriting on this rule
N
Re-run rewriting from the first rule
NC
Case-insensitive
NE
Do not hex-escape special characters
NS
Skip rule if it's a subrequest
P
PT
QSA
Add query string
QSD
Discard query string
QSL
Treat the last ? as QS delimiter
R
Redirect with code
S
Skip next N rules
T
Set the MIME-type
Multiple flags should be comma-separated

RewriteRule substitutions

path
When rule is configured in server context and file system path does exist
URL
A relative path to the resource to be served
-
Do not substitute anything
$N
Reference to capturing group in
RewriteRule
pattern
%N
Reference to capturing group in
RewriteCond
%{VARNAME}
Server variable
${map:key|default}
Mapping function

RewriteRule syntax

RewriteRule Pattern Substitution [flags]