BOSTON — David Ortiz and Mike Napoli each homered and drove in two runs, Wade Miley pitched seven strong innings and the Boston Red Sox beat the Texas Rangers 4-3 on Tuesday night.

Boston got a scare when Leonys Martin led off the ninth with a pinch-hit homer off Koji Uehara, ending the closer’s streak of eight straight hitless appearances. Uehara, however, held on for his 10th save in 11 opportunities.

After going 5-5 on their road trip, the Red Sox got off to a strong start on their six-game homestand with one run and four hits in the first inning while Miley (3-4) held the Rangers scoreless through the fifth.

Boston took a 1-0 lead in the first against Yovani Gallardo (3-6) when Dustin Pedroia doubled with one out and scored on a single by Ortiz. Singles by Hanley Ramirez and Pablo Sandoval loaded the bases, but all three runners were stranded.

Napoli, who was 4 for 27 on the trip, led off the fourth with his fourth homer of the season before Ortiz made it 3-0 with his sixth homer with one out in the fifth. Ramirez followed that with a double and scored on a single by Napoli.

Miley put runners in scoring position in five of his seven innings but allowed runs in just one of them.

Trailing 4-0, the Rangers scored two in the sixth on a double by Kyle Banks, a run-scoring single by Thomas Field and an RBI triple by Robinson Chirinos.

But Miley pitched a perfect seventh and Junichi Tazawa struck out two in the eighth before Uehara preserved the lead.

Boston, which scored two or fewer runs in five of its previous six games, also left the bases loaded in the seventh when Ramirez hit a ground-rule double and Sandoval was hit on the left knee by a pitch and left the game. Shane Victorino then drew a two-out walk, but Xander Bogaerts grounded out.