An Introduction to SSH for Web Development

Time to Read: 15 minutes Difficulty Level: Intermediate
Tools Needed: SSH access to your server, SSH client Last Updated: 26/02/2019

Introduction

For a web developer, the Linux command line can be an indispensable tool. You can access your site this way using a SSH client, such as Putty or MobaXterm for Windows or using Terminal on a Mac). There are some great out-of-the-box shell tools which can help you develop faster and smarter.

Basic navigation
Disclaimer
grep
nano
pwd
ls
tar
wget
find
mv
touch
chmod
chown
mkdir
cp
sed
rm
rm
unzip

The following commands should help with regard to the more advanced commands in this article and finding your way around the shell in general:

./

​This is Linux shorthand for the current directory that you are working in. The name of this directory can be checking using the pwd command (more on this below!).

../

​This is Linux shorthand for the parent directory to the directory you’re currently working in. For example:

cd ../

​This means “go up one directory from my current location”.

The TAB key on your keyboard auto completes file and folder names typed in the shell. So if you are in a directory which has a file called myfile.php you could type myf and then TAB and the shell will auto-complete it saving you valuable keystrokes.

The (wildcard) symbol for “everything” is:

*

​and will target all files and folders in a directory, with the possible exclusion of “hidden” files, meaning those with names beginning with a “.”.

-R

​This flag is used in several commands and means “recursive”, so will specify that you want the command performing for all the contents of the current working directory, including for sub-directories and for all files in them too.

man commandname

In this command, “man” is short for “manual”. You can run man on any Bash command to view the full manual page about it.

Disclaimer

​Some of the commands below will require use of the root user. Be careful when running commands as root: a minor syntax error can be devastating when running commands that remove or edit files. It’s extremely easy for even experienced users to make costly mistakes and require a full server restore.

As an aside, direct root login is disabled by default across our platform because it’s considered a security risk, but you can escalate to the level of the root user in a regular SSH session by using the following command and entering your root password:

su -

If you’re not feeling confident in the shell, installing Virtualbox on your computer and then installing a Linux distro without a GUI/Desktop can be a great way to experiment safely without risking breaking anything before you start running commands on your own live server.

grep

Ever needed to find out which file a class is being used but there are hundreds of files in multiple directories? Grep will find it -and almost instantly in most situations!

Example use

Navigate to the home directory of your site (usually httpdocs) and run the following command:

grep –r “the string I want to search” ./

The above command will recursively search inside every file from the web space root and the files in all sub-directories.

nano

The nano text editor is one of our developers’ favourites due to its simplicity, speed and easy search functionality (developers familiar with other text editors should be able to get going with it straight away).

Example use

In order to open a file name myfile.php, run the following command:

nano myfile.php

​If you want to create a new file called newfile.php in the current working directory using nano, enter:

nano newfile.php

​Since newfile.php does not existing in the present working directory, nano will create a new file called newfile.php.

Once inside the Nano editor, most commands are “Ctrl + ‘a key”. So common ones are

Ctrl + X

Exit the program. Nano will prompt you to save if you haven’t saved the changes you’ve made.

Use:

Ctrl + W

​to search for a string of code, which can be invaluable for editing code.

pwd

Pwd stands for “present working directory” and lets you know where you in the file structure of your server or site at the present time. This command can be very helpful for running a command in the wrong directory.

ls

​This command will list the files in the present working directory. You can use this to easily see what is where and how big your files are.

ls -lash

​This example will show all files in your current directory including hidden files. It will also output file sizes in “human-readable” form (ie: KB, MB, GB etc).

tar

Tar is a very useful program for extracting and creating backup archives of files. If you’ve just downloaded the latest Wordpress version in tar.gz format, tar is the tool to quickly extract the fileset and start working. The command is also very useful and quick for creating a backup of your files.

Example use

​This command will extract the latest.tar.gz tar-ball into the current directory:

tar –xzf  latest.tar.gz

​Adding the -v flag (for “verbose”) will perform the same operation but display an output of what tar does whilst it uncompresses the file:

tar –xzfv  latest.tar.gz

​The following command will create a complete backup named archive.tar of the folder named myDirectories and all of its subfolders. Note that the backup will be created in the present working directory and the command assumes that the myDirectories folder is also in the current working directory.

tar -cf archive.tar myDirectories/

​Lastly, this command will create a backup called archive.tar of the entire present working directory and all subdirectories.

tar -cf archive.tar *

wget

This command is essential for getting files from the web into your webspace. This command is very useful for grabbing archives of files. For example, to get the latest fileset for the popular blogging application Wordpress, run:

wget https://wordpress.org/latest.tar.gz

​and this will download the complete fileset of the latest release. You would then need to use the tar command to unpack the files.

tar –xzf  latest.tar.gz

This uncompresses the archive ready to install once you’ve chmodded the appropriate files

find

Just like it says on the tin, find finds files based on filename - and just like all Unix shell tools does it quickly too.

Example use

cd /www/vhosts/www/mydomain.com/httpdocs/

find –name myfile.php

Unlike grep, this command is automatically recursive from your current working directory.

mv

Need to move some files from one location to another in the shell? mv does what you need. It is often handy to use a wild card (*) with this command to move more than one file or the contents of a directory. Pro-tip: use .* as well to ensure that you move and pesky hidden files that start with .‘s too!

Example use

mv /myfolder/* /anotherfolder

The mv command is also used to rename existing files, so be very careful what you specify in your mv command.

touch

Although not that obviously useful, touch creates a completely empty file of your choosing. Where it can come in handy is when a application needs a config.php file creating before it can write its config data. Using touch and possibly chmod to make the file writable can achieve this very quickly.

Example use

touch config.php

chmod

Even if you’ve not been in the shell before you’ve probably know what this one does. Chmod permissions can be set using any FTchmod-ing a fileset using FTP and having time to boil the kettle while you wait.

Example use

To give the file config.php 755 permissions:

chmod –R  755 *

Or to recursively chmod every file in the current directory and directory below it to 755:

chmod –R  755 myfolder/

chown

Ever had some files that you just can’t delete via FTP? This is where chown (change ownership) often comes in very handy. Systems like Magento and other CMS applications will create files where the user and group permissions are both set to the apache user. This means that you will not be able to remove these files via FTP because your user does not have sufficient permissions.

In order to remove these files, you’ll need to chown them in order to update the file’s user and group ownership.

Example use

Change the owner and group of file.php to user myuser and group mygroup with the following command:

chown myuser:mygroup file.php

​You can modify the permissions of a single file or folder to allow it to be removed via FTP using a chmod command:

chmod 755 myfolder/

Alternatively you can chown files/folders recursively by using the -R flag:

chmod –R  755 myfolder/

mkdir

​This command makes directories/folders in your webspace.

Example use

mkdir  nameofmyfolder

​This will create a directory called nameofmyfolder.

cp

This command copies files​.

Example use

The following command will copy originalfile to create a new file called copyoforiginalfile:

cp orginalfile copyoforignafile

​Similarly the following command will copy (recursively) all files and folders within myfolder to mynewfolder:

cp –R ./myfolder   ./mynewfolder

​Finally the following will copy the entire contents of mywebfiles into the folder myotherwebfiles inside another webspace:

cp ./httpdocs/myfolder/mywebfiles/* /var/www/vhosts/myotherwebsite.com/httpdocs/myotherwebfiles/

sed

This command is Linux’s answer to “find and replace”. As with other more complex shell commands, the syntax of this command can be difficult to remember so its often handy to copy and paste a sample and adjust it for your needs.

Example use

The following command will find the string “original text” in example.php and replace it with the string “altered text”:

sed 's/original text/altered text/g' example.php

This example will delete the text ‘original text’ from the file example.php altogether:

sed 's/original text//g' example.php

rm

Removes files and directories. Please be aware: it’s possible to seriously damage or entirely destroy your site (and server!) with this command, so always exercise extreme caution.You can do this by:

  • never running rm commands as the root user unless you absolutely have to
  • always ensuring that you’re in the correct directory before running commands - use pwd to check!
  • double-checking all of your commands to ensure they’re what you want to do, before you run them.

Example use

To remove the file file.php from the current directory, run the following:

rm file.php

rm

Removes files and directories. Please be aware: it’s possible to seriously damage or entirely destroy your site (and server!) with this command, so always exercise extreme caution.You can do this by:

  • never running rm commands as the root user unless you absolutely have to
  • always ensuring that you’re in the correct directory before running commands - use pwd to check!
  • double-checking all of your commands to ensure they’re what you want to do, before you run them.

Example use

To remove the file file.php from the current directory, run the following:

rm file.php

​It may be beneficial to specify the full absolute path to the file/folders that you wish to remove, so that you can be sure you’re not removing something that you don’t intend to. So a safe and accurate way to remove the file.php file from your webroot folder would be:

rm /var/www/vhosts/mydomain.com/httpdocs/file.php

​It may be beneficial to specify the full absolute path to the file/folders that you wish to remove, so that you can be sure you’re not removing something that you don’t intend to. So a safe and accurate way to remove the file.php file from your webroot folder would be:

rm /var/www/vhosts/mydomain.com/httpdocs/file.php

...assuming that your webroot is the default httpdocs directory, of course!

Similarly, if you want to remove everything in the current directory and all files and folders below it, you can use the -rf flags. The “f” means “forceful”, which suppresses any warnings about deletions. Again the “r” is for “recursive” operation. So the command you would use for this is as follows:

rm –rf *

...assuming that your webroot is the default httpdocs directory, of course!

Similarly, if you want to remove everything in the current directory and all files and folders below it, you can use the -rf flags. The “f” means “forceful”, which suppresses any warnings about deletions. Again the “r” is for “recursive” operation. So the command you would use for this is as follows:

rm –rf *

​Or even the safer version:

rm –rf /var/www/vhosts/mydomain/httpdocs/desired-directory-here/*

​Or even the safer version:

rm –rf /var/www/vhosts/mydomain/httpdocs/desired-directory-here/*

​Finally, if you wanted to remove the directory myfolder from your current working directory, as well as all files and subfolders/directories from underneath it, you would run:

rm –rf myfolder/

​Finally, if you wanted to remove the directory myfolder from your current working directory, as well as all files and subfolders/directories from underneath it, you would run:

rm –rf myfolder/

​Or again, the safest version of the command would be:

rm –rf /var/www/vhosts/mydomain.com/httpdocs/desired-directory-here/	myfolder/

​Or again, the safest version of the command would be:

rm –rf /var/www/vhosts/mydomain.com/httpdocs/desired-directory-here/	myfolder/

unzip

​Use the unzip command for unpacking any files that have been compresssed into .zip format, as opposed to the common tar.gz compression.

Example use

The following command will unzip the archive myzipfile.zip in the current working directory, and despite the contents in the same directory.

unzip myzipfile.zip