We have synthesized and characterized porphyrin structures by UV–visible, IR and fluorescence spectroscopy studies and single crystal XRD analysis; H2(TPP) (I) (meso-tetraphenylporphyrin), [Mg(TPP)(DMF)] (II); (DMF: dimethylformamide) and [Mg(TPP)(H2O)]0.2(4-CNpy) (III); (4-CNpy: 4-Cyanopyridine). The crystal packing of the porphyrin structures is stabilized by intermolecular hydrogen bonds and by intermolecular CH⋯Cg π interactions where Cg is the centroid of the phenyl and pyrrole rings. UV–Vis spectroscopic data confirmed the creation of Mg-meso-porphyrin complexes by the red-shifted Soret bands for our derivatives compared to the free-base porphyrin. The optical gap energy values of (I), (II) and (III) are 1.92, 2.07 and 2.00 eV. Gram-positive and Gram-negative bacteria were tested against free porphyrin (I), metallated porphyrin (II) and complex (III). The results show that the magnesium porphyrin derivative is highly effective compared to H2TPP free-base porphyrins and metallated porphyrin [Mg(TPP)] and has higher inhibition character against the tested bacteria. The molecular docking of the complex (III), in the active sites of bacterial proteins was carried out to detect the degree of antibacterial activity of recognition.