MADRID, July 4— Prime Minister Felipe Gonzalez appointed new foreign and economy ministers today in a Cabinet shakeup that dramatically altered his 31-month-old Government.

Mr. Gonzalez replaced six of the 15 Cabinet members and the Government spokesman in what he said at a news conference tonight was an attempt to ''air out and give a little impulse'' to his Government as Spain prepares to join the European Economic Community in January.

But aides to the Prime Minister and other officials said that what had begun as a light remodeling of the Cabinet had ended in bitter internal feuding.

Departure Is a Surprise

The Minister of Industry and Energy, Carlos Solchaga, was named to replace Economy Minister Miguel Boyer, the most powerful member of the Cabinet, whose departure caught political and business leaders by surprize.

Mr. Gonzalez said Mr. Boyer was tired and had asked to be relieved. But sources close to the Prime Minister said that Mr. Boyer had clashed with Deputy Prime Minister Alfonso Guerra and that Mr. Gonzalez had sided with Mr. Guerra, inducing the resignation of the Economy Minister.

The Foreign Minister, Fernando Moran, was dismissed because of differences with Mr. Gonzalez over Spanish membership in the North Atlantic Treaty Alliance. He was replaced by Francisco Fernandez Ordonez, a recent convert to the governing Socialist Party.

The 59-year old Mr. Moran, a rumpled intellectual whose successful negotiations over entry into the European Community won him great popularity, reacted bitterly. Attending a Fourth of July reception in the American Embassy here tonight, he said caustically that he felt ''very good, after being fired.''

Mr. Gonzalez stressed that the Cabinet changes would not affect the Government's policies of economic austerity and close involvement with Europe. ''Spaniards can be calm,'' he said. ''The Goverment is going to maintain the politically steady and renovating hand that it has maintained until now.''

The Goverment is hardly in crisis and Mr. Gonzalez remains immensely popular in polls. Aides to the Prime Minister said that initially the Cabinet changes had been partly designed to gear up for both a referendum on NATO membership and general elections scheduled for next year.

Spokesman Is Replaced

Among the other changes, Joan Majo, who has led the Government's efforts to develop electronics industries, was named Minister of Industry and Energy. Felix Pons, a Socialist legislator, replaced Tomas de la Quadra as Minister of Territorial Administration.

Javier Saenz de Cosculleula, president of the Socialists' parliamentary group, was appointed Minister of Public Works, replacing Julian Campo. And Abel Caballero, an economist and Socialist legislator, was named Minister of Transport, replacing Enrique Baron.

Eduardo Sotillos, the Government's spokesman, who had poor relations with the Spanish press, was replaced by Cultural Minister Javier Solana, who will also retain his Cabinet portfolio.

Although he used the word crisis to characterize the events of the last day, Mr. Gonzalez sought to play down the divisions, saying that each of the departing ministers had been loyal and capable.

But he divulged that Mr. Guerra, the Deputy Prime Minister, had privately asked to step down a week ago. ''Obviously I did not attend to the suggestion,'' Mr. Gonzalez said.

Economic Team Affected

Both Mr. Guerra and Mr. Boyer are close friends of Mr. Gonzalez. He was forced to choose between the two, officials said, when Mr. Boyer refused Mr. Guerra's request to relax his austerity policies on some budget items and argued that he must have full control over economic matters.

Most of the changes affected the economic team, which has squabbled over budget requests and which is blamed for a rise in unemployment to 22 percent, the highest in Europe.

''I also did it to homogenize the econonomic team,'' Mr. Gonzalez said, although he added that its divisions were more ''in image than in practice.''

The presence of Mr. Boyer, the architect of the Government's tight money and free market policies, has been largely responsible for the ability of the Socialist Goverment to win the tentative confidence of business and to attract foreign investment. Mr. Gonzalez lavished praise on Mr. Boyer and stressed that he totally backed the former minister's policies.

Economic Views Shared

His replacement, 41-year old Mr. Solchaga, largely shares Mr. Boyer's economic views and was responsible for carrying out the Governments restructuring of industry, closing down some shipyards and steel mills.

Relations between Mr. Moran, the departing Foreign Minister, and Mr. Gonzalez were touchy.

The Prime Minister sidestepped questions on their differences over NATO membership, which he favors and which Mr. Moran endorses only halfheartedly.

Referring to the demands on Spain as it enters the Common Market, Mr. Gonzalez said: ''Its not that the minister could not carry out the changes, but that it was advisable that a new person do it. It would make the task easier.''

But Mr. Moran, in an interview published in the newspaper Diario 16, was bitter. Saying he wanted to stay on, he lashed out at unnamed aides to Mr. Gonzalez who he said spread reports Wednesday that he had asked to resign.

''My firing is the exclusive responsibility of the president of the Government,'' he said.

''Now perhaps it is pretended to give crumbs of recompense,'' he said, referring to reports he might be named delegate to the United Nations, ''but I have never needed or wanted crumbs. I am leaving to go home.''

photo of Miguel Boyer (Agence-France/Presse) (page A5); photo of Fernando Moran (Camera Press) (page A5); photo of Prime Minister Felipe Gonzalez (UPI)