In this work, we deal with the optimal control problem of maximizing biogas production in a chemostat. The dilution rate is the controlled variable, and we study the problem over a fixed finite horizon, for positive initial conditions. We consider the single reaction model and work with a broad class of growth rate functions. With the Pontryagin maximum principle, we construct a one-parameter family of extremal controls of type bang-singular arc. The parameter of these extremal controls is the constant value of the Hamiltonian. Using the Hamilton-Jacobi-Bellman equation, we identify the optimal control as the extremal associated with the value of the Hamiltonian, which satisfies a fixed point equation. We then propose a numerical algorithm to compute the optimal control by solving this fixed point equation. We illustrate this method with the two major types of growth functions of Monod and Haldane.