{"id":346,"date":"2010-09-15T19:07:33","date_gmt":"2010-09-15T23:07:33","guid":{"rendered":"http:\/\/www.xavignu.com\/?p=346"},"modified":"2010-09-25T06:01:23","modified_gmt":"2010-09-25T10:01:23","slug":"apache-mysql-authentication","status":"publish","type":"post","link":"https:\/\/www.xavignu.com\/?p=346","title":{"rendered":"Apache MySQL authentication"},"content":{"rendered":"<p>Lets explain how to configure <a href=\"http:\/\/www.apache.org\" target=\"_blank\">Apache<\/a> <a href=\"http:\/\/www.mysql.com\" target=\"_blank\">MySQL<\/a> authentication. We will suppose apache web server is already up and running on the server.<br \/>\nFirst we need to install auth_mysql module.<\/p>\n<pre>\r\n$ sudo aptitude search libapache2-mod-auth-mysql\r\np   libapache2-mod-auth-mysql       - Apache 2 module for MySQL authentication  \r\n$ sudo aptitude install libapache2-mod-auth-mysql\r\n<\/pre>\n<p><!--more--><br \/>\nOnce installed we create the database where we will authenticate. Copy the following script and save it as auth.sql. Substitute &lt;user&gt;, &lt;passwd&gt; and &lt;group&gt; for the corresponding user, password and group to authenticate.<br \/>\n<code><br \/>\ngrant all on auth.* to auth_user@localhost identified by '&lt;passwd&gt;';<br \/>\nflush privileges;<br \/>\ncreate database auth;<br \/>\nuse auth;<br \/>\nCREATE TABLE `clients` (<br \/>\n`username` varchar(25) NOT NULL default '',<br \/>\n`passwd` varchar(25) NOT NULL default '',<br \/>\n`groups` varchar(25) NOT NULL default '',<br \/>\nPRIMARY KEY (`username`),<br \/>\nKEY `groups` (`groups`)<br \/>\n) ENGINE=MyISAM DEFAULT CHARSET=latin1;<br \/>\nINSERT INTO `clients` VALUES ('&lt;user&gt;', '&lt;passwd&gt;', '&lt;group&gt;');<br \/>\n<\/code><br \/>\nNow execute the script.<\/p>\n<pre>\r\n$ mysql -u root -p &lt; \/tmp\/auth.sql\r\n<\/pre>\n<p>Check if everything went fine.<\/p>\n<pre>\r\nmysql&gt; show databases like 'auth';\r\n+-----------------+\r\n| Database (auth) |\r\n+-----------------+\r\n| auth            | \r\n+-----------------+\r\n1 row in set (0.00 sec)\r\n\r\nmysql&gt;\r\nmysql&gt; SELECT * FROM auth.clients;\r\n+----------+--------+--------+\r\n| username | passwd | groups |\r\n+----------+--------+--------+\r\n| dave     | passwd   | group   | \r\n+----------+--------+--------+\r\n1 row in set (0.00 sec)\r\n\r\nmysql&gt; \r\n<\/pre>\n<p>Once this is done we need to modify apache2.conf or the virtual host config file. Add the following. Substitute &lt;directory&gt; and &lt;passwd&gt; with your credentials.<br \/>\n<code><br \/>\n&lt;Directory \"&lt;directory&gt;\"&gt;<br \/>\n        AuthType Basic<br \/>\n        AuthName \"Please provide user and password.\"<br \/>\n        AuthMySQL on<br \/>\n        AuthBasicAuthoritative Off<br \/>\n        Auth_MySQL_Authoritative on<br \/>\n        Auth_MySQL_Host localhost<br \/>\n        Auth_MySQL_User auth_user<br \/>\n        Auth_MySQL_Password &lt;passwd&gt;<br \/>\n        AuthMySQL_DB auth<br \/>\n        AuthMySQL_Password_Table clients<br \/>\n        AuthMySQL_Username_Field username<br \/>\n        AuthMySQL_Password_Field passwd<br \/>\n        AuthMySQL_Empty_Passwords off<br \/>\n        AuthMySQL_Encryption_Types MySQL Plaintext Crypt_DES<br \/>\n        require valid-user<br \/>\n        Options +Indexes FollowSymLinks MultiViews<br \/>\n        AllowOverride AuthConfig Options FileInfo Limit<br \/>\n        Order allow,deny<br \/>\n        Allow from all<br \/>\n&lt;\/Directory&gt;<br \/>\n<\/code><br \/>\nKeep AuthBasicAuthoritative Off or else you will get errors in apache error log file like the following.<\/p>\n<pre>[Wed Sep 15 18:40:04 2010] [error] Internal error: pcfg_openfile() called with NULL filename\r\n[Wed Sep 15 18:40:04 2010] [error] [client 12.14.182.34] (9)Bad file descriptor: Could not open password file: (null)\r\n[Wed Sep 15 18:40:14 2010] [error] Internal error: pcfg_openfile() called with NULL filename\r\n[Wed Sep 15 18:40:14 2010] [error] [client 12.14.182.34] (9)Bad file descriptor: Could not open password file: (null)\r\n<\/pre>\n<p>After this restart apache and you should be ready to go. You should see a login screen like the one below.<\/p>\n<p><a href=\"http:\/\/www.xavignu.com\/wp-content\/uploads\/2010\/09\/mysqlauth.png\"><img src=\"http:\/\/www.xavignu.com\/wp-content\/uploads\/2010\/09\/mysqlauth.png\" alt=\"\" title=\"mysqlauth\" width=\"500\" class=\"alignnone size-full wp-image-352\" \/><\/a><\/p>\n<p>Suggestions are always welcome.<\/p>\n<p>References:<\/p>\n<ol>\n<li><a href=\"http:\/\/www.howtoforge.com\/mod_auth_mysql_apache2_debian\" target=\"blank\">http:\/\/www.howtoforge.com\/mod_auth_mysql_apache2_debian<\/a><\/li>\n<li><a href=\"http:\/\/www.yolinux.com\/TUTORIALS\/LinuxTutorialApacheAddingLoginSiteProtection.html\" target=\"blank\">http:\/\/www.yolinux.com\/TUTORIALS\/LinuxTutorialApacheAddingLoginSiteProtection.html<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Lets explain how to configure Apache MySQL authentication. We will suppose apache web server is already up and running on the server. First we need to install auth_mysql module. $ sudo aptitude search libapache2-mod-auth-mysql p libapache2-mod-auth-mysql &#8211; Apache 2 module for MySQL authentication $ sudo aptitude install libapache2-mod-auth-mysql<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[3],"tags":[20,6,51,23,70],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/pTQgt-5A","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/346"}],"collection":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=346"}],"version-history":[{"count":0,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xavignu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}