Friday, 19 September 2008

Apache Automatic Sites Using VirtualDocumentRoot

Right, so i hate having to go edit config files when i'm editing and changing loads of new sites every day. It's time consuming and it breaks up my development.


I have a Fedora linux development server setup. I have a staic ip address and I have bind setup and a domain (*.mydomain.com) with wildcard subdomains pointing to it. That allows me to make a new site on my subdomain per website i'm editing and then i can demonstrate that to clients without ftp-ing the site and uploading databases.


For example if i'm editing newsite.com, i download a copy to my dev server, then log into my dev server apache config file and add a virtualhost for newsite.com.mydomain.com I can then edit this version of the site, allow the client to approve the site, then ftp up my changes.


However, if you have over 20 of these sites it gets a bit confusing in your apache file, and it also breaks up development time when you edit the file, not to mention increasing the chance of you fucking things up.


Therefore, on a recent new install of my dev server, i decided to spend some time researching into apaches ability to rewrite stuff. I use mod_rewrite quite heavily and I wondered if it allows you to edit things before apache runs any php code. After much reading of peopletrying to do stupid nonsensical things with apache that have no real point... I happend across a way of doing this using the apache VirtualDocumentRoot. Here's an example;


<virtualhost>

ServerAlias *.mydomain.com

DocumentRoot /home/www/sites

VirtualDocumentRoot /home/www/sites/%-3+/

</virtualhost>


So, if you have <strong>sitea.com.mydomain.com</strong> this now maps to /home/www/sites/sitea.com/.


There is one small problem with this; If you use $_SERVER['DOCUMENT_ROOT'] in php, then this causes problems as document_root point to /home/www/sites. But, there's a work around for this. You have to make a php file to reset document_root and make that file run before anything else when the server requests this page. Here's an example of the php file;


<?

$location = explode(".",$_SERVER['HTTP_HOST']);

for($i=sizeof($location)-6; $i>=0; $i--) $lurl = '.'.$location[$i].$lurl;

$lurl = '/'.ltrim($lurl, '.');

$_SERVER[DOCUMENT_ROOT] .= $lurl;

?>


The above script resets the document_root variable correctly, Now you just need to edit the Apache config file;


<virtualhost>

ServerAlias *.mydomain.com

DocumentRoot /home/www/sites

VirtualDocumentRoot /home/www/sites/%-3+/

php_admin_value auto_prepend_file /home/www/set_path.php

</virtualhost>


Voila! You should now be able to make dev sites without having to worry about the apache config files ever again!

Monday, 9 June 2008

MySQL PHP Database Ripper

Recently, I've been working on several projects where direct database access has been an issue.

To get around this, i've made a php mysql Database Ripper. It reads the database schema and then prints out the table structure and information for each table.

Here's the source - Enjoy :)


/************************************

db connection

************************************/

$host = "localhost";

$user = "dbuser";

$pass = "dbpassword";

$dbname = "dbname";


$db = mysql_connect($host, $user, $pass) or die(mysql_error());

mysql_select_db($dbname, $db) or die(mysql_error());


/***********************************************************/


$ic = 0;


$lsql = "show tables;";

if ($result = mysql_query($lsql)){

while ($row = mysql_fetch_array($result, MYSQL_NUM)){

$mySQLArray[$ic++] = $row[0];

}

mysql_free_result($result);

}


foreach ($mySQLArray as $litem){

$lsql = "SHOW COLUMNS FROM ".$litem.";";

$lreturn .=("create table ".$litem."(\r\n");

$lcount = 0;

if ($result = mysql_query($lsql)){

while ($row = mysql_fetch_array($result, MYSQL_NUM)){

if ($lcount > 0) $lreturn .=(",\r\n");

$lreturn .=($row[0]." ".$row[1]);

if ($row[2] == "NO") $lreturn .=(" NOT NULL ");

if ($row[4] != "") $lreturn .=(" default '".$row[4]."' ");

if ($row[3] == "PRI") $lreturn .=(" primary key ");

$lreturn .= (" ".$row[5]." ");

$lcount++;

}

mysql_free_result($result);

}

$lreturn .=(") ENGINE=InnoDB DEFAULT CHARSET=latin1;\r\n\r\n");

$lsql = "select * FROM ".$litem.";";

if ($result = mysql_query($lsql)){

while ($row = mysql_fetch_array($result, MYSQL_NUM)){

$lreturn .= "insert into ".$litem." values (";

$lstr = "";

foreach($row as $lrow){

$lstr .= "'".mysql_real_escape_string($lrow) . "',";

}

$lreturn .= rtrim($lstr ,",").");\r\n";

}

mysql_free_result($result);

}

$lreturn .= ("\r\n\r\n\r\n");

}


echo($lreturn);


?>

Thursday, 31 January 2008

fedora yum mysql install guide

Here is a basic setup guide for installing mysql on a fedora systemusing the package manager yum.

First thing's first, you need to download and install mysql with the following command;

#> yum install mysql mysql-server mysql-devel

Next, open up your firewall on port 3306 - that's the port external connections (like mysql administrator) use to interact with the database.

#> vi /etc/sysconfig/iptables

Press i (to enter insert mode) and add these lines in an appropriate place;

-A RH-Firewall-1-INPUT -p tcp -m state -m tcp --dport 3306 --state NEW -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m state -m udp --dport 3306 --state NEW -j ACCEPT


Press esc to stop insert mode.
Type :wq

Next restart your firewall for the settings to take effect;

#> service iptables restart

Now navigate to the default mysql-server document directory, and edit the example large confige file for mysql;

#> cd /usr/share/doc/mysql-server-*/
#> vi my-large.cnf

Press esc
Type :w!/etc/my.cnf
Type :q

You have now overwritten your basic configuration file for mysql server. Once you have restarted the mysqld service (using #> service mysqld restart), these settings should now take effect and you should have the mysql service listening on port 3306.

However, you still require a mysql database user who can access the database from an external computer (using mysql administrator) and you also need to set your root password. For the sake of simplicity and to get you connected, i'm going to use the root user in mysql, however I would recomend you use different users to manage your server for security reasons.

Anyway, here's how to set the root password;

#> mysql -u root

use mysql
update user set Password=PASSWORD('yourpassword') where User like '%root%';
quit

Finally, restart the mysql service, for everything to take effect;

#> service mysqld restart

Voila, a workin mysql server which you can connect from external computers to port 3306 on your server.

Tuesday, 20 November 2007

Windows 2003 Shutdown Restart Command with Examples

Have you ever found loads of windows 2003 command examples, which say they work when the truth is they don't?

Well here are a couple of examples that do shutdown or restart the Server;

Shutdown Windows 2003



  • SHUTDOWN /s /d P:2:17



Restart Windows 2003



  • SHUTDOWN /r /d P:2:17

These two commands appear to work for me. visit this link (http://www.ss64.com/nt/shutdown.html) for further information.

Saturday, 29 September 2007

ie css bug fixes

Well, there are many problems that ie appears to have when it comes to css. Most problems can be fixed fairly easily when you know what the problem is, so I thought i'd list a few common fixes i use from time to time.


IE Position, Stretch or Browser Resize Problem


IE stretches too wide or after the browser is resized, ie doesn't reposition elements correctly.

ie fix

Apply position:relative to the body tag, the element and containing elements, which are effected by the bug. e.g. in your css file add

body,
#element_container,
#element{position:relative;}

You may also have to play around with width but the above code should work a treat!


IE White Space Bug

This usually occurs when you've been floating elements around each other. This manifests itself in in ie either as a big line space appears at the bottom of the elements or the elements don't stretch the page relative to each other.



IE White Space Fix

at the bottom of the elements, add a new block element like a paragraph tag with a style of clear:both; e.g.

You may also need to adjust the height, line height and the font size so the block element doesn't appear as a new line, e.g.;


Thursday, 30 August 2007

MySQL Password Reset Root

AAAAAAARRRRRRRRGGGGGGGGGHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

If you're reading this - chances are, you're an idiot. I know this as I am also an idiot as I installed a development server recently and forgot the root password for mysql. Yes I should have written the password down, no I didn't. here's the steps for resetting the root password on a working linux server;

1.) First login as root via ssh
2.) kill the mysqld service and all other mysql running processes e.g.
#> service mysqld stop
#> ps waux

ps waux gets a list of all running processes

use kill to stop any running mysql processes eg kill 6194 (kill pid).

3.) After stopping/killing mysqld, run the foillowing line;
#> mysqld_safe --skip-grant-tables &

4.) You are now logged into mysql safe mode. Cut and paste the following lines

mysql -u root mysql
UPDATE user SET password=PASSWORD("your_password") WHERE User="root";
FLUSH PRIVILEGES;
exit;


5.) then start up the mysqld service;

#> service mysqld start

That's it - all done. Now remember to write down ur password this time u idiot!

Tuesday, 28 August 2007

SELinux CURL Problem

skip to solution



Recently, I made a recursive xml sitemap generator tool, which quickly generates a sitemap, calculating how popular certain pages are related to how may linke they have internally. This helps make quick, good xml sitemaps that google likes as it follows the rough popularity that google sees of each page.

So I spent about 3 hours on this script - making it proper kick ass only to discover that when I upload the script from my dev server to my live server - the fucker doesn't work! not only that, but there's nothing reported in /var/log/messages.

After updating php-curl using yum and turning my firewall off with still no luck - I decided to get heavy handed and ask my server people to reinstall a newer version of fedora.

so afterspending another half a day setting up ftp/httpd/iptables/mysql/php/bind/etc. I uploaded the xmlsitemap generator and it still doesn't fucking work!

Oh where do I start? 'Livid' doesn't even come close - not only had i wasted the best part of a day updating my server - I didn't even manage to fix my sitemap script!!!

However, i did now have a message in /var/log/messages;

comm="httpd" dest=80 scontext=root:system_r:httpd_t:s0 tcontext=system_u:object_r:http_port_t:s0 tclass=tcp_socket

After much googling, I found this solution;

Solution

in the cli, type the following

#> getsebool -a
#> setsebool httpd_can_network_connect true


The first command displays the current SELinux settings, the second should change the SELinux setting so that httpd can access tinterweb.

I think this is one of thise annoying SELinux bugs that no-one really understands as there's not much on google about it - however i've found loads of people moaning about it and not getting it to work. Therefore I hope this helps someone!