Basic server administration tasks

Linux commands used to handle various minor issues.

Start, stop, and restart Apache

If Apache is unresponsive and you can’t load anything in a browser, first see if it’s running:

$ ps aux | grep apache

If you see multiple lines of output, Apache is running but has become unresponsive. Restart the service:

$ sudo /etc/init.d/apache2 restart

If the above command shows you no Apache processes, it has crashed or otherwise stopped. Start it again with the following:

$ sudo /etc/init.d/apache2 start

Should you need to stop the service for any reason, the command is similar to the others:

$ sudo /etc/init.d/apache2 stop

Restart MySQL

If Apache is serving pages but you’re getting database errors, you may need to restart MySQL:

$ sudo /etc/init.d/mysql restart

Check disk space

To check the available disk space:

$ df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              19G  1.3G   17G   8% /

Check resource usage

Get a list of all running processes:

$ ps aux

Show current processes sorted by CPU usage:

$ top

Once top is running you can sort by memory usage by typing F, then m, followed by enter. To switch back to sorting by CPU usage use F, k, enter.

Quit top by typing q.

Reboot a server

Should you need to reboot the entire server due to the usual service restart not working:

$ sudo reboot

This assumes you can SSH into the server to begin with. Alternately, if you have a Slicehost or Linode VPS, you can use the soft reboot control in the administration control panel to achieve the same effect (this method doesn’t rely on SSH being available). In extreme circumstances, where a soft reboot doesn’t work, you can perform a hard reboot of Slicehost VPSs from this same control panel, which has the effect of pulling the plug on the server and restarting it.