Archive for the ‘LAMP’ Category

Mounting Windows shares on Linux

Wednesday, August 22nd, 2007
Recently I had the need to back up my cvs root directory that lives on our Linux dev server to a Windows server. It wasn't as straight forward as you might think, so I thought that I would share my solution. At first I tried to mount an NFS Windows share to my Linux box, but because we don't have an NIS server I am not able to authenticate my Linux account against my Windows account. Most of you developers out there that have to support yourselves will probably have this issue because most office IT infrastructures are Windows based which don't always play nice with your Linux dev box.

I ended up creating a folder on the Windows server and sharing it, although I did have to end up giving read/write permissions to Everybody. Our development environment is secure so I'm not worried about it, but otherwise this could get you in trouble with your sys admin. This is all the set-up that is necessary on the Windows side. Now lets flip over to the Linux box and set it up.

To mount the share you first have to edit the /ect/fstab file which contains information about the file systems available and their mount options. Add the following line to the file:
//<windows server>/<name of share> /mnt/<name of directory to mount share to> cifs uid=<linux username>,credentials=/etc/cifspw,domain=<name of domian> 0 0
The line has several space separated fields:
server/share, mount directory, filesystem type, mount options, dump, fsck

Next we need to create the directory /mnt/<name of directory to mount share to> which the mount command will need to be successful. Then the last thing before we can mount the Windows share is to create the credentials file that we specified in the fstab file. In the /etc directory create the cifpw file.
# vi cifspw
Then put your info in the cifspw file:
username=<windows server username>
password=<windows server password>
After that we are safe to mount the share:
# mount <name of directory to mount share to>/
Now we're ready to set-up the backup. This is a very simple daily rotating backup script.
#!/bin/sh

# source directory to backup
SRCDIR=<path to directory to be backed up>

# destination directory to backup to
DESTDIR=/mnt/<name of directory to mount share to>

# incremental directory
INCDIR=`date +%A`

# backup options
OPTS="-av --delete --force --ignore-errors"

# clears last week backup for current day
[ -d $HOME/emptydir ] || mkdir $HOME/emptydir
rsync --delete -a $HOME/emptydir/ $DESTDIR/$INCDIR/
rmdir $HOME/emptydir

# do the backup
rsync $OPTS $SRCDIR $DESTDIR/$INCDIR
Put this script in /etc/cron.daily and you're done.
You can find more information on mounting windows shares here.
And for additional rsync examples and the inspiration behind my backup script go here.

http://localhost

Friday, August 3rd, 2007
Having a local web server to test your web sites and applications is essential to your development environment. It is very easy to set-up numerous virtual hosts for each of your projects, which allows you to develop your interactive masterpieces in a close to real world implementation. There are actually many aspects of Flash development that will exhibit different behavior or not even work at all if not served from a valid domain and by golly localhost counts.

The worst thing that can happen to a developer is deploying your site or application to the clients server and having it not work and it's usually because it was tested only in Flash or by dragging your HTML file into a browser. All developers on my team are required to have an active local web server that they manage, which also makes it easy for me to track their development efforts in basically real-time. If you have a Mac, then you're cool, but also already have everything installed that you're going to need. Let's get started.

The easiest way to do this with minimal fuss and muss is to do it though your Systems Preferences -> Sharing Preference panel. Click on the Services tab and select Personal Web Sharing from the list. Click start and there you go, Apache 1.3.33 is up and running. Your web site's URL is displayed at the bottom, which is usually your computer's IP address. You can also type in http://localhost to get there. The actual directory where you place files to be viewed at this URL is /Library/WebServer/Documents/. Anything you place in this directory can be viewed in any web browser, even IE6, just as if it were on a real grown up web server. Only other computers on your LAN will be able to access this web site, by IP only - not localhost, but that's cool. You wouldn't want to use your dev or personal computer to serve live sites anyway.

Ok, so you've got a single site up and running. Now if you're like me you have a ton of projects that should be set-up this way. Time to get our hands dirty and dig into the Apache config file, enable the root user and a brief tutorial on managing your Apache install from the Terminal.

First off you have to enable your root user and give it a password if you haven't done so already. Only the root user has permission to edit the Apache config file, so let's start there. In your /Applications/Utilities folder you will find a program called NetInfo Manager. Go ahead and launch it. In the top menu bar click on Security -> Authenticate and type in your computer's admin password. Now the Enable Root User option will be available so go ahead and do that, you'll have to authenticate again, type in the new root password, click ok, then Security -> Deauthenticate for good measure and you're done.

Now we can make the necessary changes to the Apache config file and start/stop/restart the web server from the Terminal. Be extremely careful when using the Terminal as the root user, you can cause some very serious damage to your computer if not used with knowledge and caution. Open the terminal and type the following (do not type the $ or # characters, they represent the command line prompt):
$ su -
Password: <type in your new root password>
# vi /etc/httpd/httpd.conf
Now your in the text editor vi which I will not discuss here, but believe me you will love to hate it. You can also use pico instead of vi which is a bit easier to use, but your sys admin will laugh at you if caught doing so. For now use the arrow keys to move up and down through the config file. Since we're already here we might as well turn on PHP. Arrow down until you see the following line:
#LoadModule php4_module        libexec/httpd/libphp4.so
In the config file the # symbol represents a commented out line of text, same as using // in ActionScript or JavaScript. Right now you are in the browse mode of vi and are unable to edit text. Hit the i key to go into insert mode, move the cursor in front of the # symbol and hit the delete key to remove it. Hit the esc key to get back out of edit mode and now you have uncommented that line of text. Keep arrowing down until you find this:
#AddModule mod_php4.c
Do the same thing you just did to remove the # comment symbol. If at this point you feel you are struggling with vi, Google it and there are a million vi tutorials online. Now that you have edited the config file to enable PHP lets go ahead and save, then test to make sure it's going ok so far. To save in vi type :wq, this saves the file and quits vi. To just save and not quit type :w and to quit and not save type :q!

To make this take effect you need to restart Apache, but first lets check to make sure we haven't saved a bad config. At the command prompt type the following:
# apachectl configtest
If everything goes ok, you'll see the following output: Syntax OK. Now use the up arrow key to see your previous commands until you see:
# vi /etc/httpd/httpd.conf
then hit enter.

Now were back in the config file again. Scroll all the way to the bottom of the file where the VirtualHosts directive is. The best way to have multiple virtual hosts on your local computer, since you're probably not part of a domain or if so have no access to the Domain Controller, is to set them up on ports. A virtual host URL running on a port looks like this: http://localhost:9000. You may have see this before because there all kinds of services running on other ports on your computer. If you have Tomcat set-up, it runs on port 8080 as an example. It is generally safe to pick port numbers in the 9000 and 10000 range. The secret to this kind of set-up is to be sure to configure a port based virtual host for the default server which runs on port 80. Here is an example of a VirtualHost configuration that includes the default host and a custom one. All of your custom VirtualHost can have root directories anywhere on your computer.
Listen *:80
<virtualhost *:80>
  DocumentRoot /Library/WebServer/Documents
</virtualhost>

Listen *:9000
<virtualhost *:9000>
  DocumentRoot /Users/chris/projects/sample/build/bin
</virtualhost>
Now write, save, test the config and restart - done. You now have the power to create tons of individual web sites on your own computer. Check out the Mac DevCenter for more information on this subject.

[update: 10/1/2007]
For those of us still on Tiger (10.4.x) and want to upgrade to PHP5, check out Entropy's site for a super easy installer.

[update: 12/10/2007]
For those that are cool enough to be using Leopard here are some updates to the above information.

Enabling root user:
enable root user using utilities/Directory Utility
unlock to enable changes
go to edit-->enable root password
done.

httpd.conf configuration:
check out this blog post on Bust Out Solutions

WebOrb Standard, FMS developer, etc…
No excuse not to learn

Tuesday, July 24th, 2007
Did you know that some of the most amazing server-side software for Flash is available for free? Most sever software products are available to developers for free with a 5 IP connection limit, but are full featured. Even with this limitation there are more than enough connections to be useful for learning how to take advantage of protocols like AMF and RTMP in your Flash applications.

You’ll probably want to set up a server as well, so re-purpose that old PC and install Linux on it to provide a separate server environment from your local development environment. Red Hat offers the latest version of it’s OS as Fedora Core, it’s easy to install, set-up and manage without having to do to much command-line.

Get your free software here:
Fedora Core
Flash Media Server
LiveCycle Data Services ES Express
WebOrb

Now there are no more excuses for not knowing how to use these tools to make your Flash as awesome as it can be. Having the skills to uses these tools is what could get you hired on my team! Good luck and happy coding.

Trace statements for PHP

Tuesday, June 5th, 2007
If you're a Flash developer then you probably use trace() as an essential part of your development routine. Maybe you even wrote a custom logger application to get a peak into what's happening with your Flash application. Point is that you like to see what's going on behind the scenes.

Now you've moved on to the big leagues and started connecting Flash to PHP to add server-side functionality to your application, but how do you see what's happening with your PHP code? PHP has a little know feature called error_log() that lets you output messages to your web server's log file. By tailing your log files while running your application you essentially get trace statements from your PHP code! You can do this by opening your terminal if on a Mac or use something like cygwin on Windows, change directories to where the log files are and run the tail command with the follow flag.
Welcome to Darwin!
colossus:~ chris$ cd /var/log/httpd/
colossus:/var/log/httpd chris$ tail -f error_log
[Tue Jun  5 22:31:11 2007] [error] This is my trace statement from PHP!
Enjoy and happy PHP testing!

cURL - PHP’s little known buddy

Tuesday, May 22nd, 2007
cURL my be the coolest library available to PHP next to the GD Library. Basically it allows you to execute a variety of client-server operations across the web programmatically, which opens up a whole world of possibilities for PHP applications. Not long after the web got popular there was a lot of talk about all internet users eventually having personalized bots that carried out their bidding. Check stock quotes, get news, manage online personalities and shop for us based on our wants and needs. There is so much to do on the web these days it's a full-time job just keeping up with your MySpace or Facebook page let alone the things your tracking on eBay and anything else that you spend endless hours doing on the web, but that's where cURL comes in. Users need help in navigating through the tangled mess that is our web and we interactive developers need to build more tools to help internet users get what they need out of the the worlds most amazing tool ever bestowed upon mankind.

I am currently working on a such an application called ii and along with other ongoing projects will be sharing my experiences, code and other related whatnot - so keep tuned.

Ever had this PHP error:
require_once(SAM/php_sam.php)

Monday, May 14th, 2007
php require_once(SAM/php_sam.php)

It's errors like this that can be a pain, so I wanted to share the link that helped me fix it. Actually it turned out to be quite simple to fix, but I never really figured out why or what exactly happened. Basically the pages were loading although I wasn't getting any errors in my logs and nothing was showing up but a blank page in the browser. Maybe it was an option I picked when doing the install, which was PHP 5.2.2 with Apache 2.0.58 and MySQL 5.0.41.

Anyhoo, here's the link:
http://drupal.org/node/119623