Apache linux

How to set up Virtual Hosts in Apache

Setting up Virtual Hosts in Apache on Mac OSX is straight forward after you have your local Web Development environment up and running. The process of setting up Virtual Hosts is done easily in the Terminal either using nano or vi with sudo or as a root user.

Allow the vhosts configuration from the Apache configuration file httpd.conf

Open the httpd.conf

sudo nano /etc/apache2/httpd.conf

Search for ‘vhosts‘ and uncomment the include line

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf</pre>

Also allow another module to run by uncommenting:

LoadModule vhost_alias_module libexec/apache2/

Edit the vhosts.conf file

Open this file to add in the virtual host.

sudo nano /etc/apache2/extra/httpd-vhosts.conf

An example domain in the file is given of the format required to add in additional domains, just follow this to create your new virtual host:

<VirtualHost *:80>
  DocumentRoot "/usr/docs/"
  ErrorLog "/private/var/log/apache2/"
  CustomLog "/private/var/log/apache2/" common

We can take this example and extend on it, if you wanted a domain named for example, you can copy the existing text block and edit to suit:

<VirtualHost *:80>
  DocumentRoot "/Users/USERNAME/Sites/apple"
  ErrorLog "/private/var/log/apache2/"
  CustomLog "/private/var/log/apache2/" common

So in the example above a vhost for is created and the document root is in the Sites folder, in the text block above I have also added in some log files, what you need to change is the document root location username and domain name to suit your needs. Finish and save the file.

Now also you need to map the IP address to be the localhost.

Map Your IP address to localhost

sudo nano /etc/hosts

Add the Domain and ‘www‘ alias to resolve to the localhost address

Restart Apache

sudo apachectl restart

Check out your local vhost domain in the browser

Losing Localhost

One caveat to note about virtual hosts is that once set up you lose your older document root previously at/Library/WebServer/Documents or accessed in the browser at http://localhost what happens is that you get a 403 Forbidden Error. But the ~/username document root is still compatible.

To get around this, you need to add in a vhost for localhost and declare this vhost before any of the others, in the same file:

sudo nano /etc/apache2/extra/httpd-vhosts.conf

Add in:

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /Library/WebServer/Documents/

Restart Apache

sudo apachectl restart

Changing the WebServer Default User

One of the frustrations of using the Users/username/Sites folder for vhosts is the permissions issues with things like updates and authentication.

This is because the default webserver user which runs httpd is known as _www, which will not be the user in your local account. If your machine is only in use by you and the webserver will run only under your account then you can change the user.

Find Your User and Group

In the Terminal use the id command to see your username and group

 id (use this command)

You will get a bunch of user groups, you need your primary user uid and group gid names

uid=502(<mark>swatantra</mark>) gid=20(<mark>staff</mark>)

Change this back in /etc/apache2/httpd.conf


Restart Apache

sudo apachectl restart

Restart Apache and now you are running httpd as your local account.

'Coz sharing is caring

How to Compare 2 folders or 2 files in the Command Line

One can easily compare the difference of files in two folders on the Command Line using diff command.

With folders we use rq as an argument and pass in the two folder names on the command line

diff -rq folder1 folder2

This will notify which corresponding files are different, and which files are unique in each folder. Once you know what files are different you then can run the diff file command on those files to see the actual difference.

With files one can pass below in to the command line with diff

diff file1.txt file2.txt

Commonly-Used diff Options

Here are some useful diff options to take note of:

-b Ignore any changes which only change the amount of whitespace (such as spaces or tabs).
-w Ignore whitespace entirely.
-B Ignore blank lines when calculating differences.
-y Display output in two columns.

These are only some of the most commonly-used diff options. What follows is a complete list of diff options and their function.

diff syntax

diff [OPTION]... FILES


–normal output a “normal” diff. (This is the default)
-q, –brief Produce output only when files differ. If there are no differences, output nothing.
-s, –report-identical-files Report when two files are the same.
-c, -C NUM, –context[=NUM] Provide NUM (default 3) lines of context.
-u, -U NUM, –unified[=NUM] Provide NUM (default 3) lines of unified context.
-e, –ed Output an ed script.
-n, –rcs Output an RCS-format diff.
-y, –side by side Format output in two columns.
-W, –width=NUM Output at most NUM (default 130) print columns.
–left-column Output only the left column of common lines.
–suppress-common-lines Do not output lines common between the two files.
-p, –show-c-function For files that contain C code, also show which C function each change is in.
-F,–show-function-line=RE Show the most recent line matching regular expression RE.
–label LABEL When displaying output, use the label LABEL instead of the file name. This option can be issued more than once for multiple labels.
-t, –expand-tabs Expand tabs to spaces in output.
-T, –initial-tab Make tabs line up by prepending a tab if necessary.
–tabsize=NUM Define a tab stop as NUM (default 8) columns.
–suppress-blank-empty Suppress spaces and/or tabs before empty output lines.
-l, –paginate Pass output through pr to paginate it.
-r, –recursive Recursively compare any subdirectories found.
-N, –new-file If a specified file does not exist, perform the diff as if it is an empty file.
–unidirectional-new-file Same as -n, but only applies to the first file.
–ignore-file-name-case Ignore case when comparing file names.
–no-ignore-file-name-case Consider case when comparing file names.
-x, –exclude=PAT Exclude files that match filename pattern PAT.
-X, –exclude-from=FILE Exclude files that match any filename pattern in file FILE.
-S, –starting-file=FILE Start with file FILE when comparing directories.
–from-file=FILE1 Compare FILE1 to all operands; FILE1 can be a directory.
–to-file=FILE2 compare all operands to FILE2; FILE2 can be a directory.
-i, –ignore-case ignore case differences in file contents.
-E, –ignore-tab-expansion ignore changes due to tab expansion.
-b, –ignore-space-change ignore changes in the amount of white space.
-w, –ignore-all-space ignore all white space.
-B, –ignore-blank-lines ignore changes whose lines are all blank.
-I, –ignore-matching-lines=RE ignore changes whose lines all match regular expression RE.
-a, –text treat all files as text.
–strip-trailing-cr strip trailing carriage return on input.
-D, –ifdef=NAME output merged file with “#ifdef NAME” diffs.
GTYPE-group-format=GFMT format GTYPE input groups with GFMT.
–line-format=LFMT format all input lines with LFMT.
LTYPE-line-format=LFMT format LTYPE input lines with LFMT.

These format options provide fine-grained control over the output ofdiff, generalizing -D/–ifdef.

LTYPE is old, new, or unchanged.

GTYPE can be any of the LTYPE values, or the value changed.

GFMT (but not LFMT) may contain:

%< lines from FILE1
%> lines from FILE2
%= lines common to FILE1 and FILE2.
%[][WIDTH][.[PREC]]{doxX}LETTER printf-style spec for LETTER

LETTERs are as follows for new group, lower case for old group:

F first line number
L last line number
N number of lines = LF + 1
E F1
M L + 1
%(A=B?T:E) if A equals B then T else E

LFMT (only) may contain:

%L contents of line
%l contents of line, excluding any trailing newline
%[][WIDTH][.[PREC]]{doxX}n printf-style spec for input line number

Both GFMT and LFMT may contain:

%% A literal %
%c’C the single character C
%c’\OOO’ the character with octal code OOO
C the character C (other characters simply represent themselves)
-d, –minimal try hard to find a smaller set of changes.
–horizon-lines=NUM keep NUM lines of the common prefix and suffix.
–speed-large-files assume large files and many scattered small changes.
–help display a help message and exit.
-v, –version output version information and exit.

FILES takes the form “FILE1 FILE2” or “DIR1 DIR2” or “DIR FILE…” or “FILEDIR“.

If the –from-file or –to-file options are given, there are no restrictions on FILE(s). If aFILE is a dash (““), diff reads from standard input.

Exit status is either 0 if inputs are the same, 1 if different, or 2 if diff encounters any trouble.

'Coz sharing is caring