Your message has been sent.
This article has been saved to your account.
Go to my account
This article has been emailed to your Kindle.
Send this article
In this article by Alfonso Romero, we will learn how to configure NetBeans, VirtualBox and the Turnkey LAMP Appliance to develop complex PHP applications in a virtual environment. This article explains the process of creating a virtual machine, configuring the NetBeans PHP IDE, and managing a WordPress project with NetBeans in a step wise manner.
A couple of days ago, a client asked me for an easy way to develop PHP applications. Naturally, I told him the best way would be to pay me for doing it! Just kidding…
"Use the NetBeans IDE", I said to him, almost automatically. "But… isn’t NetBeans something related to Java?" he answered back, with a startled grin on his face.
"My dear friend, you can use NetBeans to develop software on Java, PHP, C++, and almost any other programming language I’ve heard of! You could even use it to work on a WordPress live Web site!" I said to him triumphantly. And that’s when a small light bulb lit up inside my mind... I introduced him to VirtualBox and the world of virtual machines, the Turnkey Linux LAMP appliance, and how to make all these software applications collaborate between each other. And that’s how this article was born.
So now, my dear readers, let’s get on with the action! Oh, and feel free to skip any section you don’t need, ok?
Download and Install VirtualBox
Go to the VirtualBox Web site and download the most recent version: http://download.virtualbox.org/virtualbox/3.0.4/VirtualBox-3.0.4-50677-Win.exe
After downloading VirtualBox, install it with all the default options (remember to register with Sun!).
Download the Turnkey LAMP appliance
Go to the Turnkey Linux Web site and download the LAMP appliance: http://www.turnkeylinux.org/download?file=turnkey-lamp-2009.02-hardy-x86.iso .
Download the NetBeans PHP IDE
Create a virtual machine
Open VirtualBox and click on the New button to create a virtual machine (VM). In the following screenshot I used the VirtualPHPDev name for my VM, but you can use whatever you want; just don’t use strange characters or signs:
Choose Linux and Ubuntu as the Operating System and Version, respectively. Click on Next to continue. Leave the default values for RAM and hard disk. When finished, click on the VirtualBox Settings button to open your virtual machine’s settings dialog, select the CD/DVD-ROM category, enable the Mount CD/DVD drive option, select the ISO Image File button and then click on the Invoke Virtual Media Manager button .
Click on the Add button in the Virtual Media Manager to open up the Select a CD/DVD-ROM disk image file, go to the directory where you downloaded the Turnkey LAMP appliance and double-click on it to add it to the Virtual Media Manager:
Click on Select to close the Virtual Media Manager, and then click on OK to exit the Settings dialog. Now you can click on the Start button to start your LAMP virtual machine:
Select the Install to hard disk option in the Turnkey Linux menu screen and press Enter to start installing the LAMP appliance on your virtual machine. Eventually the following screen will show up:
Select the Guided option and press Enter to continue. The installer will ask if you want to save changes to disk. Select Yes and press Enter. The installer will start the disk formatting process and then you’ll get to the root password screen. Type the password twice for the root user, and then do the same for the MySQL user. The installer will continue and, after a while, the following installation completion screen will show up:
Select No, press Enter and then close the virtual machine typing shutdown -r now at the system prompt. You’ll return to the VirtualBox main screen. With your LAMP virtual machine selected, click on the Settings button to open the settings dialog box. Select the CD/DVD-ROM category and disable the Mount CD/DVD drive option. Then select the Network category, change the Attached to: option to Bridged Adapter and click on OK to continue:
You can start your LAMP virtual machine now. When ready, the following screen will appear:
The Bridged Adapter mode lets your virtual machine act as if it were another PC on the LAN, and consequently, it will have a different IP address. Write down the IP address shown in the Turnkey Linux Configuration Console screen, because you’ll need it to configure the NetBeans IDE later. You can minimize the LAMP virtual machine window now.
eBook Price: $20.99
Book Price: $34.99
Configuring the NetBeans PHP IDE
Now you can open your NetBeans IDE and create a new project. Choose File | New Project… from the main menu, select the PHP Application project and click on Next to continue:
Choose a name for your project and click on Next to continue:
On the next screen, select the Remote Web Site option on the Run As: field, replace localhost with the IP address of your LAMP virtual machine on the Project URL field and click on the Manage button to configure the remote connection for your virtual LAMP appliance.
The Create New Connection dialog will show up. Choose a name for your remote connection, select SFTP as the Connection Type and click on OK to continue:
On the Manage Remote Connections dialog, type the IP address of your virtual machine in the Host Name field, type root in the User Name field, type the password you assigned to your virtual machine’s root account in the Password field and click on OK to continue:
You’ll return to the New PHP Project dialog box. Click on Finish to continue:
NetBeans will return to the main screen and the new project will open up. To test the configuration, type:- echo "Testing my LAMP virtual appliance"; in the code window, and click on the Run Project button:
The Remote Upload dialog will appear next with a list of the files that will be uploaded to your LAMP virtual server. Click on OK to continue:
A Warning dialog will appear to tell you about the authenticity of your virtual machine host. Click on Yes to continue:
NetBeans will upload the PHP file to your LAMP virtual server and your Web browser will open up, showing the PHP code you wrote:
Now you can start developing PHP apps with NetBeans and your LAMP virtual server! In the next section I’ll show you how you can use NetBeans to manage PHP code in a WordPress live Web site.
eBook Price: $20.99
Book Price: $34.99
Managing a WordPress project with NetBeans
And now let’s squeeze some of the real juice out of NetBeans! In the next exercise I’ll show you how to manage a complex project: Download a copy of WordPress from http://wordpress.org/latest.zip, unzip it in your hard drive (I used the default Desktop/wordpress-2.8.4 directory when downloading from Mozilla Firefox) and then create a new project in NetBeans. This time use the PHP Application with Existing Sources option, because you’ll import all the WordPress files into your new project:
Click on Next to continue. On the next dialog you need to click on the Browse button to go and select the directory where you unzipped WordPress:
Click on Open to continue. NetBeans will take you back to the New PHP Project dialog. Click on Next to continue, select the Remote Web Site option on the Run As: field and replace localhost with the IP address of your LAMP virtual machine on the Project URL field. You can use the Turnkey LAMP remote connection you created before. Click on Finish to continue:
NetBeans will create your new project and the index.php page will show up. Before using your WordPress installation, you need to create a database in your LAMP virtual appliance. Open your Web browser and type https://ipaddress-of-your-appliance:12322 to open PHPMyAdmin (remember to replace the IP address of your virtual machine).
NOTE: If you receive a Secure Connection Failed error like the one shown below, just click on the Add Exception… button:
The Add Security Exception dialog will appear next. Click on the Get Certificate button and then on Confirm Security Exception to continue:
Now the Welcome to PHPMyAdmin screen will show up. Type: - root as the username and enter the password you assigned to your MySQL root user when installing the Turnkey LAMP appliance. Now you can create the database for your WordPress project. I chose the WordPressVB name for my database, as shown below:
Close PHPMyAdmin and go back to NetBeans. You need to create a wp-config.php file before uploading the WordPress project. Open the wp-config-sample.php and edit the DB_NAME, DB_USER and DB_PASSWORD lines with the correct values for your WordPress project:
Select File | Save As… on the main menu, replace the wp-config-sample.php name with wp-config.php and click on Save to create the wp-config.php file.
Now you can upload your WordPress project. Click on the Run Project button and NetBeans will start to upload WordPress to your virtual appliance. The Remote Upload dialog will show you a list of the files that will be uploaded to your LAMP virtual server. The first time it will upload all WordPress files, so be patient. Click on OK to continue and wait for NetBeans to upload all the files to your virtual server. Once the uploading is done, the WordPress installation page will show up in your Web browser. Fill in the Blog Title and E-mail fields, and click on Install WordPress to continue:
Follow the WordPress installation instructions and you’ll end up with your very own WordPress project installed in your virtual LAMP machine! In the following screenshot, I modified the PHP code in the header.php file to add the Turnkey LAMP image:
Now you can take full advantage of the NetBeans IDE to work with complex projects, like the WordPress blog, or make your own projects! NetBeans has a lot of tools to make your developing chores easier than you ever thought possible!
In this article, I showed you how to configure NetBeans, VirtualBox and the Turnkey LAMP appliance to develop complex PHP applications in a virtual environment. One of the biggest advantages, I see with this configuration is that you don’t need to mess up your main computer, since all the dirty stuff is done in the Turnkey LAMP virtual appliance, thanks to VirtualBox
Using the Remote Connection feature, you’ll just need to install the NetBeans PHP IDE on your main PC, and this wonderful piece of software will take care of the tedious process of having to edit and upload files to your Web server. And if something goes wrong, you won’t mess up your main computer! Just delete your virtual machine, create a new one, reinstall Turnkey LAMP, and voila! Since you have the source code of your project on your local machine, you won’t lose valuable information!
Oh, and you’re not limited to PHP development. The Turnkey Linux Web site has several virtual appliances you can use with VirtualBox and NetBeans as a starting point to develop complex Web applications based on open source software: WordPress, Joomla!, Drupal, LAMP, Tomcat (Java Servlets and JSPs), Rails, phpBB, and much more!
Further resources on this subject:
About the Author :
Alfonso Romero is a freelance computer consultant and translator from Mexico. He's been working with Linux and open source software since 1999. He started operating his first web server (Apache) from a PC at home, offering free hosting services to experiment with Postfix, Squirrel Mail, MySQL, Apache, Tomcat, and Virtual Hosting. Since then, he's been working as a computer consultant for several clients in Mexico – writing Java, C++, and Web applications. Since 2000, he has worked for Pearson Education in Mexico as a computer books freelance translator and consultant. His latest book translations are the Spanish versions of Java How to Program, Seventh Edition, from Deitel & Deitel, and C++ How to Program, Sixth Edition, also from Deitel & Deitel. Al enjoys writing tutorials and teaching about Java, C++, PHP, the Apache Web server, Tomcat, MySQL, Web applications like Apache Roller, and all of the wonderful open source applications used today, and when he's not experimenting with new trends in Open Source applications, he enjoys playing his electric guitar.