How to install a web server on the Raspberry Pi (Apache + PHP + MySQL)
Updated the 29 August 2019 – 32 Comments – apache, Command line, Installation, Linux, MySQL, PHP, Raspbian, tutorials
ⓘ This article may have been partially or fully translated using automatic tools. We apologize for any errors this may cause.
Apache installation
Before installing the server, make sure we have an up-to-date machine. To do this we must have administrator rights, either because of the sudo command.
sudo apt update
sudo apt upgrade
sudo apt update
Once the Raspberry Pi is up to date, we will install the Apache server.
sudo apt install apache2
By the way, we’ll take advantage of it to give rights to the apache file that you can easily manage your sites. To do this, run the following commands:
sudo chown -R pi:www-data /var/www/html/
sudo chmod -R 770 /var/www/html/
Check if Apache is working
Once the installation completed, we can test that Apache is working properly by going to the Raspberry address.
To do this, it’s necessary to try to access to the Raspberry from port 80 (this port not being opened from the outside, it will have to do since the Raspberry itself). Do not worry, it’s very easy. Simply open the Raspberry web browser, and go to “http://127.0.0.1”. You should then get a page with a message like “It works! “And plenty of other text.
If you do not already have a GUI on your Raspbian, or you use SSH to connect to your Raspberry, you can use the following command:
wget -O check_apache.html http://127.0.0.1
This command will save the HTML code of the page in the file “check_apache.html” in the current directory.
So you only have to read the file with the command
cat ./check_apache.html
If you see marked at a location in the code “It works! ” is that Apache is working.
Apache uses the directory “/var/www/html” as the root for your site. This means that when you call your Raspberry on port 80 (http), Apache looks for the file in “/var/www/html”.
For example, if you call the address “http://127.0.0.1/example”, Apache will look for the “example” file in the “/var/www/html” directory.
To add new files, sites, etc., you will need to add them to this directory.
You can now use your Raspberry to make a site in HTML, CSS and JavaScript, internally.
However, you may want to quickly allow interactions between the site and the user. For example, to allow the user to register, etc. For this, you are going to need PHP.
PHP installation on your Raspberry Pi
How to install PHP
We will again use the administrator to install PHP with the command line.
sudo apt install php php-mbstring
Control if PHP is working
To know if PHP is working properly, it’s not very complicated, and the method is quite similar to the one used for Apache.
You will first delete the file “index.html” in the directory “/var/www/html”.
sudo rm /var/www/html/index.html
Then create an “index.php” file in this directory, with this command line
echo “<?php phpinfo ();?>” > /var/www/html/index.php
From there, the operation is the same as for the Apache check. You try to access your page, and you should have a result close to this image (if you do not have an interface, use the same method as before, and look for the words “PHP Version”).

Table generated by the phpinfo command on a Raspberry.
A MySQL database for your server
How to install MySQL
To do this, we will install mariadb-server and php-mysql (which will serve as a link between php and mysql)
sudo apt install mariadb-server php-mysql
Verify that MySQL is working correctly
To check the operation of MySQL, this time we will only use the command line. To do this, we will simply connect via the command:
sudo mysql –user=root
We will no delete the default mysql root user and create a new mysql root user, because the default one can only be used with Linux root account, and so not available for the webserver and php scripts.
To do so, once your connect to MySQL, simply run thoses commands (replace password with the password you want) :
DROP USER ‘root’@’localhost’;
CREATE USER ‘root’@’localhost’ IDENTIFIED BY ‘password’;
GRANT ALL PRIVILEGES ON *.* TO ‘root’@’localhost’ WITH GRANT OPTION;
So you now have a web server, connected to PHP and MySQL. That’s all it takes.
(On your nexts connections, you will be able to connect to mysql without using sudo, with the command mysql –user=root –password=yourmysqlpassword).
Add PHPMyAdmin
The installation of PHPMyAdmin is absolutly not necessary. In this installation, we will not take care about any special security settings !
The PHPMyAdmin installation is pretty quick and easy, we simply have to use the packet manager with this command :
sudo apt install phpmyadmin
PHPMyAdmin installation program will ask you few question. About the dbconfig-common part, choose to not use it (as we have already configure our database). About the server to configure PHPMyAdmin for, choose Apache. And the root password is the one you set for MySQL.
You should also enable mysqli extension using the above command :
sudo phpenmod mysqli
sudo /etc/init.d/apache2 restart
Check that PHPMyAdmin is working properly
To check that PHPMyAdmin works, you will simply try to access it, using the address of your Raspberry followed by /phpmyadmin. For example, locally it will be http://127.0.0.1/phpmyadmin
If you still get an error, it could be because PHPMyAdmin has moved to another directory. In this case, try the command
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
Now, we can access to PHPMyAdmin from Raspberry Pi’s browser, with the url : http://127.0.0.1/phpmyadmin