How to install PHP 7, Apache and MySQL on Raspberry Pi

Let’s learn how to install Apache, PHP7 and MySQL on a Raspberry Pi 3. First thing that you’ll have to do is to be logged in on your Rpi directly in the terminal or by using SSH. If you want to access your Rpi 3 from other devices you have to active SSH from raspi-config by typing this command in the terminal:

Now go to Interfacing Options -> SSH -> Yes and you will be able to access your raspi using the IP address of you Rpi and port 22 in you SSH client (I am using Putty).

How to install Apache 2 on Raspberry Pi

First you’ll have to update the packages:

Then install apache2:

If everything went well now you should be able to access http://your-ip-address (for example http://192.168.1.200) and see the default Apache page.

The default page that you see is saved in /var/www/html/index.html and is owned by root user so we have to change the owner of the www folder to user pi:

Now you can add you own files in the html folder and see the changes.

How to install php 7 on Raspberry Pi 3

At the time this articles was written the stable and official version of PHP for ARM processors like the one Raspberry Pi 3 uses is PHP 7.0. The command to install php is:

After all these packages are installed you might be asked to activate proxy_fcgi module and to use php7.0-fpm:

Now reload Apache:

How to install MySQL on Raspberry Pi

We will install mysql-server and php-mysql:

We will also have to perform the commands in order to access and use the MySql databases. First let’s connect to MySQL with this command:

Now perform these actions to change the Linux root user with a MySql root user (use your own password on line 2 instead of “your password”:

If everything went ok now you should be able to access MySQL using this command:

Now you can create databases, tables or whatever you want on your Raspberry Pi.

How to use Apache VirtualHost

By default the /var/www/html/ folder is where you store your website files, but you might want to host multiple websites on your raspberry pi and for that you need to create different folders for each of your website.

In our example we have the website larabits.com and therefore we create the folder larabits inside /var/www/html/. Now we use this command to create a virtualhost config file for our website:

Now let’s edit this conf file:

And add these lines inside the file (replace larabits.com with the name of your domain):

Great, now let’s enable our new website, disable the default one and restart apache:

Ok, this is all, I hope everything was ok and now you can enjoy your website.

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.