Develop Locally With Linux, Apache, MySQL, and PHP

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!

A Month With Mandriva

Well, it’s been just over a month since I made the switch to Linux from Windows. My distribution of choice for desktop PCs has always been the fantastic Mandriva Linux. Available for free with plenty of included software (Open Office suite, the Firefox web browser, Kopete messenger, Amarok media player, and much more), it’s always done the trick and looks wonderful doing so.

I have two physical hard drives in my PC. The first one is mounted ‘/’ for all my system files and programs. The second drive is my ‘/home’ directory, where all of my documents are kept. All of the system files are kept entirely separate from my documents.This sort of division is done even with one single hard drive automatically by Mandriva so that if I ever need to format or upgrade the operating system I don’t lose any of my pictures, movies, or music, ever.

Me playing Morrowind in Linux
Me playing Morrowind in Linux

Life without Windows is certainly possible. I’m living proof. And the stuff I use my computer for is likely more intense than your average Joe since I’m a web developer. All of the required software that I use on a daily basis is available and runs great in Linux.

All of my games worked out-of-the-box using the Windows games and software emulator* (Read more about the Wine project). I’ve included a screenshot of me playing Morrowind. It runs great. My girlfriend and I played through Max Payne on this PC, as well, and we’re a quarter of the way through the Quest for Glory 2 remake (which is a lot of fun, by the way) on my other Mandriva Linux PC (our media center).

If you’re considering running Linux or if you’ve heard about it and are curious, give Mandriva Linux One a try. It’s pretty simple: You download it and burn it onto a blank CDR. Reboot with the disc in the drive and you can use it right off the disc without actually installing it. If you like it, go ahead and install it. Otherwise, just take the disc out and reboot — nothing has been changed on your computer.

For more information about Linux, try reading some of these sites:

* I realize Wine is technically not an emulator, but when explaining what it does it helps to use that term.