From the course: Database Foundations: Application Development
Create a PHP web server
From the course: Database Foundations: Application Development
Create a PHP web server
- [Instructor] Let's get started building our first server cluster. All of the commands that you'll need to execute can be found inside of the chapter two folder and I've written them out in a file here called postgress_setup.text. Let me just open that up. And you can see all the steps that we're about to move through listed out here, as well as the specific docker commands that we're going to be executing on the command line. So let me close that down and we can get started. Now first, I want to make sure that docker is in fact running. So I'll look down here in my menu bar and I can see the docker icon right there. If you're on Mac OS or Linux you'll see the same icon up at the top of your menu bar when docker is running. Now we're going to execute commands on the command line. So I need to open up my PowerShell window here on Windows. Again, if you're on Mac OS or Linux you're going to use the terminal application for this. Let me open that up and we can get started. Now at first, it's important to know that docker containers are typically kept isolated from one another for security reasons. In order for our web server to be able to talk to the database server we need to create a virtual network for them. And we do that with the command docker network create. And then we named the network. I'm going to call mine PGDB_network. In order for all of these exercises to work, you need to make sure that you're using the exact same names that I'm using. So go ahead and press Enter to execute the command and create the docker network. Now we can create our two server containers. Let's first set up the database server. For that I'll use the command docker run dash dash name and I'll name it PG_DB_server. Next, we need to add our database server to the network that we just created. So I type in two dashes and network, followed by the name of the network we just created PGDB_network. Then I need dash dash publish. And this will set up our ports. So for this, I'm going to use port 5430 on my local computer, and I'm going to map that to 5432 inside of the docker container. Next I need a dash E followed by Postgress_password in all capital letters. This will set up the password for the Postgress user account. I'm going to use the password, Adam, capital A-D-A-M 123456. Again, typically you'll want to your own password but in order for this exercise to work correctly make sure you using the exact same password that I'm using here. After we type in the password I need dash D and we'll specify the name of the image that we're going to be using in order to create our container. The image that I want is the Postgress image. And I want to use a specific version. So I'll type in a colon and 13.2. I'll press the Enter button. So this tells me that it can't find the image locally. So it's going to download the Postgress image and create my local container for the Postgress database server. All right, that's all set up. And now we can set up our web server container. To do that we'll run the docker run command again, once again, we'll name the container. So dash dash name, I'll name this container PHP_web_server. Once again, we need to put it in the same network so dash dash network, followed by PGDB_network. Then we'll set our ports with dash dash publish. I want to map port 8080 on my local computer to port 80 inside of the container. So that's 8080 colon 80. Then we need to specify the image so dash D and for this image, we'll use the PHP image colon and a specific version of 8.0.2 dash Apache. I'll press Enter to create that container. Once again, it tells me that it can't find the image locally so it's going to download that from the internet and this will take just a moment to create my container. Okay. So I've got both of my containers set up now. And since both the web server and the database server are in the same network they're free to communicate with each other. And the ports that we've opened up for each server will allow us to connect to each resource from our own local computer. I'm going to run the CLS command just to clear the screen and get all that off of the screen. And we'll move on to the next step. Next, we need to modify the PHP web server a little bit. By default, the PHP docker container doesn't have what it needs in order to connect to a database server. So we need to add on a Postgress QL extension. To do this I need to log into the web server. We do that with docker EXEC dash IT then the name of the web server, so PHP underscore web server is what we named it. And I'll use the bash shell. I press Enter and that'll move me inside of the container and it puts me inside of the container there at this file path var www dash HTML. Now I need to install a little bit of software. To do this we use the command apt-get update and and apt-get install dash Y, lib PQ dash dev, two more ampersands, docker dash PHP dash EXT dash install. Then PGSQL, then PDO, and finally PDO underscore PGSQL. Now this line is a little bit complicated if you've never worked with Linux software installations and you don't really need to concern yourself with what every piece is actually doing. The end result is that we're adding driver support for making PostgreSQL connections with the PHP data object extension or the PDO. So I'm just going to make sure that everything is spelled correctly, and press the Enter key to run through that installation. Now, again, this is going to download some information from the internet. It'll take a few moments to install all of the software. After a few moments you should see this line that says, build complete and everything has been installed and configured on the server. Now, all we need to do is run the command exit to get out of the web server container, that will put us back onto the command line at our main PC. And then I can run docker, restart PHP web server. That'll shut down and restart the web server so that the Postgress extension can be enabled. Then I can type in the CLS command again to clear the screen and that'll return me back up here to the top. Finally, we can now copy all of our files from the exercise files chapter two folder up into the web server. To do that, we use the docker CP command, and then I need the file path to the exercise files. So I'm going to minimize this window here and I'm going to find the chapter two folder, and I'm just going to drag in the PHP web server folder I drag that whole thing over here and drop it onto the PowerShell Window. And that'll put it into the file path to that folder. Now, after that, let's go ahead and make sure this is maximized again. Now after the file path, I'm going to type in a period. It's all going to come back here right before the closing quotation mark and I'll type in a another backslash and a period. If you're on Mac OS or Linux, you're going to use a forward slash and a period here after the PHP web server folder name. This will copy all of the containing files and folders that are within this root folder there. So we're going to copy all the files from that folder into our PHP web server container. So I'll come here after the quotation mark, type in a space, the name of the container, PHP underscore web server, then a colon and then we want it to go to a specific file path. So I'll type in a colon slash VAR slash www slash HTML and one more forward slash. Press the Enter button that'll upload all those files from the exercise files and the PHP web server folder up into the PHP web server container. Just to verify that all of those files were copied correctly. We can log back into the web server with docker EXCC dash IT, the name of the PHP web server again, PHP underscore web underscore server. And once again, we'll use the bash shell. That'll put us into that HTML folder and I can just type in LS to list out the contents and we should see the index PHP file, the scripts folder the server test folder, style sheet.CSS and the two trees folder. So everything was copied up correctly. You can type in exit. That will put me back onto my local computer and we're done with our terminal or PowerShell window. Now, if everything worked correctly we should be able to open up our web browser and go to local host, colon 8080. That'll contact the PHP web server that we just set up and you should see this success message. This gives us a little bit of information about the server. It gives me the current date and time in UTC so don't be surprised if this date looks a little bit funny from where you are right now. So this is an UTC time. I have a link here to take a look at some PHP server information. That'll tell me the version of the server that we're working with. And if I scroll down here on this page about halfway down, we should find a section that talks about the PostgreSQL connection. And here we are, we've got the PDO section and it tells us we have PDO driver support for PostgreSQL. And we also have the PDO PGSQL driver listed out right here and we can see that it's enabled. So that is good. Click and close out this tab. And I can also click on the link here to view PostgreSQL information. This will send a simple query to the page and actually this web browser it looks like, it thinks that this is written in Danish which does not make any sense let's go ahead and close that. So it gives me some information about the PostgreSQL server, tells me I'm running PostgreSQL 13.2 running on Debian Linux. I got the name of the default instance there as well as the host name. This is the name of the docker container that we set up as well as the port that we set up. So it looks like everything is working. And with that, our little server cluster is up and running. And we have successfully communicated between the PHP web server and the PostgreSQL database server.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
What is PHP and Apache?2m 55s
-
Create a PHP web server10m 58s
-
(Locked)
Connect to the PHP server3m 37s
-
PostgreSQL functions4m 55s
-
(Locked)
Use PHP variables3m 4s
-
(Locked)
Use if/else to handle errors4m 53s
-
(Locked)
Add data to a database4m 7s
-
(Locked)
Format SELECT query results6m 24s
-
(Locked)
Filter results with form controls5m 40s
-
(Locked)
Add data to a table with form controls5m 29s
-
(Locked)
-
-