Monday, September 29, 2008

Apache, Windows and the Zend Framework.

Working with the Zend Framework is a god send for most developers, however if you have never had to deal with apache's httpd configuration settings before, you maybe left scratching your head as to why the recommended directory structure for your zf based projects, isn't quite working as expected.

In this article, I will assume you already have apache and php installed and configured. If not, check out my article on installing apache, php and mysql. Ok, so lets get started shall we?

First things first however. Chances are, you have multiple directories in your web root for each of your projects, such as 'mywebsite1','mywebsite2','some-other-site-stuff', etc. The problem now lays in the fact that Zend recommends the following directory structure:



If you keep your default document root set to say C:/webdev/ for example, you can not simply go to http://localhost/my-zf-project/ and have it work as expected, without using htaccess files to redirect everything to the www (or public directory). Also, it makes it less secure to point directly at your sites root directory. Anybody interacting with your site should only ever be able to access whats in your www (public) directory.

The solution? Virtual hosts. To get this working my self, I tried several various approaches until I found one that actually worked the way I wanted it to. First off, open your apache/conf/httpd file and add the following lines: (note that I added them after the default DocumentRoot variable.

NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:\webdev"
</VirtualHost>

What this does is allow localhost to be used to access the webroot. Let's save the changes, restart apache and test our changes. Simply go to http://localhost/ once apache has started back up. If you do not have an index file in your web root, you will probably get a directory tree listing.

Ok so how do I configure virtual host for my zend framework projects? After the first virtual host block, add the following for each of your zf based projects: (note that this actually works for any site in which uses a simular directory structure)

<VirtualHost 127.0.0.1>
ServerName my-zf-site1.local
DocumentRoot "C:\webdev\
my-zf-site1\public"
</VirtualHost>


Now of course your going to need to modify the document root and servername to reflect your projects path and name. Were not done just yet though, we now have to tell our computer how to resolve the address. We do this by adding the following to our host file which is normally found in C:\windows\system32\drivers\etc. Once you have opened the host file, you should see the following line:

127.0.0.1 localhost

This is how localhost is bound to our local address by default. Underneath of this line, we'll add the name in which we set for the 'ServerName' in our virtual host settings. For example:

127.0.0.1 my-zf-site1.local

This will now bind http://my-zf-site1.local/ to our zf project's public folder. Everything else should work 'out of the box' from here on out now. Now to access your other non zf projects, you can simply do the following:

http://localhost/my-other-stuff/somefile.php

To work with additional zf based projects, simply repeat the steps above for each new project... add a new virtual host block to your httpd file, then modify the host file (example: 127.0.0.1 my-new-zf-site.local). Make sure to always restart apache for the changes to take effect.

Ok, I hope I have been able to help at least someone out there. Please feel free to post any questions or suggestions you have.

Thursday, September 25, 2008

Installing wamp...

This article is aimed at beginner php developers whom have never installed AMP (apache, mysql and php) before. I'll assume your using windows and you have a basic knowledge of what apache and mysql are. Anyway, I have found most of the other article on this same subject to have many short comings as developers often tend to assume things that can cause a beginner to go crazy if they know nothing about it.

So first of all, we need to download mysql, apache and mysql. Go to the following sites and grab the latest copies:

http://dev.mysql.com/downloads/mysql/5.0.html#downloads - for mysql, I have found the installer package usually works fine out of the box. Make sure to grab the correct package for your OS.

Now for apache go to http://httpd.apache.org/ and grab the 2.2x version for windows. Make sure to grab the MSI installer unless you have a C++ compiler setup and prefer to build everything for your self.

Last but not least, lets grab the latest version of php which can be found at: http://www.php.net/downloads.php. NOTICE: Do not download the installer for php as it will only cause you a massive headache, maybe even death. Download the zip package instead, as it comes with just about everything you'll ever need to work in php.

Ok, now lets install everything!
We'll start by installing apache. This is pretty straight forward but make sure to set the network and servername to localhost and set the email accordingly. You can also change the install path to c:\apache\ for easier navigation... I usually put apache, mysql and php all in the root.

Lets test apache!
If everything went accordingly to plan, you should now be able to hit http://localhost in your browser where you'll be greeted with a basic message "It works", if not, try restarted your computer and if this does not work, uninstall apache and start over, this is about the easiest thing to do.

Ok so next, lets install php. Since you downloaded the zip package, really all you need to do is extract everything to c:\php or whatever you would like.

Now to get php and apache talking to each other, we need to let apache know about php's existence. We do this by making a few changes in apache's httpd file which is located in the apache/conf/ directory.

First off, locate the LoadModule section and add the following line at the bottom:
LoadModule php5_module "c:/php/php5apache2_2.dll"
Make sure to edit the path accordingly to the directory in which you extracted php to.

Now, below this add these lines:

PHPIniDir "C:/php"

<Files "*.php">
AddHandler application/x-httpd-php .php
</Files>


Again, make sure to set the path correctly and also be sure to check that the path you set in PHPIniDir is the path in which the php.ini file can be found.

Additional changes in the httpd file:
You will also want to change these settings as well later on, so lets just to it now that we already have the httpd file open.

First, locate the following lines:
DocumentRoot "C:/Apache2.2/htdocs"
and
<Directory "C:/Apache2.2/htdocs">

You can change the paths to whatever directory you would like to keep your php files at or leave it as is. Note that you will not be able to access any any files from the web if they are not located in this directory or a child directory such as htdocs/mysite/.

One last thing, locate the following:
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>

And add index.php as well. For example:
DirectoryIndex index.html, index.php

This isn't required but makes our life a little easier. Now apache will automatically check for either index.html or index.php by default if we do no request an explicit file in the browser. For example, if we go to http://localhost/ apache will now know to check to see if we have an index.html or index.php file automatically.

Save the changes to the httpd file and lets test it!
Ok, so lets see if it's working. We'll do this by creating a very basic php page and placing it in our document root directory as described above. First however, restart apache to allow the changed you made to the httpd file to take effect.

In our document root, lets create a new php file. We'll call it index.php as I mentioned earlier. In this file, we'll add some simple code:

<?php>
echo phpinfo();
<?>

Save the file and lets test it. Open up your web browser and type the following: http://localhost/

You should now be show a page with all sorts of information regarding php and apache. If not, somethings wrong...

Now, we can install MySQL!
This is a pretty straight forward process. Start the mysql installer program and basically just leave everything as is. Once isntalled, make sure to run the instance configuration wizard which will get mysql ready to be used. Essentially you can simply leave everything at default. Make sure to set the root password to something you can remember later on.

Now we need to make a change to our environmental variables in windows. The quickest way to get there is by right click on 'My Computer' navigate to the advanced tab, click on the environmental variables button the locate the path settings. Double click on path and add the following at the end:

;C:\mysql-install-directory\libmySQL.dll

Warning! Make sure to add the ; at the beginning if theres not already one there or else you'll run into problems with your system!!

Now just hit apply and your ready to go to the next step.

One your done with, theres just a few more things you'll need to do in order to get everything talking to one another. First of all, go the the mysql directory and copy the libmysql.dll file over to the apache/bin directory. Next, we'll need to make a few changes to our php.ini file.

Go to the php directory and create a copy of the file named php.ini-recommended
and rename it to php.ini. Now, go ahead and open it so we can tell php about mysql. Search for the line that reads: ;extension=php_mysqli.php and remove the ";" for the extenion. Also, let's go ahead and remove the ; from ;extension=php_mcrypt.dll while were at it, as we'll need this availble in just a minute.

Once your done with this, go ahead and save the file then restart apache. If apache fails to start, there is a problem somewhere in your settings. If this happens, leave a comment and I'll give you a hand.

Note: mysql.com recommends using php_mysqli over the standard php_mysql.dll for php5 and later.

Now lets install PhpMyAdmin...
If your not sure what phpmyadmin is, don't worry because you soon will. First, lets grab the latest release from http://www.phpmyadmin.net/home_page/downloads.php. Once it's finished downloading, extract the files to your DocumentRoot directory as I mentioned earlier. I usually rename the folder to phpmyadmin for ease of use.

Now navigate through the files until you find the config.sample.inc file. Create a copy and rename it to config.inc. We need to now make just a few simple changes:

Set some random value in between ''...
$cfg['blowfish_secret'] = ''; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

example:

$cfg['blowfish_secret'] = 'as978a9s7234jk218kjl53'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

Next, remove the ; from the beginning of each of the following lines: (example: ;$cfg should be $cfg)

$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;

/* Select mysqli if your server has it */
$cfg['Servers'][$i]['extension'] = 'mysqli'; //make sure to add the 'i'

/* User for advanced features */
$cfg['Servers'][$i]['controluser'] = 'root'; //MySQL root user
$cfg['Servers'][$i]['controlpass'] = 'somepassword'; //change this to whatever you had set the password to when configuring mysql.

Thats pretty much about it. Now go ahead and save the changes then navigate to http://localhost/phpmyadmin or whatever you named the phpmyadmin folder to. You should now be confronted with a login screen. Use the same info you used in the config.inc file such as: root/somepassword for example.

If all goes well, you'll be logged in and can start creating databases and tables in the databases. PhpMyAdmin is an invaluable tool that I personally would not want to live without and it's a good way to make sure all of our work is working correctly!

If you are given any errors on the loggin screen, such as 'can not load mysqli.dll' or 'mcrypt.dll', it's because php isn't seeing them. Post a comment and I'll try to help you out.



Friday, August 29, 2008

C# and MySQL.

Ok I admit that I have been holding off on looking into using mysql with C# because for some strange reason, I thought it was going to ultra complicated. Well a recent project demanded that I figure it out so after a quick search I came accross MySQL Connect.net on dev.mysql.com. After a quick read through the documentation, I was able to establish a connection and get all of the database names in about 30 minutes and after about an hour or so, I had a pretty nice class starting to form. I realized quickly though that I couldn't treat data the same in C# as I would in php since php is typeless. With a bit of thinking and playing with the code, I figured out how to check the data type of each field, compare it to C#'s internal type names and store the data to an array accordingly. After I add some more to the class, I'll be putting it on source forge.