LYNCHBURG, Va. — President Trump, an often profane New Yorker who emerged last year as an unlikely champion for evangelical Christians, was rewarded with some sanctuary by the conservative faithful at Liberty University on Saturday after a brutal week in Washington.

Mr. Trump delivered his first commencement address as president to a Linus-blanket crowd of 50,000 at the university’s football stadium, delivering a pep talk to the graduates — and possibly to himself — after scrambling to contain the political fallout from his sudden firing of the F.B.I. director, James B. Comey, on Tuesday night.

“Never, ever, ever give up,” said Mr. Trump, who, since overcoming long odds to win the presidency, has mused about how unexpectedly difficult the job has been and how he misses his old routines.

“There will be times in your life you’ll want to quit, you’ll want to go home, you’ll want to go home to, perhaps, to that wonderful mother that’s sitting back there watching you and say, ‘Mom, I can’t do it.’ Just never quit.”