How to Create a Swap File on Ubuntu

An important step of optimising a SSD server instances especially those with a small amount of available RAM is to create a swap file.

First step is to allocate the space say 4GB.

sudo fallocate -l 4G /swapfile

Then set permissions

sudo chmod 600 /swapfile

Now to make the swap file then enable it.

sudo mkswap /swapfile
sudo swapon /swapfile

Now to make the swap file permanent on reboot edit the file /etc/fstab with the following contents:

/swapfile  none  swap  sw  0  0

Last step involves tweaking some settings regarding the swappiness and cache pressure. To do this Edit the file /etc/sysctl.conf with the following contents:


After a reboot the swap file should load automatically with the correct configuration.

Apache Site Setup on Ubuntu

Easily configure and create an Apache website on a Ubuntu LAMP server.

Firstly ensure all directories are created with the correct permissions.

mkdir /home/yourhomedir/public
mkdir -p /home/yourhomedir/public/{public,log,backup}
chmod -R a+rx /home/yourhomedir/public

Next edit the file here at /etc/apache2/sites-available/

<VirtualHost *:80>
DirectoryIndex index.php index.html
DocumentRoot /home/yourhomedir/public/
LogLevel warn
ErrorLog /home/yourhomedir/public/
CustomLog /home/yourhomedir/public/ combined

<Directory /home/yourhomedir/public/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted

Now enable the website and restart the web server on Ubuntu by the 2 following commands.

service apache2 reload
Your website should now be live on your Ubuntu web server.

How to kill all process of a script in a single command line

Instead of checking out the running processes and finding the process number you want to kill then killing the process manually sometimes it can be worthwhile writing a simple single line script to kill common background processes that you may  start or stop often.  Just substitute myscriptname.extension to whichever script you want to stop.

ps -deaf | grep -v grep | grep myscriptname.extension | awk '{ print $2 }' | xargs sudo kill -9


Deleting files from a super large directory on Linux

Sometimes on linux if you have super large directories it is not possible to use the simple wild card searches to remove files or a directory due to memory limitations. I’ve encountered this problem myself with a directory filled with an insane number of .JSON files for a project.

Here is how I solved it but please BE WARNED that the following wild card deletes are recursive and can be dangerous if used incorrectly.

find . -name "*.json" -print0 | sudo xargs -0 rm

Just substitute the *.json for whatever file type or wild card search you wish to remove.

How to clear the DNS Cache on a Mac

Sometimes when you create a new website if you make the mistake of browsing to it too early after its DNS creation or perhaps change webservers and wish to see where the DNS is currently pointing to it becomes necessary to clear the DNS cache.

This task can be performed simply on a Mac by the following command line function.

sudo killall -HUP mDNSResponder


How to create a new database, user and grant permissions via the command line

When creating a new website such as a wordpress installation quite often it will be necessary to create your own database which requires creating a user and granting permissions. This can be quite easily performed by many tools such as phpMyadmin but they aren’t really necessary to be installed on your web server. If you only need to perform only simple tasks like this you are much better off from a security perspective to just perform the tasks via the command line.

CREATE DATABASE databasename;
CREATE USER [email protected] IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON databasename.* TO [email protected];


How to easily remove DS_Store files on the Mac

One little nuisance many Mac developers face is the addition of the DS_Store files to directories and the need to quickly and easily remove them when you want to ship an archive file.

This can be easily achieved in a particular directory by the following command line.

find . -name '*.DS_Store' -type f -delete

Now zip up your clean directory and be on your way!

Common vi Text Editor Keyboard Shortcuts

One thing is always certain in a linux environment is the existence of the vi text editor. It is always useful to know a few of them to make navigating vi just that little bit easier.

F: Move forward
B: Move backwards
P: Move to previous line
N: Move to next line
A: Move to the beginning of the line
E: Move to the end of the line
T: Transpose characters
D: Delete character to right
K: Kill the rest of the line
L: Center the insertion point in the window