What should I work on to help run longer without legs hurting

Muscles and flexibility are both required for long distance running.  For muscles, glutes, quadriceps, hip flexor, core, hamstring, and calf muscles are necessary to lessen impact to your joints.  These muscles are necessary to ensure your legs can take training with high volume, intensity, and frequency.  For glutes, squats, deadlift, and hip thrusts are recommended.  For quadriceps, goblet squats, leg press, front squats, and walking lunges are recommended.  For hip flexor, bridges, Bulgarian split squat, mountain climbers, and straight leg raises are recommended.  For core, planks, leg raises, and crunches are great.  For hamstring, deadlift, good mornings, and kettlebell swings are great.  For calf muscles, calf raises, jump rope, and donkey calf raise are great exercises.

When it comes to flexibility for runners, standing hamstring stretch, piriformis stretch, figure four stretch, pigeon pose, butterfly, and lunging hip flexor stretches are great stretches to do after running.  Before running, dynamic stretches that warm up your body are recommended like high knees, walking lunges, hip circles, and leg swings.  Each stretch should be repeated 2 to four times with 15-30 second intervals and 5 seconds between as a break.  15 to 30 minutes is recommended for static stretches after running and 5 minutes for dynamic stretch prior to running.