Jack Wallen finds that the PHP module stumps new Apache admins more than any other module. He explains how to configure a LAMP server on a Debian-based system that installs Apache2.
One of the questions I receive most often is: "I just installed Apache, and I'm trying to get X running, but when I go to the website, the browser keeps trying to download .php files instead of displaying them. What do I do?"
If you've already installed a LAMP server but that server is not processing PHP files, there are steps you must take in order to get those PHP web pages served up properly. I'll walk you through this so you can get sites such as Joomla!, Drupal, and XOOPS up and running without pulling your hair out. For this tutorial, I will configure a LAMP server on a Debian-based system that installs Apache2. For variations on this setup, the steps will differ, but the fundamentals are the same.
Additional steps to the LAMP server installation
Even after installing the LAMP server, pieces of the puzzle that are required for proper PHP processing might not have been installed. Below are the commands necessary to get those pieces installed (all of these commands are issued from within a terminal window):
- sudo apt-get install php5-common php5 libapache2-mod-php5
- sudo apt-get install php5-cli
- sudo apt-get install php5-cgi
- sudo apt-get install php5-mysql
Once this is done, the PHP module must be enabled so it is usable by Apache. To enable the PHP module, you need to issue the command a2enmod and choose php5 (type php5 and hit [Enter]). Now you need to restart Apache with the command sudo /etc/init.d/apache2 restart or the command sudo service apache2 restart.
Apache configuration file changesYou must change options in the Apache configuration file. (I will be editing the /etc/apache/apache2.conf file.) You need to add the AddType directives, which can be placed at the bottom of the apache2.conf file. Open the apache2.conf file in your text editor and look for the line DirectoryIndex index.html. Add index.php to the DirectoryIndex line. Now skip to the end of that same file and add these two lines: AddType application/x-httpd-php .php3 .php and AddType application/x-httpd-php-source .phps. Save and close that file. You should restart Apache one more time for good measure.
For a very simple php test, you should create the file /var/www/test.php with the following contents:
echo("<h1>If you can see this, PHP and Apache are working!</h1>");?>
Save and close that file and then point your web browser to http://ADDRESS_TO_APACHE_SERVER/test.php (ADDRESS_TO_APACHE_SERVER is the actual address to the server). If you see the text success message "If you can see this, PHP and Apache are working!", you have an Apache server that is correctly processing PHP. If you're still getting the downloading behavior, you should clear your browser's cache and try again.
If none of the tips I described thus far have helped, I recommend looking at the Apache log files to see if you can track down the problem. Apache likes the following logs (all found in /var/log/):
- access.log: This log keeps track of any connection made to your server.
- error.log: This log keeps track of any errors that occur with Apache.
- other_vhosts_access.log: This log is where virtual hosts will log when the virtual host has not been prescribed its own log file.
Apache is a fairly complex system that often becomes more challenging when modules are added. In my experience, the PHP module stumps new Apache admins more than any other module.