Web Server for Linux/Debian/Raspberry PI
Installing Apache2 and PHP
Basically, we only need to install 2-4 packages to run basic PHP websites. First, let's install the web server:
sudo apt install apache2
This is followed by PHP. The Mbstring extension is not mandatory, but is required for various applications such as WordPress. PHP-MySQL just like that, this extension allows you to connect to MySQL databases.
sudo apt install php php-mysql php-mbstringÂ
Meanwhile, the MySQL extension is loaded automatically. If this does not work for you, restart the Apache web server once:
sudo systemctl restart apache2Â
Set permissions and create a test page
By default, Apache serves all files in the /var/www/html folder. However, this belongs to root, i.e. we can only create or edit files there if we use e.g. sudo - this is (unnecessarily) bad practice and should be changed. Our user with whom we want to work on the site (here pi) receives full rights. The group www-data running Apache2 is allowed to read + execute. Everyone else doesn't get any permissions - which makes sense if we're only working with one user.
sudo chown -R pi:www-data -R /var/www/html
chmod 750 -R /var/www/htmlÂ
Now we can easily create and edit files with our pi user. As a first test, we create a PHP file called i.php (freely selectable) and display the PHP info. This is a PHP-generated page that displays all relevant information about the PHP configuration - including the version or the installed extensions. So on a fresh install it's a good first test to make sure everything is working as intended:
echo '<?php phpinfo();' > /var/www/html/i.phpÂ
Now we can access this page by IP address or hostname followed by /i.php, for example http://testpi/i.php
With that, our Apache installation with PHP is ready. All files in /var/www/html are displayed in the root directory or, in the case of PHP, executed. i.php is freely chosen, you can also name the file something else.
Install MySQL/MariaDB
MySQL is also an older, but still widely used and useful SQL database. When this was bought by Oracle, MariaDB evolved. The fork should ensure that the database remains open source and transparent. Their goal is to be compatible with MySQL. It has established itself as a resource-saving alternative, making it ideal for the Raspberry Pi. You don't necessarily need a database for PHP, but in many cases this is at least useful. At this point it should also be mentioned that MariaDB and MySQL are not the only ones, although they are among the most common.
MariaDB can be installed via the standard package sources:
sudo apt install mariadb-serverÂ
By default, the local user root comes locally to the MariaDB server without a password when connecting as root (e.g. with sudo). On the console you can then run SQL queries with full rights - for example the status page to show us the runtime of the server as a test:
$ sudo mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 50
Server version: 10.3.29-MariaDB-0+deb10u1 Raspbian 10
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SHOW GLOBAL STATUS LIKE 'Uptime';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime    | 54  |
+---------------+-------+
1 row in set (0.003 sec)Â