Webservers edit

The SOCS Webserver runs on Linux. Compilation of code for running on the webserver (Perl, Python, Java, etc.) needs to be done on cgi64.cs.mcgill.ca or on a close enough version of Debian or Ubuntu.

The McGill Computer Science webserver runs Apache. Apache is the most popular webserver on the Internet since April 1996; it runs the majority of the world's websites.

For more information about the Apache HTTP Server, go to http://httpd.apache.org/.

Creating a Personal Webpage

NOTE: Only students in computer science are able to create personal webpages. If you do not have a McGill Computer Science account, you will not be able to create a personal webpage.

This is a recipe-type set of instructions that make next to no assumptions about where you currently are in your home directory. You can literally copy/paste these commands (pretty much everything in red) at the prompt, but SOCS is not responsible for the outcome of your actions.

  • The zeroth step is to open a shell session. To do this, either log into a Unix workstation, or use ssh to open a remote shell session on a computer server.
  • The first step is to give the home directory the proper permissions, then to create ~/public_html and give it the proper permissions.
[cgi64] [~] chmod 711 ~
[cgi64] [~] mkdir ~/public_html
[cgi64] [~] chmod 711 ~/public_html
  • The second step is to create a simple webpage containing a bit of HTML. This initial webpage (necessarily) named index.html will allow you to access your webpage via url of the form http://cs.mcgill.ca/~username where username is your username at SOCS.
[ubuntu] [~] cat > ~/public_html/index.html

<html>
<head>
<title> template webpage </title>
</head>
<body bgcolor="white" text="black">
Hello World!
</body> </html>
  • The third step is to give ~/public_html/index.html the appropriate permissions.
[cgi64] [~] chmod 644 ~/public_html/index.html
  • At this point, everything should be in place, and you should be able to access the page by going to http://cgi64.cs.mcgill.ca/~username where username is your username at SOCS.

CGI via suEXEC

Because suEXEC is installed on our Apache webserver, all CGI programs are run under the UID of the program owner. Normally a CGI program would run as the same user who is running the webserver; however this is no longer the case.

The benefit, and direct result of suEXEC is that CGI programs are no longer required to be world readable and executable. CGI programs can be readable and executable only by the owner. This means that if your CGI program writes to a file, that file no longer has to be world writable. Likewise, all directories should have the mode 711, regardless of whether a CGI program within that directory writes to files or not.

suEXEC has a great security advantage, but it comes with a minimal downside: less open file mode requirements result in a false sense of security since removing permissions from 'group' and 'others' normally means that only the owner can read, write, or execute a given CGI program; however since suEXEC is installed programs with mode 700 will still be executed by the webserver.

In general, CGI programs can be changed to mode 700 (from 755), and writable files can be changed to mode 600 (from 666). Directories can be changed to mode 711 in all cases. All executables must be placed in the cgi-bin directory of your public_html folder.

For more information about the Apache suEXEC support, go to http://httpd.apache.org/docs-2.0/suexec.html.

.htaccess Files

Summary

.htaccess files are a way to make configuration changes to specific directories on a website. You place configuration directives inside your .htaccess file, and the .htaccess file inside a directory, to apply those directives to that directory and its subdirectories. Because students at McGill do not have access to the main server configuration file, using a .htaccess file is the only way to do many things, like password protecting a file or directory.

Password Protect Tutorial

To use .htaccess files to password protect a file or directory on you website, follow these steps:

1. Create a .htpasswd file and place it ABOVE your public html folder - somewhere not visible from your website. To do this, navigate to your public_html folder, and run the "print working directory" command pwd to display the path:

[nfland1][lab2-35][~]  pwd 
/home/2010/nfland1/public_html 

Now that you're sure you're in your public_html directory, move up one directory and run pwd again:

[nfland1][lab2-35][~]  cd .. 
[nfland1][lab2-35][~]  pwd 
/home/2010/nfland1/

This means that you want to place the .htpasswd file in this directory, as it is the directory just above your public_html folder.

To create the .htpasswd file, run:

[nfland1][lab2-35][~]  htpasswd -c ~/.htpasswd username

where username is the username for the user who you want to be able to access the password protected files.

You will be prompted to create a password, type one in and then hit enter. You will be prompted to confirm it.

New Password:
Re-type new password:
username:7.a8CKWlsoiAj5

The last line is the username you chose and the encrypted password.

2. Create a .htaccess file and place it inside the directory that you wish to protect. If the you want to protect a directory /home/2010/nfland1/public_html/downloads, place the .htaccess file in this folder. Copy and paste the following into the file:

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /home/2010/nfland1/.htpasswd
AuthGroupFile /dev/null
require valid-user

The path following "AuthUserFile" should be the path where you created the .htpasswd file above, in this example, it is /home/2010/nfland1/.

3. Test to make sure your website prompts for a username and password when trying to access the directory.

Notes

.htaccess and .htpasswd will be hidden files, so make sure you have "Show Hidden Files" turned on. To enable this on Ubuntu, click on "View" at the top of the folder and make sure "Show Hidden Files" is checked.

To create .htaccess and .htpasswd files on Windows, open the file in Notepad, choose "Save as..." and select "All types (*.*)" next to file type. Now name the file ".htaccess" and click save.

For more information: http://www.javascriptkit.com/howto/htaccess3.shtml

Java

This information comes from the Wikipedia page for Java applets.

The first step is to create a Java applet - here's an example that will display "Hello, World!". Create a file named TestApplet.java and place it inside your public_html directory. Open it and paste in the following:

import java.applet.Applet;
import java.awt.*;
public class TestApplet extends Applet {
  public void init() { }
  public void stop() { }
   // Print a message on the screen (x=20, y=10).
  public void paint(Graphics g) {
    g.drawString("Hello, world!", 20,10);
  }
}

Save and compile this at the command prompt from within your public_html directory:

[nfland1][lab2-35][~] javac TestApplet.java

Now, edit the webpage you want to have this applet to include:

<APPLET code="TestApplet.class" WIDTH="200" HEIGHT="40">
This is where TestApplet.class runs.</APPLET>

This will run the TestApplet java applet on your webpage.

The Java Boutique has some Java applets to download and try out.

Perl

Perl is an interpreted programming language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. Perl's regular expression engine is in a class of its own, capable of things most languages can't even mimic. Those fluent in Perl can use its idioms to produce tiny and elegant code with minimal verbosity.

Perl has packages, modules, objects, unicode support, interprocess communication support, threads, and about a million other things. With Perl, you can have your cake and eat it too.

For a Perl script to be interpreted by our webserver, it must have the suffix .cgi.

Check what Perl modules are currently installed on the SOCS webserver.

Example

This describes how to get a basic cgi/perl script running. This is a recipe-type set of instructions that make next to no assumptions about what you currently have in your home directory. You can literally copy/paste these commands (pretty much everything in red) at the command prompt, but SOCS is not responsible for the outcome of your actions. Please download the files (mentioned below), rather then attempting create them via copy/paste.

  • The first step is to create ~/public_html and give things the proper permissions. If you already have a public_html directory from creating a personal webpage as shown above, you can skip the second line (mkdir ~/public_html).
[nfland1][lab2-35][~] chmod 711 ~
[nfland1][lab2-35][~] mkdir ~/public_html
[nfland1][lab2-35][~] chmod 711 ~/public_html
  • The second step is to create a simple webpage containing a form that will post variables to a Perl script (which we will write later). The bold green portion below is just to emphasize the form portion of the web page, and to bring your attention to action and method (this is how we will call our Perl script). The second bold green portion shows where the input variable is defined (it is called user... so look for it in the Perl script).

    The text editor called pico can be used to enter the text below into a file called test.html (in your public_html directory). You can use any editor you are comfortable with (many people enjoy xemacs because of its ease of use). For the purpose of this example, rather then typing in all the green text below, you can simply download the file test.html into your public_html directory by right-clicking on the link and selecting Save Link As.... Alternatively, you could left-click on the link, and then right-click on the page that loads, and choose View Page Source, and copy/paste the source into a file called test.html in your public_html directory.
[nfland1][lab2-35][~] pico ~/public_html/test.html
<html>
<head>
<title>test</title>
</head>
<body bgcolor="white" text="black">
<br><br>
<center>
<form action="./cgi-bin/test.pl" method="post">
<table width="450" cellpadding="3" cellspacing="2" border="0">
 <tr>
   <td valign="middle" align="left" bgcolor="#706090">
     <font color="white" size="2">Your name:</font>
   </td>
   <td valign="middle" align="left" bgcolor="#706090">
     <font color="white" size="2">
     <input type="text" name="user" value="Bono" size="30" maxlength="30">
     </font>
   </td>
 </tr>
 </table>
 <br><br>
 <font color="blue"><input type="submit" name="submit" value="OK"></font>
 </form>
 </center>
 </body>
 </html>
  • The third step is to give ~/public_html/test.html the appropriate permissions.
[nfland1][lab2-35][~] chmod 644 ~/public_html/test.html
  • The fourth step is to create ~/public_html/cgi-bin (the place where we put our perl scripts), and give that proper permissions.
[nfland1][lab2-35][~] mkdir ~/public_html/cgi-bin
[nfland1][lab2-35][~] chmod 711 ~/public_html/cgi-bin
  • The fifth step is to create a simple CGI/Perl script that will grab the variable user which we created in the webpage above.

    Note that we need to pull in declarations and definitions for cgi, so we do that by adding the line: use CGI;

    Secondly, we also need to create a new query object (in order to parse the input gained by either the POST or GET method of the calling html page), so we do that by adding a line similar to: $q = new CGI;

    Finally, we have to fetch each value passed, by doing something like the following, for each named parameter: $username = $q->param( 'user' );

    As above, the code below can be typed into a file using the text editor called pico. You can use any editor you are comfortable with. For the purpose of this example, rather then typing in all the green text below, you can simply download the file test.perl into your public_html/cgi-bin/ directory by right-clicking on the link and selecting Save Link As.... However, if you choose to download the file, you will have to save it as test.pl in your public_html/cgi-bin/ directory, or you will have to rename it to test.pl before continuing with the next step (if you download it as test.perl). Alternatively, you can left-click on the link and copy/paste the content of test.perl into a file called test.pl in your public_html/cgi-bin/ directory.
[nfland1][lab2-35][~] pico ~/public_html/cgi-bin/test.pl
#!/usr/bin/perl
use CGI;
my $q = new CGI;
my $theuser = $q->param( 'user' );
$theuser = '' unless $theuser;
print "Content-Type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>CGI/Perl Test </title>\n";
print "</head>\n\n";
print "<body>\n\n";
print "Hello, world!<br><br>\n";
print "Your name: $theuser<br><br>\n";
my $date = `/bin/date`;
chomp( $date );
print "date: $date<br>\n\n";
print "</body>\n";
print "</html>\n";
  • The sixth step is to give ~/public_html/cgi-bin/test.pl the appropriate permissions. The reason why it's OK to give so few permissions (700), is because our webserver is running suEXEC. If it weren't, then the permissions would need to be 755.
[nfland1][lab2-35][~] chmod 700 ~/public_html/cgi-bin/test.pl
  • At this point, everything should be in place, and you should be able to test the page and script by going to http://cgi.cs.mcgill.ca/~username/test.html where username is your username. When you get there, just click OK. You will be forwarded to another page (generated by the Perl script).

Notes

If you want to see errors (from within McGill) on the web server associated with your username, click HERE.

For more information on CGI/Perl, do `perldoc CGI`.

If you're looking for reliable pre-made CGI scripts, you should check out nms: http://nms-cgi.sourceforge.net/

Python

For a beginner's tutorial of using Python on your website visit Python's HOWTO Use Python in the Web.

Python is an interpreted, object-oriented programming language. It is capable of many things beyond web scripting, including GUI implementation.

For a Python script to be interpreted by our webserver, it must have the suffix .py.

Here's an example python script to display "Hello World!" on the page. Create a new file called test.py and paste the following inside:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
print "Content-Type: text/plain;charset=utf-8"
print
print "Hello World!"

Remember to place this executable .py file inside the cgi-bin directory and give it the appropriate permissions as outlined above in the "CGI via suEXEC" section.

For more information about Python, go to http://www.python.org/.

PHP

For a great tutorial about making PHP-enabled web pages, visit the PHP tutorial page.

PHP is an interpreted, server-side, HTML embedded scripting language, somewhat based on Perl, and C. PHP makes dynamic webpages possible becasue its code can be embedded within HTML. To differentiate between PHP and HTML context, PHP's code must be enclosed by special start and end tags that mark the start and end of PHP blocks.

For a PHP file to be interpreted by our webserver, it must have the suffix .php. For info about PHP on our webserver, click here.

To see how PHP works, put the following in a file called testphp.php and place this in your public_html directory:

<html>
  <head>
   <title>PHP TEST</title>
  </head>
  <body>
  <?php echo '<p>Hello, World!</p>'; ?>
  </body>
 
</html>

Navigate to http://cs.mcgill.ca/~username/testphp.php to see your page.

For more information about PHP, go to http://php.net/.

SSI

For a tutorial on how to use SSI on your webpage visit the apache page for SSI and scroll down to "Basic SSI Directives".

Server Side Includes (SSI) are directives placed within HTML webpages to generate some dynamic side-effects to otherwise static HTML webpages.

SSIs are evaluated on the server at the same time as the webpage, are capable of things such as:

  • obtaining the current date and time
  • obtaining the modification date of the file being evaluated
  • including a file, for example a standard header or footer
  • setting variables
  • evaluating conditional expressions: if/elif/else

For the sake of added security, the exec feature of SSI has been intentionally turned off.

There are two ways of using SSI in webpages.

  • The first way is to place the directives into a file having a .html extention, and turning on that file's "execute" bit.
  • The second way is to place the directives into a file having a .shtml extention, and turning on just the "read" bit.

Notice the s in the latter case, and the absence of it in the former.

If you've already got a lot of pages and links between them, it is probably easier to just turn on the execute bit for the pages rather than going back and renaming all of them to .shtml.


An example of SSI that you could place on your webpage to give the current date:

<!--#config timefmt="%A %B %d, %Y" -->
 Today is <!--#echo var="DATE_LOCAL" -->

This would print the date on a page that had been properly configured to display SSIs.

AFT

Almost Free Text (AFT), is a mostly free form document preparation system, which means that you can write documents, lists, notes, etc., with "little intrusive markup".

AFT has a few rules for formatting a document; unlike HTML which embeds tags within the data, there is next to no such polution in AFT documents. This means that AFT documents appear clean and similar, before and after processing (through an aft2html translator).

For an AFT file to be interpreted by our webserver, it must have the suffix .aft.

For more information about AFT, go to http://www.maplefish.com/todd/aft.html, or check out the manual at http://www.maplefish.com/todd/aft-refman.html.