Start with the actionS[ϕ]=d4xL(ϕ,μϕ)S[\phi]=\int d^4x \, \mathcal{L}(\phi,\partial_\mu \phi)S[ϕ]=∫d4xL(ϕ,∂μ​ϕ)

The physical field configuration is the one for which a small variation of the field,ϕ(x)ϕ(x)+δϕ(x)\phi(x)\rightarrow \phi(x)+\delta\phi(x)ϕ(x)→ϕ(x)+δϕ(x)

does not change the action to first order:δS=0\delta S=0δS=0

Now vary the action:δS=d4x[Lϕδϕ+L(μϕ)δ(μϕ)]\delta S = \int d^4x \left[ \frac{\partial \mathcal{L}}{\partial \phi}\delta\phi + \frac{\partial \mathcal{L}}{\partial(\partial_\mu\phi)} \delta(\partial_\mu\phi) \right]δS=∫d4x[∂ϕ∂L​δϕ+∂(∂μ​ϕ)∂L​δ(∂μ​ϕ)]

Since variation and differentiation commute,δ(μϕ)=μ(δϕ)\delta(\partial_\mu\phi) = \partial_\mu(\delta\phi)δ(∂μ​ϕ)=∂μ​(δϕ)

soδS=d4x[Lϕδϕ+L(μϕ)μ(δϕ)]\delta S = \int d^4x \left[ \frac{\partial \mathcal{L}}{\partial \phi}\delta\phi + \frac{\partial \mathcal{L}}{\partial(\partial_\mu\phi)} \partial_\mu(\delta\phi) \right]δS=∫d4x[∂ϕ∂L​δϕ+∂(∂μ​ϕ)∂L​∂μ​(δϕ)]

Now integrate the second term by parts:d4xL(μϕ)μ(δϕ)=d4xμ[L(μϕ)]δϕ\int d^4x\, \frac{\partial \mathcal{L}}{\partial(\partial_\mu\phi)} \partial_\mu(\delta\phi) = – \int d^4x\, \partial_\mu \left[ \frac{\partial \mathcal{L}}{\partial(\partial_\mu\phi)} \right] \delta\phi∫d4x∂(∂μ​ϕ)∂L​∂μ​(δϕ)=−∫d4x∂μ​[∂(∂μ​ϕ)∂L​]δϕ

The boundary term vanishes because we assumeδϕ=0\delta\phi=0δϕ=0

on the boundary of spacetime.

Therefore,δS=d4x[Lϕμ(L(μϕ))]δϕ\delta S = \int d^4x \left[ \frac{\partial \mathcal{L}}{\partial \phi} – \partial_\mu \left( \frac{\partial \mathcal{L}}{\partial(\partial_\mu\phi)} \right) \right]\delta\phiδS=∫d4x[∂ϕ∂L​−∂μ​(∂(∂μ​ϕ)∂L​)]δϕ

For the action to be stationary for arbitrary δϕ\delta\phiδϕ, the bracket must vanish:Lϕμ(L(μϕ))=0\boxed{ \frac{\partial \mathcal{L}}{\partial \phi} – \partial_\mu \left( \frac{\partial \mathcal{L}}{\partial(\partial_\mu\phi)} \right) =0 }∂ϕ∂L​−∂μ​(∂(∂μ​ϕ)∂L​)=0​

This is the Euler–Lagrange equation for a field.

For the scalar field,L=12μϕμϕ12m2ϕ2\mathcal{L} = \frac{1}{2}\partial_\mu\phi\,\partial^\mu\phi – \frac{1}{2}m^2\phi^2L=21​∂μ​ϕ∂μϕ−21​m2ϕ2

we getLϕ=m2ϕ\frac{\partial \mathcal{L}}{\partial \phi} = -m^2\phi∂ϕ∂L​=−m2ϕ

andL(μϕ)=μϕ\frac{\partial \mathcal{L}}{\partial(\partial_\mu\phi)} = \partial^\mu\phi∂(∂μ​ϕ)∂L​=∂μϕ

So the Euler–Lagrange equation becomesm2ϕμμϕ=0-m^2\phi-\partial_\mu\partial^\mu\phi=0−m2ϕ−∂μ​∂μϕ=0

or(μμ+m2)ϕ=0\boxed{ (\partial_\mu\partial^\mu+m^2)\phi=0 }(∂μ​∂μ+m2)ϕ=0​

That is the Klein–Gordon equation.

In plain English:δS=0field chooses stationary actionEuler–Lagrange equationKlein–Gordon equation\boxed{ \delta S=0 \quad\Rightarrow\quad \text{field chooses stationary action} \quad\Rightarrow\quad \text{Euler–Lagrange equation} \quad\Rightarrow\quad \text{Klein–Gordon equation} }δS=0⇒field chooses stationary action⇒Euler–Lagrange equation⇒Klein–Gordon equation