How do I upgrade to PHP8.1 from 7.2?

SYSTEM INFORMATION
OS type and version Ubuntu Linux 18.04.6
Webmin version 2.001
Virtualmin version 7.3-1
Related packages apache, php, mysql, ssh

Without breaking my apache?

Thanks in advance.

You should be more worried about your PHP websites/apps breaking more than Apache tbh. Make sure your websites/applications PHP code is compatible with PHP 8+ before adding it to your server.

That is a good point. My server only runs the latest wordpress.

We have documentation about this: Multiple PHP Versions – Virtualmin

You should not upgrade PHP. You should keep the one provided by your OS, and optionally install additional versions if you need them. But, the version provided by your OS will receive better maintenance and longer term support than third party repo packages. If you need new versions of services, and PHP, etc. we recommend you run a new version of your OS. We support Ubuntu 20.04 and 22.04, as well (though you have to be careful when switching to the new repos, as we now use MariaDB instead of MySQL on those distros…you need to make sure you stick with MySQL, for ease of migration).

In place OS upgrades also work, but are a little more prone to downtime and issues you might need to work through. A fresh install on a freshly installed new server that you then restore your sites onto will be easier.