PHP Design Pattern
Focus on writing PHP Code
In fiddling around with PHP on a Raspberry Pi running on a Debian Linux OS more or less directly from a terminal mode, I realized that the focus (in my case) was getting the Linux commands right and very little with actually writing PHP programs. Most queries about getting the setup right involved Linux system administration and not PHP programming.
This short post is for Raspberry Pi users (and perhaps Linux users in general), and it focuses on setting up your Raspberry Pi so that you can use default Raspberry Pi editors (LeafPad) and the File Manager to work with PHP programs. Once set up, you will find the process of creating server-side programs in PHP much easier with no need to use the terminal editors after setting up your system.
Installing Apache and PHP
Because PHP is a server-side language, you will need both a server and PHP installed on your Raspberry Pi. Using the Root Terminal (Accessories → Root Terminal), enter the following line:
sudo apt-get install apache2 php5 libapache2-mod-php5
Press the Enter key and patiently wait until it’s completed the setup. Once done, you computer will have both a web server and PHP installed. To test whether installation was successful, open a browser from the main menu (Internet → NetSurf Web Browser) and enter the following:
http://localhost/
If everything works, you will see the message shown in Figure 1 on the right:
This location (http://localhost) is the root for your Web pages—PHP and any other Web page you decide to put on your Raspberry Pi Apache server. The name of the file is index.html. On your Raspberry Pi, in the Linux file system, the address is:
/var/www/
Open the File Manager (Accessories → File Manager) and in the window where you see /home/pi enter /var/www. You will now be able to see the icon for the html file that was automatically created when you installed the Apache server.
Saving PHP and Other Web Files
Before you can start creating and saving PHP programs, you have to make one more adjustment to your file system using the Root Terminal editor. (Accessories → Root Terminal). Enter the following and press the Enter key at the end of each line:
sudo chown www-data:www-data /var/www
sudo chmod 775 /var/www
sudo usermod -a -G www-data pi
sudo reboot
Now you can write (save) files to the www directory and its subdirectories (folders).
PHP Programming Workflow on Raspberry Pi
Now, we’re all set to look at a fairly simple workflow using Raspberry Pi tools. Begin by opening the File Manager from the Pi home:
Navigate to /var/www. You will see the index.html file. Select from the top menu, File → Create New… → Folder and name the new folder “php.” This will be the directory where you place your PHP files and PHP-related subdirectories and files. Figure 3 shows how your File Manager will now look with your new php directory/folder.
At this point, you’re all set to start working with server-side PHP. To get started, open the php folder by double-clicking it. Next open Leafpad and enter the following program:
Save it as billz.php in the /var/www/php/ directory. Next, enter and save the following class as Client.php using Leafpad:
Finally, place a graphic—png, gif or jpg—on a USB drive, and plug the drive into your Raspberry Pi USB port or hub. Using the File Manager, copy and paste the graphic into the php directory along with the billz.php and Client.php files. You’re all set to test it. Your file window should look like Figure 4:
As you can see, it looks pretty much like what you will see in a Windows or Macintosh directory system. Open your Raspberry Pi browser, and in the URL window enter:
http://localhost/php/billz.php
Press the Enter key, and you should see your page as shown in Figure 5:
If you have an internet connection, you can access your Raspberry Pi from another computer either on your LAN or over the internet. I just use my Local Area Network (LAN). Using the terminal command,
sudo ip addr show
find your IP address. For my LAN, it was 192.168.1.84. Using the URL:
http://192.168.1.84/php/billz.php
on one of my other computers on the LAN, I am able to use my Raspberry Pi as a Web server and a nice development platform for testing PHP files. (For an internet connection, I can highly recommend the Wi-PI wireless adapter. It is about the size of a USB drive and plugs into your Raspberry Pi.)
Big PHP on Little Systems
The origin of the Raspberry Pi is in the educational field with the goal of getting kids interested in computing. Learning Linux cli code and Python coding (Python comes installed) are two important features of that learning process. Now a server-side language, PHP, can also be part of that process of getting people involved in programming.
In fiddling around with PHP on a Raspberry Pi running on a Debian Linux OS more or less directly from a terminal mode, I realized that the focus (in my case) was getting the Linux commands right and very little with actually writing PHP programs. Most queries about getting the setup right involved Linux system administration and not PHP programming.
This short post is for Raspberry Pi users (and perhaps Linux users in general), and it focuses on setting up your Raspberry Pi so that you can use default Raspberry Pi editors (LeafPad) and the File Manager to work with PHP programs. Once set up, you will find the process of creating server-side programs in PHP much easier with no need to use the terminal editors after setting up your system.
Installing Apache and PHP
Because PHP is a server-side language, you will need both a server and PHP installed on your Raspberry Pi. Using the Root Terminal (Accessories → Root Terminal), enter the following line:
sudo apt-get install apache2 php5 libapache2-mod-php5
Press the Enter key and patiently wait until it’s completed the setup. Once done, you computer will have both a web server and PHP installed. To test whether installation was successful, open a browser from the main menu (Internet → NetSurf Web Browser) and enter the following:
http://localhost/
If everything works, you will see the message shown in Figure 1 on the right:
This location (http://localhost) is the root for your Web pages—PHP and any other Web page you decide to put on your Raspberry Pi Apache server. The name of the file is index.html. On your Raspberry Pi, in the Linux file system, the address is:
/var/www/
Open the File Manager (Accessories → File Manager) and in the window where you see /home/pi enter /var/www. You will now be able to see the icon for the html file that was automatically created when you installed the Apache server.
Saving PHP and Other Web Files
Before you can start creating and saving PHP programs, you have to make one more adjustment to your file system using the Root Terminal editor. (Accessories → Root Terminal). Enter the following and press the Enter key at the end of each line:
sudo chown www-data:www-data /var/www
sudo chmod 775 /var/www
sudo usermod -a -G www-data pi
sudo reboot
Now you can write (save) files to the www directory and its subdirectories (folders).
PHP Programming Workflow on Raspberry Pi
Now, we’re all set to look at a fairly simple workflow using Raspberry Pi tools. Begin by opening the File Manager from the Pi home:
Navigate to /var/www. You will see the index.html file. Select from the top menu, File → Create New… → Folder and name the new folder “php.” This will be the directory where you place your PHP files and PHP-related subdirectories and files. Figure 3 shows how your File Manager will now look with your new php directory/folder.
At this point, you’re all set to start working with server-side PHP. To get started, open the php folder by double-clicking it. Next open Leafpad and enter the following program:
<?php include_once('Client.php'); $worker = new Client(); ?> |
<?php class Client { function __construct() { echo "<html><body>"; echo '<h2>Welcome to OOP PHP,<br/>Raspberry Pi developers!</h2>'; echo "<img src= 'billGreg.png'>"; echo "</body></html>"; } } ?> |
As you can see, it looks pretty much like what you will see in a Windows or Macintosh directory system. Open your Raspberry Pi browser, and in the URL window enter:
http://localhost/php/billz.php
Press the Enter key, and you should see your page as shown in Figure 5:
If you have an internet connection, you can access your Raspberry Pi from another computer either on your LAN or over the internet. I just use my Local Area Network (LAN). Using the terminal command,
sudo ip addr show
find your IP address. For my LAN, it was 192.168.1.84. Using the URL:
http://192.168.1.84/php/billz.php
on one of my other computers on the LAN, I am able to use my Raspberry Pi as a Web server and a nice development platform for testing PHP files. (For an internet connection, I can highly recommend the Wi-PI wireless adapter. It is about the size of a USB drive and plugs into your Raspberry Pi.)
Big PHP on Little Systems
The origin of the Raspberry Pi is in the educational field with the goal of getting kids interested in computing. Learning Linux cli code and Python coding (Python comes installed) are two important features of that learning process. Now a server-side language, PHP, can also be part of that process of getting people involved in programming.
No comments:
Post a Comment