Showing posts with label Apache. Show all posts
Showing posts with label Apache. Show all posts

Using Basic access authentication HTTP Auth With PHP

How to set up your App in a way that the browser prompts for username/password.


Problem

You probably don't want the whole world to see your development in progress. So you want to restrict access to a fortunate few using HTTP (basic) auth. In the fortrabbit PHP/FPM infrastructure neither PHP_AUTH_USER nor PHP_AUTH_PW are available - but you can hack around easily.


Solution

To utilize HTTP (basic) Auth, you need to add a directive in your .htaccess file, forwarding the Authorization header as an environment variable. This variable then contains the base64 encoded authentication data, which you can then decode to the PHP_AUTH_USER and PHP_AUTH_PW.

Modify .htaccess file


RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]


Decode auth header in PHP


// header was not provided

if (empty($_SERVER['REMOTE_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Need auth!';
    exit;
}

// extract user and pw from encoded auth data
list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(
    ':',
    base64_decode(substr($_SERVER['REMOTE_USER'], 6))
);


Default to utf8 encoding for MySQL 5.5

Been banging my head several times trying to set the correct configuration for setting utf8 as default encoding for my MySQL database. Searching the web shows old configuration that is not compatible with MySQL 5.5. However, I’ve found one that really works with 5.5.
Simply insert the following line into /etc/my.cnf (or similar) under mysqld section.
1
2
3
[mysqld]
...
character-set-server = utf8
Then restart the mysqld daemon.
My specs: Slackware 14.0 beta (32 bit), MySQL 5.5, kernel 3.2.23 and PHP 5.4.x.
Enjoy and share.

White-listing IP addresses for your Apache virtual hosts

I tried setting up some sites on public hosting company where I needed to set it up in a way where only specified IP addresses are allowed to access them. Below is what I did.

Basic config

Below is the basic configuration:
1
2
3
4
5
6
7
8
9
<Directory "/srv/httpd/htdocs/mydomain.com">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from 127.0.0.1
    Allow from xxx.xxx.xxx.xxx
    Allow from xxx.xxx.xxx.xxx
    Deny from all
</Directory>

Organized

Since I’m doing this IP blocking for many sites, it make sense to simplify the configuration so that I only need to edit a single list of IP that would apply to all sites. What I did is put the config on file and include it on each virtual host config. Below is the filename and the sample content.
File: /etc/httpd/block-world.conf
1
2
3
4
5
Order deny,allow
Allow from 127.0.0.1
Allow from xxx.xxx.xxx.xxx
Allow from xxx.xxx.xxx.xxx
Deny from all
Then for each virtual host, I include the file like this:
1
2
3
4
5
<Directory "/srv/httpd/htdocs/mydomain.com">
    Options Indexes FollowSymLinks
    AllowOverride All
    Include /etc/httpd/block-world.conf
</Directory>
That’s it. Share and enjoy.

Install SSL Certificate On wamps server (localhost)

How to enable SSl on Wamp Server(apache2) - HTTPS Configuration

In this article, I will show how to Configure/Enable SSL in Wamp Server.I have installed wampserver with apache 2.2.12 and php 5.3.5. I have to configure the Open SSL server.
How to Setup HTTPS SSL on WAMP Server for apache 2.2

1. Install WAMPServer
2. Open Control Panel
3. Click System >> Select Advanced System Setting >> Select Environment Variables.
4.Create new system variable OPENSSL_CONF:
Value: C:\wamp\bin\apache\apache2.2.12\conf\openssl.cnf

5. Create a SSL Certificate and Key for the Web Server
Open command prompt and Run the following commands:

>cd wamp\bin\apache\apache2.2.12\bin
>openssl genrsa -des3 -out server.key 1024
>copy server.key server.key.org
>openssl rsa -in server.key.org -out server.key
>openssl req -new -x509 -nodes -sha1 -days 365 -key server.key -out server.crt -config C:\wamp\bin\apache\apache2.2.12\conf\openssl.cnf

Create a folder c:\wamp\OpenSSL with the following subfolders:
i. certificats
ii. private

Copy server.cert, server.csr, server.key files

from C:\wamp\bin\apache\Apache2.2.12\bin to C:\wamp\OpenSSL\certificats folder

Copy server.key.org file from

C:\wamp\bin\apache\Apache2.2.12\bin to C:\wamp\OpenSSL\private folder

6. Open Php.ini file

Remove comment of following line:
extension=php_openssl.dll

OR

Left click the wampserver icon >> PHP >> php extensions >> click php_openssl

7. Open Httpd.conf file (C:\wamp\bin\apache\Apache2.2.12\conf\)

Remove comment of following lines:

LoadModule ssl_module modules/mod_ssl.so
LoadModule setenvif_module modules/mod_setenvif.so
Include conf/extra/httpd_ssl.conf

8.Open httpd-ssl.conf file(C:\wamp\bin\apache\Apache2.2.12\conf\extra\)

Find and replace follwing lines:

SSLMutex default

DocumentRoot "C:/wamp/www"
ErrorLog "C:/wamp/logs/ssl_ErrorLog.txt"
TransferLog "C:/wamp/logs/ssl_TransferLog.txt"
SSLCertificateFile "C:/wamp/OpenSSL/certificats/server.crt"
SSLCertificateKeyFile "C:/wamp/OpenSSL/certificats/server.key"
CustomLog "C:/wamp/logs/ssl_request.txt" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
Change Directory Tag:
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

9.Verify SSL/HTTPS

•Run httpd –t and make sure the syntax is OK.

•Restart Apache.

10.Check whether the WAMP server is running or not

Right click the wampserver icon >> Rsstart All Services
•If icon is partially red, it means no service is currently running
•If icon is White, that means all services are running.

11.Open https://localhost
12.Done.

I hope this article will help you to enable SSL on the Apache server.

Ohh Missed below Conf..

Found a solution:

Here are the instructions how to get rid of the SSLSessionCache message in XAMPP

1) Open file \xampp\apache\conf\httpd.conf

2) Somewhere in the LoadModule area add the following line:

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
3) Save the file

4) Now open file \xampp\apache\conf\extra\httpd-ssl.conf

5) at line 70 add the line

SSLSessionCache "shmcb:logs/ssl_scache(512000)"
6) Save the file

7) Restart Apache