Bradford Morgan White: Zero to Hosted, Part 1

20251108

Getting a website online isn't difficult, and it doesn't require much. What you need:

To get going, on say a Raspberry Pi running Raspbian, you can easily just:

sudo apt update
sudo apt install nginx-full -y
sudo systemctl enable --now nginx

You now have a computer with Linux running a webserver. You're most of the way there. Now, the webserver, nginx, needs to be made aware of a few things. So, now, using nano or using vim you need to create a new file; something like mywebsite.com.conf. In that file, add the following:

server {
  listen 80;
  listen [::]:80;
  server_name mywebsite.com www.mywebsite.com;
  root /var/www/mywebsite.com;
  charset utf-8;
  client_max_body_size 256M;
  access_log /var/log/nginx/mywebsite.com_access_log;
  error_log /var/log/nginx/mywebsite.com_error_log;
  index index.html;
  location / {
    try_files $uri $uri/ =404;
  }
}

Here, we are telling nginx to listen on port 80 (the standard port for HTTP), to look for the server name "mywebsite.com", to look for files to serve in /var/www/mywebsite.com, to use utf-8 encoding, to allow a large client body size (avoiding a 413 error), to log accesses, log errors, and use index.html as the default file to be served. Finally, we are telling nginx to either load the requested file or return a 404.

Now, we need to do five more things:

sudo rm -f /etc/nginx/sites-enabled/default
sudo mkdir -p /var/www/mywebsite.com
sudo chown pi:www-data /var/www/mywebsite.com
sudo mv mywebsite.com.conf /etc/nginx/sites-available/
sudo ln -s /etc/nginx/sites-available/mywebsite.com.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo nginx -s reload

We removed the "default" site config, created the directory to hold our HTML files, applied the proper directory ownership, put the config in place, and then enabled that config.

Now, let's create a file to serve in /var/www/mywebsite.com/index.html:

<html>
  <head>
    <title>My Website</title>
  </head>
  <body>
    <h1>This is my website.</h1>
    <p>There are many like it, but this one is mine.</p>
  </body>
</html>

As we did with the directory, let's set permissions:

sudo chown pi:www-data /var/www/mywebsite.com/index.html

All that's left is to view the site. On your RPi, type ip a, and you should have a line that is somewhat similar to:

inet 10.0.0.33/24 brd 10.0.0.255 scope global dynamic noprefixroute eth0

Now, the first four number sets separated by dots before the /24, those are octets, and they comrpise the IP address of your RPi. On another Linux/UNIX system on the same network, open up /etc/hosts and add the following line to the bottom:

10.0.0.33 mywebsite.com www.mywebsite.com

Now, on that computer, you should be able to open a browser and type http://mywebsite.com and see the HTML page that you created. On the RPi serving the site, you should be able to see the access you made at /var/log/nginx/mywebsite.com_access_log. If you'd like to watch accesses in real-time, you can do something like: sudo tail -F /var/log/nginx/mywebsite.com_access_log.

We will cover getting on the internet in the next article :-)

⇠ back

© MMIX - MMXXVI, Bradford Morgan White
Licentiam Absurdum