Family Best Time >> Electronics

How to turn your Raspberry Pi into a personal web server

Want to build your own website but don't need large-scale server-side processing? Wouldn't it be easier to create, host and maintain a website right on your Raspberry Pi? Here we'll show you how to turn a Raspberry Pi into your own personal web server.

ContentsWhat is Apache Web Server?What You'll NeedUpdate your Raspbian Install Apache Web ServerGet Permission:Edit Apache HTML FileHTML:Customize Apache Web PageMake Your Website Dynamic:Install PHP 7Test your dynamic PHP

By the end of this tutorial, you will have learned how to install the popular Apache web server on your Raspberry Pi, configure PHP, and create a simple web page that anyone can access through your local network.

What is Apache Web Server?

Apache is one of the most popular web server applications in the world, accounting for nearly 40 percent of all web servers at the time of writing.

Once you've configured Apache on your Raspberry Pi, you can use it to serve various files to anyone on the local network.

What you'll need

To complete this tutorial you will need:

  • Raspberry Pi running Raspbian. If you don't already have Raspbian, you can grab the latest version and flash it using Burner.
  • Power cable compatible with your Raspberry Pi
  • External keyboard and way to attach it to your Raspberry Pi
  • HDMI or micro HDMI cable, depending on your Raspberry Pi model
  • External monitor
  • Ethernet cable or Wi-Fi connection

Update your Raspbian

If you haven't already, connect your external keyboard, monitor, and other peripherals to your Raspberry Pi, then connect it to a power source.

Before you start, it's best to make sure your Raspbian is the latest version. Launch a Terminal window by clicking on the small "Terminal" icon in the toolbar. Type the following commands in the terminal:

sudo apt-get updatesudo apt-get upgrade

If Raspbian installs one or more updates, restart your Raspberry Pi by running the following command:

reboot

Once your Raspberry Pi has restarted, it will run the latest version of Raspbian.

Install Apache web server

You are ready to install the Apache2 package on our Raspberry Pi. In Terminal, type the following command:

sudo apt install apache2 -y

There you go:your Raspberry Pi is now functioning as a basic web server!

To see your Apache web server in action, you'll need to enter your Raspberry Pi's IP address into a web browser. To retrieve this IP address, run the following command in the Terminal window:

hostname -I

This will return your Raspberry Pi's IP address; just enter this address in any web browser. You should see the following page.

How to turn your Raspberry Pi into a personal web server

Before you can edit this file, you must take ownership of it. You can change the property using a Terminal command. The following example assumes you are using the Raspbian username "pi"; if you changed it manually, make sure it is reflected in your Terminal command:

sudo chown pi:index.html

If you run the ls -al again command, you should see that "pi" now has permission to modify this file.

How to turn your Raspberry Pi into a personal web server

HTML:Customize Apache web page

You can now open the "It's Working" page for editing by running the following Terminal command:

nanoindex.html

This launches the "index.html" file in Raspbian's Nano text editor.

How to turn your Raspberry Pi into a personal web server

You can modify every part of the code on this page, but to make things easier, the text displayed in its header has been modified in this example.

How to turn your Raspberry Pi into a personal web server

Once you have made your changes, save the file by pressing Ctrl + O , followed by Ctrl + X .

Now load your Raspberry Pi's IP address into your web browser and you should see your changes.

How to turn your Raspberry Pi into a personal web server

Make your website dynamic:install PHP 7

By default, the Apache web server is limited to static content, so your pages will not react to information provided by users. If you want to make your content dynamic, you will need to install the latest version of PHP, which was PHP 7.4 at the time of writing.

In this section, you will install the latest version of PHP and the PHP module for Apache:

sudo apt install php libapache2-mod-php -y

To test that PHP is correctly configured, you will create a PHP file in the "/var/www/html/" directory, then check that this file appears in our web browser.

To create a PHP file called "mywebpage.php", run the following command in the Terminal window:

sudo nano /var/www/html/mywebpage.php

The “mywebpage.php” file opens automatically in Nano. In the Nano text editor, enter the following PHP script:

 

This simple script retrieves today's date and displays it as part of a web page.

To save your script, press Ctrl + O , followed by Ctrl + X .

Test your dynamic PHP

To test that this PHP file is served correctly, enter your Raspberry Pi's IP address into your web browser, followed by "/mywebpage.php". For example, if your IP address was 190.100.1.100, you would enter the following URL:

http://190.100.1.100/mywebpage.php

If the PHP file is served correctly, your browser should display something like the following image.

How to turn your Raspberry Pi into a personal web server

As you can see, it's easy to turn your Raspberry Pi into a web server, although you'll need to set up a dynamic IP address to be able to connect to your web server from a public network.