Apep, also called Apophis, was an 'evil' deity in Egyptian mythology, the personification of that of darkness and chaos; thus being opposite to Maat, the ancient Egyptian concept of truth, balance, order, law, morality, and justice.
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/serpent, or occasionally as a dragon in later years, leading to such titles as Serpent from the Nile and Evil Lizard. Though because of his size considered to be a 'python' by some Egyptologists, this identification does not square with texts that mention Apep's 'poison' and never mention an act of strangling.
Also, comparable hostile snakes as enemies of the sun god existed under other names (in the Pyramid Texts and Coffin Texts) already before the name Apep occurred.
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.
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, storytellers said that Apep must lie just below the horizon. This appropriately made him a part of the underworld. 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 gained him 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 hypnotize 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 Sek and Mot. Ra was assisted by a number of defenders who travelled with him, the most powerful being Set, who sat at the helm.
In a bid to explain certain natural phenomena 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).
However, in other myths, it was the cat goddess Bast, daughter of Ra, who slew Apep in her cat form one night, hunting him down with her all seeing eye.
Apep was not so much worshipped, as worshipped against. His defeat each night, in favour of Ra, was thought to be ensured by the prayers of the Egyptian priests and worshipers at temples. The Egyptians practiced a number of rituals and superstitions that were thought to ward off Apep, and aid Ra to continue his journey across the sky.
In an annual rite, called the 'Banishing of Apep', priests would build an effigy of Apep that was thought to contain all of the evil and darkness in Egypt, and burn it to protect everyone from Apep's influence for another year, in a similar manner to modern rituals such as Zozobra.
In some myths, Apep was an earlier and discarded sun-god himself. This helps to explain the snake's strength and his resentment of the daily journey of the sun. In Seth's battle for the throne of Egypt, he claimed that he was stronger than Horus because it was he that stood at the prow of the solar barque and defeated the enemies of Ra.
A book called, The Books of Overthrowing Apep contained a list of his secret names and a number of hymns that celebrate Re's victories. According to the book, Apep had been previously killed, hacked to pieces, dismembered and thrown into the abyss. However, he always came back to life to attack Re the next day. Egyptians would go to the temples and make images of snakes out of wax. They would spit in the images, then burn and mutilate them. Doing this and reciting the spells in the Books of Overthrowing Apep helped ensure Re's continued success and victory over the snake.
Titles of the chapters of the first book are as follows:
Chapter of Spitting Upon Apep
Chapter of Defiling Apep with the Left Foot
Chapter of Taking a Lance to Smite Apep
Chapter of Fettering Apep
Chapter of Taking a Knife to Smite Apep
Chapter of Putting Fire Upon Apep
Following books describe in detail the destruction which will fall upon Apep. According to these, Apep will first be speared, then sliced with red-hot knives so that every bone of his body has been separated, his head, legs and tail are cut off. His remains are then scorched, singed, and roasted, finally to be consumed by fire. The same fate awaits Apep's confederates and everything which formed parts of him, them, and all their offspring (their shadows, souls, doubles, and spirits).
Book of the Dead (papyrus of Ani), Chapter XVII
J.F.Borghouts, Book of the Dead
P.Kousoulis, Magic and Religion as Performative Theological Unity: the Apotropaic Ritual of Overthrowing Apophis
H. Te Velde, Seth, God of Confusion
J. Assmann, Egyptian Solar Religion in the New Kingdom