Search code examples
htmlmacoslocalhost

How to run a html file on localhost (any port)


I am creating a website using HTML, CSS and js with java for server side. I need to run it on localhost. I am using MacOS X 11 (el Capitan).

I have already tried python and node.js but it clashes with Java, so I need a technology that won't clash with Java and will help me localhost on Mac.


Solution

  • IIRC macOS comes with PHP preinstalled, and PHP has built-in web-server which should be enough for serving static content.

    So, open Terminal.app and then:

    cd your/project/dir
    php -S localhost:8080
    

    After than you can navigate to http://localhost:8080/ and see your site in the browser (given you have index.html in your project, otherwise there will be "Not Found" message).

    There are more advanced and/or less terminal-oriented ways, of course, but since you already tinkering with python and node, another terminal command should not be a problem.

    BTW, you might want to look at that terminal window from time to time, as it outputs nice log of what things were requested from server. Good if you want to check for invalid references, 404 errors, etc. Here is a sample output:

    $ php -S localhost:8080
    PHP 7.3.6 Development Server started at Sat Jun 22 20:00:28 2019
    Listening on http://localhost:8080
    Document root is /private/tmp/test
    Press Ctrl-C to quit.
    [Sat Jun 22 20:00:32 2019] [::1]:51640 [200]: /
    [Sat Jun 22 20:00:32 2019] [::1]:51641 [200]: /style.css
    [Sat Jun 22 20:02:35 2019] [::1]:51670 [404]: /oops.html - No such file or directory
    

    As you can see, root folder (/, which was translated to index.html in my case) and a stylesheet (style.css) were requested and successfully delivered (code is 200). But non-existent file oops.html resulted in error (code is 404).