Before doing this, I recommend backing up your system, in case things go wrong.
To upgrade to the latest version of PHP in Ubuntu, first, update the system:
Then add the third-party repository:
Finally, upgrade PHP to the latest version:
If upgrading PHP for WordPress, you’ll need a few more modules:
If using Apache, you’ll need to set the correct PHP version: