The Apache web server – Part 2
At first we create a site with a PHP script to test the functionality. On the main site we create a link to this PHP site. So we create a file called ‚phpTest.php‘ in the ‚/var/www‘ directory with the following contents:
<?php echo "<h1>PHP Testseite</h1>"; phpinfo(); ?>
Now the server can not interpret this code and may only returns this text. So to execute PHP script we have to install the following packages:
After a restart of the Apache web server, the PHP script is executed and the browser displays the following:
We now create a second script file to test the Perl functionality. But first we need to create a directory for both Perl and Python scripts. This directory is called ‚cgi-bin‘ and is also located in the ‚/var/www‘ directory. Now its time to create a file called ‚perlTest.pl‘ (pl is the normally used for Perl scripts) and copy this contents into the file:
#!/usr/bin/perl print "Content-type: text/html\r\n\r\n"; print "<h1>Perl Testseite</h1> print "Perl funktioniert mit diesem Webserver!";
This file and the newly created directory need execute rights. You have to change this.
The last thing here is to install the Perl module for the web server:
Now Perl is installed an activated. But we need to configure our virtual host. You can find this host in ’sites-avaliable‘. The default virtual host already has a configuration for the ‚cgi-bin‘ directory. This configuration needs to be replaced with this:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/ <Directory "/var/www/cgi-bin"> Options ExecCGI AddHandler cgi-script cgi pl </Directory>
The first line defines an alias to the directory of our scripts. The line AddHandler defines all file endings that should be treated as script files. For our example this are *.cgi and *.pl files.
After a restart of the server, the browser display the Perl script output:
Again we create a new file inside the ‚cgi-bin‘ directory and call it ‚pythonTest.py‘.
#!/usr/bin/python def index(): return "<html><body><h1>Python Testseite</h1></br><Python funktioniert mit diesem Webserver!</body></html>"
Here we also need to set executable rights for the file.
Now we install the Python module:
And the last thing is also a change on the configuration of the virtual host. We add the following:
<Directory "/var/www/cgi-bin"> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all AddHandler mod_python py PythonHandler mod_python.publisher PythonDebug on </Directory>
If you follow the instructions it can happen, that there may be some errors. The main source of problems are script languages, because the configuration is not that easy. First you should check if all needed modules are correctly installed. A restart of your server can also be helpful. If you want to test scripts, you can do this also on the command line (for example type ‚./perlTest.pl‘ and watch the output). With this you can easily check if the file has executable rights. If the scripts still does not work when they are started with the browser, you may check the Apache log. You can find all log files in ‚/var/log/apache2‘.
The third Part is about the Tomcat web server and how to run Java Programs.