Minecraft 1.21.9 and newer
PvP is a game rule. Run gamerule pvp false in the console on 1.21.9 and 1.21.10. From 1.21.11, including all 26.x versions, the rule is called minecraft:pvp, so run gamerule minecraft:pvp false. It applies immediately, needs no restart and is saved with the world. Type the start of the command and use tab completion to see the exact name your version uses.
Minecraft 1.21.8 and older
Set pvp=false in server.properties and restart the server. This applies to every world on the server.
PvP in some areas only
Most servers want PvP off at spawn and on in an arena or the wilderness. WorldGuard does this with regions: select the area with WorldEdit, define a region, and set its pvp flag to deny or allow. The __global__ region controls the default for each world.
- /rg define spawn
- /rg flag spawn pvp deny
- /rg flag arena pvp allow
Letting players choose
On an SMP, per-player PvP toggle plugins let each player opt in or out, usually with a cooldown so nobody switches mid-fight. Land-claim plugins such as GriefPrevention can also protect players inside their claims.