Self discipline is a skill in itself and it is something you can learn.
At it's most basic you restrict things you want and make them a reward for doing a task.
It can be hard to restrict things as you say. When I used to study, I used to go to a "3rd place" to do it. That is somewhere that is not home or work - I used to go to a library. In that environments you don't have TV, or food, and hopefully you won't be masturbating.
Mobiles can be very difficult though - if you can't stop yourself using your phone to watch YouTube then either leave it at home (I know, shocking idea in this day and age!) Or install parental locking/anti distraction software that locks your phone down for certain periods. This can help you learn self discipline with your phone.
Similarly if you study with a laptop, then look at anti distraction tools to keep you focused on your work rather than surfing or on YouTube.
The reward side is very important. You need to be consistent and follow through on your promises to yourself. Don't use unrealistic rewards - like "if I study for 6 hours today I'll have dinner tonight". You're going to have dinner anyway, and you don't want to go down the road of punishing yourself. Make it a favourite meal, or promise to watch next episode in a favourite TV show.
The idea is that you will be still enjoying those things because you will study and work. But be prepared to deny yourself those things if you fail to reach your goals in the beginning.
Self discipline is hard, not least because you can cheat yourself too easily. But it's worth putting in the effort, and the forced physical separation from the distractions and rewards at home makes it easier.