Setting up Apache, PHP, MySQL
From SUSE Wiki
Type: Howto, Application-Specific (Apache, MySQL)
Tested Versions: 10.0, 10.1
Written By: Kamatsu
Further Modifications By: Cheeze
This article deals with how to install a simple Apache webserver, with PHP enabled and a MySQL database backend set up and linked to the previous two components.
Initial Installation
Open up YaST, go to "Software Management". Search for "apache", and select:
- apache2
- apache2-doc
- apache2-mod_php5 (for version 10.1)
- apache2-mod_php4 (for version 10.0)
- apache2-prefork
- libapr1
- libapr-util1
Then, Search for "php5" (for version 10.1), or "php4" (for version 10.0) and select all packages (RMB->All Packages in this List->Install) Then, Search then for mysql, and install:
- mysql
- mysql-client
Now that you've installed the necessary software, we can move on to configuration
Configuration
Once you have done this, go to "System" under YaST. Go to "System Services (runlevel)", and Find apache2 in the list and select it. Click "Enable". Find mysql in the list and select it, and also Click "Enable"
If you want to serve pages to other computers, do not forget to change your Firewall settings. In YaST select "Security and Users" and then "Firewall". On the first screen select "Allowed Services" and select "HTTP Server" from the drop down list under "Service to Allow". Click "Add" and then "Next" to complete configuration. You may complete this step after testing Apache, PHP, and MySQL using localhost (as described below).
Testing
The first thing to test is if your Apache is working correctly. Open up a browser (e.g. Firefox) and enter into the address bar the following:
localhost
If you see two words "It works!" then Apache is working correctly. The next step is to proceed to check if PHP is working correctly for you.
Open up the public_html file in your home folder, and, using your favorite editor, (e.g gedit, or emacs, or kwrite), type:
<?php phpinfo(); ?>
and save it as a new file called "index.php" in your public_html folder (inside your home folder). Then, open up Firefox or your web browser of choice and type in the address bar: "http://localhost/~username" replacing "username" with your username. Press Enter, and a bunch of info should come up. If not, something isn't working correctly, so ensure you followed these steps.
Once you have checked that PHP is working the next thing to check for is your installation of MySQL. It is important to follow these checks in this order as one will not work without the previous component working correctly (e.g. PHP will not work if Apache isn't functioning).
An observation here. Earlier versions of SUSE Linux seems to have a bug with MySQL installation. One additional step is necessary to get MySQL installed. Refer to Novell Cool Solutions. The remastered version of SUSE Linux 10.1 seems to have that fixed.
The simplest way, in my opinion, to check for a working installation of MySQL is to simply list the databases available in the server. Therefore I always use the script below to do so. To use the script, open an editor and type the following (removing the space between the single quotes in the $mysqlpasswd line):
<html>
<head>
<title>Test PHP with MySQL</title>
</head>
<body>
<?php
$mysqluser = 'root';
$mysqlpasswd = ' ';
$mysqlhost = 'localhost';
$connID = mysql_connect($mysqlhost, $mysqluser, $mysqlpasswd);
$result = mysql_list_dbs();
echo "<p>Databases in the local MySQL server</p>\n";
echo "<p>";
while ($row = mysql_fetch_row($result)) {
echo "<br><em>$row[0]</em>\n";
}
echo "</p>";
?>
</body>
</html>
Save this file as testmysql.php in your public_html folder and fire up your Web broser. Enter the following into the address bar
localhost/~username/testmysql.php
replacing 'username' with your own user name. The page should display a simple listing of the databases in your MySQL server. Take note that the code assumes a fresh installation of MySQL where no password is set for the root user yet. If such a password is already set, remember to assign it to the $mysqlpasswd variable e.g.
$mysqlpasswd = 'root-password';
Bear in mind also that this file is in plain text. Once you've ensured that your MySQL server is working, you should remove it in case your root password gets compromised.
Troubleshooting
I've done according to the steps described here but I still cannot view my webpage. It seems as though Apache is not working.
Assuming that you've double-checked that the packages are installed via YaST, the next thing you need to check is the runlevel as described in the section Configuration.
Here's where it gets a little weird. "System Services (Runlevel)" may show that Apache is running. However, that MAY not be the actual case. What you should do is to open a terminal (also called console or command-line) and switch to the superuser 'root' using the su command. Enter the password and type the command
rcapache2 status
If it says "Checking for httpd2: unused", you know that the server hasn't really started. If so, type the following command to start it:
"rcapache2 start"
Then run the status check command to ensure that the server is running. If you see 'running' in place of 'unused', open a browser and type in the location field "http://localhost". You should see "It works!". Congratulations!
The URL http://localhost/~username was working fine but after I configured Apache using the YaST tool, it stopped working.
After configuring the Apache server for the first time, the configuration files will be re-organised somehow. Check in the /etc/apache2 directory to see the configuration files that are created by SUSE in an attempt to make things easier for you. One possible reason why the mod_userdir module stopped working is because the configuration file for this module is not read by Apache.
In order to get the mod_userdir module parsed by Apache, you can either do it manually or use the SysConfig program found under YaST. SUSE seems to recommend that the configuration files be kept pristine as they were designed to be modular, so the inclusion of configuration files may be done using YaST. You can choose to edit the configuration file by hand (detailed instructions on the directives can be found in Apache's website "http://httpd.apache.org/docs/". In this section, you will learn how to do it using the sysconfig editor.
Regardless of which method you choose, the /etc/apache2/mod_userdir.conf file must still be edited. Open this file using any text editor of your choice (take note that you have to be the root user before you can edit the file). Look for the line containing
#UserDir public_html
The # sign in front signifies that this line is a comment. Remove the # sign to uncomment the line. This must be done otherwise the module will not work as you expect it to. Once you have done this, proceed to the next step.
Start YaST. Enter the root user password and click on System on the left pane. The right pane will change to show a different set of options. Click on /etc/sysconfig Editor. Another window will pop up. On the left are a list of options.
Click on the + sign next to Network, then on the + sign next to WWW, and finally on the + sign next to Apache2. From the list that appears below, click on APACHE_CONF_INCLUDE_FILES. The default value should be blank. Enter the value
/etc/apache2/mod_userdir.conf
into text field.
Click on Finish. A confirmation dialog box subsequently appears. Click OK to proceed. Take note that this final step includes restarting the Apache Web server (which must be done in order for Apache to re-read the configurations).
It should work now!
Why can't I view the server information when I can view the server status?
The Apache distribution that comes with SUSE Linux only comes with the mod_status module enabled by default. In order to view the mod_info module contents as well, you need to tell Apache to load it. Again, inline with SUSE's design principles, instead of manually adjusting the configuration file, you can do it via sysconfig. Start the sysconfig editor and locate the Apache2 settings as described in the section above.
Look for the setting APACHE_MODULES; there should already be some values there by default. Click on the entry field and position the text caret right at the end. Type in
info
This tells Apache to load the mod_info module as well. (Warning: do not remove the original values there as you would prevent all the modules from loading when starting Apache. Just add the value 'info' at the end of the default settings.) You may notice that the mod_status module is already enabled by the status value in the field. Click on Finish to complete the process. Double-check the httpd.conf file to see if the following is inside.
Include /etc/apache2/mod_info.conf
It should be right below the "Include /etc/apache2/mod_status.conf" line. Start your Web browser and enter the following to see your server's information:
localhost/server-info
.php files open file download dialog box
If .php files fail to execute on the server side, and rather simply get served as the original source code text files, the php5 module is not loading correctly.
Follow the same steps as in the above section, but instead of appending "info" add
php5
If the .php files still fail to execute after the above step, open the httpd.conf file and look for the following line:
# List of resources to look for when the client requests a directory DirectoryIndex index.html index.html.var index.php
See if index.php is missing and add it there.
If apache fails to restart make sure apache2-mod_php5 was installed using the YaST software manager.
I can't seem to get the mod_rewrite module to work
The mod_rewrite module is a powerful and complex module. This section here will not describe how to use the module and its directives. Instead, it is assumed that you have a .htaccess with the rules written, and that all you want to do is to enable the mod_rewrite module so that the rules may be applied. Take the CakePHP RAD framework for example. It has a .htaccess file inside the directory that has rules written for URL rewriting. All that needs to be done is to enable the module.
You first must enable the module like how the mod_info module is enabled as described in the section above. The thing to note is that instead of the word info, you append rewrite to the APACHE_MODULES setting. (Note that there is no default configuration file for mod_rewrite.)
Since the rules are placed inside the .htaccess file, it will not be in effect as the default installation of Apache in SUSE Linux is rather restrictive. To allow this to happen, you need to first modify the default-server.conf configuration file.
Inside default-server.conf, look for the directory container for /srv/www/htdocs which is the default directory for holding Web pages. Change the Options and AllowOverride lines
From
<Directory "/srv/www/htdocs">
Options None
AllowOverride None
...
to
<Directory "/srv/www/htdocs">
Options All
AllowOverride All
...
The AllowOverride setting lets the .htaccess file take over whereas the Options setting facilitates the use of symbolic links in Apache. The latter is a must in order to let mod_rewrite work.
You can use the same setting in mod_userdir.conf to ensure that mod_rewrite also works for the individual user directories. URL rewriting should work now!

