ICT
 
 
home search Contact
Lmarsden is a non-profit information site

Setting up a LAMP

The LAMP stack is the foundation for Linux hosted websites is the Linux, Apache, MySQL and PHP (LAMP) software stack.

The Four Layers of a LAMP Stack

Linux based web servers consist of four software components. These components, arranged in layers supporting one another, make up the software stack. Websites and Web Applications run on top of this underlying stack. The common software components that make up a traditional LAMP stack are:

  • Linux: The operating system (OS) makes up our first layer. Linux sets the foundation for the stack model. All other layers run on top of this layer.
  • Apache: The second layer consists of web server software, typically Apache Web Server. This layer resides on top of the Linux layer. Web servers are responsible for translating from web browsers to their correct website.
  • MySQL: Our third layer is where databases live. MySQL stores details that can be queried by scripting to construct a website. MySQL usually sits on top of the Linux layer alongside Apache/layer 2. In high end configurations, MySQL can be off loaded to a separate host server.
  • PHP: Sitting on top of them all is our fourth and final layer. The scripting layer consists of PHP and/or other similar web programming languages. Websites and Web Applications run within this layer.

  • We can visualize the LAMP stack like so:




  • Install LAMP Server Packages - Note if you just updated in the previous steps you can skip this.

    Before we install the LAMP packages, we need to update the internet certificates and Raspbian:

     

     

  • Open up Terminal on the raspberry pi (RPi)



  • Install MFAC SSL:

    • /usr/local/share/ca-certificates
    • sudo update-ca-certificate




    Update the Raspberry Pi operating system
    • sudo apt-get update
    • sudo apt-get upgrade -y

 

 

Install Apache2

Once Raspbian has finished installing updates, we can now install the LAMP packages, we start with Apache2 and we also want to enable mod_rewrite:

1. sudo apt-get install apache2 -y

2. sudo a2enmod rewrite


We’re using the -y flag which will automatically install required and suggested Apache2 packages. We need to change one setting on Apache2 to allow .htaccess overrides in the /var/www directory, so open:

sudo nano /etc/apache2/apache2.conf 

 

Move down towards the bottom of the document using the cursor keys and change the AllowOverride None to AllowOverride All as shown below:

1. <Directory /var/www/>

2. Options Indexes FollowSymLinks

3. AllowOverride All Require all granted

4. </Directory>

 

Now on your keyboard, hold Ctrl + O to save, press enter to write to the file and then Ctrl + X to exit nano.

 

…then restart Apache2:

sudo service apache2 restart

Now we need to find the IP address of your Pi, so type the following command:

ifconfig eth0

Look for inet addr: 192.xxx.x.xxx which will show your Pi’s IP address (it could also start with 10.x.x.x or 172.x.x.x). Go to any desktop computer that’s connected to your local network and enter the IP address into your browsers address bar ie. http://172.18.0.xxx and hit Enter. You should be presented with the following Apache2 Debian Default Page:










  • Install PHP

    Now let’s install PHP by entering the following command:

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

    When finished installing PHP, we need to test it’s working, so let’s create a new PHP file:

    cd /var/www/html

    sudo nano index.php

    …then enter the following code into your new PHP file:

    <?php echo "www.lmarsden.com"; ?>

    Now on your keyboard, hold Ctrl + O to save, then Ctrl + X to exit nano. Now we need to delete the default Apache2 Debian page and restart Apache:

    sudo rm index.html

    sudo service apache2 restart

    Go to your web browser again and enter the IP address of your Pi just like we did before, or if you’re still on that page, hit Ctrl + F5 to refresh. You should be greeted with a blank webpage that just says “www.lmarsden.com“.




    Install MySQL

    Now we install the MySQL database engine by typing the following command:

    sudo apt-get install mysql-server php-mysql mysql-client vsftpd -y

    sudo service apache2 restart




    Install phpMyAdmin

    In order to manage our MySQL databases, it’s far easier to use phpMyAdmin so let’s go ahead and install it by entering the following command, but note*you’ll be asked the following questions during the installation process:

    • Automatic Configuration? – Choose [*] apache2 with your Spacebar, hit Tab then Enter

    • Configure database for phpmyadmin with dbconfig-common? – Choose Yes

    • phpMyAdmin application password – [enter new password] [confirm new password] this is for logging into the phpMyAdmin web interface - use root as the password - all lower case.

    Once you are ready, type the following command:

    sudo apt-get install phpmyadmin -y

    Now let’s check if phpMyAdmin is working by entering the IP address of your Pi into your web browsers address bar appended with /phpmyadmin/, like this:

    http://172.18.0.xxx/phpmyadmin/ 

    hit Enter and you should be presented with the standard phpMyAdmin login page as shown below:

    …now login with username: phpmyadmin and password: root that you entered during phpMyAdmin setup.

    Select Databases:

    If you see No Privileges then you will need to create a new Admin account.



Create an Admin account

  • From the terminal, log into mysql with the following command line

    sudo mysql -u root

     

 

You will see the following command line change from the RPi operating system to the MariaDB mysql server database.





To create new users we will need to create the user and then give them privilegers

  • CREATE USER 'admin'@'localhost' IDENTIFIED BY 'root';

    Username = admin

  • password = root

  • GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost';

  • GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost';

    FLUSH PRIVILEGES; when we grant some privileges for a user, running the command flush privileges will reloads the grant tables in the mysql database enabling the changes to take effect without reloading or restarting mysql service.

FLUSH PRIVILEGES;



Show all users on the sql server

select user, host from mysql.user;



Show grants to what databases

SHOW GRANTS FOR 'user_name'@'localhost';



Delete a user

DROP USER 'user_name'@'localhost';

DROP USER 'user_name'@'%';

DROP USER 'user_name'@'192.168.0.1;




  • loggin as phpmyadmin:

    sudo mysql -p -u phpmyadmin

    enter the password: root

 

 

 

 

Focus Point

 

 

Information