Installing Apache, PHP and MySQL on a Raspberry PI

Why Apache?

I like to go into projects with my eye’s open.  There are choices about which server software to use.  On the linux platform there are two web servers.  Apache, and nginx.  Looking at the January 2016 Web Server Survey apache is the number one used web server on the internet.  It’s running the websites of about 35.59% of the internet.  nginx is running about 17.43%.  It’s up and coming, so does mean that it’s worth looking at.  However, for my needs, I’ll be going with the no.1 software at this time.  Also I’ve used Apache for other projects and have some experience with it.   Not only that, Apache has been the number one web server for the internet for nearly all of the last 20 years.  They’ve got to be doing something right for have a statistic like that.

 

Why PHP?

PHP is a scripting language. It’s the language that is used for WordPress, and other projects.  Again, I have some experience with it, it’s also quite similar to C# which is my first language, so I’m comfortable switching between the two.

 

Why MySQL?

There are choices here, MySQL, Postgress, MariaDB for starters.  Again, this is mainly a personal choice.  Pogress would be my second choice.  MariaDB is based on MySQL from what I know about this MariaDB is shadowing MySQL and is trying to improve on what MySQL has already implemented.  From the sounds of things, I get the feeling that this is some political thing. MySQL is alot easier to install than MariaDB, so I’ll stick with MySQL.

 

I’m not going to start completely from scratch.  My starting point is this…

  1. hostname changes to LampPi
  2. Filesystem expanded to 8GB (Size of the card)
  3. GPU set to 16 Mb (Minium value)
  4. Locale information set correctly.
  5. PI Boots to console. (No GUI)
  6. Pi Not overclocked.  I’m doing this on a PI 512MB, so that means 700Mhz processor.  My PI3 is 1.2 Ghz, so in production there will be free performance boost.

 

So, let’s get this show on the road.

 

apt-get update

apt-get -y upgrade

 

Installing everything in one go. (It’s easier to run the installs, then we’ll configure it all later)

First apache

apt-get -y install apache2 apache2-doc apache2-utils

 

next php

apt-get -y install libapache2-mod-php5 php5 php-pear php5-xcache


now a library to let php access mysql

apt-get -y install php5-mysql and finally mysql

apt-get -y install mysql-server mysql-client You will be asked to provide an admin password. For my development PI, I’m going to choose “raspberry” make a note of the password that you choose as you will need it later. At this point we have all the basics installed. We still need to be able to use the web server, so for this I’m going to setup ftp and samba, so that you can either use FTP to upload files, or you could use samba to access the files using a windows share. We also need a way to administer the MySQL databases, so we will install phpMyAdmin to do this. Also for extra credit, we’ll install a way for the web server to be able to send e-mails using postfix

PHP MyAdmin

We'll start with the phpMyAdmin.  I would try not to put this onto a production system, as doing so will lower the security of that system.  For a development platform it will help to get things up and running.  You could also install it to help get things running, then disable it to keep the security aspect.

apt-get -y install phpmyadmin

During this part of the install, will be asked if you want to use Apache2, or lighttpd.  Pick Apache2.

After a short while, you'll be prompted to configure the database with dbcomfig-common.  choose Yes.

You'll be asked for an admin password  this is the root password that you chose when installing MySql, I'm entering raspberry here.
Next you will be asked to provide a MySQL application password.  As I don't really care about security for this install, I'm entering raspberry again, in a production system, none of my passwords are raspberry, and the would all be different too, just to make it harder for anyone that manages to hack into my network.


We need to modify Apache so that we can access phpMyAdmin

nano /etc/apache2/apache2.conf

Add this to the bottom of the file.

Include /etc/phpmyadmin/apache.conf

save that file then execute this command

systemctl restart apache2

now, from a webbrowser on the same network as your PI navigate to.

http://lamppi/phpmyadmin/

where lamppi is the hostname of your raspberry pi.

The username is root, and in my case the password is raspberry.
At this point, we can confirm that MySQL is working, as is PHP and Apache. This is all needed to be able to get the page to show.

FTP Access

chown -R pi /var/www

apt-get -y install vsftpd

nano /etc/vsftpd.conf

Search through the file and change the following lines:
anonymous_enable=YES Change To anonymous_enable=NO
#local_enable=YES Change To local_enable=YES
#write_enable=YES Change To write_enable=YES

Also, add a line to the bottom of the file:
force_dot_files=YES

save the file then restart the ftp service

systemctl restart vsftpd

You should now have a functional ftp server, so you can now start working on the website. You’ll find the root of the website at /var/www/html

 

Samba

Samba is the name of the service in linux that allows you to make windows shares.   I want to have this on my Lamp box, so that I can work on a website directly using my main windows installation rather than having to upload the files to ftp every time.

 

apt-get -y install samba samba-common-bin


nano /etc/samba/smb.conf

Read through the file and make sure you have the following parameters set:

workgroup = WORKGROUP
wins support = yes

You can use anything as your workgroup name as long as it is alphanumerical and matches the workgroup you would like to join. The default workgroup in Windows 7 is WORKGROUP.

 

Scroll to the bottom of the file and add

 

[html]
comment=html website root
path=/var/www/html
browseable=Yes
writeable=Yes
only guest=no
create mask=0777
directory mask=0777
public=no

 

Safe the file then execute

 

smbpasswd -a pi

Enter the password for the pi user, this is NOT the same as the pi used used to log into the Raspberry PI, but it is a good idea to make it the same password (raspberry) so that you don't need to remember more than one password for the same account name.


At this point you should be able to log into the html share on the PI.

The username will be lamppi\pi
and the password is raspberry.

The username is a combination of the hostname of the PI - lamppi in my case.  and the smb use that you added - pi


You should now have a fully functional LAMP installation with a windows share thrown in as well.

One Response to Installing Apache, PHP and MySQL on a Raspberry PI

  1. Talentelgia says:

    Thanks for the information you shared regarding PHP. I have been looking for it since last few days.

Leave a Reply

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