Apache + PHP + MySQL –

This is a typical question you can ask in different projects on your Pi Framboise.
Because I’m also a web developer, I do a lot of web projects and I’m used to it, but I understand that for some of you it can be overwhelming.
Don’t worry, you’ll find out at the end of this message.

Three packages are required to install a complete web server on a Raspberry Pi (LAMP): Apache, PHP and MySQL.
Apache responds to HTTP requests, PHP generates dynamic content and MySQL stores and reads information from the database.

In this article I will show you how to install and configure them to work together.
I’ll also explain some extra tips based on what you really need to do.

By the way, if you really want to improve your skills on Raspberry Pi, I suggest you check out my e-book here. This is a 30 day mission for beginners, with step-by-step instructions and many projects to practice on the go.

How does it work?

Before starting the installation steps, I want to make sure everyone understands what we are doing and how the web server works.

Apache

The main purpose of the Apache service is to respond to requests on the HTTP and HTTPS ports of your Raspberry Pi.
HTTP and HTTPS are the main protocols used on the internet for surfing
. This works because Apache responds here to requests from your browser on ports 80 and 443.

Apache receives a request for http:// and should display something, depending on how Apache is configured.
This is usually an HTML page for your website or an application you use on your Raspberry Pi.
HTML is a language used by Apache and read by your web browser to display web pages.

Mastering the Pi raspberry in 30 days
Download the ebook.
Discover the secrets of the Raspberry Pipe in a 30 day challenge.

PHP

PHP is here to bring dynamic content to your web pages.
HTML is a static page language that you can expand with PHP code to make your page different according to external circumstances (visitor language, visitor history, time of day, etc.).

This is what happens when a visitor sees your page:

  • PHP compiles the PHP code of your page (via an Apache module).
  • PHP generates specific HTML code
  • Apache always displays static HTML code, but this time PHP generated it earlier.

I will show you examples later in this post to better understand this part.

MySQL

MySQL is a way to manage a database on your web server that allows you to store all the data you need on your website.
For example, on , there is a database with all messages, all links to images, all comments, etc….

MySQL consists of three parts :

  • Server, to store data in files and respond to requests.
  • Client to connect to the server locally or remotely.
  • A specific language (SQL) to retrieve the required data accurately (e.g. from PHP).

How do I install the components of a web server?

At this point you need to understand what each component (Apache, PHP and MySQL) is and whether you need it or not.
Maybe you want to create a simple, static web server, then Apache will suffice.
Or if you want dynamic pages, but do not need to store data on the server, then Apache and PHP are sufficient.

I leave it up to you to make your own selection and skip the unnecessary parts in the following procedures.
Before you start, you must meet two conditions:

  • Install Raspberry Pi OS on your Raspberry Pi if you have not already done so.
  • Update your Raspberry Pi operating system to get the latest version of each part:
    sudo apt update
    sudo apt update

Installing Apache

The first step is to install Apache on the Pi of the raspberry.
Use the correct method:
sudo apt install apache2

Then we’ll see if Apache works:

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

You can also delete this page and simply upload the files you want to share to this folder.
If you z. For example, if you want to share files over the network, just delete index.html and put your files in /var/www/html, Apache will automatically generate a web page with links to each of these files.

InstallingPHP

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

Installation

To install PHP, we need to add two packages:
sudo apt install php libapache2-mod-php

The first adds the most important PHP package and the ability to run PHP scripts in the terminal.
The second allows us to add PHP code to web pages.

First page PHP

We will check that everything is in order with our installation before we proceed.

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

  • Navigate to the Apache web directory:
    cd /var/www/html
  • Create a PHP file:
    sudo nano test.php
  • Copy this code:

    This is the main PHP function to display the PHP configuration in the browser.

  • Save and exit (CTRL+O, CTRL+X)
  • Use your web browser to go to this URL: http:///test.php.
  • It has to look like this:

When you see this page, all is well, your Apache and PHP web server is ready.
If necessary, go to the next chapter.

Course Raspberry Pi
Go to the next step.
I’m here to help you get started with your Raspberry piss and learn all the necessary skills in the right order.

Installation of MySQL

The last component is MySQL, and again we have 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.
To add this feature, you need to restart Apache:
sudo service restart apache2

Create your first MySQL user

MySQL stores the data. Authentication is necessary to ensure that only authorised persons have access to this data.
To get started quickly, we are creating a MySQL super-user which we will use later in our PHP code.
However, you can create as many users as you like with different authorizations:

  • MySQL console connection:
    sudo mysql
  • Create your first database:
    Test CREATE DATABASE ;
  • Create your first user:
    Create a WEBSITE USER IDENTIFIED with a password;
    Try to use a strong password right away, you will forget to change it later.
  • Add all rights from our test database to this user:
    GRANT ALL REQUIRED TO ‘webuser’@’localhost’ IDENTIFIED by ‘password’;
  • Save the changes:
    TO DEPRIVE HIM OF HIS PRIVILEGES;
  • Leave the MySQL console: Leave

Creating the first table in the database

We now have a database and a test user. We will test if everything works properly by creating the first table in the database, which will later be used in the PHP code.

If you are not familiar with these database terms, imagine the database as an Excel spreadsheet and the table as a tab in that spreadsheet. We will store the data in this table.

  • Back to the MySQL console, but this time with a web server:
    mysql -uwebuser -password test
  • Creating a basic table :
    CREATE TABLE IF NO TEST EXIST ( line_id INT AUTO_INCREMENT, VARCHAR(255) NOT NULL data, PRIMARY KEY (line_id));
    This is clearly a simple and useless array if you want to use something else.
    After inserting the prompt, press ENTER.
  • Insert a line in this new table:
    IN TEST (data) VALUES (This is the test line in my database);
    We insert the first line in your table with the selected test in the data field.
    Yeah, I’m very creative that way.
  • Result:
    stop

We are now ready to proceed with the PHP code.
We have everything we need (database, table and user).

First PHP script to connect to MySQL

After all these preparatory steps we can now test if PHP can talk to MySQL:

  • Navigate to the Apache folder:
    cd /var/www/html
  • Create a new PHP file:
    sudo nano test-mysql.php
  • Insert these lines:
    $link = mysqli_connect(127.0.0.1, web user, password, test);
    if($link) {
    $query = mysqli_query($link, SELECT * FROM test);
    while($array = mysqli_fetch_array($query)) [
    echo $array [data]).
    ;
    }
    else {
    echo MySQL error: .mysqli_error();
    }
    ?>
    You may need to change the first line to match your initial username and password.
  • Save and exit (CTRL+O, CTRL+W)
  • Go to your web browser and check the URL: http:///test-mysql.php
    . It should display the contents of the table in your database.

Here we know how to set up a simple web server and create the first page to connect to MySQL and display something from your database.
Feel free to customize the database and PHP code to your needs.

Additional tips

I will give you two tips to improve your web server or make it easier to manage.

PHPMyAdmin

Earlier in this article we have manually made all changes to the MySQL database by running queries to create users, grant access or insert data.
But there is a popular tool that makes it faster and more intuitive: PHPMyAdmin.
It is a free tool that provides a web interface to manage your MySQL server.

Follow the steps below to install PHPMyAdmin:

  • Install the package with apt:
    sudo apt Install phpmyadmin
  • Select these options during the installation process:
    • Select Apache2 (press the space bar and type).
    • Configure the database for PHPMyAdmin with db-common : No
  • After installation, go to http:///phpmyadmin.
  • Login with the previously created user
  • In the menu you will find our database with a table and data
    on the right side like this:
  • You will also find an intuitive menu to do everything in your database.

Virtual Landlords

If you want to host multiple websites or applications on your Raspberry Pi, you have two options:

  • Use a folder for each application, e.g. B. http://192.168.1.1/app1 and http://192.168.1.1/app2.
  • Use virtual hosts to get the addresses http://app1.domain.com and http://app2.domain.com.

For the first option you create as many folders as you need in /var/www/html
For the second option you have :

  • Add subdomains to the DNS server or host file.
  • Creating virtual hosts in Apache configuration, one for each subdomain

You can make this configuration in the /etc/apache2/sites-enabled/
folder. On this page you will find, for example, all the help you need. B.

Completion

I hope this article will help you install a simple web server on your Raspberry Pi.
I think it’s more of a beginner’s guide, and I could write a lot more about it.
If you are interested, I will create other messages on Apache/PHP/MySQL to help you master these basic services on Linux.

Related Tags:

configure php with apache linux,configure apache for php 7,apache, php windows,deploy php application on apache linux,apache-php docker,httpd.conf php,lamp server download,ubuntu 16.04 install lamp,ubuntu 18.04 install apache php mariadb,mamp,install php ubuntu 20,install php 7.4 ubuntu,error: module php does not exist!,unable to locate package php,apache run php ubuntu,xampp 5.6 download,xampp full form,xampp vs wamp,xampp tutorial,how to install xampp in ubuntu,xampp default files,que es xampp,advantages of xampp,features of wamp server,wampserver review,amp stack windows,php mysql apache book,install apache, mysql phpmyadmin,apache installation for windows 10,windows 10 install httpd,install web server on windows 10,software requirements of php,enable php in apache,php 7.4 windows binary,apachelounge/download,how to start apache server in windows,how to install php,wamp,php download,mysql download,uninstall apache windows 10,apache lounge,install apache on windows 10,bitnami wamp stack,apachephp-mysql ubuntu,xampp,apache-php, mysql docker,lamp stack,xampp download,xampp mysql