Linux webserver useful commands – Ubuntu Part 1

Linux was very boring and complicated operating server to me when I started my career as web developer. Later on, I had to deal more with Linux web servers and my role evolved more as web server manager along with a web developer. I had to setup LAMP environment from scratch, I had to install various packages and libraries on the server and eventually started getting more interested in Linux environment. Now, its not that boring and complicated as it was many years ago.

In my day-to-day life, I had to come across many things which I never knew before. All I had to rely on different online resources, luckily there are many resources available for free or paid.
Following is a list of some common commands which a web server admin normally needs.

Change default port for SSH:

  •  sudo vi /etc/ssh/sshd_config
    If it we want to stop root login, use following :
  • PermitRootLogin no
  • sudo service ssh restart

List the hidden files

When we list files in SSH using commands like ‘ls’  or ‘ls -l’ we don’t get any hidden file or files starting with dot (.) like .htaccess, you may use following command to list such files

  • ls -ld .?*

Remove a virtual host / website completely

Recently, I had to remove a virtual host from server. We can not simply remove the public directory to remove a website/virtual host from web server, first we have to disable its link in the configuration.

For example you have virtual configuration file at /PATH_TO_HOST_CONFIG/

Following steps can help removing it properly:

  • sudo a2dissite
  • sudo rm
  • sudo service apache2 reload
  • sudo rm -rf

Last command is very dangerous, it will not ask for confirmation and will delete all the files and directories recursively. I am using it as within website there are usually hundreds of files and directories and without -f in the command, we will have to press Yes for confirmation on each file and directory that is not very handy.

Search and replace a text from a file using SSH

  • sed -i 's/original/new/g' file.txt

Following is the explanation for each segment of the command

  • sed = Stream Editor
  • -i = in-place (i.e. save back to the original file)
  • s = the substitute command
  • original = a regular expression describing the word to search or complete word itself
  • new = Text to replace the searched string(s)
  • g = make it global, means, don’t just stop at first occurrence, search and replace all occurrences
  • file.txt = file name to be processed

Leave a Reply

Your email address will not be published. Required fields are marked *