How to Host Your Own Website on a Raspberry Pi
Whatsapp Pinterest
Advertisement

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 Raspberry Pi 3 B+

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 Windows 10 SSH vs. PuTTY: Time to Switch Your Remote Access Client? Windows 10 SSH vs. PuTTY: Time to Switch Your Remote Access Client? Let's take a look at how to set up SSH access in Windows 10, and whether the new tools can supplant PuTTY. Read More , 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.

Check your Apache server is up and running

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.

The Apache test screen

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

Configure vsftpd to enable FTP access to your Pi web server

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 How to Host a WordPress Site on Raspberry Pi How to Host a WordPress Site on Raspberry Pi Want to host your own WordPress site on Raspberry Pi? Here's how to get started by installing LAMP and WordPress. Read More .

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!

A test website running on the Raspberry Pi

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 Quickly Set Up Your Own Home Intranet With ocPortal Quickly Set Up Your Own Home Intranet With ocPortal Have you ever thought about what it might be like to have a "portal page" for your family, just like many companies offer to employees? Wouldn't it be convenient to have a central web-based repository... Read More !

Explore more about: Apache Server, DIY Project Tutorials, Raspberry Pi, Web Hosting.

Enjoyed this article? Stay informed by joining our newsletter!

Enter your Email

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Siamak
    August 20, 2017 at 9:15 am

    Hi All,

    I will set this up in a very near future, what I would like to know is when it is set-up and it will be located in the UK, would it allow me to geospoof my location where ever I will be.

    Please advise.
    Cheers

    Siamak

  2. Margareta
    February 8, 2017 at 12:12 am

    fantastic post you guys have at this time how everyone's first impressions on mine website on the subject of mino monsters
    2 evolution cheats free

  3. Prasanna
    May 18, 2015 at 2:23 am

    How to make this website accessed from any where in the world

  4. David
    May 7, 2015 at 8:39 am

    Good article and it works, but other than editing "index.html" can someone tell me how to add content to the default web page or how to add another web page?

  5. Henry
    April 12, 2015 at 12:57 am

    Nice article. I love the Raspberry Pi.. I have a couple of self hosted websites that are working well for the moment using old Windows machines. I am using WAMP Server which is simple to setup --> http://hsny.co/host-your-own-site-with-wamp. Maybe I will try the Pi soon.

  6. Anonymous
    April 2, 2015 at 2:43 pm

    Can I set it up as a AD HOC network and when people connect to it will it show my webpage

  7. shardros
    March 1, 2015 at 4:56 pm

    how do you change the html file?

  8. Vic C
    June 1, 2014 at 12:12 am

    Nice usage. I'll give it a try.

  9. Josemon M
    May 30, 2014 at 9:14 am

    Nice one..But the problem with this is that , it can't handle a lot's of user's limited capacity to traffic ..

  10. pmshah
    May 30, 2014 at 1:55 am

    I think the page loading problem has more to do with the upload speed provided by the ISP than the data fed by the server.

  11. Don Gateley
    May 30, 2014 at 12:25 am

    How is Comcast likely to respond to this use of their service? Assuming first that they'll give you a static IP.

    • Christian C
      May 30, 2014 at 6:42 am

      As I'm in Britain, I couldn't comment on Comcast. However there are alternatives to straightforward static IPs: http://www.noip.com/

  12. ????? ????
    May 29, 2014 at 10:22 pm

    Great post.This article is very helpful. keep sharing such information

  13. Melroy D
    May 29, 2014 at 3:39 am

    I cannot thank MUO enough for this. I've been breaking my head over it for weeks. This is just perfect. Kudos.

  14. Simen B
    May 28, 2014 at 7:58 pm

    Nice article! I've been running a little server on my Pi for a year now, and it works great!
    However, it requires a bit of patience. Ten seconds to load each page on WordPress is common, depending on the theme. My current setup, which is a theme using mostly the same logic as _S (same as I use on http://simen.codes and two plugins, takes about five seconds to load frontend pages, and up to twenty on the backend.

    On the other hand, you could use a ultrasimple cms., like this one: [Broken Link Removed] . Simply unzip it somewhere on your computer, change the password in img.php and new.php, and replace the logo in menu.php. Please note that this was something I made while getting bored to death in IT class a few months ago, so I wouldn't consider it bulletproof.

    Finally, to anyone setting up a server for the first time: have fun, you'll learn a lot while doing this!

  15. Pijush G
    May 28, 2014 at 6:56 pm

    makeuseof.com is now on right track. Very good article!