SAMBA part 1

IPConfig
eth1 : 192.168.0.11

#yum install samba*  -y

if you need to share system created directory(let selinux allow you to write )
setsebool   -P   samba_export_all_ro   on

setsebool   -P  samba_export_all_rw   on
 if -P option is given- the value of policy is persistent after reboot , if not then its value is changed temporarily till reboot.
To share home directory of users:-
setsebool   -P   samba_enable_home_dirs   on

To share the folder you have created you must do :-
eg. /myshare , /public/disk1 etc

#chcon   -t   samba_share_t   /myshare
#chcon   -t   samba_share_t  /public/disk1
                //check the selinux status by running "ls -lDZ" for dir and "ls -lZ" for files
In above fig. check the selinux context of Screenshot.png which I drag and dropped from Desktop of root to /pub_drive  directory and when i tried to share.. it failed.the reason was the mis labled selinux context   after changing it  the share was successful. look below:-


Sample  Example :
share a directory /pub_drive which is readable to everyone but writable/readable to the "test1" group.

#vim  /etc/samba/smb.conf
//edit around line 70+
 workgroup = WORKGROUP
server string = ...  //you can leave default
deadtime = 15  //this is optional ,it will treat 15 minutes idle time connection as dead
 netbiosname = sambasamba  // you can put any name,this name can also be used to connect by win clients.
host allow = 192.168.0.  198.167.89.  //here only /24 network of two subnet will be allow to access
invalid user = admin_user   root
max connection = 20  //connections up to 20 is enabled
security = user  //username and its password will be asked
//at end of file or share section
[pub_drive]    //you can give other names
path = /pub_drive
comment = everyone read but test1 gorup write
browseable = yes
writeable = yes
writelist = +test1
printable = no  //optional .it disable to load priint drivers
guestok = no

save/exit

service smb start
check file/dir permissions and make sure /pub_drive is in ownership of group test1 and writeable
eg.
chown  root.test1  /pub_drive/
chmod  775  /pub_drive/
Settingup users
#smbpasswd  -a   ganesh
password: ***********
#usermod -G test1 ganesh

Allow samba and samba client through firewall

Logon into a Windows xp/Vista/7 machine go to run type your samba serveraddress 
\\192.168.0.11
type your username and password....success!!

Check and delete connections (useful for deleting session in cache)
net use              ////to check the session
net use   \\192.168.0.11\IPC$ /delete            ///to delete
eg.

user mod

#useradd -g $GROUP $newUSER

# groupadd NewGroupName

            add secondary groups to a user, with the option “-G”

usermod -G $GROUP $USER 


            You can also remove a use from secondary group with:
 
gpasswd -d $USER $GROUP 

SUID ,SGID ,sticky bit

So what is the sticky bit?

A sticky bit is a permission bit that is set on a directory that allows only the owner of the file within that directory or the root user to delete or rename the file. No other user has the needed privileges to delete the file created by some other user.
This is a security measure to avoid deletion of critical folders and their content (sub-directories and files), though other users have full permissions.

Why does /tmp have the t sticky bit?

The /tmp directory can be used by different Linux users to create temporary files. Now, what if an user deletes/rename a file created by some other user in this directory?
Well, to avoid these kind of issues, the concept of sticky bit is used. So for that a 777 is given but preserving the sticky bit is not a bad idea.
A stickybit is a workaround method for shared directories not to be deleted accidentally. When a directory has a stickybit then only the owner or the root can delete it even that every user can take the full other permissions.
/tmp is the most shared directory between processes and users and for that it contains the stickybit to ensure that no user can delete the directory, even that the permission is 777 , and it must be so to give the ability to the users and processes to use the directory without conflict in permissions.

This usage of the sticky bit only applies to directories, not files.

sticky bit
$ chmod +t directory     or    $ chmod 1754 directory
$ chmod -t directory     or    $ chmod  754 directory
 
 
SGID 
chmod g+s reids.pl or chmod 2755 reids.pl 
$ chmod g-s reids.pl
$ chmod 755 reids.pl
 
SUID 
# chmod u+s /home/venu/reids.pl
To assign SUID in an absolute manner, simply prefix 4 to whatever
octal string you would otherwise use (like 4755 instead of 755) 

Apache webserver 2.2.15.el6 SSL/TLS part 2

how SSL actually works for securing your communications over the Internet. Before the communications occur, the following takes place:
  1. A company wishes to secure communications to their web server company.com.
  2. They create a public and private key for company.com (this is also known as as “SSL Certificate“).
  3. They go to a trusted third party company such as Thawte or Verisign: Thawte makes the company prove its identity and right to use the company.com domain. This usually involves a lot of paperwork and paying a hefty fee.
  4. Once the verification is complete, Thawte gives the company a new public key that has some additional information in it. This information is the certification from Thawte that this public key is for the company and company.com and that this is verified by Thawte. This certification information is encrypted using Thawte’s own private key… we will see why below.
Then, when Jason wishes to communicate with the company at company.com,
  1. Jason makes a connection to company.com with its computer (e.g. from his web browser if this is a web site). This connection is made to a special “port” (address) on company.com that is set up for SSL communications only (alternately, Jason could connect to a non-secure port and the initiate a secure connection after the fact … see SSL vs TLS – what’s the difference).
  2. Jason connects to company.com on its SSL-secured port, the company sends back its public key (and some other information, like what Ciphers it supports).
  3. Jason gets the public key and decides if it is “OK”…
    1. If the public key has expired, this could be a problem (these are only valid for a certain number of years before they have to be re-verified)
    2. If the public key claims to be for some domain that is not company.com that could be a significant problem.
    3. Jason has the public key for Thawte (and many other third party companies) stored in its computer — because these come with the computer. Thus, Jason can decrypt the validation information, prove the validation is actually from Thawte, and verify that the public key is certified by Thawte. If Jason trusts Thawte, then he can trust that he is really communicating with Company. If Jason doesn’t trust Thawte, or whatever Third Party verification company is actually being used, then the identity of who is running the servers to which Jason is connecting is suspect.
  4. If Jason doesn’t trust the server, then the communication is terminated before anything sensitive is communicated.
    1. Note: If Jason has his own SSL certificate, he may send that to the server at this point to see if the server trusts Jason.  These “Client-side SSL certificates” are not commonly used, but provide a good way for users like Jason to authenticate themselves with a server without using a username or password (or in addition to doing so, as a second factor).  In cases where this is used, the server would have to already know about the Jason’s certificate and verify it in a similar way to how the client verified the server.  If this fails, the connection is terminated.  If a client-side certificate is not needed, this step is skipped.
  5. Once Jason is happy with the server (and the server with Jason, if needed), then he can  choose an SSL Cipher to use from the list of encryption methods provided by the server, and generates a “symmetric key” (a password) for use with that Cipher.  Jason encrypts this password using the server’s public key and sends it back to the server.  The server (and only the server) can decrypt this message and get this password, which is now shared by both Jason and server.
  6. Jason will then start communicating with the company by encrypting all data using this password and the chosen Cipher.  Normal “symmetric” (password-based) encryption takes place from this point forward because it is much faster than using the public and private keys for everything. These keys were needed to enable the company (and possibly Jason to prove its identity and right to domain.com and to enable Jason and server to generate and securely communicate the unique, shared password.
SSL vs TLS
Transport Layer Security (TLS) and its predecessor, Secure Sockets Layer (SSL), are cryptographic protocols which are designed to provide communication security over the Internet. They use X.509 certificates and hence asymmetric cryptography to assure the counterparty whom they are talking with, and to exchange a symmetric key. This session key is then used to encrypt data flowing between the parties
There are two distinct ways that a program can initiate a secure connection with a server:
  1. By Port: Connecting to a specific port means that a secure connection should be used.  For example, port 443 for https (secure web), 993 for secure IMAP, 995 for secure POP, etc.  These ports are setup on the server ready to negotiate a secure connection first, and do whatever else you want second.
  2. By Protocol: These connections first begin with an insecure “hello” to the server and only then switch to secured communications after the handshake between the client and the server is successful. If this handshake fails for any reason, the connection is severed.  A good example of this is the command “STARTTLS” used in outbound email (SMTP) connections.
The “By Port” method is commonly referred to as “SSL” and the “By Protocol” method is commonly referred to as “TLS” in many program configuration areas.  Sometimes, you have only the option to specify the port and if you should be making a secure connection or not and the program itself guesses from that what method should be used … many old email programs like Outlook and Mac Mail did th
To Review: In email programs and other systems where you can select from SSL or TLS together with the port a connection will be made on:
  1. SSL means a “by port” connection to a port that expects to the session to start with security negotiation
  2. TLS means a “by protocol” connection where the program will connect “insecurely” first and use special commands to enable encryption
  3. Use of either could result in a connection encrypted with either SSL or TLS of any version based on what is installed on the sever and what is supported by your program.
  4. Both methods of connection result in equally secure communications.

So then, should I choose TLS or SSL?

If you are configuring a server, you should install software that supports the latest version of the TLS standard, and configure it properly.  This ensures that the connections that your user make is as secure as possible.  Using an excellent security certificate will also help a lot — e.g. one with 2048+ bit keys, Extended Validation, etc.
 current vers:
TLS 1.2 was defined in RFC 5246 in August 2008.
 From a security standpoint, SSL 3.0 should be considered less desirable than TLS 1.0. The SSL 3.0 cipher suites have a weaker key derivation process; half of the master key that is established is fully dependent on the MD5 hash function, which is not resistant to collisions and is, therefore, not considered secure. Under TLS 1.0, the master key that is established depends on both MD5 and SHA-1 so its derivation process is not currently considered weak

Basic TLS handshake

A simple connection example follows, illustrating a handshake where the server (but not the client) is authenticated by its certificate:
  1. Negotiation phase:
    • A client sends a ClientHello message specifying the highest TLS protocol version it supports, a random number, a list of suggested CipherSuites and suggested compression methods. If the client is attempting to perform a resumed handshake, it may send a session ID.
    • The server responds with a ServerHello message, containing the chosen protocol version, a random number, CipherSuite and compression method from the choices offered by the client. To confirm or allow resumed handshakes the server may send a session ID. The chosen protocol version should be the highest that both the client and server support. For example, if the client supports TLS1.1 and the server supports TLS1.2, TLS1.1 should be selected; SSL 3.0 should not be selected.
    • The server sends its Certificate message (depending on the selected cipher suite, this may be omitted by the server).[102]
    • The server sends a ServerHelloDone message, indicating it is done with handshake negotiation.
    • The client responds with a ClientKeyExchange message, which may contain a PreMasterSecret, public key, or nothing. (Again, this depends on the selected cipher.) This PreMasterSecret is encrypted using the public key of the server certificate.
    • The client and server then use the random numbers and PreMasterSecret to compute a common secret, called the "master secret". All other key data for this connection is derived from this master secret (and the client- and server-generated random values), which is passed through a carefully designed pseudorandom function.
  2. The client now sends a ChangeCipherSpec record, essentially telling the server, "Everything I tell you from now on will be authenticated (and encrypted if encryption parameters were present in the server certificate)." The ChangeCipherSpec is itself a record-level protocol with content type of 20.
    • Finally, the client sends an authenticated and encrypted Finished message, containing a hash and MAC over the previous handshake messages.
    • The server will attempt to decrypt the client's Finished message and verify the hash and MAC. If the decryption or verification fails, the handshake is considered to have failed and the connection should be torn down.
  3. Finally, the server sends a ChangeCipherSpec, telling the client, "Everything I tell you from now on will be authenticated (and encrypted, if encryption was negotiated)."
    • The server sends its authenticated and encrypted Finished message.
    • The client performs the same decryption and verification.
  4. Application phase: at this point, the "handshake" is complete and the application protocol is enabled, with content type of 23. Application messages exchanged between client and server will also be authenticated and optionally encrypted exactly like in their Finished message. Otherwise, the content type will return 25 and the client will not authenticate.

Client-authenticated TLS handshake

The following full example shows a client being authenticated (in addition to the server like above) via TLS using certificates exchanged between both peers.
  1. Negotiation Phase:
    • A client sends a ClientHello message specifying the highest TLS protocol version it supports, a random number, a list of suggested cipher suites and compression methods.
    • The server responds with a ServerHello message, containing the chosen protocol version, a random number, cipher suite and compression method from the choices offered by the client. The server may also send a session id as part of the message to perform a resumed handshake.
    • The server sends its Certificate message (depending on the selected cipher suite, this may be omitted by the server).[102]
    • The server requests a certificate from the client, so that the connection can be mutually authenticated, using a CertificateRequest message.
    • The server sends a ServerHelloDone message, indicating it is done with handshake negotiation.
    • The client responds with a Certificate message, which contains the client's certificate.
    • The client sends a ClientKeyExchange message, which may contain a PreMasterSecret, public key, or nothing. (Again, this depends on the selected cipher.) This PreMasterSecret is encrypted using the public key of the server certificate.
    • The client sends a CertificateVerify message, which is a signature over the previous handshake messages using the client's certificate's private key. This signature can be verified by using the client's certificate's public key. This lets the server know that the client has access to the private key of the certificate and thus owns the certificate.
    • The client and server then use the random numbers and PreMasterSecret to compute a common secret, called the "master secret". All other key data for this connection is derived from this master secret (and the client- and server-generated random values), which is passed through a carefully designed pseudorandom function.
  2. The client now sends a ChangeCipherSpec record, essentially telling the server, "Everything I tell you from now on will be authenticated (and encrypted if encryption was negotiated). " The ChangeCipherSpec is itself a record-level protocol and has type 20 and not 22.
    • Finally, the client sends an encrypted Finished message, containing a hash and MAC over the previous handshake messages.
    • The server will attempt to decrypt the client's Finished message and verify the hash and MAC. If the decryption or verification fails, the handshake is considered to have failed and the connection should be torn down.
  3. Finally, the server sends a ChangeCipherSpec, telling the client, "Everything I tell you from now on will be authenticated (and encrypted if encryption was negotiated). "
    • The server sends its own encrypted Finished message.
    • The client performs the same decryption and verification.
  4. Application phase: at this point, the "handshake" is complete and the application protocol is enabled, with content type of 23. Application messages exchanged between client and server will also be encrypted exactly like in their Finished message.

CPU Load Average

cmds: top , uptime

         nproc , /proc/cpuinfo
load average: 0.09, 0.05, 0.01 
the three numbers represent averages over progressively longer periods of time (one, five, and fifteen minute averages), and that lower numbers are better. Higher numbers represent a problem or an overloaded machine.
Example traffic on bridge
  • 0.00 means there's no traffic on the bridge at all. In fact, between 0.00 and 1.00 means there's no backup, and an arriving car will just go right on.
  • 1.00 means the bridge is exactly at capacity. All is still good, but if traffic gets a little heavier, things are going to slow down.
  • over 1.00 means there's backup. How much? Well, 2.00 means that there are two lanes worth of cars total -- one lane's worth on the bridge, and one lane's worth waiting. 3.00 means there are three lane's worth total -- one lane's worth on the bridge, and two lanes' worth waiting. Etc.
= load of 1.00
= load of 0.50
= load of 1.70


This is basically what CPU load is. "Cars" are processes using a slice of CPU time ("crossing the bridge") or queued up to use the CPU. Unix refers to this as the run-queue length: the sum of the number of processes that are currently running plus the number that are waiting (queued) to run.
Like the bridge operator, you'd like your cars/processes to never be waiting. So, your CPU load should ideally stay below 1.00. Also like the bridge operator, you are still ok if you get some temporary spikes above 1.00 ... but when you're consistently above 1.00, you need to worry.
The problem with a load of 1.00 is that you have no headroom. In practice, many sysadmins will draw a line at 0.70:

  • The "Need to Look into it" Rule of Thumb: 0.70 If your load average is staying above > 0.70, it's time to investigate before things get worse.
  • The "Fix this now" Rule of Thumb: 1.00. If your load average stays above 1.00, find the problem and fix it now. Otherwise, you're going to get woken up in the middle of the night, and it's not going to be fun.
  • The "Arrgh, it's 3AM WTF?" Rule of Thumb: 5.0. If your load average is above 5.00, you could be in serious trouble, your box is either hanging or slowing way down, and this will (inexplicably) happen in the worst possible time like in the middle of the night or when you're presenting at a conference. Don't let it get there.

    What about Multi-processors? My load says 3.00, but things are running fine!

    Got a quad-processor system? It's still healthy with a load of 3.00.
    On multi-processor system, the load is relative to the number of processor cores available. The "100% utilization" mark is 1.00 on a single-core system, 2.00, on a dual-core, 4.00 on a quad-core, etc.
    If we go back to the bridge analogy, the "1.00" really means "one lane's worth of traffic". On a one-lane bridge, that means it's filled up. On a two-late bridge, a load of 1.00 means its at 50% capacity -- only one lane is full, so there's another whole lane that can be filled.
     = load of 2.00 on two-lane road
    Same with CPUs: a load of 1.00 is 100% CPU utilization on single-core box. On a dual-core box, a load of 2.00 is 100% CPU utilization.


    • The "number of cores = max load" Rule of Thumb: on a multicore system, your load should not exceed the number of cores available.
    • The "cores is cores" Rule of Thumb: How the cores are spread out over CPUs doesn't matter. Two quad-cores == four dual-cores == eight single-cores. It's all eight cores for these purposes.


      Which average should I be observing? One, five, or 15 minute?
      For the numbers we've talked about (1.00 = fix it now, etc), you should be looking at the five or 15-minute averages. Frankly, if your box spikes above 1.0 on the one-minute average, you're still fine. It's when the 15-minute average goes north of 1.0 and stays there that you need to snap to. (obviously, as we've learned, adjust these numbers to the number of processor cores your system has).
To get your cpu info ==> # cat /proc/cpuinfo

.htaccess Use

to use .htaccess
at around line 341 change  
AllowOverride None    to ==>

AllowOverride AuthConfig

saveit

cd outside webdir eg /etc/httpd/conf/ 

#htpasswd -c .htpasswd rambo
password : ******
..................................................................
to add more users like rambo do
#htpasswd .htpasswd  samsung
password:********
.................................................................

Create a file .htaccess in the dir which you want to control web/dir access for example /var/www/html/www.internal.ninja.com/ and add following lines

AuthType Basic
AuthName " Restricted Area Please insert userame password "   ##this will appear in msg box
AuthUserFile /etc/httpd/conf.d/.htpasswd
require valid-user
.............................................
next
#service httpd reload
#httpd -S   (if made on VH)

inheritance and performance issues if used in large scale..

date time



For example, set new data to 2 Oct 2006 18:00:00, type the following command as root user:
 
# date  -s   "2 OCT 2006 18:00:00"
 
OR

# date --set="2 OCT 2006 18:00:00"

You can also simplify format using following syntax:
 
# date +%Y%m%d -s "20081128"

$ date --date="12/2/2014"
Tue Dec  2 00:00:00 PST 2014

$ date --date="2 Feb 2014"
Sun Feb  2 00:00:00 PST 2014

$ date --date="Feb 2 2014"
Sun Feb  2 00:00:00 PST 2014


Linux Set Time
To set time use the following syntax:

# date  +%T  -s  "10:13:13"

Set timezone using /etc/localtime configuration file [any Linux distro]

Often /etc/localtime is a symlink to the file localtime or to the correct time zone file in the system time zone directory.

Generic procedure to change timezone under Linux

Cd to /etc, run:
# cd /etc
Create a symlink to file localtime:
# ln -sf /usr/share/zoneinfo/EST localtime
OR some distro use /usr/share/zoneinfo/dirname/zonefile format (Red hat and friends):
# ln -sf /usr/share/zoneinfo/EST localtime
OR if you want to set up it to Asia/Kathmandu
# ln -sf /usr/share/zoneinfo/Asia/Kathmandu localtime
Various Date Command Formats
You can use formatting option to display date command in various formats using the following syntax:
$ date +%<format-option>
The following table displays various date command formatting options.
Format options
Purpose of Option
Output
date +%a
Displays Weekday name in short (like Mon, Tue, Wed)
Thu
date +%A
Displays Weekday name in full short (like Monday, Tuesday)
Thursday
date +%b
Displays Month name in short (like Jan, Feb, Mar )
Feb
date +%B
Displays Month name in full short (like January, February)
February
date +%d
Displays Day of month (e.g., 01)
07
date +%D
Displays Current Date; shown in MM/DD/YY
02/07/13
date +%F
Displays Date; shown in YYYY-MM-DD
2013-02-07
date +%H
Displays hour in (00..23) format
23
date +%I
Displays hour (01..12) format
11
date +%j
Displays day of year (001..366)
038
date +%m
Displays month (01..12)
02
date +%M
Displays minute (00..59)
44
date +%S
Displays second (00..60)
17
date +%N
Displays nanoseconds (000000000..999999999)
573587606
date +%T
Displays time; shown as HH:MM:SS
Note: Hours in 24 Format
23:44:17
date +%u
Displays day of week (1..7); 1 is Monday
4
date +%U
Displays week number of year, with Sunday as first day of week (00..53)
05
date +%Y
Displays full year i.e. YYYY
2013
date +%Z
alphabetic time zone abbreviation (e.g., EDT)
IS

vim

:set number   ##display line num

case insensitive search set on and off

:set ic
:set noic

Apache webserver 2.2.15.el6 (centos 6.4 x86) part1



Apache
yum install httpd -y

inside /etc/httpd/conf/httpd.conf
line 57
ServerRoot   /etc/httpd  ##ServerRoot is the path to the server’s configuration, error and log                                      files.. it can be changed [ no trailing / at end of path e.g /etc/httpd/  ]
line 65
PidFile  run/httpd.pid
##PidFile is the process identification number for the httpd. This process number
is important, because Apache spawns numerous child processes when running
to accommodate the web traffic. It allows you to monitor and manipulate your
server processes

Line 276
ServerName www.ninja.com:80
this setting you will have to change to get your server running. This
is where you declare the name of your website. I will use www.ninja.com
Line 292
DocumentRoot "/var/www/html"
Line 484
ErrorLog logs/error_log
Line 136
Listen 80

Virtual Hosts

Running several name-based web sites on a single IP address.

You must have the names in DNS, resolving to your IP address, or nobody else will be able to see your web site. You can put entries in your hosts file for local testing,

Listen 80 #this is default

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here
</VirtualHost>

or  

NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>   ##172.20.30.40 is real ip of system
DocumentRoot /www/example2
ServerName www.example2.org

# Other directives here
</VirtualHost> 
 
The asterisks match all addresses, so the main server serves no requests. Due to the fact that www.example1.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first VirtualHost.
------------------------------------------------------------------------------------------------
For example, suppose that you are serving the domain www.domain.tld and you wish to add the virtual host www.otherdomain.tld, which points at the same IP address. Then you simply add the following to httpd.conf:
NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>

<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
You can alternatively specify an explicit IP address in place of the * in both the NameVirtualHost and <VirtualHost> directives. For example, you might want to do this in order to run some name-based virtual hosts on one IP address, and either IP-based, or another set of name-based virtual hosts on another address.
Many servers want to be accessible by more than one name. This is possible with the ServerAlias directive, placed inside the <VirtualHost> section. For example in the first <VirtualHost> block above, the ServerAlias directive indicates that the listed names are other names which people can use to see that same web site:
ServerAlias domain.tld *.domain.tld
then requests for all hosts in the domain.tld domain will be served by the www.domain.tld virtual host. The wildcard characters * and ? can be used to match names. Of course, you can't just make up names and place them in ServerName or ServerAlias. You must first have your DNS server properly configured to map those names to an IP address associated with your server.
The complete list of names in the VirtualHost directive are treated just like a (non wildcard) ServerAlias.
Finally, you can fine-tune the configuration of the virtual hosts by placing other directives inside the <VirtualHost> containers. Most directives can be placed in these containers and will then change the configuration only of the relevant virtual host. To find out if a particular directive is allowed, check the Context of the directive. Configuration directives set in the main server context (outside any <VirtualHost> container) will be used only if they are not overridden by the virtual host settings.
Now when a request arrives, the server will first check if it is using an IP address that matches the NameVirtualHost. If it is, then it will look at each <VirtualHost> section with a matching IP address and try to find one where the ServerName or ServerAlias matches the requested hostname. If it finds one, then it uses the configuration for that server. If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a <VirtualHost> container and list it first in the configuration file.


Name-based hosts on more than one IP address.
# This is the other address
NameVirtualHost 172.20.30.50

<VirtualHost 172.20.30.50>
DocumentRoot /www/example1
ServerName www.example1.com

# Other directives here ...

</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org

# Other directives here ...

</VirtualHost> 

Any request to an address other than 172.20.30.50 will be served from the main server. A request to 172.20.30.50 with an unknown hostname, or no Host: header, will be served from www.example1.com. 

Serving the same content on different IP addresses (such as an internal and external address).

The server machine has two IP addresses (192.168.1.1 and 172.20.30.40). The machine is sitting between an internal (intranet) network and an external (internet) network. Outside of the network, the name server.example.com resolves to the external address (172.20.30.40), but inside the network, that same name resolves to the internal address (192.168.1.1).
The server can be made to respond to internal and external requests with the same content, with just one VirtualHost section.

Server configuration

NameVirtualHost 192.168.1.1
NameVirtualHost 172.20.30.40

<VirtualHost 192.168.1.1 172.20.30.40>
DocumentRoot /www/server1
ServerName server.example.com
ServerAlias server
</VirtualHost>
Now requests from both networks will be served from the same VirtualHost.

Note:

On the internal network, one can just use the name server rather than the fully qualified host name server.example.com.
Note also that, in the above example, you can replace the list of IP addresses with *, which will cause the server to respond the same on all addresses.

Running different sites on different ports.

You have multiple domains going to the same IP and also want to serve multiple ports. By defining the ports in the "NameVirtualHost" tag, you can allow this to work. If you try using <VirtualHost name:port> without the NameVirtualHost name:port or you try to use the Listen directive, your configuration will not work.

Server configuration

Listen 80
Listen 8080

NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080

<VirtualHost 172.20.30.40:80>
ServerName www.example1.com
DocumentRoot /www/domain-80
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
ServerName www.example1.com
DocumentRoot /www/domain-8080
</VirtualHost>

<VirtualHost 172.20.30.40:80>
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
</VirtualHost>

IP-based virtual hosting

The server has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example1.com and www.example2.org respectively.

Server configuration

Listen 80

<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost> 


Mixed port-based and ip-based virtual hosts

The server machine has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example1.com and www.example2.org respectively. In each case, we want to run hosts on ports 80 and 8080.

Server configuration

Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080

<VirtualHost 172.20.30.40:80>
DocumentRoot /www/example1-80
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
DocumentRoot /www/example1-8080
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.50:80>
DocumentRoot /www/example2-80
ServerName www.example1.org
</VirtualHost>

<VirtualHost 172.20.30.50:8080>
DocumentRoot /www/example2-8080
ServerName www.example2.org
</VirtualHost> 


Mixed name-based and IP-based vhosts

Server configuration

Listen 80

NameVirtualHost 172.20.30.40

<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/example3
ServerName www.example3.net
</VirtualHost>

# IP-based
<VirtualHost 172.20.30.50>
DocumentRoot /www/example4
ServerName www.example4.edu
</VirtualHost>

<VirtualHost 172.20.30.60>
DocumentRoot /www/example5
ServerName www.example5.gov
</VirtualHost> 

 





 

Multiple SSL Certificate install on apache2 server  [Ubuntu 12.04]
Enable SSL on your webserver
sudo a2enmod ssl
Follow up by restarting Apache.
sudo service apache2 restart
Copy your SSL certificate and key to any location
Eg. Here we copied to /etc/apache2/ssl  
Make sure permission of those files are 400 or 600 [root only read/write]
In   sites/sites-available/<your domains>

vim btlg.com.au
<VirtualHost *:80>
ServerName btlg.com.au 
ServerAlias 
www.btlg.com.au
DocumentRoot /home/bootleg/public_html
</VirtualHost>
<VirtualHost *:443>
ServerName btlg.com.au
ServerAlias 
www.btlg.com.au
DocumentRoot /home/bootleg/public_html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/btlg/281027f6c6f445.crt
SSLCertificateKeyFile /etc/apache2/ssl/btlg/www_btlg_com_au.key 
SSLCertificateChainFile /etc/apache2/ssl/btlg/sf_bundle-g2-g1.crt
</VirtualHost>

vim example.com.au
<VirtualHost *:80>
ServerName btlg.com.au 
ServerAlias
example.com.au
DocumentRoot /home/example.com/public_html
</VirtualHost>
<VirtualHost *:443>
ServerName
example.com.au
ServerAlias www.example.com.au
DocumentRoot /home/example.com/public_html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/example/281027f6c6f445.crt
SSLCertificateKeyFile /etc/apache2/ssl/example/www_example_com_au.key 
SSLCertificateChainFile /etc/apache2/ssl/btlg/sf_bundle-g2-g1.crt
</VirtualHost>


Restart apache2 and browse to your domain with http or https

Restricting  virtual host by ip address example

<virtualhost *:80>
ServerName mail.rpmanpower.com
ServerAlias mail.rpmanpower.com
ServerAdmin zadmin@localhost
DocumentRoot "/etc/zpanel/panel/etc/apps/webmail"
<Location />
Order deny,allow
Deny from all
Allow from 182.93.95.225 198.50.239.107
</Location>
</virtualhost>

this will restrict access to all ip address to mail.rpmanpower.com
EXCEPT 182.93.95.225 198.50.239.107  thest TWO CAN ACCESS

Deny only one ip 192.168.16.100
<Location /server-status>
    SetHandler server-status
    Order Allow,Deny
    Deny from  192.168.16.100
    Allow from 192.168.16.0/24
</Location>
Deny from all is not needed. In fact it will screw up because everything will match all, and thus denied (and I think Apache is trying to be smart and do something stupid). I have always found Apache's Order, Allow and Deny directives confusing, so always visualize things in a table (taken from the docs):
Match      | Allow,Deny result   | Deny,Allow result
-------------------------------------------------------
Allow only | Allowed             | Allowed
Deny only  | Denied              | Denied
No match   | Default: Denied     | Default: Allowed
Match both | Final match: Denied | Final match: Allowed
With the above settings:
  • Requests from 192.168.16.100 get "Match both" and thus denied.
  • Requests from 192.168.16.12 get "Allow only" and thus allowed.
  • Requests from 123.123.123.123 get "No match" and thus denied.

Apache 2.4.X  restricting URLS (ubuntu 14.04.X)

open up the apache host ffile and add below to allow only listed IP to access this url

 Example 2:
===========

<VirtualHost *:80>
...
ServerName hottiehunter.com
        ServerAlias  www.hottiehunter.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/hottiehunter/public
<Location /admin>
             Require ip 202.51.76.236 110.44.116.70 202.51.76.238

</Location>

 <Directory /var/www/hottiehunter/public>
        Options -Indexes
        </Directory>



Example 2
========

Alias /PMA /usr/share/phpmyadmin
<Location /PMA>
Require ip 202.51.76.236 110.44.116.70 202.51.76.238
</Location>

<Directory /usr/share/phpmyadmin>
        Options FollowSymLinks
        DirectoryIndex index.php

<IfModule mod_php5.c>
                AddType application/x-httpd-php .php
....
......