PHP
Image via Wikipedia

If you’d like to develop PHP and MySQL web apps in Linux but you’re not sure how to get started then feel free to follow along with this blog post. For the most part, installation and configuration is simple and straightforward.

Though this article is directed toward users of Mandriva Linux (my Linux distribution of choice for a desktop / web-development PC), the same instructions can apply to many of the different Linux distributions including Fedora, Red Hat Enterprise Linux, Ubuntu, and Eeebuntu. For a huge list and up-to-date news of Linux distributions available to you, take a look at the Distrowatch news site.

The easiest way to install all of the software in the LAMP stack (Linux Apache MySQL and PHP) quickly is to do it using the command-line (also known as the console). Since many new users are uncomfortable with the command-line, feel free to do all of these installations graphically using the software installer from your respective distribution.

If you’d like to proceed using the graphical installer built into Mandriva Linux, use the “Install & Remove Software” icon located in the main menu.

To install apache, mysql, and php using the Mandriva command-line, follow these instructions:

  • Open up a terminal by clicking on the Mandriva star and clicking on Terminal
  • Type “su” and press enter (this will log you in as the administrator or “root” user)
  • Enter your root password
  • Type “urpmi apache php mysql phpmyadmin nano”
  • If asked which version of apache, select a stable version to install (likely the first choice)
  • If asked which version of php, select the apache module version (not CGI or CLI)
  • If asked for permission to install extra software that is required for proper operation of the LAMP stack, select “yes” and proceed

Once the software has been installed, you should be able to open up Firefox and navigate to http://localhost . This should bring up a screen that says “It works!”, meaning that apache has been properly installed.

For reference, Mandriva Linux puts your web files in the directory /var/www/html . Straight away you may not be able to access those folders with your regular user so feel free to change the permissions of the directory recursively by using the command

  • chown -R yourusername:yourusername /var/www/html

Note that this operation is definitely not secure if you plan on actually hosting your website on the live Internet using this computer, but for local development you should be okay. :) To learn more about file and directory permissions in Linux, take a look at the official documentation.

Before you are able to access your databases through phpmyadmin, you will need to set your MySQL root password using the following command (being sure to change NEWPASSWORD to a password of your choice):

  •  mysqladmin -u root password NEWPASSWORD

Using Firefox (or whatever browser you normally use) navigate to http://localhost/phpmyadmin . Log into MySQL with your “root” user and the password you just entered into the command-line. This should give you access to your MySQL databases. For more information on how to use phpmyadmin, take a look at the official website.

Let’s create a small Hello World PHP web application by navigating to our web directory and creating it. Use the following commands to achieve this:

  • cd /var/www/html
  • nano test.php

In the editor screen that appears, enter

  • <?php echo "Hello World!"; ?>

Press CTRL-X and save the file before quitting. You should now be able to navigate to http://localhost/test.php and see your hello world application :)

Hopefully this has given you enough information to get you up and running. Please feel free to post comments if you’ve run into problems and hopefully I or another person in the community will be able to help you out.

Have fun with PHP on Linux!

Reblog this post [with Zemanta]
Tagged with:
 

Singleton To The Rescue, A Quick Update

Things have been crazy busy lately with work (both the full-time job as well as the freelance work I’ve been doing) and as such I haven’t had a chance to do a full blog post in a while. I appreciate all the great feedback I’ve been getting about the blog, though — I’m glad it’s helped a few of you already (and I’m not even to 100 posts yet!). Keep checking back as I’ll likely be putting up a few solid articles in February (possibly the continuation of my Subversion article!)

I just wanted to quickly post a link to a totally fantastic article I found about using the Singleton pattern for a database object. This is one of those times as a blogger where instead of re-inventing the wheel by writing about a similar subject you’re just better off setting your users up with a link to the best of the best and let it stand on its own. If you’re interested in learning more about the Singleton pattern, look no further.

I’m using a database class based off of the code in the article for a few of my projects currently, and I have to say, it works great. Let me know what you think!

Tagged with:
 

Aptana Color Themes

Aptana Studio Rules

Aptana Studio Rules

When I develop at home and at work I use Aptana Studio. I purchased a license for myself at home and I use the community edition (read: free version) at work. It’s a wonderful editor with a ton of features and great community. One thing it lacks, however, is a good set of themes, out-of-the-box.

So what, you ask? Just go and download some third-party custom themes from the web!

Pishaw! It’s not that easy. I’ve done a few Google searches but not a whole lot turns up, to be honest. And that’s a shame because I bet a lot of developers are in the same boat as me.

I’ve been tinkering with themes over the past few weeks and I’ve decided to start releasing some on my site for everyone to use. The themes page will grow over time as I continue to release themes. I’d appreciate any feedback you have. The Aptana Themes page is not only for my themes, they also include some of the best themes I’ve found around the web. If you have a theme you’d like me to put up, send it to me and I’ll do just that.

Anyway, without further ado, I present the Aptana Themes page!

Tagged with:
 

When dealing with file paths in your scripts, it’s best to not specify the absolute path. Keep it simple and specify a relative path. What’s the difference? Check out the example below:

Example: In your index.php file you are using the php include() statement to include a file named “includeme.php” located in the “includes” directory. Two methods of doing this are found below:

Method 1: Relative paths: include(“includes/includeme.php”);
Method 2: Absolute paths: include(“/var/www/html/project/trunk/includes/includeme.php”);

Notice how the first include() statement refers to a file in the “includes” directory, one directory down from the file calling it (in this case index.php). In the second, the include() statement has the entire path to the file right from root.

Why choose option 1 over option 2? Well …

#1. It makes server management easier, faster, and cheaper.

During the lifetime of a web server, the web files may need to be moved and/or directories renamed. Imagine what would happen to your website if you were to rename the “project” directory to “old_project”? You’d have to go through all of your scripts and rename the project directories in the include() statements in all of your scripts.

Using method 1, everything keeps on working with no updates to your scripts.

#2. It makes moving your site to another server easier, faster, and cheaper.

If while maintaining your website you end up switching which VPS company you host with or you change the operating system on your server, you could find yourself in a position where instead of having your web files located in the /var/www directory like most Linux distributions you have them located in the /usr/local/www directory like some BSD installations.

In this case you could use symlinks to forward requests of /var/www to /usr/local/www, change your apache configuration to put your web files to /var/www, or update all of your scripts so that they have the correct path. For now.

All of those things feel like hacks.

Best thing to do is just to code from the start with relative paths, thereby avoiding all of these path problems.

Tagged with:
 

Arrays? Who need’s em?

If you’re into developing websites chances are there will be sometime during your life/career when you’ll need to have users enter data into a HTML form but you have no idea how many of a certain variable they’re going to be sending in or how much data they’re going to fill in of the same type.

An example would be a HTML form that has three input boxes, one for each of your friend’s names (first and last). You are able to enter between 1 and 3 friends into the boxes. For such an example your HTML form may look something like this:

<form method="post" action="">
  <p>Enter your friend's names (first, last):</p>
  <input maxlength="30" name="friend1" size="30" type="text" />
  <input maxlength="30" name="friend2" size="30" type="text" />
  <input maxlength="30" name="friend3" size="30" type="text" />
  <input type="submit" value="Submit" />
</form>

That all looks fairly normal. Keep in mind this is a simple example. But, what about if you’ve got the option to enter 10 friends. What about 20? You might have to re-work your form if you want to enter 20 friends. Any more than 10 and you might want to look at alternatives like importing from XML or CSV.

Let’s say you’ve got the option to enter up to 10 names. Your increasingly ugly form would then look like this:

<form method="post" action="">
  <p>Enter your friend's names (first, last):</p>
  <input maxlength="30" name="friend1" size="30" type="text" />
  <input maxlength="30" name="friend2" size="30" type="text" />
  <input maxlength="30" name="friend3" size="30" type="text" />
  <input maxlength="30" name="friend4" size="30" type="text" />
  <input maxlength="30" name="friend5" size="30" type="text" />
  <input maxlength="30" name="friend6" size="30" type="text" />
  <input maxlength="30" name="friend7" size="30" type="text" />
  <input maxlength="30" name="friend8" size="30" type="text" />
  <input maxlength="30" name="friend9" size="30" type="text" />
  <input maxlength="30" name="friend10" size="30" type="text" />
  <input type="submit" value="Submit" />
</form>

When you submit this form then you’ve got to check each input box to first ensure they entered a variable and then check to see what that variable is. With so many input boxes to check from it becomes a repetitive and arduous task.

Example PHP code:


// Good God.. okay let's start this horrible task
if ($_POST['friend1']) {
  doSomething($_POST['friend1']);
} else {
  complain();
}
if ($_POST['friend2']) {
  ...
} else {
  complain();
}
if ($_POST['friend3']) {
  ...
} else {
  ...
}
...
...
// There's got to be a better way!

Save time with arrays.

Using PHP and HTML, the HTML form code can be re-written to have the server create a PHP array of your friends’ names, like this:

HTML Form:

<form method="post" action="">
  <p>Enter your friend's names (first, last):</p>
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input maxlength="30" name="friend[]" size="30" type="text" />
  <input type="submit" value="Submit" />
</form>

Server-Side PHP:

// Loop through the friend array
foreach ($_POST['friend'] as $value) {
  // Do something with each valid friend entry ...
  if ($value) {
    echo $value."<br />";
     ...
  }
}

Let’s walk through that PHP code.

It’s fairly simple. We walk through the php $_POST['friend'] array that we retrieved from the HTML form using foreach and for all of the entries on the form that the user typed in we do something with (in this case we simply echo them to the screen).

This simple foreach loop will save you time and the good part is that you can have any number of friends on your form (I’m sure there’s a maximum somewhere though.. 256 maybe?) and this block of code will still work.

In the next article I’ll show you how to do the same thing with ColdFusion.

Tagged with: