There are two key points for PHP's success: The timing and the focus on shared hosting.
In the beginning the Web was static. Web-Servers could only distribute pre-generated HTML this obviously wasn't enough after some time so in 1993 the NCSA team extended their web server (the predecessor of Apache httpd) with a technology called CGI which allowed calling specific programs from the web server which could generated HTML (or other content) on the fly. This was great as people could do all the things. A commonly used language at that time for that purpose was Perl. Perl is a language strong in text processing which is a key property for handling HTTP request data and producing HTML. So people used it.
But there was a problem: For each and every request the web server had to run an external program, Perl had to initialize and run the script. This caused quite some headache. A solution was mod_perl coming ca. 1997. mod_perl is a module for the Apache webserver embedding the Perl interpreter directly into the web server process so no extra startup was need.
But again this approach had a problem: mod_perl was extremely powerful and had access to the complete server configuration. You couldn't use one server to host multiple independent clients as they could easily harm the other people - be it on purpose or by accident. And that's where PHP came.
PHP was built in a way that it could run as Apache module (thus not having the startup hit for each request) but provides a shared-nothing environment between requests. Once a request was done all information was lost and a subsequent request to another virtual host would be served independently. PHP also offered features to restrict access (safe_mode, open_base_dir). With that architectural choice companies could install PHP on their server and provide customers FTP (or similar) access to upload their files and by that host many many customers on a single machine without much work. This caused a competition leading to cheap prices for PHP-based hosting making PHP an ubiquitous platform.
Additionally PHP had a few benefits over Perl, like allowing to mix HTML and PHP code or direct access to request variables (see also register_globals) which in Perl was harder (custom parsing or dependency on CGI.pm) which developers liked.
Another aspect which made PHP successful in those days was good support on Windows. Getting Perl or other languages working on Windows was hard, but back at that time many developers where using Windows at home and virtualization or containers weren't a thing, yet. PHP did run easily on Windows so people could use it for development and then pack up the files and deploy on Linux. With Perl for instance you would have to get the correct perl distribution and then check which modules are available at all etc.
What about other languages? Compiled languages like C or C++ never got traction since scripting serves the rapid developing market better. Java existed, but Java Virtual machines required way more resources and shared hosting in the way PHP supports it was hardly doable. Investment in harddware was way higher. ASP (as predecessor of ASP.Net) was bound to the Windows platform, limiting it to organizations which already run Windows and who don't fear the licensing costs, and again no shared hosting support.