Industry Leader in Digital Business

Howto: Managing your webserver using a CLI (part 1: the basics)

Until the nineties most computers did not include a rich graphical interface as the one most users are nowadays used to. The interface with which most users are currently familiar is a so called WIMP interface, which stands for ‘window, icon, menu, pointing device’. Developed by Xerox PARC in 1973, it was introduced to replace the older Command Line Interfaces (CLI) that only used textual commands to accomplish tasks. But even in 2010 the CLI has not been completely expelled by the WIMP interface and it can still be found on most servers. Lately, I have been managing several (web)servers from a distance using the CLI for a variety of clients. As I deem it important that all webdevelopers and website administrators have at least some basic knowledge about CLI, I will cover the most common tasks these users will want to accomplish in two articles.

CLI Software

In Windows the required software for using the CLI is not included in the default installation, but there is a great tool available for free that you can use: PuTTY. After you’ve downloaded PuTTY, you can simply start the program by double clicking the downloaded software since there is no need to install anything. Although Linux does sometimes come with a pre-installed CLI client (most likely SSH), you can also install PuTTY from your software center (Ubuntu and similar distros), through the Command Prompt using the command sudo apt-get install putty or download it from the previously linked website.

Connecting to the server

Most likely you received an IP-address (the host), a username and password from the hosting provider when your account created. This information can be used to connect to your webserver, if your hosting provider allows it. Please bare in mind that your provider will not talk about a ‘CLI’ compatible server, but that it will use the name of the protocol used to secure the connection: SSH. When starting PuTTY for the first time, you might notice that it is rich in options. For a simple connection you luckily do not need all these options: simply fill in the host name and click ‘Open’. A new window will open and prompt you for a username and password to establish the connection. In Linux you might also choose not to use PuTTY, but instead use the build in SSH software. In this case you can connect to the server, by issuing the following command and filling in the password when prompted: ssh user@127.0.0.1. Obviously, you must replace the username and host with the details required by your server.
After the connection to the server has been successfully established, you will see information in your new window that will probably look somewhat similar to the following example:

{code}Last login: Sat Apr 24 14:06:39 CEST 2010 from [Your Provider] on pts/2
user@SYcommerce-Server ~ ${/code}

The tilde (~) represents your home-directory on the server: the place where you start browsing. There might be parent and subdirectories that you are allowed to browse using the commands you will learn in this article. Since you are now successfully connected, you can now start issuing command that will be executed directly once you hit [Enter]. In the following paragraphs you will learn some basic commands to get you started in your new environment.

Navigating using the CLI

You can show a list of all files in the current directory using ls -a or ls -al followed by an [Enter]. The latter gives detailed information about the files in the current directory (including for example hidden files) as the example below shows. Please note that only ls is required for basic usage, but that the addition of -a has been made to show hidden files as well.

{code}user@SYcommerce-Server ~ $ ls
classes images includes index.php license.txt robots.txt templates
user@SYcommerce-Server ~ $ ls -a
. .htaccess images index.php robots.txt
.. classes includes license.txt templates
user@SYcommerce-Server ~ $ ls -al
total 26
drwxr-xr-x 13 user www-data 4096 Apr 24 21:24 .
drwxr-xr-x 4 user www-data 16 Sep 22 2009 ..
-rwxrwxr-x 1 user www-data 2141 Feb 2 18:07 .htaccess
drwxrwxr-x 3 user www-data 4096 Feb 2 18:09 classes
drwxrwxr-x 5 user www-data 24 Feb 2 18:10 images
drwxrwxr-x 3 user www-data 72 Feb 2 18:10 includes
-rwxrwxr-x 1 user www-data 2047 Feb 2 18:13 index.php
-rwxrwxr-x 1 user www-data 35147 Feb 2 18:13 license.txt
-rwxrwxr-x 1 user www-data 280 Feb 2 18:13 robots.txt
drwxrwxr-x 4 user www-data 16 Feb 2 18:13 templates
user@SYcommerce-Server ~ ${/code}

As you can see, directories can be identified by the leading ‘d’ in the details overview as opposed to the ‘-‘ used for files. In some CLI you can also identify different file types or even different user permissions by the colors that are used within the file list. As it it not very useful to navigate only one directory, there are also commands to visit a directory below or above the current directory. For this, the command cd has been introduced which is almost similar to the command used on DOS systems in the eighties and nineties. You can navigate to parent directories by issues the command with two dots as parameter: [code]cd ..[/code] (note that one dot represents your current directory). To navigate to a subdirectory you have to include the name of the directory you want to visit as a parameter. For example: if you want to visit the subdirectory ‘templates’, then show the files in this directory and return to the parent directory, you issue the following three commands:

{code}user@SYcommerce-Server ~ $ cd templates
user@SYcommerce-Server ~/templates $ ls
default scanyours_home scanyours_portal
user@SYcommerce-Server ~/templates $ cd ..
user@SYcommerce-Server ~ ${/code}

Editing files

Once you have found the file that you want to modify, you can open it with one of the software packages that are available. In most cases, either Nano or Joe will be installed to open the file for editing. But, since Nano is most likely to be installed at most hosting providers, we will use Nano in our example to explain the basics of file editing. Bare in mind, that because of the limited interface (only text with no graphical elements) you can not open complex files like images or PDFs, thus you should limit yourself to files containing only ASCII characters. To open a file for editing you can simply use the command of the selected text editor with as argument the filename in 9 out of 10 cases. For Nano for example, one would for example use nano index.php to edit the file ‘index.php’ in the current directory. Within Nano you can use a small variety of key combinations for simple tasks, including cutting, pasting & exiting (which will prompt you about saving). For the most useful and simple commands you can refer to the list below:

Ctrl + K Cut current line
Ctrl + U Page cutted lines
Ctrl + W Search for a word / characters
Ctrl + C Displays the current line/position of the cursor
Ctrl + X Exit
Ctrl + G Help

Creating new files / directories

Through the CLI you can also create new directories and files using two simple commands. To create a file you use the command touch with the filename as parameter and for the creation of a directory you can specify a parameter for the command mkdir. In the example below, a new directory is created which is filled with one file. As you have just learned, you can edit the contents of this file using Nano or other software so it suits your needs.

{code}user@SYcommerce-Server ~ $ mkdir documents
user@SYcommerce-Server ~ $ cd documents
user@SYcommerce-Server ~/documents $ touch my-document
user@SYcommerce-Server ~/documents $ ls
my-document
user@SYcommerce-Server ~/documents ${/code}

Removing files / directories

Removal of files and directories through the CLI is as easy as it is dangerous, because it offers you the possibility to render your server or website useless with very simple commands. The easiest and safest way to remove a single file is to use the command rm with the filename as parameter. The command to remove directories is rmdir and takes the directory as a parameter. However, using these two commands to remove a lot of files and directories can take a very long time and is not very efficient. To make life easier, you can use wildcards in the parameter to target multiple files or directories with one command. There are two wildcards available for you to use: ? and *. The question mark is used to replace one character in the given name, while the asterisk can replace one or more characters. Though these wildcard enable you to easily manage a large amount of files, you must use wildcards with caution. An accidentally misplaced wildcard can remove sensitive or important files within the blink of an eye, which is not easily undone. To ensure that the use of wildcards is clear, both the removal of single files as well as the removal of multiple files using wildcards are explained in the example below. First the file and directory created in the previous paragraph will be removed, after which we will remove all files with an extension in the parent directory:

{code}user@SYcommerce-Server ~/documents $ rm my-document
user@SYcommerce-Server ~/documents $ cd ..
user@SYcommerce-Server ~ $ rmdir documents
user@SYcommerce-Server ~ $ rm *.*
user@SYcommerce-Server ~ $ ls -a
. .. classes .htaccess images includes templates
user@SYcommerce-Server ~ ${/code}

As you can see it is very easy to do some basic file managent through the CLI. In the next part of this small howto I will cover rights management through the commands chmod and chown, so you can improve the security on your server through clever CLI management. Feel free to leave a comment with requests, feedback and/or questions.

Related Posts