First Time Linux

A simple HTML file

This won't be a HTML reference guide, because there are plenty of those already. More importantly, it will discuss how to do this programming using linux, and Mandriva in particular. Let's start first of all with a basic HTML page.

<html>
    <head>
        <title>Simple html</title>
    </head>
    <body>
        <h1>Simple html page</h1>
        <p>This is just a verrrry simple html page.</p>
    <body>
</html>

and let's save this file in our home directory as testpage.html.

The simplest way to view it is to fire up your browser (Konqueror, Mozilla, Firefox, Opera, whatever you like. Even Lynx if you want!) and open the file that you've just saved. Congratulations, you may begin coding :)

Running the web server

The above of course should work with any system, because it's just using the file system rather than a web server. So let's get a web server running and deliver the same file via http.

The first thing to do is check that the web server is running - this will depend on which packages you chose to install and which services to activate. Try it by pointing your browser to http://localhost/. You should get an Apache information page if the web server is running. If not, make sure you have the apache packages installed and check that the httpd service is running (use "Configure Your Computer" under "System" and "System Services").

On Mandriva, the default root directory for the Apache web server is /var/www/html, so you can copy your testpage.html file over to the /var/www/html/ directory (you'll need to be either root or the apache user to do this) and the web server will be able to find it. Then just point your browser to http://localhost/testpage.html and it should display the page as before.

Javascript

Javascript is where the programming bit comes in - and you can experiment with this either with the file protocol or using http. It doesn't matter, because the javascript runs inside the browser, wherever the file came from.

Let's put some javascript in the file, and see if it works:

<html>
    <head>
        <title>Simple html</title>
    </head>
    <body>
        <h1>Simple html page</h1>
        <p>This is just a verrrry simple html page.</p>
        <ul>
            <script type="text/javascript">
                for (i=1; i<=10; i++)
                    if (i != 4)
                        document.writeln("<li>This is a javascript loop number " + i + "</li>");
            </script>
        </ul>
    <body>
</html>

If javascript is enabled in your browser, you should see the results of this loop. It's important to note though, that this loop is not running in the web server. The javascript is sent to the browser, and it's executed there (or not, if it's switched off in the browser). If we want to run code in the web server, we'll need a different technique.

PHP

PHP is a scripting language which gets inserted into web pages in a similar way to the javascript above. The important difference is, that this is executed on the server before the page gets sent to the browser. So it can do very clever things with data on the server (often using databases) and doesn't have to rely on anything running on the browser. When the browser gets the page, the code has already been executed, and only the results (as HTML) are received, not the code.

To run these examples, you need to run the php inside Apache, so make sure you've got the php and libphp packages, and that apache is running properly with the above html pages.

Now let's edit the previous page to put a couple of simple php tags to test the PHP:

<html>
    <head>
        <title>Simple html</title>
    </head>
    <body>
        <h1>Simple html page</h1>
        <p>This is just a verrrry simple html page.</p>
        <ul>
            <script type="text/javascript">
                for (i=1; i<=10; i++)
                    if (i != 4)
                        document.writeln("<li>This is a javascript loop number " + i + "</li>");
            </script>
        </ul>
        <p>This is normal html again</p>
        <p><?php echo 'this is a line written with php'; ?></p>
        <p>We can also do loops: 
            <?php
                for ($i=1; $i<=10; $i++)
                {
                    echo $i;
                }
            ?>
        </p>
    <body>
</html>

Another good tag to experiment with is <?php phpinfo(); ?>. Try it!

From here, there's a load of stuff you can do to create dynamic web pages, but make sure wherever you're planning to host the pages can also cope with php. It's the kind of thing that doesn't come as standard service so shop around.

Perl

There's a separate page on Perl, but for now we'll just remark that Perl goes very well together with web programming, where the perl generates pages on the server side in a similar way to the PHP above. Perl has very powerful text-manipulation functions though, so is well-suited to processing forms and parsing inputs.

If you install the apache-mod_perl package, you'll get built-in support for Perl in the web server. To try it out, just point your browser at http://localhost/perl/test.pl which should have a test script waiting for you. See the Perl page for more about the language and what you can do with it.

Editors and tools

I use a simple text editor, Kate, for HTML and web programming - it's included in the KDE and provides all the basic editing tools for the source code that you'll need. It provides colour-coded syntax highlighting to help spot typos but it isn't a wysiwyg web editor. For a specialist, dedicated tool for web programming, try Bluefish which provides a great deal of shiny, advanced features and support for very tight control of the generated code. It gives plenty of help and editing functions but the code is still yours. Similar functions are available in Quanta Plus, but I personally find the interface much more cluttered and confusing.

Other tools that you could try include Openoffice Writer (also comes as standard) and in the "Save as" dialog choose HTML as the file type. You could also try Netscape composer for basic HTML but it won't be able to cope with complex stuff like PHP.

More info

For a variety of guides to html and javascript, see w3schools.com, wikibooks (HTML and javascript) and about a million other sites on the internet. For php, see the excellent manual at php.net and for its integration with Linux, Apache and MySQL, see Wikipedia's article on LAMP.