In Egyptian mythology, Apep (also spelled Apepi, and Aapep, or Apophis in Greek) was an evil demon, the deification of darkness and chaos, and thus opponent of light and Ma’at (order/truth), whose existence was believed about from the Middle Kingdom onwards.

Apep
Apep formed part of the more complex cosmic system resulting from the identification of Ra as Atum, i.e. the creation of Atum-Ra, and the subsequent merging of the Ogdoad and Ennead systems. Consequently, since Atum-Ra, who was later referred to simply as Ra, was the solar deity, bringer of light, and thus the upholder of Ma’at, Apep was viewed as the greatest enemy of Ra, and thus was given the title Enemy of Ra.

As the personification of all that was evil, Apep was seen as a giant snake, crocodile, serpent, or in later years, in a few cases, as a dragon, leading to titles such as Serpent from the Nile, and Evil lizard. Indeed, his name is thought to translate roughly as Great snake. Some elaborations even said that he stretched 16 yards in length and had a head made of flint.

Apep’s name can also mean (he who was) spat out; the Romans referred to Apep by this translation of his name.

Apparently because of the spat out interpretation of the name Apep, it was said that he had been formed by Neith, who had been the deification of the primordial waters in the Ennead, from a length of spit she had spat at Nu, the deification of the primordial waters in the Ogdoad. Some subsequent commentators speculated that Apep must have been an earlier creator, and chief god, thus explaining why he had so much enmity towards Ra, but evidence for such a conclusion is slight.
After the end of the Middle Kingdom, the foreign Hyksos, now rulers over Egypt, chose Set, as their favourite deity, since he had been protector of Ra, and was associated with Lower Egypt, where their power base was. Consequently, because the foreign overlords were hated by nationalistic groups, Set became gradually demonised, and started being thought of as an evil god. Indeed, because of the extreme level of nationalism and xenophobia, Set eventually became thought of as the god of evil, and gradually took on all the characteristics of Apep. Consequently, Apep’s identity was eventually entirely subsumed by that of Set.
Battles with Ra
An Egyptian deity slays the snake-like Apep
Tales of Apep’s battles against Ra were elaborated during the New Kingdom. Since nearly everyone can see that the sun is not attacked by a giant snake during the day, every day, story tellers said that Apep must lie just below the horizon. This appropriately made him a part of the underworld, which attacked the sun each night. In some stories Apep waited for Ra in a western mountain called Bakhu, where the sun set, and in others Apep lurked just before dawn, in the Tenth region of the Night. The wide range of Apep’s possible location gaining it the title World Encircler. It was thought that his terrifying roar would cause the underworld to rumble. Myths sometimes say that Apep was trapped there, because he had been the previous chief god and suffered a coup d’etat by Ra, or because he was evil and had been imprisoned.

In his battles, Apep was thought to use a magical gaze to hypnotise Ra and his entourage, attempting to devour them whilst choking the river on which they travelled through the underworld with his coils. Sometimes Apep had assistance from other demons, named Sebau and Nak. Ra was assisted by a number of defenders who travelled with him, the most powerful being Set, who sat at the helm. Other gods who feature in later tales as the defenders include fierce warriors such as Mehen, Serket, Maahes, and Bast, together with Shu.

In a bid to explain natural phenomona it was said that occasionally Apep got the upper hand. The damage to order caused thunderstorms and earthquakes. Indeed: it was even thought that sometimes Apep actually managed to swallow Ra during the day, causing a solar eclipse, but since Ra’s defenders quickly cut him free of Apep, the eclipse always ended within a few minutes. On the occasions when Apep was said to have been killed, he was able to return each night (since he lived in the world of the dead already). In Atenism it is Aten who kills the monster, obviously, since Aten is the only god in the belief system.

text taken from wikipedia