Some useful Linux commands for web developers

7 May, 2013
Updated
1 May, 2013
Created

Here’s a list of some Linux commands that I find myself using everyday. Most of these commands you might already know, but it doesn’t hurt to repeat them here. As I use and love Ubuntu, most of these commands are based on Ubuntu but most of them will apply to others distros as well.

First up is creating a new user under Linux. The reason why you might be wondering is because I prefer to run each virtual host in Apache 2.2 under a separate user using the apache2-mpm-itk module. This allows for more security to individual vhosts, specially on a shared hosting environment. I will write about how you can run each virtual host in Apache under its own user.

Syntax → sudo useradd -s SHELL -m -d HomeDirectory -g GroupName UserName

  • -s SHELLLogin shell for the user.
  • -mCreate user’s home directory if it does not exist.
  • -d HomeDirHome directory of the user.
  • -g GroupGroup name or number of the user.
  • UserNameLogin id of the user.

Example → sudo useradd -s /bin/sh -m -d /home/www/ -g web-vhostname

Once you have created a new user, you should create a password for the user. Use the following command.

Create or change password for a user → sudo passwd UserName.

Note: If you want to change the password for an existing users using the above command, the system will ask to you to provide for the existing password.

Next up is SSH key generation. If you use git (you definitely should use it if you aren’t), it will save you a lot pulling and pushing changes to your repository. I use Bitbucket. The following commands will help you create SSH public and private key pair for you newly created web-user. Login as the user for whom you want to generate SSH keys by typing su UserName. Enter password when prompted for. Make sure you are in the home directory for that user. If not type cd ~ to change to the home folder.

Generating SSH Key for a user → ssh-keygen -t rsa.

Third most used set of commands for me is enabling and disabling vhosts in Apache, enabling and disabling modules and restarting Apache. These are a few command but they are short and easy to remember.

Note: In order for you to be able to enable or disable a module or a site, it must first be installed or available.
→ To find out the list of available modules for your Apache 2.2 installation, type
ls -l /etc/apache2/mods-available.
→ Similarly, for a list of all enabled modules, type ls -l /etc/apache2/mods-enabled.
→ To get a list of all configured vhosts, type ls -l /etc/apache2/sites-available.
→ All enabled vhosts can be listed by typing ls -l /etc/apache2/sites-enabled.

Enabling a module → sudo a2enmod ModuleName.
Disabling a module → sudo a2dismod ModuleName.
Enabling a site → sudo a2ensite VhostName.
Disabling a site → sudo a2dissite VhostName.
Restart Apache → sudo apache2ctl restart or sudo /etc/init.d/apache2 restart.

Finally, any list of useful Linux commands won’t be complete without the following two commands.

Changing owner and group of:
A file → sudo chown UserName:GroupName FileNameOrPath.
A directory → sudo chown UserName:GroupName DirectoryNameOrPath.
All contents under a directory recursively → sudo chown -R UserName:GroupName DirectoryNameOrPath.

Changing permissions of:
A file → sudo chmod XXX FileNameOrPath.
A directory → sudo chmod XXX DirectoryNameOrPath.
All contents under a directory recursively → sudo chmod -R XXX DirectoryNameOrPath.

Note: In the above command, each X needs to be replaced by a number between 0 and 7 depending on the permission you want to grant.. Here’s a brief explanation. The X in the first position from the left represents permissions of the user, the second X represents permission of the group and the last X is used to define permission of everyone else. You have to select the number based on the level of permission you want to grant out of read, write and execute. The list below summarises the numbers associated with the permission levels.

0 → No Permission
1 → Execute Permission
2 → Write Permission
3 → Execute + Write Permission
4 → Read Permission
5 → Read + Execute Permission
6 → Read + Write Permission
7 → Read + Write + Execute Permission

These commands are not the only once that you would find yourself using as a web developer, but they are the most frequent ones. Let me know if you think I missed any.