This blog includes a series of videos and references to help new users or enthusiasts better understand how to use open source and free technology tools. The quick links includes more information for accessing many of the tools covered along with other references to learn more for taking advantage of these tools.
This post goes over how to take the functions of the 'mysqli' function we covered in the previous PizzaOrder site, and create a more usable flow in a nicer format. The new site adds an order confirmation page and website management pages for internal users to add products and review new orders.
User login and control is coming in a separate video.
While this particular code is largely my own, I also wanted to link back again to the JoyofPHP, and also help signal out W3schools website which has a ton of info on managing and creating feature websites. Their site is linked on the right-hand side column in the blog as well, but it is a great source of know-how for getting started with website creation/optimization.
One other note to add is the W3schools' explanations of the enctype for uploading a file to PHP - Note https://www.w3schools.com/tags/att_form_enctype.asp
This tutorial covers the basics of using HTML and PHP to interact with a user, collect data into an HTML form, capture the data, then submit to an existing database.
Primarily we cover HTML <form></form>, PHP mysqli, PHP echo, and the basic formatting of an HTML table.
I designed this to be very simple on purpose to act as a base. In the next two or three videos I hope to explore more about formatting and creating a more useful flow of user data and a shopping cart to open orders. In many ways these fields are just building on what is already shown in this video, but help to demonstrate how powerful the mysqli integration is with HTML, CSS, and PHP for create dynamic websites.
Of course there are a multitude of other features and optimizations that can be done and included. I hope this can be a good foundation for new users or those interested to take the first steps - like I am - into web and software design.
I also wanted to reference The Joy of PHP by Alan Forbes again as his book taught me what little I know of PHP. The examples I'm showing are just pieces of code and know-how that his more in-depth book details, and I found it really helpful and easy to apply.
Finally, I can't emphasize enough, the current site is NOT secure. We built our LAMP stacks without an SSL (Secure Sockets Layer) certificate so that would need to be added to the Apache server/service on a server facing the internet. More information can be found on various sites, and currently one of the best open SSL implantation is Let's Encrypt. It does require some access to the Apache server to setup, but automates the SSL certificate re-registration/authentication so is quite nice for most sites. As an admin it will be up to your team to pick the best solution, but I wanted to ensure that while previous LAMP stacks are both viable test environments, a production environment (hosting a site on the Internet) shoudl have both SSL certicates and strong Firewall protection.
Generally it's easier to setup the LAMP stack or most stacks for development using Docker since pre-configured images can be pulled from Docker Hub and downloaded quickly without having to know all the packages, and then configuring the packages. That said, our Alpine VM did end up taking up less space, and roughly took the same amount of time to setup - partly my being too talkative, partly just download speed.
Overall, Docker is a great tool. Its ability to leverage and re-purpose existing images to create new containers, and relative easy install on Windows and MacOS, makes it a go to choice for developers. The mount-bind which we show in the video - ./LAMPcontained:/var/www/html - is amazing as a VM would need the user to upload files using FTP or create another SMB service to share files to in order to upload changes to the VM, whereas Docker can just allow changes from the host to appear in the container using that command. For these reasons, and the relative ease vs setting up a VM environment, Docker is a great choice for developers, and also highly robust in a production / server environment as well.
I also want to highlight that the original docker-compose.yml file is a modified version I found in the book The Joy of PHPby Alan Forbes which is a great set of example code to get started using PHP and HTML to interact with databases and create interactive websites. I used it to learn PHP and found it very helpful.
Dockerfile template <- note that the link displays Dockerfile.txt to better show the contents, but Dockerfile should not have a extension.
One other note with running LinuxMint I've had to manually add the Docker additional repository in the /etc/apt/sources.list.d/additional-repositories.list file, so please be mindful of that if you are a LinuxMint user. Poking around forums it seems this can be finicky so if you can't find the Docker packages after apt-add-repository and then apt update, check that file link to make sure it's there and correct to your build of the system. This could happen on any Linux distro it seems, but I ran into on LinuxMint, while I did not on Ubuntu.
Hope this was helpful, more to come. Thank you as always.
This video covers setting up a LAMP stack (Linux+Apache+MySQL(Maria)+PHP) in Alpine Linux. Alpine is a super lightweight distro popular with developers using Docker, but also a valid choice for VM work or standalone implementation.
This video walks through the process of setting up the various components of LAMP, though omits the Alpine install (it takes minutes if you know what you want and what your doing - select sys). Overall I'm quite impressed with Alpine, though there would certainly be conveniences just running Ubuntu Server, Alpine does the job with a fraction of the CPU/memory/capacity/cruft so if you can figure out how it can work for you, I think its worth a look.
Here are the steps shown in the video:
Alpine LAMP deployment
apk add apache2
apk add mariadb mariadb-client
rc-service start apache2
Check the homepage at the machine's IP, confirm you see 'It works'
Source files are located in /var/www/localhost/htdocs#
Ensure repositories are added - /etc/apk/repositories <- uncomment commmunity and edge
apk add php7 php7-mysqli phpmyadmin php7-apache2
service restart apache2
Create php test doc - nano phpinfo.php
service mariadb start
Setup Maria
Error message appears, need to first run the /etc/init.d/mariadb setup
Re-run service mariadb start
Then we change the password for mariadb root user (this gets created on the system by the mariadb-client).
Run mysql_secure_installation
Walk through the prompts to set a root user password
I do understand that this stack isn't necessarily the best in terms of performance or security, its simply the 'bare-minimum' to start developing. For optimization I'd suggest reading more on general Apache or NGINX settings, as well as looking to additional 3rd party sources about the various ways to integrate PHP with a web server.
This video is designed to run through the steps for setting up an SMB share in Ubuntu. All steps are shown as based on the Ubuntu tutorial blog, but I wanted to explain some of the steps - like adding a user who can act as the dedicated SMB login account. https://ubuntu.com/tutorials/install-and-configure-samba#1-overview
The process of using the command line to interact with a remote system is very important for more advanced Linux usage, and we spend a lot of time in the command line using SSH, and that can be setup when first installing Ubuntu Server. Of course with Linux everything can be done, undone, and redone using the command line, so important to get your feet wet if you really want to delve into Linux.
Previously, I ran through similar steps using FreeNAS which has a useful GUI for setting up the share. FreeNAS with its ZFS file system is definitely useful for robust data protection which may or may not apply depending on how the underlying Ubuntu install is set up. FreeNAS is also very useful for advanced shares like NFS, iSCSI, S3, AFP, and SMB. Essentially it saves users from having to install services and config files for each type of share.
Where this is useful is for creating file shares on systems not designed for FreeNAS. For example in a virtual machine, on smaller systems like the Raspberry Pi, or even an old laptop which only has 1 hard drive where FreeNAS typically will need at least 2~3 drives (one for boot and another 2 for data with mirroring).
Beyond setting an SMB share itself, the end of the video mentions additional steps to help protect data. Personally I have a simple SMB share on an old notebook sharing a folder/directory on the boot drive, then to protect that I backup the files to an external drive using over a USB cable. In Ubuntu, a USB drive gets mounted under the system folder /media and users can schedule a nightly backup using a cron job.
In the terminal, run crontab -e.
The file will open and scroll to the bottom. The first five spaces represent:
m - minute
h - hour
dom - day of month
mon - month
dow - day of the week (0-6, 1-7 - 0 & 7 both are Sunday)
The picture is an example that runs the copy command, cp -uR, at midnight and 12 noon everyday with the * symbol representing 'any'.
Note there are many other commands and ways to back up using cron jobs, just cp -uR is likely one of the easiest and is quite performant.
Use other distros?
I also recently in reading up on Docker, stumbled upon a very lightweight distro called AlpineLinux. It can be set to host an SMB share and a host of other things just like Ubuntu - steps are largely the same.
TIP: In AlpineLinux, ensure when running 'apk add samba' you also add the samba-common-tools, which are needed to run smbpasswd.
What Alpine is useful for is just being small. To compare, Ubuntu desktop .iso is around 2.1~2.5 GB in size now; Ubuntu server that doesn't have the desktop runtime and graphical elements - is around 800 MB; Alpine is only around 120 MB. Of course, Ubuntu has things like firewall ufw and other features baked in out-of-the-box to make it quick and easy to get up and running, but I like Alpine for portability and makes a good test environment for running in a VM, on a Raspberry Pi, or something else.
Finally, I wanted to say THANK YOU to the Ubuntu Podcast who were kind enough to mention this blog in a recent episode. I hope these tutorials are useful, and while I am having trouble keeping a regular cadence, I hope the blog itself helps to consolidate the material so its available anytime for anyone.
Thank you for reading this far. Hope to have some more on creating a web server and basic PHP tips soon.