Post by Marti-Pair Furxheir S.H. on Aug 10, 2013 5:53:35 GMT -6
When doing Web Development, we usually build websites for the Internet or an Intranet. But how do our visitors manage to view those files? Putting them in a folder on our home computer doesn't make the site magically appear on the Internet, for that, we need a server.
So, let's shed a light on web servers and the most popular type of server: a LAMP server.
LAMP is an acronym for Linux, Apache, MySQL and PHP, which are respectively the Operating System, Web Server software, Database system and programming language.
Let's go in order. What is an operating system?
Software runs on a computer. Computer vary from each other. You might have 1 monitor at home and 2 monitors at the office, while I have 9 monitors on my home office computer. We have different CPUs, we have different hard disks, keyboards and mouse.
In order for a software to run, it need to be able to make an abstraction of these differences in order to be able to run on all compatible computers. For a software like Sublime Text 2, the keyboard is just an abstract device which sends keystrokes.
That abstraction is the responsibility of the operating system which offers software (and users) a platform with multiple tools to make it easier to use any computers with the same Operating system. It's an overly simplistic explanation, but bear with me please.
Your computer is most likely a Windows or a Mac machine, but most webservers use Linux, in part because its security model is more appropriate to a server thanks to its multiple user architecture, in part because it exists in streamlined versions specialized for web hosting, but also in part because it is open and free. In this context, free both means that you are free to do whatever you want with it and that it is free of charge.
Back in the nineties, when the first web servers were being deployed for the Internet, Universities were the first major publishers and Windows 3.1 was not even an operating system: it was just a skin on top of ms-dos.
Mac OS pre Os X was not powerful enough either. Linux was the only appropriate choice and almost 2 decades of popular usage as a web server has helped it turn it into a lean mean web machine…
What was needed however, was a web server that is easy to setup to serve web pages and that's where Apache came from. It has been consistently the most popular web server software since April 1996.
Apache's role on a server is to serve web pages. When you visit the page www.example.com/directory/file.html it is Apache's role to fetch the file and serve it to your browser with the proper headers.
In this case, the file might be in /home/user/public_html/directory/file.html or /etc/local/htdocs/directory/file.html. It doesn't matter to the visitor of the site: Apache knows where to take the file and send it.
When you visit the SSL protected version ( using https:// ) it is Apache's role to encrypt the page.
When a site uses a .htaccess and a .htpasswd file to require a password to visit a site, Apache handles it. When you request a .php file to be compiled, executed and output by PHP, it is Apache which coordinates the call.
Apache can now run on Linux, Mac OS and even on Windows server, even if Microsoft made its own web server software (IIS, or Internet Information Service).
It is widely believed that the name Apache comes from the fact that it is "a patchy software", even if officially, it is out of respect for the Native American Apache tribe. The reality is that the creator claims he didn't realize it was a play on words.
Apache is open source, meaning that users have access to the source code and can legally make changes to the program and even redistribute those changes, as long as they don't sell copies of the software.
But if Apache only served static .html files like in 1994-1995, the web wouldn't be very interesting.
As such, Apache config files allows to specify certain file extensions or MIME types to be handled by an external program, the output of which will be sent to the client.
What are MIME types? Mime is an acronym for Multi-Purpose Internet Mail Extension, and in short, it is a way to specify in which format the next content is.
For example, the Mime type: text/plain specifies that the following content is in plain text. The mime type: text/html specifies HTML content.
So, we can configure Apache to call the PHP compiler when encounters a web application/x-httpd-php5, and then to configure the .php file extension to have the MIME type application/x-httpd-php5
When you setup a brand new Apache System, you need to manually add those configuration options for PHP to work, but when you install a LAMP server, these options are generally pre-configured.
Speaking of PHP, let's skip over MySQL for now, and talk about the P in LAMP.
The P, By The Way, is a little controversial at this moment, as the 3 top server-side scripting languages all start with a P: PHP, Perl and Python. So you might see LAMP as Linux, Apache, Mysql, PHP/Pyhton/Perl. In this certificate, we will only focus on PHP however.
PHP is the most popular server-side scripting language, though its almost monopoly on server-side languages is slowly fading as a multitude of new better designed languages are taking their spot.
PHP began in 1994 when Rasmus Lerdorf built a series of Perl scripts to help manage his Personal Home Page (that's where the PHP name comes from). It evolved and became its own system.
PHP became popular, replacing Perl as the primary Server Side language in part because of its C-like syntax (which is also used by C++, C# and Java) which was easier to learn compared to Perl, in part because of the simplicity of deployment (you can simply upload a .php and run it in place) and in part because of the simplicity to include PHP code in an HTML document (as long as that document is parsed by PHP).
For example, if you want to display the current time on your site, you can simply create the file time.php and upload the following code:
The majority of the page is in pure HTML and returned as is, but everything between <? And ?> is compiled by PHP and executed. In this case, we echo (print) the return of the function date which formats a timestamp (or the current time, in this case) according to parameters.
Unlike client side scripts however, the time will be compiled and will not change in the output until the page is refreshed. Also, the page is compiled for every visitor, so that if you and I visit the page with a 2 second delay, we will get 2 different time stamps.
In Lecture 5 of this course, we will talk into a little more details about PHP and MySQL.
Well, we did skip over earlier MySQL, so let's talk about it.
I have mentioned in the second lecture that MySQL is a database system and that:
Let's say that we are building an elevator management system which allows to call elevators from your cell phone, and once you are inside, to press on the floor where you are going.
The system would need to store a few information: which floors have their buttons pressed, where each elevator is and in which direction it is moving (or if it is stopped), which buttons are pressed inside each elevator.
In such a system, we might have 2 tables: one for the floors, and one for the elevators.
Floors would have 3 basic columns (fields): Floor Number, If the Up Bottom is pressed, if the Down Bottom if pressed. We might also decide to store, for each, which elevator is handling the call, to prevent 2 elevators from trying to fulfill the request.
We would have 1 row for each of the floors.
As for the elevator table, we would have for the columns (fields) the following: the ID of the elevator, the direction it is moving( probably UP, DOWN, STOPPPED, DOORS OPENED, OFF, FIRE SERVICE MODE PHASE 1, FIRE SERVICE MODE PHASE 2), the current floor, and the various buttons currently pressed. In a simulation, we would also store the last time the floor changed if we want the elevator to move in real time. Finally, we would have a flag to indicate if the elevator is the fire elevator which behaves differently during a fire.
We would have 1 row for each of the elevators.
The Elevator example is not a coincidence: I have taught development using the elevator example several times in the past because it is a simple looking system almost everyone understands, it is easy to scale and the rules of operations are more complex then they sound.
We will use this example further in future lectures and courses, as well as other examples.
With MySQL, we are example to perform queries, such as SELECT which allows to retrieve information from the database. For example, if elevator 2 is on Floor 3, moving up, we could perform the query "SELECT * FROM Floors WHERE UpButtonPressed AND FloorNumber > 3 AND UpAssignedElevator IS NULL " to know which floors are located above floor #3, with the Up Button Pressed which have not been assigned an elevator yet.
If we decide to service floor #5 following this Query, we might update the Floor as follow: "UPDATE Floors SET UpAssignedElevator=2 WHERE FloorNumber=5", which would set the field UpAssignedElevator to 2 for Floor #5.
In addition to Tables, Columns and Rows, a database also has indexes, which are way to more rapidly access data.
A few weeks ago, a client of mine complained one of his queries was too slow: it took over 35 seconds to retrieve the data he wanted. I discovered that a few of the fields he was searching on were not indexed, so I simply create a custom index of all of the fields his query was using and the time to query was reduced to 0.2 seconds.
MySQL was one of the first web optimized, almost full featured open source database systems and quickly became the number one choice.
Over time, other alternatives became available, like PgSql, SQLLite, Berkley DB, MariaDB, NoSQL , but none have so far really replaced MySQL even if several are actually better.
Saldly, that's it for this week, but not for this lecture. I have simply decided to split the lecture in 2 parts, with part 2 next week.
This also means: no assignment this week, since the lecture is not completed.
So, let's shed a light on web servers and the most popular type of server: a LAMP server.
LAMP is an acronym for Linux, Apache, MySQL and PHP, which are respectively the Operating System, Web Server software, Database system and programming language.
Let's go in order. What is an operating system?
Software runs on a computer. Computer vary from each other. You might have 1 monitor at home and 2 monitors at the office, while I have 9 monitors on my home office computer. We have different CPUs, we have different hard disks, keyboards and mouse.
In order for a software to run, it need to be able to make an abstraction of these differences in order to be able to run on all compatible computers. For a software like Sublime Text 2, the keyboard is just an abstract device which sends keystrokes.
That abstraction is the responsibility of the operating system which offers software (and users) a platform with multiple tools to make it easier to use any computers with the same Operating system. It's an overly simplistic explanation, but bear with me please.
Your computer is most likely a Windows or a Mac machine, but most webservers use Linux, in part because its security model is more appropriate to a server thanks to its multiple user architecture, in part because it exists in streamlined versions specialized for web hosting, but also in part because it is open and free. In this context, free both means that you are free to do whatever you want with it and that it is free of charge.
Back in the nineties, when the first web servers were being deployed for the Internet, Universities were the first major publishers and Windows 3.1 was not even an operating system: it was just a skin on top of ms-dos.
Mac OS pre Os X was not powerful enough either. Linux was the only appropriate choice and almost 2 decades of popular usage as a web server has helped it turn it into a lean mean web machine…
What was needed however, was a web server that is easy to setup to serve web pages and that's where Apache came from. It has been consistently the most popular web server software since April 1996.
Apache's role on a server is to serve web pages. When you visit the page www.example.com/directory/file.html it is Apache's role to fetch the file and serve it to your browser with the proper headers.
In this case, the file might be in /home/user/public_html/directory/file.html or /etc/local/htdocs/directory/file.html. It doesn't matter to the visitor of the site: Apache knows where to take the file and send it.
When you visit the SSL protected version ( using https:// ) it is Apache's role to encrypt the page.
When a site uses a .htaccess and a .htpasswd file to require a password to visit a site, Apache handles it. When you request a .php file to be compiled, executed and output by PHP, it is Apache which coordinates the call.
Apache can now run on Linux, Mac OS and even on Windows server, even if Microsoft made its own web server software (IIS, or Internet Information Service).
It is widely believed that the name Apache comes from the fact that it is "a patchy software", even if officially, it is out of respect for the Native American Apache tribe. The reality is that the creator claims he didn't realize it was a play on words.
Apache is open source, meaning that users have access to the source code and can legally make changes to the program and even redistribute those changes, as long as they don't sell copies of the software.
But if Apache only served static .html files like in 1994-1995, the web wouldn't be very interesting.
As such, Apache config files allows to specify certain file extensions or MIME types to be handled by an external program, the output of which will be sent to the client.
What are MIME types? Mime is an acronym for Multi-Purpose Internet Mail Extension, and in short, it is a way to specify in which format the next content is.
For example, the Mime type: text/plain specifies that the following content is in plain text. The mime type: text/html specifies HTML content.
So, we can configure Apache to call the PHP compiler when encounters a web application/x-httpd-php5, and then to configure the .php file extension to have the MIME type application/x-httpd-php5
When you setup a brand new Apache System, you need to manually add those configuration options for PHP to work, but when you install a LAMP server, these options are generally pre-configured.
Speaking of PHP, let's skip over MySQL for now, and talk about the P in LAMP.
The P, By The Way, is a little controversial at this moment, as the 3 top server-side scripting languages all start with a P: PHP, Perl and Python. So you might see LAMP as Linux, Apache, Mysql, PHP/Pyhton/Perl. In this certificate, we will only focus on PHP however.
PHP is the most popular server-side scripting language, though its almost monopoly on server-side languages is slowly fading as a multitude of new better designed languages are taking their spot.
PHP began in 1994 when Rasmus Lerdorf built a series of Perl scripts to help manage his Personal Home Page (that's where the PHP name comes from). It evolved and became its own system.
PHP became popular, replacing Perl as the primary Server Side language in part because of its C-like syntax (which is also used by C++, C# and Java) which was easier to learn compared to Perl, in part because of the simplicity of deployment (you can simply upload a .php and run it in place) and in part because of the simplicity to include PHP code in an HTML document (as long as that document is parsed by PHP).
For example, if you want to display the current time on your site, you can simply create the file time.php and upload the following code:
<html>
<body>
The current time is: <?php echo date('H:i:s'); ?>
</body>
</html>
The majority of the page is in pure HTML and returned as is, but everything between <? And ?> is compiled by PHP and executed. In this case, we echo (print) the return of the function date which formats a timestamp (or the current time, in this case) according to parameters.
Unlike client side scripts however, the time will be compiled and will not change in the output until the page is refreshed. Also, the page is compiled for every visitor, so that if you and I visit the page with a 2 second delay, we will get 2 different time stamps.
In Lecture 5 of this course, we will talk into a little more details about PHP and MySQL.
Well, we did skip over earlier MySQL, so let's talk about it.
I have mentioned in the second lecture that MySQL is a database system and that:
a database is a structured repository to store data in an efficient matter. Data is stored in Tables, which consist of columns (or fields) and rows. Columns are properties, like the Name of the person, while rows are elements stored in the table, like individual clients.
Let's say that we are building an elevator management system which allows to call elevators from your cell phone, and once you are inside, to press on the floor where you are going.
The system would need to store a few information: which floors have their buttons pressed, where each elevator is and in which direction it is moving (or if it is stopped), which buttons are pressed inside each elevator.
In such a system, we might have 2 tables: one for the floors, and one for the elevators.
Floors would have 3 basic columns (fields): Floor Number, If the Up Bottom is pressed, if the Down Bottom if pressed. We might also decide to store, for each, which elevator is handling the call, to prevent 2 elevators from trying to fulfill the request.
We would have 1 row for each of the floors.
As for the elevator table, we would have for the columns (fields) the following: the ID of the elevator, the direction it is moving( probably UP, DOWN, STOPPPED, DOORS OPENED, OFF, FIRE SERVICE MODE PHASE 1, FIRE SERVICE MODE PHASE 2), the current floor, and the various buttons currently pressed. In a simulation, we would also store the last time the floor changed if we want the elevator to move in real time. Finally, we would have a flag to indicate if the elevator is the fire elevator which behaves differently during a fire.
We would have 1 row for each of the elevators.
The Elevator example is not a coincidence: I have taught development using the elevator example several times in the past because it is a simple looking system almost everyone understands, it is easy to scale and the rules of operations are more complex then they sound.
We will use this example further in future lectures and courses, as well as other examples.
With MySQL, we are example to perform queries, such as SELECT which allows to retrieve information from the database. For example, if elevator 2 is on Floor 3, moving up, we could perform the query "SELECT * FROM Floors WHERE UpButtonPressed AND FloorNumber > 3 AND UpAssignedElevator IS NULL " to know which floors are located above floor #3, with the Up Button Pressed which have not been assigned an elevator yet.
If we decide to service floor #5 following this Query, we might update the Floor as follow: "UPDATE Floors SET UpAssignedElevator=2 WHERE FloorNumber=5", which would set the field UpAssignedElevator to 2 for Floor #5.
In addition to Tables, Columns and Rows, a database also has indexes, which are way to more rapidly access data.
A few weeks ago, a client of mine complained one of his queries was too slow: it took over 35 seconds to retrieve the data he wanted. I discovered that a few of the fields he was searching on were not indexed, so I simply create a custom index of all of the fields his query was using and the time to query was reduced to 0.2 seconds.
MySQL was one of the first web optimized, almost full featured open source database systems and quickly became the number one choice.
Over time, other alternatives became available, like PgSql, SQLLite, Berkley DB, MariaDB, NoSQL , but none have so far really replaced MySQL even if several are actually better.
Saldly, that's it for this week, but not for this lecture. I have simply decided to split the lecture in 2 parts, with part 2 next week.
This also means: no assignment this week, since the lecture is not completed.