Open In App
Related Articles

How to host local server on your Computer with PHP ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will see how to use the Localhost server to run the PHP code. Localhost server simulates a similar environment to compile & debug, in order to develop the code, just the same as in the real-time environment. Basically, the computer is talking to itself when you call the local host. When we are building a website on PHP, then we have to face some difficulties while hosting our websites on a local server. Especially if we store the PHP file other than htdocs (if using the XAMPP server), it always shows some errors. So, we need to strictly write code in the htdocs folder, that resides in the C-Drive of our computer.

How to use localhost in other drives or folders of our computer?

  • We have to copy the path of xampp>> htdocs. which mostly looks like the following:
C:\xampp\htdocs
  • Now, we have to add this path to our environment variables. For that, go to Control Panel > Environment Variables > System Variables > Path > Edit Path > now paste the copied path of htdocs

We can see this in the image below:

Environment Variable

Now just press ok and save the changes.

  • Now, run the command “php -v ” in the windows terminal or in CMD to check if the environment variable is set properly or not. You must get this type of message in return, check the below image.

Check PHP version

  • Now, if you are using VS Code, then open windows PowerShell by clicking: ctrl + shift + “~” this button & simply create a PHP file and follow these commands:
php -S localhost:8000

And the server will start running. Check the below images:

 

  • The following is the default PHP code that will be rendered:

Example: This example describes the basic implementation of the PHP code to run into the localserver in the computer.

PHP

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
    "width=device-width, initial-scale=1.0">
    <title>
        Host local server on your
        Computer with PHP
    </title>
</head>
  
<body>
    <?php
        echo"<h1>Welcome to php Website</h1>";
        echo"Hello World";
    ?>
</body>
  
</html>

                    

Output:

 


Last Updated : 05 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads