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!