Need to run a website but can’t afford the hosting costs?
You could use this link to get a special discount at InMotion Hosting.
Or you could try turning a Raspberry Pi into a web server, which is more than capable of running basic web server software. Best of all, setting it up is simple.
Why Host a Website on Raspberry Pi?
There are several good reasons to setup a Raspberry Pi as a web server.
- Hosting costs are expensive
- Your desktop PC is too expensive to run all day
- Raspberry Pi has a very low energy footprint
- The Pi can be configured as a portable device
- Potential for beating censorship
Whatever your motivation for choosing to host your website on a Raspberry Pi, it can be set up in minutes. Better still, you can use any version of the Pi, even the Pi Zero. If the device can be connected to the internet, you can host a website on it.
Setting Up the Raspberry Pi Web Server Hardware
Start with a reliable power supply for your Raspberry Pi. You should also have the latest version of your preferred Raspberry Pi distro installed to a high-quality SD card.
The steps and screenshots in this guide are for setting your Raspberry Pi web server up with Raspbian Stretch.
As with most servers, you’ll need to access the Raspberry Pi web server using SSH. In Linux and macOS you can do this via the terminal. In Windows, use PowerShell to connect over SSH, or install a third-party tool.
Setting Up Wireless Networking and SSH
While you can use your Raspberry Pi as a web server with just a keyboard and monitor, remote access is simpler.
The best way to do this is to enable SSH on your Pi and set up wireless networking in advance. If you’re using an existing Raspberry Pi installation, you’ve probably done both already. Starting fresh? You can configured SSH and wireless networking while your SD card is still in your PC.
Begin by ensuring your SD card is visible in your computer’s file manager, and the /boot/ directory selected. (In Windows, this is the only portion of the SD card that is readable.)
Next, in the /boot/ directory window, right-click in empty space and select New > Text Document. Name the document ssh taking care to remove the .TXT file extension. When the Pi boots, it will detect the ssh file and enable this feature.
To connect over SSH, you’ll need to first enable wireless networking. To do this, create another file, this time calling it wpa_supplicant.conf. Again, delete the TXT extension. Open the file, and copy and paste the following:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
ssid="YOUR_NETWORK_NAME"
psk="YOUR_PASSKEY"
key_mgmt=WPA-PSK
}
Once pasted, edit the values to show your network name (SSID) and password (PSK). Save the file, then safely eject your SD card. After replacing it in the Raspberry Pi, boot up. You should be able to connect to the Raspberry Pi over SSH, using the Pi’s IP address. Find this by checking your router’s admin screen (check the device documentation), or by briefly connecting a keyboard and display.
With SSH set up, you can remotely connect to your Raspberry Pi web server at any time.
Configuring Your Raspberry Pi as a Web Server
Before you upload your HTML pages to your Raspberry Pi, you’ll need to configure the server and its software. To do this, install Apache and PHP. This will enable the serving of pages written in HTML and PHP.
Begin by updating Raspbian:
sudo apt update
sudo apt upgrade
Next, install Apache and associated libraries, by entering:
sudo apt install apache2 -y
Then check that Apache is running:
sudo service apache2 status
If the server displays “active (running)” in green text, everything is fine.
Should the server be down, start it with:
sudo service apache2 start
At this stage, you can browse to your Raspberry Pi’s web server from a browser.
If the browser is installed on your Raspberry Pi, visit this URL in your address bar:
http://localhost
However, if you are using SSH, use the IP address of your Raspberry Pi from your PC browser (e.g. http://123.456.789.0
). You should see confirmation that Apache is installed.
Your Raspberry Pi is now setup as a basic web server. All you need to do now is add pages!
Configuring FTP on the Raspberry Pi
While you can check in the browser that your Pi is running as a web server, the page on offer will be very basic. This is a typical placeholder index.php file, one that you will have to replace with your own PHP or HTML document.
Begin by creating a www directory and installing the FTP software:
sudo chown -R pi /var/www
sudo apt install vsftpd
With the FTP application vsftpd (“Very Secure FTP Daemon”) installed you’ll need to make some changes to the configuration. First, open the config file in nano…
sudo nano /etc/vsftpd.conf
…and make the following permissions/security changes:
First, change anonymous_enable=YES to anonymous_enable=NO
Then, uncomment the following lines (by removing the # symbol)
#local_enable=YES
#write_enable=YES
This enables the settings the lines pertain to. Finally add this line to the end of the file:
force_dot_files=YES
Doing this will force the display of server files starting with a “.”, such as the all-important .htaccess file.
Press Ctrl + X to save and exit, confirming with Y and Enter.
Finally, restart FTP with:
sudo service vsftpd restart
Using a standard desktop FTP you will then be able to connect to your Raspberry Pi. Upload files to /var/www/html.
HTML Not Enough? Raspberry Pi Also Supports LAMP!
The Raspberry Pi isn’t only capable of serving basic HTML pages. The LAMP server configuration can be installed if you want MySQL with PHP support. Simply follow the installation steps above to install and set up Apache, then add install MySQL followed by PHP.
The following packages install MySQL and the requisite PHP component:
sudo apt install mysql-server php-mysql -y
Once this is installed, you’ll need to restart Apache:
sudo service apache2 restart
Next, install PHP itself with:
sudo apt install php -y
Again, restart Apache using the command above. Your LAMP server is now installed, ready for you to setup database-driven PHP web applications and websites.
As with any web server project, you should determine if the Pi will be suitable for your needs. While it is possible to setup WordPress to run on the Raspberry Pi, adding a multitude of plugins and multiple daily updates will make it extremely slow.
The website you plan to run on your Raspberry Pi should be light on resources and software requirements. A database driven solution can be used if necessary, you should limit the number of pages that can be viewed.
Want to go further? Check our guide to hosting a WordPress site on your Raspberry Pi.
Security and Technical Considerations
You’re unlikely to get anything nearing production-level speed when using the Raspberry Pi as a web server for anything resembling a popular website.
There are ways you can improve performance, however, such as setting up your server software on a USB hard disk drive rather than on the SD card in order to reduce degradation through regular read/write processes. Alternatively, using your device RAM for temporary read/write file storage is an option too. This is something you will perhaps want to change when you have decided how you’re going to use your very small web server.
Although suitable for setting up a test version of a database-driven PHP website, the Raspberry Pi’s online duties are probably best suited to a small collection of static pages.
Note that if you do plan to open access to your Raspberry Pi as an internet-connected web server, you’ll need to set up your router with a static IP address.
Finally, take the time to change your Raspberry Pi’s default password. This can be done in the command line via SSH using:
passwd
You will then be prompted to input and confirm a new password. Doing this will stop anyone familiar with Raspbian from being able to access the backend of your web page.
Host Your Own Raspberry Pi Website!
The potential for using a Raspberry Pi as a web server is considerable. Realistically, you won’t be able to host a website as large as MakeUseOf. However, with a battery pack and mobile internet dongle you could host a compact website from anywhere in the world.
Remember, all you need to do is:
- Configure SSH and wireless networking
- Install Apache (MySQL and PHP optional)
- Install FTP
- Set up a secure password
With a static IP address or Dynamic DNS service set up, your website is ready. And if you don’t want it to be accessed from the internet, simply host a home intranet instead!
Read the full article: How to Host Your Own Website on a Raspberry Pi
Read Full Article
No comments:
Post a Comment