..
#web

How to Host a Static Website Locally

The next steps I gonna show you how to host a static website locally.

You'll need:

  • Nginx
  • Static Site Structure (index.html)
  • MacOS
  • Local hosts domain

Modify localhost

Access the file /etc/hosts with sudo in your MacOS and add your custom domain.

127.0.0.1	localhost
127.0.0.1	wiki.tiagoaguiar.dev
255.255.255.255	broadcasthost
::1             localhost

Configure Nginx

Install the nginx and configure it to map the port 8000 to the servername wiki.tiagoaguiar.dev in this example.

brew install nginx

Create and open the file /opt/homebrew/etc/nginx/servers/wiki.conf.

You can change the filename `wiki.conf` to you custom filename.

Add this content to the file.

server {
    listen 80;
    server_name wiki.tiagoaguiar.dev;

    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

Restart the nginx.

brew services restart nginx

Access the localhost:8080 to see the default nginx page. Access the wiki.tiagoaguiar.dev to the a 500 error (because we don't configure the server yet).

Start the Server

Navigate to the static site structure (that folder contains index.html) and start a small server with python command:

cd your_static_folder
python3 -m http.server

Access the wiki.tiagoaguiar.dev to see your static website.

Start the Server (macOS Startup)

Create a file /Library/LaunchDaemons/dev.tiagoaguiar.wiki.localserver.plist

Add the content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>dev.tiagoaguiar.wiki.localserver</string>

    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/python3</string> <!-- Path to your Python binary -->
        <string>-m</string>
        <string>http.server</string>
        <string>8000</string> <!-- Port number -->
        <string>-d</string>
        <string>/Users/tiagoaguiar/wiki/_site</string> <!-- Path to your site directory -->
    </array>

    <key>RunAtLoad</key>
    <true/>

    <key>KeepAlive</key>
    <true/>

    <key>StandardErrorPath</key>
    <string>/var/log/wiki.tiagoaguiar.dev.localserver.err</string>

    <key>StandardOutPath</key>
    <string>/var/log/wiki.tiagoaguiar.dev.localserver.log</string>
</dict>
</plist>

Add the permissions:

sudo chown root:wheel /Library/LaunchDaemons/dev.tiagoaguiar.wiki.localserver.plist
sudo chmod 644 /Library/LaunchDaemons/dev.tiagoaguiar.wiki.localserver.plist

Enable the script to startup with system:

sudo launchctl load /Library/LaunchDaemons/dev.tiagoaguiar.wiki.localserver.plist

You can verify the startup with tail or grep from a list.

tail -f /var/log/wiki.tiagoaguiar.dev.localserver.log
sudo launchctl list | grep dev.tiagoaguiar.wiki.localserver

If you want to host your static website on the internet, see the post How to Host a Static Website.