Tutorials - Servers > CGI/Shell Server Basics
Tutorials and FAQS: Servers: CGI/Shell Server BasicsNote: Throughout this tutorial, green is used to indicate the server responses and blue is used to indicate the user's typing.
This tutorial describes activating the CGI/Shell Service part of certain PlusNet accounts and basic commands for using the CGI/Shell Server. The tutorial contains information on the following:
- Activating the CGI/Shell Server
- A brief recap of CGI/Shell basics
- Connecting to the CGI/Shell Server
- Using FTP to navigate, update and maintain your CGI/Shell filespace
- Basic CGI/Shell Server Commands
- More Information on the Server
Activating the CGI/Shell Server
If your PlusNet account type allows you access to the CGI server, you should see the following link on your Website Settings page.
If it is deactivated, you should click the button and wait for up to 4 days for the details to be emailed to you on postmaster@username.plus.com - although it will tend to be quicker than this.
Understanding your CGI Account's Password
On activating your CGI webspace, your CGI password is set to the same value as your normal PlusNet account password, but the CGI password is independent of your normal account password -- changing one does not affect the other.
- Changing your CGI password does not change your normal account password.
CGI Basics
Unlike an .html page, which is static, a CGI page is generated dynamically. When a web browser is directed to the URL of a CGI page, the CGI server executes the code associated with that page. The usual result of this execution is to generate a blob of .html, which is then served to the web browser.
Therefore, there are two ways to view your CGI webspace. The first is to view it through a web browser; this is the mechanism that you (and your users) will use on a day-to-day basis. The second is to view it through an FTP (file transfer protocol) session. FTP gives you direct control of your webspace and this is the mechanism that you will use to add, delete and manage files in your webspace.
Connecting to the CGI Server
Web: When connecting to the CGI server through a web browser, you will be able to use http://ccgi.username.plus.com or if you host a domain, you are able to setup Domain records which enable URIs of the form http://cgi.domain.co.uk to work as well. For more information on this please see the Domain Records tutorial.
FTP: To connect to the CGI server through an FTP client, you should use ccgi.plus.net as the server to connect to and use the username and password detailed in the email. Initially this is the same as your PlusNet account username and password, but you are able to change this at the command line.
- An attempt to FTP to any PlusNet server will succeed only if your Internet connection has been established through PlusNet or one of the other PlusNet ISPs.
Connecting through FTP
To connect to the CGI/Shell server, you need an FTP client. If you are using a Unux system (Linux or BSD, for example) you are likely to have a suitable command-line client already installed. If you are using Windows, you may need to download and install a suitable FTP client (the FTP command-line client provided as part of windows does not support the CHMOD command that you will need to use to control permissions on the files in your cgi webspace). One suitable FTP client with a graphical user interface is Filezilla, an open source program which you can download free of charge from http://www.filezilla-project.org. Versions are available for Windows, Mac OS X and Linux.
Once you have installed your FTP client, connect to the CGI server using the server, username and password as described above.
If you are using a command-line client you should be greeted by a friendly ftp> prompt. For a GUI client, consult the documentation and example screen-shots. The examples in this section assume the use of a command-line client.
Core Commands
The main FTP commands you will need to use are put, get, chmod, delete, cd, mkdir.
Moving around your filespace
When you log on, you start off in your "home" directory. Since the FTP server runs in a "chroot jail", FTP will always report your "home" directory as "/".
When you first activate your CGI webspace, your filespace will be empty apart from a directory (folder) named "cgi-bin".
Use the "cd" (change directory) command to move around your filespace. For example, to move into the cgi-bin directory:
| ftp> cd cgi-bin 250 CWD command successful |
To move back to your "home" directory either move relative (up one level) or absolute (back to "/") using one of these commands:
| ftp> cd .. 250 CWD command successful ftp> cd / 250 CWD command successful |
Use the "pwd" (print working directory) command to display the full pathname of your current directory. For example:
| ftp> cd / 250 CWD command successful ftp> cd cgi-bin 250 CWD command successful ftp> pwd 250 "/cgi-bin" is current directory |
- Many applications you might install require you to enter the full directory path of a particular file or directory. You cannot discover this directory path using FTP. Refer to the examples later in this tutorial.
Exiting
When you have finished using FTP, you can terminate it by using the "exit" or "quit" commands.
| ftp> exit 221 Goodbye |
Your session will also close automatically after a period of inactivity.
Finding What Files are in the Current Directory
Use the "ls" (list) command to find out what files are stored in the current directory:
| ftp> ls 200 PORT command successful 150 Opening ASCII mode data connection for file list drwxr-x--- 2 joeuser cgiusers 4096 Mar 30 22:31 cgi-bin 226 Transfer complete. |
Creating Directories (Folders)
Use the "mkdir" (make directory) command to create a new directory (folder) in the current directory:
| ftp> mkdir test 257 "/test" - Directory successfully created |
You can move into the new directory you have just created using the command "cd test" (see "Moving around your filespace" above).
Removing Files or Directories
Use the "delete" command to remove (delete) a file from the server. Use the "rmdir" (remove directory) command to remove a directory from the server. You must remove all the files from a directory before you can remove the directory. For example:
| ftp> mkdir test 257 "/test" - Directory successfully created ftp> cd test 250 CWD command successful ftp> put web_status.cgi local: web_status.cgi remote: web_status.cgi 200 PORT command successful 150 Opening BINARY mode data connection for web_status.cgi 226 Transfer complete. 155 bytes sent in 0.00 secs (1645.3 kB/s) ftp> ls 200 PORT command successful 150 Opening ASCII mode data connection for file list -rw-r----- 1 joeuser cgiusers 155 Apr 4 22:23 web_status.cgi 226 Transfer complete. ftp> cd .. 250 CWD command successful ftp> rmdir test 550 test: Directory not empty ftp> cd test 250 CWD command successful ftp> delete web_status.cgi 250 DELE command successful ftp> cd .. 250 CWD command successful ftp> rmdir test 250 RMD command successful |
Setting file and folder permissions
Use the "chmod" (change mode) command to set the permissions on a file. The permissions control who can read, write and execute the file. Permissions are most concisely expressed using a three-digit octal (base 8) number, and are reported in directory listings. If you think something on the server ought to work but it doesn't, check the permissions.
| ftp> ls 200 PORT command successful 150 Opening ASCII mode data connection for file list -rw------- 1 joeuser cgiusers 155 Apr 4 22:39 web_status.cgi 226 Transfer complete. ftp> chmod 700 web_status.cgi 200 SITE CHMOD command successful ftp> ls 200 PORT command successful 150 Opening ASCII mode data connection for file list -rwx------ 1 joeuser cgiusers 155 Apr 4 22:39 web_status.cgi 226 Transfer complete. |
These are the permissions you should use on your files and directories:
- 750 - directories (folders)
- 700 - scripts
- 640 - static content
- 600 - other (non-served files).
Uploading files to your CGI webspace
Use the "put" command to transfer a file from your local machine to your current working directory on the CGI server:
| ftp> put web_status.cgi local: web_status.cgi remote: web_status.cgi 200 PORT command successful |
Files that are to be executed by the CGI server (in other words, files that you would direct your web browser to) need should have one of these extensions:
- .cgi - shell scripts
- .pl - PERL scripts
- .php - PHP scripts
- If your script doesn't have one of these extensions, it will not get executed.
Basic CGI/Shell administration
It is not possible to log into the CGI server directly. However, many administrative tasks can be performed using small shell scripts that you can upload using FTP and then execute using CGI.
If you intend to use PHP, create and upload a file called test.php with the following content:
| <?php phpinfo(); ?> |
When you have uploaded this file to the server and set appropriate permissions (chmod 700), point your browser at this file. You should see a web page displaying the configuration of PHP, which modules are installed and the full (true) path name to your CGI webspace.
If you intend to use shell scripting or PERL upload a file called web_status.cgi with the following content:
| #!/bin/sh echo "Content-type: text/plain" echo whereis perl pwd echo and that is that |
When you have uploaded this file to the server and set appropriate permissions (chmod 700), point your browser at this file. You should see a web page displaying a few lines of ASCII output; some file paths (including the full (true) path to your webspace) followed by the message "and that is that". When you examine this script recall that all CGI applications must output a valid HTTP header followed by a blank line. That is the purpose of the "echo" commands in the second and third lines.
Unpacking an application into your CGI webspace
If you want to install a software package that includes many files, you could FTP a .tar file to your webspace and upack in using a shell script executed as a CGI file. For example, upload a file called unpack.cgi with the following content:
| #!/bin/sh echo "Content-type: text/plain" echo tar xvf some_file.tar |
When you have uploaded this file to the server and set appropriate permissions (chmod 700), point your browser at this file.
Backups
Your CGI webspace is not backed up and therefore you should devise a backup strategy to protect your data. The crudest backup strategy would be to FTP to the server periodically and fetch (a copy of) all the files in your webspace.
If you have only a small amount of data in your webspace you could tar and compress it and email it to yourself on a regular basis (for example, daily). If you have a lot of data in your webspace you might need a more sophisticated approach (for example, doing a diff and emailing the diff). None of this can be accomplished using CGI. Instead you should create a script and a cron file to run the script periodically. For example, a simple backup script might look like this:
| #!/bin/sh #this file is "/backup/backup_my_data" # 2> stuff redirects warning about stripping ../ from files. tar cf my_data.tar ../my_data_directory 2>/dev/null gzip -f my_data.tar mutt -s "my cgi data backup" -a my_data.tar.gz joe@joeuser.free-online.co.uk < backup_my_data |
and the crontab entry to run it every night might look like this:
| # my crontab file # run backup script each day at 4:00am 0 4 * * * cd backup ; ./backup_my_data |
Currently, there is no way for you (an ordinary user) to submit a crontab to cron. When you have FTP'd your backup script and crotab to your CGI webspace, you need to raise a support ticket to get your crontab applied.
More Information on the Server
If you wish more information on the configuration of PHP and Perl on the CGI server, you can find more information at the following links.
PHP Information: http://ccgi.plus.net/phpinfo.php
Perl Information: http://ccgi.plus.net/cgi-bin/perlinfo.cgi
Acknowledgement: Thanks to Community user "nac" for reviewing and updating this tutorial.
Original Article by: csogilvie - Edited by: spraxyt