Friday, April 19, 2013

Create .htaccess & .htpasswd files for your web projects


Make secure your web Project in Apache using .htaccess

Create a filename .htaccess and put following content in it.

AuthName "Restricted Area"  AuthType Basic  AuthUserFile /opt/lampp/.htpasswd  AuthGroupFile /dev/null  require valid-user

Create a filename .htpasswd and generate a password using following site.
http://www.htaccesstools.com/htpasswd-generator/

Enter Username and Password. Click on create.
Now copy the generated line and paste into your .htpasswd file.
The .htpasswd file contains only this, a username:encrypted password

root:$apr1$7I58.7SX$zkt0..fl0ZBwU3ShGpDjZ1

a) In .htaccess file, AuthUserFile is path of the directory, where .htpasswd file is placed.
In above example I am trying to make my phpmyadmin password protected that's why I have put my .htpasswd file in parent directory and .htaccess file in /opt/lampp/phpmyadmin.
Now when I try to access http://localhost/phpmyadmin , It asks password to open the phpmyadmin.

b) Put the .htpasswd file in parent directory of the password protected directory and place the .htaccess in directory which you want to protect.

c) In above example username is root & password is linux.

d) It asks for password when directory is accessed through browser.

e) Once you put the log-in details and open your website, you have to close the browser completely to see the log-in box again. It does not ask the password every time you refresh the page.


To protect a single file instead of whole directory


If you do not want to protect whole directory instead of you want to protect single file.
Example : If you or anyone in LAN access your phpinfo.php , you want it should ask password.
In this case the contents of .htaccess file is
AuthName "Name of Page" AuthType Basic AuthUserFile /opt/lampp/htdocs/.htpasswd <Files "phpinfo.php"> require valid-user </Files>

Put the .htaccess in the directory where file you want to  protect is placed.

In this example, I want to protect my phpinfo.php. This file is placed in /opt/lampp/htdocs/xampp that's why I have put the .htaccess in xampp directory and .htpasswd in parent directory of xampp.

It will ask for password when user tries to access that file using browser.
http://localhost/xampp/phpinfo.php
But it will not ask for password if you try to access the directory.

Saturday, April 13, 2013

XAMPP: Another web server daemon is already running / XAMPP: Another MySQL daemon is already running.


XAMPP: Another web server daemon is already running.
XAMPP: Another MySQL daemon is already running.



There are two ways to remove these errors.
1) Stop the running services
$ sudo service service-name stop
If you are using Debian or Ubuntu :
$ sudo service apache2 stop $ sudo service mysql stop
If you are using CentOS , RHEL or Fedora :
$ sudo service httpd stop $ sudo service mysqld stop

2) Kill the running services

Now here is the second method.

XAMPP-web server works on port 80 and XAMPP-MySQL works on port 3306.

We have to find the services of respective ports and kill them.

Run following command in terminal
$ sudo netstat -lnp | grep -e :80 -e :3306

It will show you all processes of port 80 & 3306 like this
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15268/apache2   
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      14699/mysqld 

Now kill them using
$ sudo killall -9 process-name

It shows the process name beside pid (process id). In above example pid is 15268 & process-name is apache2. Another pid is 14699 & process-name is mysqld.

To kill the above processes, Run
$ sudo killall -9 apache2 $ sudo killall -9 mysqld
Now if you start XAMPP, it does not give process running error.

But if it still gives, kill the running process again.

You can kill the processes using pid too.
$ sudo kill -9 pid

To kill the above processes, Run
$ sudo kill -9 15268 $ sudo kill -9 14699

#2002 - The server is not responding (or the local MySQL server's socket is not correctly configured)

phpMyAdmin is giving error
#2002 - The server is not responding (or the local MySQL server's socket is not correctly configured)

Probably there are two mysql running. Open the terminal and run
$ service mysql stop
or
$ service mysqld stop
(according to your Linux distribution)
Now restart lampp and try to open phpMyAdmin.

Make sure you have following code in your /opt/lampp/phpmyadmin/config.inc.php
$cfg['Servers'][$i]['connect_type'] = 'tcp';
If not, add above line in config.inc.php and Restart mysql.

If you are still facing the problem. Add this line in your config.inc.php too.
$cfg['Servers'][$i]['socket'] = '/var/run/mysql/mysql.sock';
and Restart mysql.

If you are still facing the problem, Make following changes in your config.inc.php

change 
$cfg['Servers'][$i]['host'] = 'localhost';
to 
$cfg['Servers'][$i]['host'] = '127.0.0.1';
Now restart mysql and open phpmyadmin.

If you are still facing the problem, check permission of the file /opt/lampp/phpmyadmin/config.inc.php, it should not be less than 755.
Set it to 755.
Run following command.
$ sudo chmod 755 /opt/lampp/phpmyadmin/config.inc.php
Now restart LAMPP and try to open phpmyadmin again.
http://localhost/phpmyadmin