European Community foreign ministers reached agreement early today on the terms of entry for Spain and Portugal, ending years of bitter negotiation over the scope of Europe's economic integration.

Community foreign ministers reached the agreement with Spanish and Portuguese representatives in a negotiating session that ended several hours after midnight.

"A lot of time, a lot of toil, a lot of sweat was necessary," said Italian Foreign Minister Guilio Andreotti, who chaired the negotiating session. "Now we have the great joy to announce an agreement."

Andreotti said that at a time when the community's difficulties "might have given an image of weakness and crisis," the successful enlargement negotiations have shown the community to be a "living reality with a future."

Spanish Foreign Minister Fernando Moran said, "We can now get behind us this inferiority complex in international affairs Spaniards have had in the past. We can hold our heads high now."

In order for Spain and Portugal to enter the community officially, the conditions of membership must be ratified by the parliaments of the 10 member nations. Community officials had said the negotiations should be completed by the end of March to allow ample time for ratification by Jan. 1, 1986, the target entry date for the two nations.

While the internecine bickering over budgets and agricultural surpluses that has long wracked the existing 10-member community is likely to continue apace with membership expanded to 12, the decision holds both symbolic and substantive significance.

For Europe, admission of Spain and Portugal will round out the geographic integration of Western Europe, in the process putting additional strains on an economic system that has seen growing divisions between the richer and poorer member nations.

For Spain and Portugal themselves, entrance into the community is viewed as holding potential economic gains. Perhaps more importantly, it is seen as a rite of passage, signaling their final transformation from the military dictatorships of recent history into full-fledged members of the family of modern European democracies.

The ministers completed the details of a package of conditions to offer Spain and Portugal, and community officials then met separately with the two nations' negotiators. Yesterday, officials said the 10 community members agreed on the major points of a package of conditions to offer Spain, which covered three main areas of negotiation.

The three areas are the terms under which the huge Spanish fishing fleet will enter community waters and thus come into competition with community fishermen; a timetable for the introduction of Spanish agricultural products into sensitive EC markets, and the rights of Spanish workers to jobs in member states.

French reservations last week about two aspects of a package offered to Spain by Andreotti had held up an accord. France was unable to agree on the number of Spanish fishing vessels to be allowed into EC waters during a transitional period, and wanted tighter restrictions on production of Spanish wine.

The French difficulties were resolved Thursday morning in a meeting of Andreotti, French Foreign Minister Roland Dumas and their Spanish counterpart, Moran, EC officials said.

Andreotti pushed the ministers to conclude the enlargement negotiations before the start of the EC summit of community leaders later today to avoid entangling the leaders in the details of the talks.

Community officials believe the negotiations must be concluded by the end of March to allow parliaments of the member states to ratify the agreement by Jan. 1.

The drive to conclude an agreement on schedule has been given urgency by the concern of some officials that there would be repercussions to the North Atlantic Treaty Organization if Spain's EC membership were delayed.

The Spanish government of Prime Minister Felipe Gonzalez is to hold a public referendum by early next year on whether Spain is to remain in NATO. The officials believe that a symbolic rejection by the EC could add to public opposition to participation in NATO.

Spain and Portugal applied for membership in 1977, but serious negotiations over terms of entry began about two years ago.

Among the final points under discussion last night was the size of the rebate Portugal would get on its contributions to the community treasury.

Community officials have proposed that Portugal receive rebates on its value-added tax contributions for seven years to compensate for its economically disadvantaged position. The payments to Portugal could total as much as $800 million.