Canonical scheme structure on the singular locus of (affine) schemes so that the inclusion map is closed immersion Let $R$ be an excellent local ring (https://en.wikipedia.org/wiki/Excellent_ring) . Let $X:=Spec (R)$ and $Y:=Sing (R)=\{P\in X: R_P $ is not regular$\}$. How can we give a canonical Scheme structure on $Y$ such that the inclusion map $i:Y\to X$ is a closed immersion ? Do we need the local, or excellent assumption on $R$ ? And more generally can we do this for the singular locus of schemes that are not necessarily affine ?  
 A: The first thing to note is that per the wikipedia link provided, $R$ excellent implies $R$ is J-2 and thus the locus of non-regular points is closed. So the first question is really just "given a closed subset of a scheme, what scheme structure should be put on it so that inclusion is a closed immersion?". If you don't already have any idea about what the scheme structure should be (I should point out that in the case where $R$ is an algebra of finite type over a perfect field, one may use the Jacobian criteria to find the ideal), one can always place the reduced induced scheme structure on $Y$. This is the unique reduced scheme structure on a closed subset of a scheme, so if you have to pick something, this is the thing to pick. For a reference, any introductory algebraic geometry book or StacksProject will describe the construction.
In regards to your follow up questions, the fact that $R$ is local is not needed here, nor is the full generality of excellent. By looking around on the wikipedia page you've linked, one may discover that all one needs in order to have the non-regular points be a closed subset of $\operatorname{Spec} R$ is that $R$ is a J-1 ring - in fact, this is a definition. In the case of non-affine schemes, as long as one can produce a covering by $\operatorname{Spec} R$ so that $R$ is J-1, one will have that the singular locus is a closed subset because a set is closed iff it's closed when intersected with every element of an open cover. From there, we are back to the discussion of the first paragraph on the subscheme structure.
