Apache Web Server On Raspberry Pi



What You Need to Make an Apache Raspberry Pi Web Server. Making an Apache web server with Raspberry Pi is fairly simple as well as economical. You'll only need the Raspberry Pi, a microSD card, power supply, and Linux-based distribution. A case is optional, but recommended. What you'll need for a Raspberry Pi Apache Web Server: Raspberry Pi board. Raspberry Pi 3B+ Raspbian GNU/Linux 9.4; Install Apache 2 $ sudo apt-get install apache2 $ sudo apache2 -v Server version: Apache/2.4.25 (Raspbian) Server built: 2018-03-31T08:47:16 $ ifconfig inet x.x.x.x.

  1. Learn how to install the Apache web server on your Raspberry Pi in this tutorial.Get a Raspberry Pi 4 on Amazon 👉 snippets and m.
  2. To install a full web server on Raspberry Pi (LAMP), three packages are required: Apache, PHP and MySQL. Apache answer HTTP requests, PHP generate dynamic content and MySQL store and read information in a database.
  3. Step 1: Install Raspberry Pi and Apache Web Server As we mentioned before, we need some pieces of software as well as to do some configurations in order to have a working web server.

In this tutorial we will learn how to use Raspberry Pi 4 as a web server. In addition to that we will also see how to make web pages in HTML & style with CSS. So let’s get started.

Components Required:

  1. Dual Fan Heat Sink aluminium Armor Case. (Optional)

Installing Apache:

Apache is web server application, it serve HTML files over HTTP.

First, update the available packages. Open terminal and type command

type this command to install apache2

Installing PHP:

PHP is used for server-side programming which will interact with databases to retrieve information, storing, email sending, runs the logic and provides content to HTML pages to display on the screen.

Install PHP and the PHP module for Apache.

Testing The Web Server:

When you install apache by default it will put web page on web folder. You can access that web page within pi itself by opening your browser and typing http://localhost/ or raspberrypi.local (if your pi host name is raspberrypi). If you want to access that web page on another PC or computer, you can type http://192.168.1.102 (you have to type your pi IP address here). For finding pi IP address you can open terminal and type

If you see this page on your browser it means apache is working.

How to change default web page:

After installing apache and testing web page. You are thinking to change web page to your web page. But you can’t edit it right now. You need to change its ownership to your own username (by default it is pi, if you haven’t changed).

Navigate to this directory

Change ownership to your username

Now you are able to change, edit your web page and refresh the web page to see changes.

Enable SSH:

Go to Menu → Preferences → Raspberry Pi Configuration → Interfaces → enable SSH

WinSCP:

You can edit index html file in raspberry pi itself but if you are testing a lot, I would recommend to download WinSCP. WinSCP (Windows Secure Copy) is an open source SecureFTP client for Windows. It allows secure file transfers between the client’s local computer and the remote server. You can directly edit your html file inside the WinSCP.

Select File Protocol → SFTP

Host name Raspberry pi → IP address

port number → 22

user name → pi

password → raspberry

save & Click on login.

Now you will be able to see raspberry pi files on right side and Your PC’s file on the left side.

Make you own Website:

You can add HTML & CSS page to make your own website with raspberry pi.

HTML:

Title Tags:

The title goes between <title> and </title> tags. Title tag should go within <head> and </head> tags. This title will not be visible on webpage, it will be visible on web browser tab.

Heading Tags:

Heading tags start with h followed by a number that indicates the heading level.

Apache Web Server On Raspberry Pi

h1 → Highest level

h6 → Least level

Heading tag should go between the <body> and </body> tags.

Paragraph Tags:

You have to write content of your website in between <p> and </p> tag. You can add many paragraphs as well as headings.

Adding Hyperlinks:

Steam acc gen. Add link by <a> tag. This should be in between <body> and </body> tags.

Adding Image:

for inserting image we use <img> tag. It doesn’t have closing brackets <>. File image you want to include must be in same folder where your index file is.

Adding Buttons:

We want to make button clickable so, inserting button tags between the <a> hyperlink tag.

CSS:

Embedding a Style Sheet:

Before going to CSS file, you need to embed the style sheet in the index.html file so that the HTML knows to reference an external CSS file. To do this add following lines between the <head> and </head> tags

Styling Headings, Paragraph and Links:

Define text size here. We are using rem unit. Rem is the computed value of font-size on the root element, we use one font size as a reference across the page. This means all font size are relative to each other.

Main section formats the main content of your page. We are setting maximum width of the content to 500px.

Hyperlinks are underlined by default. Set text-decoration to none to remove the underline.

Styling Button:

Set display to block ensures the button is displayed as a block element. We are also using padding property to add space around the content. For text color and background color we are using hexadecimal color code. Feel free to change that.

Start Testing Our Own Web Page:

Type pi IP address to any web browser, you will be able to your live website by using raspberry pi.

Video:

If you like this kind of content please subscribe to my YouTube channel for more updates. Till then Keep Learning Keep Making.

This guide covers setting up a light webserver on Linux using the lighttpd web server on the Raspberry Pi. Most of these instructions can also be applied to other Debian or Ubuntu based distributions (except the tasks using the raspi-config tool). I've already written a tutorial based on the 'full-fat' Apache webserver (running a LAMP Apache based webserver on the Raspberry Pi).

Lighttpd provides a way of setting up a web server without putting too much load on the limited processing capability. It's ideal for providing web access to the Raspberry Pi as a monitoring tool, or as a lightweight webserver for a personal website.

Debian Linux

This is based on the Debian Raspberry Pi Raspbian.

Using the command line and editing files

As we are going to be doing this through the command line it is useful to understand a little about the shell. Although this provides step-by-step instructions if you haven’t used the command line previously I suggest you read the basic shell reference guide, and then return to this point.

Throughout the install you will see many commands prefixed with sudo. The sudo command allows the user to issue a command as the superuser (or in certain circumstances as another user). Without using the sudo command many of the commands would fail stating insufficient permissions. This is a security feature that protects the system from other users, but also limits the amount of damage that a user can do by mistake (although if prefixed with the sudo command it will not help against accidents).

Security

The first priority is to make the Raspberry Pi a little more secure. The image includes a default username and password, which once connected to the Internet would allow anyone to login and have free roam of the device.

To change the password for the pi user after logging in issue

passwd

and follow the prompts for changing the password.

You may also want to add your own username. I have used user1 as the username, but typically this will be a persons name. You can skip this and go straight to the performance / networking steps if this is not required.

This will add a new user and change their password.

Here you will see the first use of the sudo command which we will be using a lot. As used above the sudo command allows the user to issue a command as the superuser. Without using the sudo command this would fail as regular users are not allowed to create other users. This is a security feature that protects the system from other users, but also limits the amount of damage that a user can do by mistake (although if prefixed with the sudo command it will not help against accidents).

The new user will need to be added to certain groups to allow the same privileges that the pi user had.
You can add the new user to the groups using the usermod command or you can edit the file directly. I’ve done the following by editing the file so that you can see the file (it’s also arguably a little quicker as you can make multiple changes whilst editing the file). Please be aware that when editing files like these a mistake can result in not being able to login.

There are two command line text editors. The nano editor is the easiest for new users (so that’s what I’ve referred to below), but I do recommend learning the vi text editor as it is useful tool that is installed on all linux systems. If you are familiar with vi then replace nano with vi for the rest of this guide.

sudo nano /etc/group
Go through the file adding ,user1 to the end of all of the groups that pi is in.
eg
adm:x:4:pi,user1

Use CTRL-O to save and CTRL-X to quit after editing the file.

The most important is the admin entry as without that the user will not be able to run sudo and hence perform any system administration. Of course if you want to add a different user and don’t want to give them admin access then you don’t need to make any updates to the /etc/group file.

type

exit
to logout and now login under the new username to check that it is working correctly.

By default the shell for the new user is the bourne shell. The bash shell is an improvement on that allowing the user of the arrow keys on the command line and autocompletion.

To set the default shell for you new account (when logged in under that account) use:

chsh -s /bin/bash

You could now remove the pi username if it is no longer required.

userdel pi

This is just the initial stages in making the Pi more secure. There are other aspects to Linux security including making sure that appropriate security fixes are applied as they become available (eg. apt-get update).

Performance tuning the operating system

Performance tuning is something that you would normally leave until later, but in the case of the Raspberry Pi there is an single option that can be done to improve performance for servers. By configuring it here we can let it get picked up by the reboot later saving us from having to reboot the server.

The Raspberry Pi has up to 1Gb of RAM. This RAM is however shared between the graphics and main system memory. By default 64Mb is allocated to graphics. This is overkill if you don’t plan to run the graphical interface (or rarely) as in the case of a server. To reduce the amount of memory available for graphics to 32MB enter use
sudo raspi-config and see the memory split option under advanced options.

This requires a reboot to take effect, but we will be rebooting later, so there is no need to reboot at this point.

Setting up networking

The next step is to give the Raspberry Pi a static IP address. This is a little more complex as it depends upon your own setup and what router you have on how to achieve this.

By default the Raspberry Pi will request a dynamic IP address which is issued by your router as required. This however may change in future which would make it hard to connect to the webserver. Instead we provide it with an address that doesn’t change such as 192.168.1.4.
Note that this address can be used on the local network, but not on the Internet – later you will see how to provide access through your router / firewall from the Internet.

First find out what DHCP address has been allocated by using the ifconfig command – see the extract below

This is saying that the ethernet port 0 – has an IP address of 192.168.1.110

You will also need to find out what address your router is, using the route command

This shows that the router IP address (Gateway) is 192.168.1.1 and all traffic is sent via that router.

At this point you will also need to check on what address range is being issued by the router for dynamic (DHCP) requests. This depends upon the individual router. In my case I have a Belkin Wireless Router which can be reached by pointing a web browser to the IP address of the router 192.168.1.1

The LAN settings are shown below:

In this case the local network has valid addresses from 192.168.1.1 to 192.168.1.254. The router is at address 192.168.1.1 and any DHCP requests will be given entries between 192.168.1.100 and 192.168.1.150 (you can change the range of the DHCP addresses if required). I have used 192.168.1.4 for this server.

To change to static IP address you should look in the /etc/dhcpcd.conf file which allows you to specify static addressing. Nonton american horror story season 8.

add the following entries to the bottom of the file

Note that for the domain_name_servers entry I have used the Google DNS servers, but you may prefer to use your local router or ISP DNS servers.

Whilst you can dynamically reload the network interface I suggest a reboot at this stage to make sure that the configuration is correct.

Raspberry pi website host

sudo reboot

After logging in check using ifconfig to confirm that we have a static ip address

Enabling ssh

SSH (Secure Shell) is a network protocol that allows you to login and control the computer through the command line remotely. As the name suggests it is secure as it encrypts communication across the network (so that others cannot see your password etc). It also has some great features such as tunnelling, which we won’t go into here.

The ssh server is installed on the default image but needs to be enabled so that it starts at boot time. You can enable/disable the ssh server using sudo raspi-config.

You can now connect to the Raspberry pi remotely (on the same network) via ssh.
If you have another linux computer on the network then from a terminal run

ssh 192.168.1.4
which will login with the same username. If you want to use a different username then prefix that before the ip address with an @ sign.
eg
ssh user1@192.168.1.4

SSH is also included on a MAC so you can use the same commands as above.

If you want to connect from Windows then there are several options, but I suggest the open source software Putty.

Making the server available on the Internet

Next we are going to configure the router to allow ssh logins and web traffics through its firewall to the Raspberry Pi.

You did remember to change the default password for the pi username didn’t you! If you haven’t already changed the default password then do it now otherwise anyone will be able to login to your Raspberry Pi.

As a home user the ip address used on your local network is a private address range that will not work over the Internet. Instead your ISP will provide a single dynamic IP address which is used by the router. To allow traffic to flow from the Internet to your Raspberry Pi needs the IP address of the Pi to be made to look as though it is from the router. This is a process called Network Address Translation (NAT).

The ports that need to be allowed through are port 80 (http) and if you would like to be able to login to the computer from the Internet then port 22 (ssh).

To do this you will need to consult the instructions on your router. In the case of my Belkin router this is through the Firewall > Virtual servers settings (see below), but Netgear this is Advanced > Security > IP Forwarding.

DNS

Raspbian Web Server

The final stage is to have a DNS entry point at your router’s IP address. Unless you have a fixed IP address from your provider then your IP address can change. I use DuckDNS.

Install apache webserver

The Apache webserver is available to download from the Debian repositories. This can be done through the apt tools.

First have you refreshed the software repositories? If not run sudo apt update to make sure that it knows about any new packages / versions available.

Installing lighttpd

To install the lighttpd web server issue the command.
sudo apt install lighttpd

This will install the web server and also pull in any other packages (called dependencies) that are required. The server will be automatically started and set to start by default after a reboot.
[ ok ] Starting web server: lighttpd.

Install Maria database (optional)

Whilst you can have a perfectly good website without a database, database provide a good way of holding data and are a requirement for many content management systems (CMS) and web based applications. If you don't need a databse the you can skip this and go straight to configuring php.

In the past the Mysql database server was used by most distributions. Since mysql is now managed by Oracle many distributions (including Debian on which Raspbian is based) have now moved to the similar Maria database software. Maris can be isntalled from the software repositories using:

sudo apt install mariadb-server

During the install there is a prompt request for a password.
The password is for the mysql root user and it is strongly recommended you choose an appropraite password.

Install PHP

In the past Perl was frequently used for web programming, but since then PHP gained popularity for being easier to integrate into web pages. Python is also gaining popularity for web programming. For Python there are two different versions, the old Python (which is what runs when you just type python) and the new (recommended) Python which is version 3 (run by entering python3).

You can install all these different programming languages and then just pick and choose based on your particular needs.

The good new is that Perl (version 5) and Python (version 2.7 and 3) are installed as part of the operating system so you will just need to install PHP.

The following commands will install PHP version 5.

If you installed Maria database then you should also issue the following command to install the php mysql libraries to allow PHP to access the Maria database. Note this is not a typo, the Maria database is based off MySQL, so you can use the mysql libraries to interact with Maria as well.
sudo apt-get install php5-mysql

To enable the server to handle php scripts the fastcgi-php module should be enabled by issuing in the command
sudo lighty-enable-mod fastcgi-php
Then reload the server using
sudo service lighttpd force-reload

Set permissions on the web directory /var/www/

It is useful to change the permissions on the www directory to allow your user to update the webpages without needing to be root.

Change the directory owner and group
sudo chown www-data:www-data /var/www
allow the group to write to the directory
sudo chmod 775 /var/www
Add the pi user to the www-data group
sudo usermod -a -G www-data pi

You should logout and back in - to pick up group permissions, or if running X you can just start a new terminal.

Testing the server

Once the setup is complete you can access the web page by pointing your browser to the router IP address or DNS entry.

Apache On Raspberry Pi

You should get a page back stating that it works, but that there is no content loaded.

To test that the webserver and PHP are working correctly then delete the file /var/www/index.lighttpd.html and create a file /var/www/index.php with the contents of this page.

Raspberry Pi Home Server

Note that the filename of the link ends in .txt to prevent my webserver from running this, you should have the file ending with .php so that the file is run as a PHP script.