In statistical mechanics, temperature is typically introduced via the Boltzmann entropy as follows. The Boltzmann entropy is $S=k_B \ln W$ where $W$ is the number of microstates. If the system's energy, $E$ is fixed, then $W$ is a function of the energy, $W=W(E)$. Considering two isolated systems with energy $E_1$ and $E_2$, their number of microstates are $W_1(E_1)$ and $W_2(E_2)$. The total entropy of the two systems is then $S=S_1 + S_2 = k_B \ln [W_1(E_1) W_2(E_2)]$.
Let us put the two systems in thermal contact with each other. They are allowed to exchange energy. Equilibrium sets in when the total number of microstates with the new distribution of energies is the largest. Equivalently, one can consider the logarithm of microstates to be the largest. Let the equilibrium energies be $E_1'$ and $E_2'$. The equilibrium condition reads:
$$\left . \frac{\partial \ln W_1(E_1)}{\partial E_1} \right|_{E_1'} = \left . \frac{\partial \ln W_2(E_2)}{\partial E_2} \right|_{E_2'} $$
Since this is the condition for thermal equilibrium, one may identify the temperature as
$$ \left . \frac{\partial \ln W_1(E_1)}{\partial E_1} \right|_{E_1'} = \left . \frac{\partial \ln W_2(E_2)}{\partial E_2} \right|_{E_2'} = \frac{1}{T}$$
What confuses me here is that from the moment where the two systems are allowed to exchange energy, they are no longer individually in a microcanonical ensemble. Their energy can fluctuate and their individual microstates are not equally likely. Moreover, since the energy can fluctuate, $E_1'$ and $E_2'$ are not well defined. My understanding is that, if the energy is not fixed and the microstates are not equally likely, the Boltzmann formula is not valid any more and one should use the Gibbs entropy formula $S = -k_B \sum P_i \ln P_i$ where the sum runs over all microstates $i$.
I believe the correct statement would be that equilibrium sets in for the most probable allocation of the mean energies $\langle E_1' \rangle$ and $\langle E_2' \rangle$ between the two systems.
Question: Is it possible to introduce the temperature, similarly to the above, but using the Gibbs entropy, and finding the most probable distribution of mean energies, rather than instantaneous energies? If so, how?
I understand that none of this matters in the thermodynamic limit, as energy fluctuations are small and the Gibbs entropy is close to the Boltzmann entropy due to the sharpness of the $P_i$ distributions. I need, however, a rigorous definition of temperature that applies to arbitrarily small systems as well. I want therefore to relax the condition of thermodynamic limit. Indeed, one could formulate problems like single particle or single spin in contact with a heat bath and consider the single particle to have a well defined temperature and apply statistical mechanics concepts.