If the above command returns package not found or similar error do this step else go to next step, then you need to add the PPA, you can add them by sudo add-apt-repository ppa:ondrej/php;sudo apt update and now repeat the above step
Now remove the apache2 server which is installed when installing PHP, by sudo apt purge apache2*