XAPURI, Brazil (AP) _ The main defendant in the murder trial of ecologist and union activist Chico Mendes stunned a courtroom today by confessing to the crime during the first day of the proceeding.

During questioning by Judge Adair Longhini, Darci Alves Pereira, 23, admitted shooting Mendes in an ambush in 1988. The young cattle rancher had denied any role in the killing during routine opening motions this morning.

As bailiffs led Pereira from the courtoom in this remote Amazon jungle town, Longhini stopped them and questioned the defendant again.

Pereira paused, then looked up and said, ''I confess.''

''What?'' said the judge. ''Did you kill him?''

''Yes,'' Pereira said.

There was a buzz in the tiny courtroom, and Longhini called for order.

Ilzamar Mendes, the widow of Chico Mendes, clapped her hands and asked, ''He confessed? It's hard to believe.''

She added, ''He's probably doing it to protect his father.''

Pereira's father, 53-year-old cattle rancher Darly Alves da Silva, is the other defendant in the trial.

He is charged with planning the shooting and has pleaded innocent.

The confession was a startling development in a trial that has drawn international attention and is seen as a test of Brazil's commitment to protecting its rain forests and to equal justice for rich and poor.

Thousands of Brazilians, politicians and international observers had traveled to this western village, drawn by one of the biggest legal spectacles in Brazil's history: the trial of two ranchers accused of killing Mendes.

At stake, many say, is the future of the world's largest rain forest. The struggle in which Mendes was killed pits the traditional way of life in the Amazon against ranchers and others who want to open up large tracts of lands.

Mendes, a 44-year-old rubber tapper who led a movement to stop destruction of the rain forest, was gunned down Dec. 22, 1988, in the backyard of his modest wooden home in Xapuri, 2,650 miles northwest of Rio de Janeiro.

''This is a historic trial not only for Brazil, but the world,'' Marcio Thomaz Bastos, the chief lawyer for the prosecution, said earlier today.

''In Brazil, it is a test case against reckless lawlessness, and for the world, a marker of whether or not man will allow the Amazon to be destroyed.''

Private and government development efforts in the Amazon region have intensified since 1980; the World Resources Institute says as much as 20 million acres of forest were destroyed in Brazil in 1987 alone. The Amazon rain forest is the sole habitat for thousands of plant and animal species.

Peasants from around the region have come to Xapuri in canoes, mule- and steer-drawn carts, on horseback, by bus or simply on foot. Tents were popping up in the main square. Hammocks hung on front porches and matresses were laid wherever there's a free spot in people's homes.

''I want to see what Chico did - justice, and men living at peace with the forest,'' said Luis Vieira de Souza, a 63-year-old rubber tapper, who arrived at Xapuri after an eight-hour canoe journey on the Acre River.

''Our lives will fall when the trees do,'' said Souza.

About 2,000 people make a living tapping rubber and collecting wild nuts, resins and other forest produce.

Locals are scampering to accomodate the crush of visitors. Telephone booths are sprouting on street corners. Street curbs, park benches and tree trunks are getting fresh coats of white and green paint.

Telephone trucks, television vans and food delivery trucks come head-to- head with horse and steer-pulled carts. Town workers are removing weeds from in front of the one-room, wooden courthouse.

To get to the village, Rubber tapper Raimundo da Silva Maia, 51, walked 30 miles over 12 days through thick forest with more than 30 pounds of rice, beans and manioc slung in packs on his back.

''If Darly and Darci are found not guilty, only God will be able to saave my family and the rain forest,'' said the father of 13.

Hundreds of politicians, including some government officials and many representatives of leftist Workers Party of which Mendes was a member, began to arrive late Tuesday aboard chartered planes and buses from Rio Branco, 110 miles away.

More than 100 state troopers were flown in to prevent violence between ranchers, residents and rubber tappers. Roadblocks of heavily armed police were set up along the muddy, rutted road that connects Xapuri to Rio Branco.