There are many ways to protect yourself from malevolent magic using magic items, rituals or spells. This kind of magic is called apotropaeic magic. Along with curses, it is the most common purpose for magic.
Witch bottles are filled with protective items like salt and nails, and serve as magical decoys. Physical barriers of special ingredients can be sprinkled or placed across or near doorways. Prayers can be written near or on doorways as well.
Apotropaeic magic has been used for all of recorded history, and probably before that! Here is an article about ancient apotropaeic witch bottles: http://www.ancient-origins.net/artifacts-other-artifacts/good-and-evil-witch-bottles-countermagical-devices-through-history-006649