This tutorial shows how to configure the PHP development environment in the Ubuntu operating system (7.10 and later). This involves installing and configuring the PHP engine, a MySQL database, an Apache web server, and the XDebug debugger.
his tutorial shows how to configure the PHP development environment in Ubuntu 7.04 and later. You need to:
- Install the Apache2 HTTP server, the PHP5 engine, the MySQL 5.0 database server, and the PHP5-MySQL module. These packages can be installed together as the LAMP stack, or they can be installed separately.
- Specify the Document Root for the Apache2 HTTP server
- Configure the MySQL database server
- Install and enable the XDebug 2.0 debugger
See the Ubuntu community for more information on installing Apache, MySQL, and PHP.
Installing the Software Packages Together
Ubuntu provides a Linux AMP (LAMP) package that contains all the necessary packages for your PHP environment. You can install the software by executing the following command at the command prompt in the Terminal window:
sudo tasksel install lamp-server
The lamp-server package includes the most suitable version of PHP, Apache 2, MySQL, and PHP5-MySQL.
Installing the Software Packages Separately
Instead of installing the entire set of LAMP packages, you can also install the packages individually. This is useful if you already have installed one of the components, such as the Apache server or MySQL database server. You can use command-line tools or the Synaptic Package Manager GUI.
The individual packages to install are the following:
Checking the Installation
After you set up your PHP web stack, check that it is installed correctly and that your Apache server recognizes your PHP engine.
To check that Apache and PHP are installed and running, open NetBeans IDE and create a PHP project. In the index.php file, enter the PHP method phpinfo(). Run the file. The standard PHP information page should display.
The following are some frequently encountered problems when checking the installation of your PHP stack in Ubuntu:
- The browser window displays a Not Found error for ~USER/PROJECT/index.php. Remove the ~USER string from the URL. For example, if this error appears for the URL ~ubuntu/test1/index.php, change the URL to test1/index.php. Note that you can set the URL for a PHP project in NetBeans IDE either when you create the project, or by right-clicking the project node and going to Properties > Run Configuration.
- The browser shows you a popup asking you to open the file, as if the PHP engine is not recognized. There’s a problem with your php5-common package. Replace it with php5 and phpmyadmin. To replace php5-common, run the following two commands:
apt-get --purge remove php5-common apt-get install php5 phpmyadmin
Specifying the Document Root for the Apache2 HTTP Server
The Document Root is the directory where the Apache HTTP server takes files for displaying in the browser. The Document Root is specified in the file that defines your virtual host. The default virtual host configuration file is
with the document root
We recommend that you create your own virtual host and enable it instead of editing the default one.
- Create the Document Root Location
- Specify the new document root in a new virtual host
- Activating the new virtual host
Creating the Document Root Location
- Choose Places > Home Folder.
- From the context menu, choose Create Folder.
- Enter the name of the folder, for example, public_html.
Creating a New Virtual Host
- To launch the Terminal, choose Applications>Accessories>Terminal. The Terminal window opens.
- To copy the configuration file of the default virtual host to a new file (mysite), type the following command at the command prompt:
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mysite
3. Run the gedit application and edit the new configuration file (mysite) in it:
gksudo gedit /etc/apache2/sites-available/mysite
If asked, enter the password that you specified for the root user during the installation of your operating system.
4. Change the Document Root to point to the new location:
5. Change the Directory directive, replace
6. Save the file mysite
Activating the New Virtual Host
- To deactivate the default host and activate the new host, launch the Terminal and run the following two utilities in the Terminal window: sudo a2dissite default && sudo a2ensite mysite
- Restart the Apache HTTP server: sudo /etc/init.d/apache2 reload
Configuring the MySQL Database Server
During the installation of the MySQL database server, a root user is created. A dialog opens during installation in which you set a root user password. If this dialog did not open, or you did not set a password in this dialog, you need to create a MySQL root user password now. You will need the password for creating other MySQL server users.
- To connect to the MySQL server, launch the Terminal and in the Terminal window enter the following command: mysql -u root -p The MySQL command prompt appears.
- At the command prompt enter the following command and press Enter: SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘<yourpassword>’); If the command is executed successfully, the following message is displayed: Query OK, 0 rows affected (0.00 sec)
« 15 Useful .htaccess Snippets for Your WordPress Site
How to Build A Fully Customized WordPress Login Page »