In this tutorial, I’m going to write about a number of things that will be useful to you if you have your own website hosting. This article is geared for beginners, and requires no prerequisites apart from some common sense! Now, I host nointrigue.com on a shared Linux server with Bluehost, which means that my website shares a physical computer with a number of other users. This setup is fairly common, so I’ll be writing with an assumption that this is the kind of arrangement that you have. I’ll cover some basic, but useful, shell commands to make your life easier, and explain how to install additional software on your host.
Typically, you will have FTP access to your shared host, but not all shared hosts allow shell access to your server. To follow this tutorial, you will need to have shell (SSH) access to your host (which, by the way, I am assuming is a Linux box, which is the usual case). Even if your hosting package has this feature, you may have to file a support request to enable it, as is the case with Bluehost (see the SSH link in cPanel). (Actually, I lie. You can still do the installing software part, but it may prove far more difficult — see below for details.)
To connect to your server, you will need an SSH client on your computer. If you are using Mac OS X or some version of Linux, you will probably have OpenSSH installed already. If you are using Windows, you can either install Cygwin (make sure you select the OpenSSH package) or use PuTTY. If you don’t have a need for a Unix-like environment on your Windows machine, and you just want SSH, the latter is probably a simpler choice.
- If you are using OpenSSH, open up a command prompt or terminal, and type
ssh firstname.lastname@example.org in the username and your domain name as required.
- If you are using PuTTY, run
putty.exe, enter your host name and press Open.
For both, if you are prompted about storing the server’s fingerprint, just type or click yes.
You should now be presented with a shell prompt, where you can type in commands (which you issue by pressing Enter after typing them in). It may seem like a regression back to the 1980s days of MS-DOS, but it’s quite a convenient way of controlling a server that’s far away from you.
To avoid having to enter your password every time, you can use an SSH agent.
Getting to know your shell commands
This is not intended to be an exhaustive list of commands that you can type; instead, my aim is to write down a number of commands that inexperienced users can just quote verbatim to do what they need to do. Parts in italics are to be replaced as appropriate.
cd directory: this is how you enter into a directory. Commands are always executed relative to the current directory. ~ is your home directory. Just type
cdto return to your home directory.
ls: display the contents of the current directory. Type
la -lato make it display hidden files and detailed information about your files.
mkdir directory: create a directory.
rmdir directory: remove a directory. The directory must be empty.
rm file: delete a file. Use
rm *to delete all files in the current directory, or use
rm -rf *to delete all files and subdirectories in the current directory (use with caution!).
cp oldname newname: copy a file.
mv oldname newname: move a file. This command is also used to rename files, since the act of renaming is just the same as moving. You can move a file up a directory by using
mv file .., and a file into a subdirectory using
mv file subdirectory.
pwd: display the full path to the current directory.
less file: view a file. Use the arrow keys or page up and page down to move around the document. Press q to exit.
find -name wildcard: use this to search the current directory and its subdirectories for a file with a name that matches the wildcard expression.
ln -s linkname filename: a symbolic link is like a shortcut that allows a file to appear in two different places — but both the existing name and the new link name refer to the same file.
To find out more about these commands, type
man command, and press q to exit. If something screws up, pressing Ctrl+C should kill whatever command you’ve just issued.
Editing on the go
Another activity that I use SSH for quite a bit is editing configuration files directory on the server. That saves me having to download the file to my computer, making the changes, and then uploading it back up again. A good text-based editor is
vim, and here are just a few commands to get you started.
- To start vim with a file open, use
- To move around, the arrow keys, home, end, page down and page up work as expected.
- To go into editing mode, press
i, and then any keys that you press will be added to the document itself. To get out of this mode, press Esc.
- When you’re not in editing mode, you can issue commands to the editor. For example:
:q!to exit without saving changes,
:wqto save then exit.
- That should be enough to edit a file, but if you want to know more, type
:helpand there are links to on-line tutorials.
Installing software on your shared host
If your server is a shared host, you can be pretty sure that you can’t install or modify the software installation on the server on a global level. You can, however, install additional software just for yourself in your home directory (which you can definitely write to!). If you don’t have SSH access to your server, you could try and recreate a similar environment to that on your server, compile the programs (using, perhaps, cross compilation), and then upload the resulting executables like you would normally — let me know if you can get that to work!
Why would you want to install additional software? There are plenty of reasons for that. For example, you might want to run Ruby scripts, but your host hasn’t updated the software installed since the dark ages. Alternatively, you might want to run software that isn’t typical for a shared web server; for example, I have Subversion (a version control system) and Privoxy (a proxy server) installed. I also have a LaTeX installation so I can get maths rendering in MediaWiki.
- Create a directory for downloading and compiling files: I call mine
src, and it’s a subdirectory of my home directory ~. This is useful in case you make a mess; that way, you can use
rm -rf *(see above) to obliterate everything in that directory and start again.
- Download the source files: you will need to download the source files, and create the executable (binary file) yourself, instead of downloading the compiled executable. This will ensure that there are no problems installing or using the program. Open source projects will have the source code available. To download something to the current directory, use
- Unpack the source files: typically, the source file will have one of several extensions. If it ends in
tar -xzvf file. If it ends in
tar -xjvf file. If it ends in
- Read the installation instructions: there’s often a
INSTALLfile amongst the files that were unpacked. Read it using
- Compile it: if there are installation instructions, follow those. However, if there’s a part where it asks you type in
./configure(which is almost always), then you should type
./configure --prefix=$HOMEinstead. This will make sure that your software is installed in your home directory. If there are no installation instructions, a typical sequence of commands is:
- Test it out: your program will probably have been installed into
~/sbin, where ~ is your home directory. Now, you’ll notice that you can only type the name of the program when your current directory (which you change using
cd) is the directory where that executable resides. In order for the shell to find your newly installed program, regardless of what directory it resides in, you’ll need to add it to your
PATHenvironment variable. To do this, you need to add a line to the
.bashrcfile that resides in your home directory (create one if it doesn’t exist). For example, my line looks like this:
export PATH=$PATH:~/bin:~/bin/tetex, and that means that I’ve added
~/bin/tetexto the locations where the shell will search for programs; note the use of the colon to separate paths.
That’s all for now folks! I hope the instructions are clear, and that your days of being a webmaster are more productive than before. If you want to learn more, Google is your friend.