How to set up a basic web server on Raspberry Pi?

This is a typical question you could ask yourself in various projects on Raspberry Pi
As I’m also a web developer, I make a lot of web projects and I’m used to do this, but I could understand if it’s not clear for everyone

How to set up a basic web server on Raspberry Pi?
To turn your Raspberry Pi into a full LAMP server you’ll need three packages: Apache, PHP and MySQL
Apache to answer HTTP requests, PHP to generate dynamic content and MySQL to store and read information in a database
These are all available with apt-get on Raspbian

In this post, I’ll show you how to install and configure them to work together
I’ll also explain some extra tips depending on what your really need to do

How does it work?

Before going deeper on the installation steps, I want to be sure that everyone understand what we are doing and how a web server works


The main goal for the Apache service is to answer requests on HTTP and HTTPS ports of your Raspberry Pi
HTTP and HTTPS are the main protocols on the Internet for web surfing
It works because Apache is here to respond to your browser requests on ports 80 and 443

Apache receives a request on http://<RASPBERRYPI_IP> and has to display something, depending on your Apache configuration
Typically, it’s an HTML page for your website or the app you’re using on the Raspberry Pi
HTML is a language served by Apache and readable by your web browser to display web pages


PHP is here to bring dynamic content to your web pages
HTML is a static page language you can supercharge with PHP code to make your page look different depending on external conditions (visitor language, visitor history, time of the day, etc …)

Here’s what happens when a visitor sees your page

  • PHP compile the PHP code in your page
  • PHP generate a specific HTML code
  • Apache always display static HTML code, but this time PHP generated it before

I’ll show you examples later in this post to better understand this part


MySQL is a way to manage a database on your web server, to store any data you need to keep on your site
For example on RaspberryTips, there is a database with all the posts, all the images links, all the comments, etc …

MySQL includes three parts:

  • A server, to store data in files and answer requests
  • A client, to connect the server locally or remotely
  • A specific language (SQL), to grab the needed data precisely

How to install web server components?

At this point, you know what each component is and if you need all of them or not
Maybe you want to create a basic and static web server, then Apache will be enough
Or if you want dynamic pages but you don’t need to save data on the server, Apache and PHP will be enough
I’ll let you make your choice and skip the unnecessary components

Before starting you need two prerequisites:

  • Install Raspbian on your Raspberry Pi if not done already
  • Update your Raspbian repository to get the last version of each component
    sudo apt update
    sudo apt upgrade

Install Apache

The first step is to install Apache on your Raspberry Pi
To do this use apt like this:

sudo apt install apache2

Then we’ll check if Apache is working

As you can see in this page, web pages are under /var/www/html on the Raspberry Pi
You can edit index.html to change this page or add new pages in the same folder

You can also remove this page and just upload the files you want to share in this folder
For example, if you want to share softwares on your network, just remove the index.html and put your files in /var/www/html, Apache will automatically generate a web page with links to each software

Install PHP

Then we need to install PHP to add dynamic content capabilities to our web server


To install PHP we need to add two packages:

sudo apt install php libapache2-mod-php

The first one adds the core package for PHP and the possibility to run PHP scripts in the terminal
The second allows us to add PHP code in web pages

First PHP page

We’ll check if everything is fine with our installation before going further

Follow these steps to create a PHP file and test it in your browser:

  • Move to the Apache web folder
    cd /var/www/html
  • Create a PHP file
    sudo nano test.php
  • Copy this code into

    This is a basic PHP function to display PHP configuration in the browser

  • Save and Exit (CTRL+O, CTRL+X)
  • Go to this URL with your web browser: http://<RASPBERRY IP>/test.php
  • This should display something like this:
    php configuration test with apache

If you see this page, everything is OK, your Apache and PHP web server is ready
Move to the next chapter if needed

Install MySQL

The last component is MySQL, and again we need to install it with this command:

sudo apt install mysql-server php-mysql

The second package adds the possibility to use MySQL functions in PHP code
You need to restart Apache to add this functionality:

sudo service apache2 restart

Create your first MySQL user

MySQL stores data. There are authentication stuff to be sure that only allowed people can access these data
For a quick start, we’ll create a MySQL super user to use in our PHP code later
But you can create as many users as you want with different privileges

  • Log into the MySQL console
    sudo mysql
  • Create your first database
  • Create the first user
    CREATE USER 'webuser' IDENTIFIED BY 'password';

    Try to use a strong password immediately, you’ll forget to change it later 🙂

  • Add all privileges to our test database to this user
    GRANT ALL PRIVILEGES ON test.* To 'webuser'@'localhost' IDENTIFIED BY 'password';
  • Save the changes
  • Quit the MySQL console

Create a first table in the database

Now we have a test database and a test user, we’ll check that everything works fine by creating a first table in the database to use it later in the PHP code
If you’re new with these database words, imagine the database as an Excel spreadsheet and a table as a tab in this spreadsheet. We’ll store data in this table

  • Go back to the MySQL console, but with the webuser this time
    mysql -uwebuser -ppassword test
  • Create a basic table
        line_id INT AUTO_INCREMENT,
        data VARCHAR(255) NOT NULL,
        PRIMARY KEY (line_id)

    Obviously, this is a basic useless table, if you want to use anything else you can
    Press ENTER after pasting the request

  • Insert one line in it
    INSERT INTO test (data) VALUES ("This is a test string in my database");

    We add a first line in your table, with “test” in the data field
    Yes, I’m very creative with this 🙂

  • Exit

Now we’re ready to move to the PHP code
We have all we need (database, table and user)

First PHP script to connect to MySQL

After all these preparation steps we can now check if PHP can speak with MySQL

  • Move to the Apache folder
    cd /var/www/html
  • Create a new php file
    sudo nano test-mysql.php
  • Paste these lines into it
    $link = mysqli_connect("", "webuser", "password", "test");
    if($link) {
       $query = mysqli_query($link, "SELECT * FROM test");
       while($array = mysqli_fetch_array($query)) {
          echo $array['data']."<br />";
    else {
       echo "MySQL error :".mysqli_error();

    You may need to edit the first line to fit your first user login and password

  • Save and Exit (CTRL+O, CTRL+W)
  • Switch to your web browser and check the URL: http://<RASPBERRY IP>/test-mysql.php
    This should display the content of your database table

Here we are, you know how to install a basic web server and create a first page to connect to MySQL and display something from your database
Feel free to adapt the database and the PHP code to fit your needs

Extra tips

I’ll give you here two tips to improve your web server or to make it easier to manage


Previously in this post, we made all the changes in the MySQL database manually, doing requests to create users, grant access or insert data
But there is a famous tool to do this faster and intuitively: PHPMyAdmin
It’s a free tool, providing a web interface to manage your MySQL server

To install it, follow these steps:

  • Install the package with apt:
    sudo apt install phpmyadmin
  • During the installation process, select these options:
    • Select Apache2 (press space and enter)
    • Configure the database for PHPMyAdmin with db-common: No
  • After the installation, go to http://<RASPBERRY IP>/phpmyadmin
  • Log in with the user created before
  • You’ll find our database in the menu, with the table inside and the data on the right
    Something like this:
    phpmyadmin interface
  • You’ll also find intuitive menu to do everything within your database

Virtual Hosts

If you want to host several websites or apps in your Raspberry Pi, you have two choices:

  • Use one folder for each app, like and
  • Use virtual hosts to have an address like and

For the first option, just create as many folders as your need in /var/www/html
For the second one, you need to:

  • Add sub-domains in your DNS server or in your hosts file
  • Create virtual hosts in the apache configuration, one for each sub-domain

You can make this configuration in the /etc/apache2/sites-enabled/ folder
You’ll find all the help you need on this page for example


I hope this article will help you to install a basic web server on your Raspberry Pi
I think this is rather a beginner how-to guide, and I could write a lot more on this topic
I’ll create other posts on Apache/PHP/MySQL to help you master these main services on Linux



Patrick Fromaget

I'm the lead author and owner of My goal is to help you with your Raspberry Pi problems using detailed guides and tutorials. In real life, I'm a Linux system administrator with a web developer experience.

6 thoughts on “How to set up a basic web server on Raspberry Pi?

  1. This post was clear and precise every steps of the way. I was able to set things up nicely on my pi and i am now working on putting together my site. Thank you, this was very helpful!

  2. Dear:
    When installing php-mysql this message appear: unable to locate php-mysql.
    Do you know what can i do?

    Thank you very much!

    1. Hi Herman,

      Seems to work on my side
      Try to use “apt search php-mysql” to see if there is a small change in the package name

      I know on Debian it’s php5-mysql or php7-mysql, but on Raspbian it seems to be simply php-mysql

      Let me know

  3. hey there . I have an important Question if you could give me an answer on how to execute it .

    I am implementing a project based on Atrial Fibrillation using rasspi .
    Our code in rasspi generates a excel(Csv) file when a user puts on a ppg (heart) sensor. Now the question remains that how could we upload the excel sheet on myphp admin . I just want that csv file to be uploaded to the database in the same way as it generates the file automatically .

    Can you help me out please.

    1. Hello,

      Yes, you can import it
      Create a table with the same columns
      Then go to PHPMyAdmn, in the “Import” tab
      You just need to have the same fields in your table and your Excel file

      Maybe it’s easier if you save your Excel file in CSV before importing

Leave a Reply

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

Recent Content