How To Install PHP & What Are Web Servers - PHP 8 Tutorial

How To Install PHP & What Are Web Servers – PHP 8 Tutorial

How To Install PHP & What Are Web Servers

before we can write any php we need to have the local development environment set up for it let’s do that in this video and also talk a little bit about web servers because php mostly runs on the web server and it’s a good idea to know what a web server is and how it works when talking about a web server you could either refer to a hardware or software or both hardware and software together a web server on the hardware is just a computer that stores the web server software source code and some other stuff a web server can process incoming requests using different protocols typically it’s the http protocol which is the protocol used by the browser to view the web pages a web server can host either a single or multiple websites on the same server using the same resource this is done by something called virtual hosts where the single web server is able to allocate and share resources across multiple websites here is a simple diagram of how the communication between the client and the server happens when you visit the website the browser will request for the file from the server the web server will process this request and respond accordingly if it cannot locate the file then it will respond with the 404 status code we can actually see this in action by visiting any website and opening developer tools to inspect the requests you can either open devtools by either right clicking on your mouse and then clicking on the inspect element or by hitting f12 on the keyboard let’s refresh the page and see what happens as you can see the first request is what gets sent to the server and then it responds back with the html that then gets rendered on the screen don’t worry about the other requests for now also don’t worry if this is new to you or if it doesn’t really make sense you don’t really need to worry about this too much at this stage of your learning i just wanted to explain how it works and what web servers are so things make more sense when you’re actually installing the necessary software to run the php the two most common web servers are apache and nginx they both have their pros and cons i’m not going to get into much detail on which one is better or which one to choose because i don’t want to overwhelm you with too much information we’ll be using the apache for now because it comes with the software bundled in that we’re going to be installing in this video there are many different ways you could have php installed in your local environment you might even have it installed on your os if you have php manually installed you will need a local server either apache or nginx you would need to install that manually yourself later if you decided that you also needed a database you would need to install that as well this does not sound beginner friendly you probably just want to get it installed as simply and as fast as possible this is why all-in-one solutions like xampp mamp and wamp exist they bundle in the web server php database and some other useful tools and handle the configuration for you which makes it very easy for beginners to get started with php some other better alternatives to xampp to get a php environment set up are virtual machines or containers i personally don’t use xampp i use docker and i will have a separate video about getting php environment setup using docker but don’t worry about it right now we are not going to be installing docker right now we are going to be installing xampp this simplicity though comes with the trade-off and that is flexibility one of those trade-offs is you cannot have multiple php versions installed at the same time out of the box there are ways you could achieve this with xampp but it’s not simple and requires some work you may also need a different version of the database per project which goes back to the point one it also becomes harder to manage and maintain multiple projects especially when you’re trying to upgrade your current php version it is not suited for production due to its security so i would not be advising using x-amp for production and because you would not use x-amp in production you would always have some sort of disconnect differences between your local setup and your production these differences could be minor if you know what you’re doing or you could be major like a difference in php versions or the packages it’s installed for example which could yield to issues like it works on my local machine but it does not work in production there is absolutely nothing wrong with using xampp for hobby projects or just playing around with php and getting it running as fast as possible it is entirely up to you how you want to get your local environment set up or what tools you want to use if you are a beginner then i would suggest to stick with xampp for now and then look for alternatives once you’re comfortable so just go to the xampp website download it and follow the instructions to install on one of the steps it will ask you what services you want to include in the bundle you could unselect the ones that you do not care or you don’t want to use for this course though we’re only going to be using database and php so we don’t need all other services so we could disable them for now let’s start the control panel and briefly review it right away as you can see i’m getting an error and this is actually a pretty common error for cases where you may already be running some other service on the same port for example in my case i have a database running in a docker container on the same port 3306 and because this port is already taken xampp can start the database service this can easily be fixed by turning off the service that’s currently running on port 3306 or we could use a different port to run it same thing can happen for apache for example so if you see such errors just check what services are using that port and either disable or change them we won’t be needing the database right now so we can ignore this error for now and fix it once we actually start using the database you could also change the xampp’s configuration you could change the default text editor or you could enable some of the services to start automatically whenever the xm starts you could also stop and start the necessary services from the control panel view and edit configuration files and check logs don’t worry about the config files for now we’ll touch on that topic in a separate video let’s head over to the browser and type http localhost and hit enter it opens the welcome dashboard page for xampp which means that the web server is working as expected you might be wondering where you would put your source code and that depends mainly on how the web server is configured so let’s open xampp control panel and click on the explorer button here this opens the directory where xampp is installed and here we see that that’s where apache phpmysql and other services are installed but you don’t have to worry about these directories for now the only thing that you need to know is that htdocs right here this is your document root which means that this is where your source code and project files will go to if we open that we see that this is the source code for this dashboard page that we see on the local host so we could go ahead and delete this because we’re going to be adding our own files now what happens when you go to localhost is that it will try to locate index.php within your document root if it finds the index.php then it will serve the website using that index.php if it cannot find the index.php then it will just list the files and directories within the document root so for example right now we don’t have any content if i were to create just some file here and go to localhost we see that it just lists the document tree so what we can do is we can create directories here so we can call this program with geo and here we could create an index dot php and we could just say hello world for now now a quick note here we’re not writing a php code yet this is just a simple uh text so if we go to the local host now we see the program with geo directory if we click that we see hello world as you noticed we have not written a php code yet we’ll do that on the next video where we’ll go over the basic syntax before we can do that though we need a code editor because even though you could write php in notepad it’s not so pleasant to work with there are a lot of options when it comes to code editors try some of them and pick the one that you like and feel more comfortable with sublime text adam vs code and phpstorm are the ones that i recommend though it’s up to you what you use this is it for this video now you know how to get up and running with php on your local environment using xampp but in addition to that you also know what web server is and how it works thank you for watching hope you liked this video please hit like and subscribe and i will see you on the next one where we’ll go over the syntax and write our first php code

Watch How to Install On Youtube

About the author

Leave a Reply

Your email address will not be published.