Installing LAMP (Linux, Apache, MySQL, PHP) on a Raspberry Pi

by GaryLittlemore in Circuits > Computers

25261 Views, 12 Favorites, 0 Comments

Installing LAMP (Linux, Apache, MySQL, PHP) on a Raspberry Pi

install_lamp_on_amazon_linux_os.jpg

Set up a LAMP (Linux Rasbian Stretch Lite, Apache2, MySQL (MariaDB-10), PHP7) stack with PHPMyAdmin and FTP access on your Raspberry Pi and configure it to work as a web server.

You will need a Raspberry Pi computer connected to the internet with a 8GB (or greater) MicroSD card. At first you'll need to be able to configure the Raspi-config with a keyboard to change the default Raspbian password and to enable SSH. Once the Raspi-config has been completed you'll only need to connect via a SSH connection.

You should already be able to configure the Rasbian image to the MicroSD card, you'll need knowledge of being able to SSH to the Raspberry Pi and be able to configure your SQL database using PHPMyAdmin.

Where you see $, this is the command you need to paste/use to install and configure the software.

## Raspbian Image ##

Create a Raspbian Stretch Lite image to a MicroSD card (>8GB recommended)

Once the RasPi has booted log in with Username: pi Password: raspberry

then

$ hostname -I

(Note down the IP Address, you will need this to SSH to the RasPi. e.g. 192.168.0.100)

$ sudo raspi-config

Option 1 - 'Change User Password' > Ok > Enter new UNIX password > Retype new Password > Ok

Option 5 - 'Interfacing Options' > 'P2 SSH' > Yes Tab to 'Finish' > Enter

$ sudo reboot

## Update Raspbian ##

Now using Putty connect to the RPi using the IP address from earlier.
log in with Username: pi & your password

$ sudo apt update && sudo apt upgrade -y
$ sudo reboot

## Install Apache2 ##

Again using Putty connect to the RPi using the IP address from earlier.

log in with Username: pi & your password

$ sudo apt install apache2 -y

Once finished installing Apache2 open a web browser on the same Wi-Fi network and enter the IP address of the RasPi. If Apache2 installed correctly you will see a page that says 'Apache2 Debian Default Page' and 'It works!'.

$ sudo a2enmod rewrite
$ sudo systemctl restart apache2
$ sudo chown -R pi:www-data /var/www/html/
$ sudo chmod -R 770 /var/www/html/
$ sudo nano /etc/apache2/apache2.conf

Find: (You can use Ctrl & W to find)

Directory /var/www/

Options Indexes FollowSymLinks

AllowOverride None

Require all granted

/Directory

Change to:

Directory /var/www/
Options Indexes FollowSymLinks

AllowOverride All

Require all granted

/Directory

Ctrl & O > Enter > Ctrl & X

$ sudo service apache2 restart

## Install PHP7 ##

$ sudo apt install php libapache2-mod-php -y

Testing PHP

You will first need to 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

Refresh the web broswer on the same Wi-Fi network, you should now see the PHP info page.

## Install MySQL ##

$ sudo apt install mysql-server php-mysql -y
$ sudo service apache2 restart
$ sudo mysql_secure_installation

You will be asked enter current password for root (default is blank): press Enter.

Set root password, type Y and press Enter.

Type in a new password and press Enter. Important: remember this root password.

Re-enter the new password and press Enter.

Type Y and press Enter to Remove anonymous users.

Type Y and press Enter to Disallow root login remotely.

Type Y and press Enter to Remove test database and access to it.

Type Y and press Enter to Reload privilege tables now.

When complete, you will see the message All done! and Thanks for using MariaDB!.

$ sudo mysql -uroot -p

Enter the root password.

$ create database YOURDATABASENAME;
$ GRANT ALL PRIVILEGES ON YOURDATABASENAME.* TO 'root'@'localhost' IDENTIFIED BY 'YOURROOTPASSWORD';
$ FLUSH PRIVILEGES;

Ctrl & D

## Install PHPMyAdmin ##

$ sudo apt install phpmyadmin -y

Select Apache2 with the cursor keys and press the spacebar to highlight Apache2 > Tab > Enter.

Configure database for phpmyadmin with dbconfig-common? Select 'No' > Enter, we have already setup a database above with the MySQL installation.

To access phpmyadmin use the IP address of the RasPi e.g. 192.168.0.100/phpmyadmin/ Username: root and YOURROOTPASSWORD

## Setup an FTP ##

$ sudo apt install vsftpd -y
$ sudo nano /etc/vsftpd.conf

Find: (You can use Ctrl & W to find)

local_enable=YES
ssl_enable=NO

Change to:

#local_enable=YES
#ssl_enable=NO

Add to the bottom of the file:

# CUSTOM
ssl_enable=YES local_enable=YES chroot_local_user=YES local_root=/var/www user_sub_token=pi write_enable=YES local_umask=002 allow_writeable_chroot=YES ftpd_banner=Welcome to my Raspberry Pi FTP service.

Ctrl & O > Enter > Ctrl & X

$ sudo usermod -a -G www-data pi
$ sudo usermod -m -d /var/www pi
$ sudo chown -R www-data:www-data /var/www
$ sudo chmod -R 775 /var/www
$ sudo reboot

The process is now complete.