Or At Least The Way I Did It
One of the many things you can do with a Raspberry Pi is use it to host your own website. Hosting a website usually involves buying some space on a web hosting service or using a free service that has space limitations or advertisements. Not all types of content are suitable for hosting on one of the smallest, cheapest computers you can buy. Basic HTML and photos can be served quickly and efficiently, but dynamic websites with video or interactive content may not perform as well. There is also the management of downtime and risk of data loss. For example, if your power goes out, your website goes down. If you Pi crashes and needs to be re-imaged, you'll need to have a backup of your website stored somewhere to build in some redundancy. So this approach isn't for everyone, but if you are still interested in creating your own Pi-hosted website, this article can help you learn one way of getting started.
If you just bought your Raspberry Pi without a pre-loaded OS, you'll need to format a micro SD card so that it contains a bootable Raspberry Pi OS. Raspberry Pi now offers a convenient software tool for creating a bootable SD card, available here. I won't spend too much time talking about this process, there are several resources available that can show you different ways to install Raspberry Pi OS. It is easiest to connect your Pi to a monitor, keyboard, and mouse to get it set up for the first time. After you get some remote desktop software installed, you can optionally remote in from another computer to finish setting up.
There are a few different ways to remote into your Pi. I chose to install xrdp, which enables the Windows remote desktop protocol on your Raspberry Pi. Installation is straightforward if you are familiar with using the Linux terminal.
sudo apt update
sudo apt upgrade
sudo apt install xrdp
It may be necessary or desirable to create a new account to be used for remote access. You can go ahead and create that account now if you need/have to. I did not need to create a separate account to use xrdp. I was able to log in using the account I made while setting up my Pi for the first time.
sudo adduser USERNAME
Follow the prompts to create a new account.
Now you should be able to test your remote desktop connection. Run the commandhostname -I to get your Pi's IP address. Enter that IP into the remote desktop viewer software (running on another computer) and make sure you can connect.
If you want to use your Raspberry Pi without a monitor, you may need to specify a headless resolution. If you do not specify a resolution, the Pi might not fully boot unless a display is connected. Since we're going to be using this Pi as a server and only access it via remote desktop, we'll need to specify a headless resolution so that the Pi will fully boot without a display connected. Not all Pi's will require this step, so check to see if your Pi boots without the HDMI plugged in. You'll need to set up a remote desktop protocol first and attempt to remote in to verify. If your Pi boots without a monitor connected and you are able to remote in, then you don't need to worry about this step.
You should now be able to boot your Raspberry Pi without HDMI connected and remote in using a remote desktop application on another computer. If you are still having trouble, try these steps:
cd /boot/sudo nano config.txtIn order for your Pi to serve html files, you need to install Apache server software. This is again pretty straightforward and can be done through the terminal.
Open a new terminal window and enter the following command:
sudo apt install apache2 -y
Now get the IP address for your Pi
hostname -I
Type the IP address into a web browser (on the Pi) and you should see a default Apache 2 page.
This page is the index.html file located in /var/www/html. This directory serves as the root of your website. Any files or folder structures placed in this directory will be available to the web. Create a few simple html files and experiment with this functionality. You can also add css files to manage how your site looks, along with images and other files that can be linked for download.
If you don't mind finding and typing your Pi's IP address into the browser every time you need to access your site (IP addresses can randomly change), you can skip this step. If your website is just for you or a small group of people you know, then the IP address will serve the purpose just fine. However, if you want your website to have an easy to remember name like www.something.com, then you'll need to use a Dynamic DNS to link your IP address to a host name. No-IP is a free way to create an easy to remember host name and link it to your Pi's website.
Get started by creating an account at No IP with a hostname of your choosing. You will need to select a suffix to your hostname, like .hopto.org or .ddns.net. No IP has some step-by-step instructions for getting started.
Since this is a free service, there are a few catches. Your web address will have one of the No IP suffixes like ".hopto.org" appended to your chosen hostname. You will also be required to log in every 30 days to confirm you are still using the host name. This is not as bad as it sounds, but if this is something you don't want to deal with, you can pay about $15 per month to get around these two limitations.
While setting up your Dynamic DNS, you'll need to know your public IP address. The easiest way to get this on Raspberry Pi is to open a terminal window and type:
curl icanhazip.com
Press enter and your public IP address will be displayed in the terminal window.
user@raspberrypi:~$ curl icanhazip.com
12.345.678.910
We will also want to make our Pi's IP address static rather than dynamic. This ensures that when we set up port forwarding on our router, it will always be the address of the Pi. If you don't set up a static IP,your Pi can get a new IP address assigned to it and break the port forwarding link made in the router. There's a good set of instructions here for setting a static IP using the GUI or the terminal.
Now we are ready to set up our router to forward HTTP requests to our Pi's IP address. Log in to your router (usually by typing the router IP into a web browser) and look for a port forwarding option. Once there, you can type in your Pi's static IP address, and select Port 80 or HTTP web server.
At this point, you should be able to type in your hostname.hopto.org (or whatever suffix you chose) and see your index.html page. We're almost done. The last step is to set up the No IP DUC, or DNS update client to run on your Raspberry Pi. This application will check your IP and compare it to the IP address stored at No IP. If the Pi's IP has changed, the application will automatically update the No IP server. This check happens every 5 minutes by default. Here are some instructions for installing and running the DUC on your Raspberry Pi. In the next step, we'll add a crontab task to make sure this application starts if your Pi loses power or reboots.
Your Pi server might need certain programs to run or restart automatically when the Pi is powered on. One way to do this is to create a crontab task. Start by opening a new terminal window and typing this command.
crontab -e
You may see a message about creating a new crontab for the current user along with some options for editing the crontab. Select the nano text editor (option 1), which will open the new crontab file. Scroll to the bottom of the file and add a new line starting with @reboot followed by the command you want to run when the Pi boots up. If you are using the No IP DUC, add this line to make sure the update client is always running at startup. The username and password will be the same credentials you use to log into No IP. If you set up a DDNS key, then use those instead.
@reboot noip-duc -g all.ddnskey.com --username abc --password 123
Now if you reboot or if your Pi loses power, the DUC will start automatically when power is restored. Otherwise, you will need to remember to do it manually every time the power goes out.