The invention discloses a halogen-free flame retardant epoxy resin used for an electronic packaging material and a preparation method of the halogen-free flame retardant epoxy resin. The flame retardant epoxy resin comprises the following components in parts by weight: 50-70 parts of liquid epoxy resin, 2-10 parts of a reactive diluent, 5-15 parts of a curing agent, 0.2-2 parts of a curing accelerator and 2-20 parts of phosphoric acid doped polyaniline nanometer material, wherein the phosphoric acid doped polyaniline nanometer material has the inflaming retarding effect in the epoxy resin, and a large number of P-N bond intermediates generated during the combustion process of the phosphoric acid doped polyaniline nanometer material is a phosphorylation agent which has better inflaming retarding effect than the conventional phosphorus compound; the nitrogen content of the prepared epoxy resin is 0.8-5 wt%, and the phosphorus content of the prepared epoxy resin is 0.2-3 wt%. According to the invention, the flame resistance of an aromatic nucleus, the flame retardance of a nitrogen heterocyclic ring and organic phosphorus as well as the toughening effect of a polyaniline nano-structure are combined, so that the synchronous improvement of the multiple properties of the material is realized, and further, the requirements on the toughness and halogen-free flame retardance of the electronic component packaging material are met.