There are significant risks if you build your own package. You will need to track upstream changes and keep up to date. (Otherwise you will miss security, functionality, and optimization changes.)
Rather than build on the production server, build on the the development server and package the software for installation on the production server. For many distributions, you can download the package source and build your own distribution package. Do this on your development server and deploy to production.
While there are possible opportunities for optimization. It has been my experience many optimizations actually slow performance. You need the same hardware for development and production if you are building and testing your own packages. (Several factors can cause optimizations that work in development to fail in production and vise versa.)
If you really need a newer version in production, check for a backport version. Even if a backport is not available, it may be possible to deploy the package from a newer OS release in the current environment. This would be safer than build your own. You would need to monitor the distribution for updates.