Consumer prices, rebounding from last year's fall in energy costs, rose at an annual rate of 5.4 percent for the first half of 1987, the steepest six-month inflation rate in five years, the government said yesterday.

The Labor Department reported a moderate 0.4 percent gain in its consumer price index for June, up from a 0.3 percent advance in May and matching the increases of February to April. Prices were up 0.7 percent in January.

The June index, equivalent to an annual rate of 4.3 percent, was paced by higher energy and food costs. Although below the six-month average, it still underscored the higher inflation levels that have been present all this year.

Most economists expected some settling down of prices for the second half of the year.

At the White House, presidential spokesman Marlin Fitzwater said increases in consumer and producer prices in June indicate inflation "is returning to the 4 percent pace reported the first three years of the {current economic} expansion."

But through June, consumer inflation has been running at nearly five times last year's minuscule 1.1 percent pace -- and more than a full percentage point above the 4 percent levels that prevailed in 1982-1985.

"It's moderate inflation, but it's still too high," said Allen Sinai, chief economist for Shearson Lehman Bros. Inc. in New York. "Five percent-plus consumer inflation is going to trigger some tough wage negotiations. This will be the next battleground on inflation."

Gasoline pump prices were up 1.4 percent in June, electricity advanced 2.5 percent and natural gas was up 0.4 percent. Heating oil declined 0.3 percent.

Overall consumer energy costs rose at an annual rate of 16.7 percent for the first six months of the year, all but erasing their 19.7 percent plummet through 1986.

Food prices rose 0.7 percent in June, paced by higher prices for beef, pork, fruit and vegetables. For the first half of 1987, food prices advanced at an annual rate of 4.4 percent.

In general, analysts expected food price increases to begin to subside but energy costs to continue rising, but more slowly, at least for the next few months.

Subtracting food and energy, consumer prices rose a modest 0.2 percent in June, suggesting that the recent firming of the U.S. dollar on foreign exchange markets temporarily may be helping to hold down rising import prices.

A weaker dollar can help ease this nation's trade deficit, a record $166.3 billion last year, by making foreign goods more expensive at home and U.S. goods more competitive abroad.

But too fast a fall in the currency can trigger an inflationary spiral. The dollar is about 50 percent lower now against other major key currencies than it was two years ago.

"I think we've seen most of the inflation that was caused by the sharp drop in the dollar," said S. Jay Levy of Levy Economic Forecasts in Chappaqua, N.Y. "I think that inflation is under control, and that the problem with the economy is now sluggishness rather than rising prices."

The Labor Department also noted:

Grocery store prices were up 0.8 percent; restaurant meal prices gained 0.5 percent. Beef prices rose 2.1 percent; pork prices jumped 2.3 percent; fruit and vegetable prices advanced 3.9 percent. Poultry prices dropped 1 percent.

New car prices rose 0.4 percent. Used car prices jumped 1.1 percent.

Clothing prices overall dipped 0.8 percent, reflecting summer sales.

Medical care costs rose 0.7 percent.

Housing expenses were up 0.3 percent.

The index for all products listed in the consumer price index stood at 340.1 for June, compared with 327.9 for June 1986 and 100 for the base period of 1967. Thus, a market basket of goods that cost $10 in 1967 cost $32.79 last June and $34.01 last month.