Showing posts with label linux web server. Show all posts
Showing posts with label linux web server. Show all posts

Monday, December 9, 2013

LAMP Server on Debian Squeeze

1. Change your source list:-
    # nano /etc/apt/sources.list


============================================================
## Debian security updates:
deb http://security.debian.org/ squeeze/updates main contrib non-free
deb-src http://security.debian.org/ squeeze/updates main contrib non-free

## Debian.org:
deb http://ftp.debian.org/debian/ squeeze main contrib non-free
deb-src http://ftp.debian.org/debian/ squeeze main contrib non-free
============================================================

2. Now let’s update the system

    #apt-get update

    #apt-get upgrade

3. Server should sync the time with the outer world frequently so we need ntp

    #apt-get install ntp ntpdate

4. Now we install Apache2 and PHP5

    #apt-get install apache2 apache2.2-common apache2-doc apache2-mpm-prefork apache2-utils libexpat1 ssl-cert libapache2-mod-php5 php5 php5-common php5-mysql php5-cli apache2-suexec libapache2-mod-suphp php-pear php5-suhosin php5-xcache

5. Enable the Apache modules now:-
   
    #a2enmod suexec rewrite ssl actions include

6. Now Restart Apache :

    #service apache2 restart


7.Now let’s install Mysql

    #apt-get install mysql-server mysql-client libmysqlclient15-dev




--------------------------------------------------------------------------------------------------
Optional: Install Support for Scripting:
---------------------------------------

The following commands install Apache support for server-side scripting in PHP, Ruby, Python,
and Perl. Support for these languages is optional based on your server environment.

To install Ruby support, issue the following command:
----------------------------------------------------

    #apt-get install libapache2-mod-ruby

To install Perl support, issue the following command:
-----------------------------------------------------

    #apt-get install libapache2-mod-perl2

To install Python support, issue the following command:
------------------------------------------------------

    #apt-get install libapache2-mod-python

If you need support for MySQL in Python, you will also need to install Python MySQL support:
-------------------------------------------------------------------------------------------
    #apt-get install python-mysqldb

=============================================================
---------------------------------------------------------------------------
Your PHP application may require additional dependencies included in Debian.
To check for available PHP dependencies run this command:
---------------------------------------------------------------------------
=============================================================
    #apt-cache search php

The output of that command will show you a list of package names and descriptions.
To install them, issue the following command:

    #apt-get install libapache2-mod-php5 php5 php-pear php5-xcache

To install the php5-suhosin package, which provides additional security for your PHP installation, issue the following command:

    #apt-get install php5-suhosin


-------------------
TUNE UP php CONFIG:
-------------------
Once PHP5 is installed, you'll need to tune the configuration file located
in /etc/php5/apache2/php.ini to enable more descriptive errors, logging, and better performance.

    # vim /etc/php5/apache2/php.ini

------------------------------------------------
max_execution_time = 30
memory_limit = 64M
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
display_errors = Off
log_errors = On
error_log = /var/log/php.log
register_globals = Off
------------------------------------------------



=======================
Multi-Processing Module:
=======================

This multi-processing module can handle a large number of requests quickly by using multiple threads per worker process.


Install the mpm-itk module:
--------------------------
    #apt-get install apache2-mpm-itk

Open the /etc/apache2/sites-available/example.net file for editing:
------------------------------------------------------------------
nano /etc/apache2/sites-available/example.net

Add the following lines to the file's <VirtualHost > block:

File excerpt:/etc/apache2/sites-available/example.net

======================================
 <IfModule mpm_itk_module>
    AssignUserId webeditor webgroup
 </IfModule>
======================================
In this example  I want this vhost to run as the user webeditor and group webgroup.

If user and group not exist......


    # groupadd webgroup
    # useradd -s /bin/false -d /home/webeditor -m -g webgroup webeditor (-s /bin/false is used for no login...)


=======================================================
=======================================================
Note:
-----
If you would like to enable Perl support, add the following lines to the VirtualHost entry,
right above the closing </VirtualHost> tag:

File excerpt:/etc/apache2/sites-available/example.net

--------------------------
Options ExecCGI
AddHandler cgi-script .pl
--------------------------

=======================================================
=======================================================


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
                       Configure Apache for Named-Based Virtual Hosting
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

1.First we will disable the default Apache virtual host:
    # a2dissite default
2.Each virtual host needs its own configuration file in the /etc/apache2/sites-available/ directory.
  Now we will create for  example.net

    # vim /etc/apache2/sites-available/example.net


-------------------------------------------------------------
<VirtualHost *:80>
      ServerAdmin webmaster@example.net
      ServerName example.net
      ServerAlias www.example.net
      DocumentRoot /srv/www/example.net/public_html/
      ErrorLog /srv/www/example.net/logs/error.log
      CustomLog /srv/www/example.net/logs/access.log combined
</VirtualHost>
-------------------------------------------------------------


3. Add or host another domain for better understanding.....(named example.org)

    # vim /etc/apache2/sites-available/example.org

-------------------------------------------------------------
<VirtualHost *:80>
      ServerAdmin admin@example.org
      ServerName example.org
      ServerAlias www.example.org
      DocumentRoot /srv/www/example.org/public_html/
      ErrorLog /srv/www/example.org/logs/error.log
      CustomLog /srv/www/example.org/logs/access.log combined
</VirtualHost>
--------------------------------------------------------------


4. We have to create the directories for example.net and example.org to keep website files and store logs.

For example.net:
---------------
mkdir -p /srv/www/example.net/public_html
mkdir /srv/www/example.net/logs


For example.org:
---------------
mkdir -p /srv/www/example.org/public_html
mkdir /srv/www/example.org/logs


5. Finally enable the added sites by following commands:

    # a2ensite example.net
    # a2ensite example.org


6. Don't forget to restart your apache service to take effect the changes....
    # /etc/init.d/apache2 restart




/////////////////////////////////////////////////////////////////////////////
Special Commands:
----------------
1. If you want to enable a particular site:
    # a2ensite example.com

2. If you want to disable a particular site:
    # a2dissite example.com

3. If you want to enable a mode:
    # a2enmod rewrite

4. To disable a module that is currently enabled:
    # a2dismod rewrite

/////////////////////////////////////////////////////////////////////////////

=====================================
SSL Support in Debian SQUEEZE (https)
=====================================
1. Install openssl
    # apt-get install openssl ssl-cert (it will say already installed as we did above)

2.Generate A certificate
    # openssl req $@ -new -x509 -days 365 -nodes -out /etc/apache2/apache.pem -keyout /etc/apache2/apache.pem

[You are about to be asked to enter information that will be incorporated
into your certificate request.]


3.Set appropriete permission
    # chmod 600 /etc/apache2/apache.pem



4. Enable SSL Support
    # a2enmod ssl    (it will say already enabled as we did above)

5. Configuring SSL Certificate to Virtual Hosts in Apache2:
    # vim /etc/apache2/sites-available/default


------------------------------------------------------------------------------


<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

################## ALL THE VIRTUAL HOSTS ###############

NameVirtualHost 172.16.0.250:80
NameVirtualHost 172.16.0.250:443

<VirtualHost 172.16.0.250:80>

        ServerName mithu.com
        ServerAlias www.mithu.com
        DocumentRoot /var/www
        CustomLog /var/www/logs/mithu.log combined
        ErrorLog /var/www/logs/mithu.log
</VirtualHost>

<VirtualHost 172.16.0.250:443>
        ServerName mithu.com
        ServerAlias www.mithu.com
        DocumentRoot /var/www
        CustomLog /var/www/logs/mithu.log combined
        ErrorLog /var/www/logs/mithu.log
SSLEngine on
SSLCertificateFile /etc/apache2/apache.pem
</VirtualHost>

<VirtualHost 172.16.0.250:80>
        ServerName mehedi.com
        ServerAlias www.mehedi.com
        DocumentRoot /var/www/mehedi
        CustomLog /var/www/logs/mehedi.log combined
        ErrorLog /var/www/logs/mehedi.log

</VirtualHost>

<VirtualHost 172.16.0.250:443>
        ServerName mehedi.com
        ServerAlias www.mehedi.com
        DocumentRoot /var/www/mehedi
        CustomLog /var/www/logs/mehedi.log combined
        ErrorLog /var/www/logs/mehedi.log
SSLEngine on
SSLCertificateFile /etc/apache2/apache.pem
</VirtualHost>
<VirtualHost 172.16.0.250:443>
        ServerName mehedi.com
        ServerAlias www.mehedi.com
        DocumentRoot /var/www/mehedi
        CustomLog /var/www/logs/mehedi.log combined
        ErrorLog /var/www/logs/mehedi.log
SSLEngine on
SSLCertificateFile /etc/apache2/apache.pem
</VirtualHost>
==============================================================

6. Now restrat our apache web server using the following comamnd

# /etc/init.d/apache2 restart

//////////////////////////////////////////////////////////////////////////////
Special Note:
----------------
If we don't follow the above steps as described, phpmyadmin will not work
properly.(At least in my case I completely failed to load default phpmyadmin page.)

//////////////////////////////////////////////////////////////////////////////



######################################################################
            @@@@@@@  FULL IPTABLES SCRIPT FOR SERVER @@@@@@
######################################################################

#!/bin/sh

modprobe ip_conntrack
modprobe ip_nat_ftp
modprobe ip_conntrack_ftp
modprobe iptable_nat

iptables -F
iptables -X
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X

iptables -P INPUT DROP   
iptables -P OUTPUT DROP
iptables -P FORWARD DROP


# Enable SSH.

iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 2 --rttl --name SSH -j LOG --log-level info --log-prefix "Anti SSH-Bruteforce: "
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 2 --rttl --name SSH -j DROP

iptables -A INPUT -p tcp --dport 22 -j ACCEPT

iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT



# Allow Loopback Access

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

#Allow Ping
iptables -A INPUT -p icmp --icmp-type 0 -m limit --limit  2/s --limit-burst 5 -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type 0  -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -m limit --limit  2/s --limit-burst 5 -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type 8  -j ACCEPT


# Prevent TCP Sync Attack.
#iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j RETURN
iptables -A INPUT -p tcp -m state --state NEW -m recent --update --seconds 60 --hitcount 20  -j DROP

iptables -A INPUT -p tcp -m state --state NEW -m recent --set -j ACCEPT

#Force SYN packets check
iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP

# Force Fragments packets check
iptables -A INPUT -f -j DROP

# Incoming malformed XMAS packets drop them
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP

# Drop all NULL packets
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP

# Allow DNS


iptables -A INPUT -p tcp -i eth0 -m hashlimit \
         --hashlimit-mode srcip --hashlimit-upto 100/m --hashlimit-burst 100 \
         --hashlimit-name DNSTHROTTLE --sport 53  -j ACCEPT
iptables -A OUTPUT -p tcp -o eth0 --dport 53 -j ACCEPT

iptables -A INPUT -p tcp -i eth0 -m hashlimit \
         --hashlimit-mode srcip --hashlimit-upto 100/m --hashlimit-burst 100 \
         --hashlimit-name DNSTHROTTLE --dport 53  -j ACCEPT
iptables -A OUTPUT -p tcp -o eth0 --sport 53 -j ACCEPT

iptables -A INPUT -p udp -i eth0 -m hashlimit \
         --hashlimit-mode srcip --hashlimit-upto 100/m --hashlimit-burst 100 \
         --hashlimit-name DNSTHROTTLE --sport 53  -j ACCEPT
iptables -A OUTPUT -p udp -o eth0 --dport 53 -j ACCEPT

iptables -A INPUT -p udp -i eth0 -m hashlimit \
         --hashlimit-mode srcip --hashlimit-upto 100/m --hashlimit-burst 100 \
         --hashlimit-name DNSTHROTTLE --dport 53  -j ACCEPT
iptables -A OUTPUT -p udp -o eth0 --sport 53 -j ACCEPT


# Allow HTTP & HTTPS with rate limiting
iptables -I INPUT -p tcp --dport 80 -m state --state NEW -m recent --set
iptables -I INPUT -p tcp --dport 80 -m state --state NEW -m recent --update --seconds 60 --hitcount 10 -j DROP
iptables -I INPUT -p tcp --dport 443 -m state --state NEW -m recent --set
iptables -I INPUT -p tcp --dport 443 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 -j DROP
iptables -A INPUT -i eth0 -p tcp -m multiport --dports 80,443 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp -m multiport --sport 80,443 -j ACCEPT



# Allow FTP with Rate Limiting
iptables -I INPUT -p tcp --dport 20 -m state --state NEW -m recent --set
iptables -I INPUT -p tcp --dport 20 -m state --state NEW -m recent --update --seconds 60 --hitcount 3 -j DROP
iptables -I INPUT -p tcp --dport 21 -m state --state NEW -m recent --set
iptables -I INPUT -p tcp --dport 21 -m state --state NEW -m recent --update --seconds 60 --hitcount 3 -j DROP
iptables -A INPUT -p tcp -i eth0 -m multiport --dports 20,21 -j ACCEPT
iptables -A OUTPUT -p tcp -o eth0 -m multiport --sport 20,21 -j ACCEPT

# Allow Dynamic Ports (to update & upgrade or downloading required packages. Uncomment the follwing lines while needed)
iptables -A INPUT -p tcp -i eth0 --dport 1024:65535 -j ACCEPT
iptables -A OUTPUT -p tcp -o eth0 --sport 1024:65535 -j ACCEPT

iptables -A INPUT -p udp -i eth0 --dport 1024:65535 -j ACCEPT
iptables -A OUTPUT -p udp -o eth0 --sport 1024:65535 -j ACCEPT


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

How to install and configure Apache HTTPD web server in RHEL6

Step1: yum install httpd*

Step2:  [root@server ~]# cd /etc/httpd/conf
    [root@server ~]# vim httpd.conf
------------------------------------------------------------
Uncomment the line....

NameVirtualHost *:80

Add the the following lines at the bottom end for creating new virtual hosts....



<Directory /var/www/html/>
Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>


<VirtualHost *:80>
ServerAdmin mehedi@mehedi.com
ServerName mehedi.com
ServerAlias www.mehedi.com
DocumentRoot /var/www/html/mehedi.com
ErrorLog /var/log/httpd/mehedi.com/error.log
CustomLog /var/log/httpd/mehedi.com/access.log common
</VirtualHost>


<VirtualHost *:80>
ServerAdmin hasan@hasan.com
ServerName hasan.com
ServerAlias www.hasan.com
DocumentRoot /var/www/html/hasan.com
ErrorLog /var/log/httpd/hasan.com/error.log
CustomLog /var/log/httpd/hasan.com/access.log common
</VirtualHost>


<VirtualHost *:80>
ServerAdmin mithu@mithu.com
ServerName mithu.com
ServerAlias www.mithu.com
DocumentRoot /var/www/html/mithu.com
ErrorLog /var/log/httpd/mithu.com/error.log
CustomLog /var/log/httpd/mithu.com/access.log common
</VirtualHost>


or we can individually create each virtual host in /etc/httd/conf.d directory...

In this scenerio we also shoud uncomment the line NameVirtualHost *:80 in
[ /etc/httpd/conf/httpd.conf ] file.

Then,

[root@server ~]# cd /etc/httd/conf.d
[root@server conf.d]# vim mehedi.com.conf


--------------------------------
<VirtualHost *:80>
ServerAdmin mehedi@mehedi.com
ServerName mehedi.com
ServerAlias www.mehedi.com
DocumentRoot /var/www/html/mehedi.com
ErrorLog /var/log/httpd/mehedi.com/error.log
CustomLog /var/log/httpd/mehedi.com/access.log common
</VirtualHost>

--------------------------------


[root@server conf.d]# vim hasan.com.conf

---------------------------------
<VirtualHost *:80>
ServerAdmin hasan@hasan.com
ServerName hasan.com
ServerAlias www.hasan.com
DocumentRoot /var/www/html/hasan.com
ErrorLog /var/log/httpd/hasan.com/error.log
CustomLog /var/log/httpd/hasan.com/access.log common
</VirtualHost>
----------------------------------





How to configure ssl on Apache web server RHEL6? (HTTPS)
-----------------------------------------------
Step1: Install required packages...
------
[root@server ~]# yum install mod_ssl openssl

Step2: Generate a self-signed certificate....
------

# Generate private key
[root@server ~]# openssl genrsa -out ca.key 1024


# Generate CSR
[root@server ~]# openssl req -new -key ca.key -out ca.csr


# Generate Self Signed Key
[root@server ~]# openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt


# Copy the files to the correct locations
[root@server ~]# cp ca.crt /etc/pki/tls/certs
[root@server ~]# cp ca.key /etc/pki/tls/private/ca.key
[root@server ~]# cp ca.csr /etc/pki/tls/private/ca.csr



IF SELINUX IS ENABLED IN YOUR SYSTEM THEN PLEASE ENTER THE FOLLOWING COMMAND TO ALLOW THIS CERTIFICATE FILES....

[root@server ~]# restorecon -RvF /etc/pki



Step3:
-----

Now we need to update the Apache SSL configuration file
[root@server ~]# vim /etc/httpd/conf.d/ssl.conf


Change the paths to match where the Key file is stored...
--------------------------------------------
SSLCertificateFile /etc/pki/tls/certs/ca.crt
--------------------------------------------


Then set the correct path for the Certificate Key File a few lines below...
--------------------------------------------------
SSLCertificateKeyFile /etc/pki/tls/private/ca.key
--------------------------------------------------

Quit and save the file and then restart Apache

[root@server ~]# /etc/init.d/httpd restart or Service httpd restart





Step4: Setting up the virtual hosts
-----

[root@server ~]# vim /etc/httpd/conf/httpd.conf


----------------------------------------------------------------------
NameVirtualHost *:80
NameVirtualHost *:443


#Allow for HTTP Access for mehedi.com

<VirtualHost *:80>
ServerAdmin mehedi@mehedi.com
ServerName mehedi.com
ServerAlias www.mehedi.com
DocumentRoot /var/www/html/mehedi.com
ErrorLog /var/log/httpd/mehedi.com/error.log
CustomLog /var/log/httpd/mehedi.com/access.log common
</VirtualHost>

# Allow for HTTPS Access for mehedi.com

<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/ca.crt
SSLCertificateKeyFile /etc/pki/tls/private/ca.key
        <Directory /var/www/html/mehedi.com>
        AllowOverride All
        </Directory>
ServerAdmin mehedi@mehedi.com
ServerName mehedi.com
ServerAlias www.mehedi.com
DocumentRoot /var/www/html/mehedi.com
ErrorLog /var/log/httpd/mehedi.com/error.log
CustomLog /var/log/httpd/mehedi.com/access.log common
</VirtualHost>
---------------------------------------------------------------------

Step5: Configuring the firewall for allowing the secure port...(Https)
-----
[root@server ~]# vim /etc/sysconfig/iptables


--------------------------------------
-A INPUT -p tcp --dport 443 -j ACCEPT
--------------------------------------

[root@server ~]# service iptables restart

======================================================================
REDIRECT http request to https forcefully for a specific virtual host:
======================================================================


<VirtualHost *:80>
ServerAdmin mithu@mithu.com
ServerName mithu.com
ServerAlias www.mithu.com
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
#DocumentRoot /var/www/html/mithu.com
#ErrorLog /var/log/httpd/mithu.com/error.log
#CustomLog /var/log/httpd/mithu.com/access.log common
</VirtualHost>




# Allow HTTPS for mithu.com
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/ca.crt
SSLCertificateKeyFile /etc/pki/tls/private/ca.key
        <Directory /var/www/html/mithu.com>
        AllowOverride All
        </Directory>
ServerAdmin mithu@mithu.com
ServerName mithu.com
ServerAlias www.mithu.com
DocumentRoot /var/www/html/mithu.com
ErrorLog /var/log/httpd/mithu.com/error.log
CustomLog /var/log/httpd/mithu.com/access.log common
</VirtualHost>
---------------------------------------------------------------------------