Lab 5 mnjk: Difference between revisions

From ITCwiki
Jump to navigation Jump to search
 
(71 intermediate revisions by 3 users not shown)
Line 2: Line 2:
In this lab you will perform the following tasks:
In this lab you will perform the following tasks:
*Learn about static network configuration of Debian Linux systems
*Learn about static network configuration of Debian Linux systems
*Install PHP and MySQL on your server
*Install '''[https://www.php.net PHP]'''
*Install '''[https://www.php.net MariaDB]''' on your server, this is an open-source MySQL alternative
*Experiment with websites and databases
*Experiment with websites and databases
*Explore the standard log files on your system.
*Explore the standard log files on your system.


You will use the following commands:
You will be introduced to the following commands:


*'''[https://linux.die.net/man/8/ip ip address show]'''
*'''[https://linux.die.net/man/8/sudo sudo]'''
*'''[https://linux.die.net/man/8/ifup ifup]'''
*'''[https://linux.die.net/man/8/ifup ifup]'''
*'''[https://linux.die.net/man/8/ifdown ifdown]'''
*'''[https://linux.die.net/man/8/ifdown ifdown]'''
*'''[https://linux.die.net/man/8/ping ping]'''
*'''[https://linux.die.net/man/8/ping ping]'''
*'''[https://linux.die.net/man/8/shutdown shutdown]'''
*'''[https://linux.die.net/man/8/shutdown shutdown]'''
*'''[https://linux.die.net/man/8/apt apt]'''
*'''[https://www.commandlinux.com/man-page/man8/a2dissite.8.html a2ensite]'''
*'''[https://www.commandlinux.com/man-page/man8/a2dissite.8.html a2dissite]'''
*'''[https://linux.die.net/man/1/wget wget]'''
*'''[https://linux.die.net/man/1/wget wget]'''
*'''[https://linux.die.net/man/1/tar tar]'''
*'''[https://linux.die.net/man/1/zcat zcat]'''
*'''[https://linux.die.net/man/1/zcat zcat]'''
*'''[https://linux.die.net/man/8/service service]'''
*'''[https://linux.die.net/man/8/service service]'''
*'''[https://linux.die.net/man/1/tail tail]'''
*'''[https://linux.die.net/man/1/tail tail]'''


:You will also use the following MySQL (MariaDB) Commands
You will also use the following MySQL (MariaDB) commands:


*'''[https://mariadb.com/kb/en/use USE]'''
*'''[https://mariadb.com/kb/en/use USE]'''
Line 92: Line 91:


==Set a static IP==
==Set a static IP==
'''''[https://www.youtube.com/watch?v=VcPA6gJ0Ohw&feature=youtu.be Video tutorial - Setting a Static IP Address]'''''
'''''[https://www.youtube.com/watch?v=VcPA6gJ0Ohw&feature=youtu.be Video Tutorial - Setting a Static IP Address]'''''


: An important first step is to learn how to diferenciate between network interfaces. Take a look at [https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/ this site] and [https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-net_id.c#L20 this site] to understand how network interface names in Debian 9 and later are identified.
: An important first step is to learn how to diferenciate between network interfaces. Take a look at [https://www.freedesktop.org/wiki/Software/systemd/PredictableNetworkInterfaceNames/ this site] and [https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-net_id.c#L20 this site] to understand how network interface names in Debian 9 and later are identified.
Line 112: Line 111:
<li>Apply your static IP address</li>
<li>Apply your static IP address</li>
* Now we are going to apply the static IP change. Try using '''ip address show''' to view your active configuration now and you should see that your old address is still active.
* Now we are going to apply the static IP change. Try using '''ip address show''' to view your active configuration now and you should see that your old address is still active.
'''NOTE: You should only make network configuration changes when you have physical access to a machine. This way, if you mess up your configuration you will be able to fix it from a local console.''' If you lose working SSH access to your system after making these changes you'll need to connect in through the VMware console interface (which is equivalent to physical access) and find and correct your configuration issues.
:''NOTE: You should only make network configuration changes when you have physical access to a machine. This way, if you mess up your configuration you will be able to fix it from a local console.''' If you lose working SSH access to your system after making these changes you'll need to connect in through the NetLab console interface (which is equivalent to physical access) and find and correct your configuration issues.
* Using the ifdown and ifup command, we are going to restart the network interface, this step is required to apply the change.
* Using the '''ifdown''' and '''ifup''' command, we are going to restart the network interface, this step is required to apply the change.
'' Note: A good method to watch this change is to have to ping windows open on your local machine ping both your old IP address and your new IP address with a '''ping 172.17.50.xx -t this will allow you to see how quickly the change will happen.
:'' Note: A good method to watch this change is to have to ping windows open on your local machine ping both your old IP address and your new IP address with a '''ping 172.17.50.xx -t''' this will allow you to see how quickly the change will happen. The first image below is with the DHCP address, the second is after the static address is applied.
:[[File:Lab5_ping_ifup_ifdown.png|link=https://wiki.ihitc.net/mediawiki/images/2/2f/Lab5_ping_ifup_ifdown.png|500px]]
:[[Media:Lab5_ping_ifup_ifdown.png|Click here for a larger image]]
* In a SSH terminal, run '''sudo ifdown ens192 && sudo ifup ens192'''. Notice the two ''&&'' symbols. This tells the Linux shell that it should run the second command right after the first. If we do not define this, then we would be left with a machine that has its networking turned off.
* In a SSH terminal, run '''sudo ifdown ens192 && sudo ifup ens192'''. Notice the two ''&&'' symbols. This tells the Linux shell that it should run the second command right after the first. If we do not define this, then we would be left with a machine that has its networking turned off.
<code>sudo ifdown ens192 && sudo ifup ens192</code>
: <code>sudo ifdown ens192 && sudo ifup ens192</code>
'' Note: you will lose your SSH connection because the IP your SSH session is connected to is no longer in use by your VM.
'' Note: you will lose your SSH connection because the IP your SSH session is connected to is no longer in use by your VM.
* At this point, your machine should now be using a static address. You may receive an error message indicating that an address cannot be assigned which is related to your old DHCP address still being on the interface as well. Use '''ip address show''' to verify the new IP address is assigned to the interface.
* At this point, your machine should now be using a static address. You may receive an error message indicating that an address cannot be assigned which is related to your old DHCP address still being on the interface as well. Use '''ip address show''' to verify the new IP address is assigned to the interface.
Line 127: Line 128:
==Install PHP & MySQL/MariaDB==
==Install PHP & MySQL/MariaDB==


'''''[https://www.youtube.com/watch?v=Az0qtg4LSjA&feature=youtu.be Video tutorial -  Install PHP and MySQL MariaDB v3]'''''
'''''[https://www.youtube.com/watch?v=Az0qtg4LSjA&feature=youtu.be Video Tutorial -  Install PHP and MySQL MariaDB]'''''
<ol>
<ol>
<li>Using '''apt''', install the ''php'' and ''mariadb-server'' packages as well as the ''php-mysql'' package which provides the link between php and mysql and the ''libapache2-mod-php'' package which provides a copy of PHP which allows the Apache webserver to run website PHP scripts.</li>
<li>Install packages</li>
* Note: Remember to do an '''apt update''' before installing packages to make sure you get the latest versions
:* Using '''apt''', install the ''php'' and ''mariadb-server'' packages as well as the ''php-mysql'' package which provides the link between php and mysql and the ''libapache2-mod-php'' package which provides a copy of PHP which allows the Apache webserver to run website PHP scripts.
* Note: Maria-DB is a fully compatible replacement for MySQL that isn't controlled by Oracle.
'' Note: Remember to do an '''apt update''' before installing packages to make sure you get the latest versions
<li> After the dependencies are found, go ahead and accept them to continue the install.</li>
'' Note: Maria-DB is a fully compatible replacement for MySQL that isn't controlled by Oracle.
* After the dependencies are found, go ahead and accept them to continue the install.
<li> After the install is complete, '''cd''' to ''/var/www/html'' which is the root directory of your webserver and so where all website files are served from.</li>
<li> After the install is complete, '''cd''' to ''/var/www/html'' which is the root directory of your webserver and so where all website files are served from.</li>
<li> In order to fully enable the PHP-MySQL module you should restart your Apache webserver. You don't need to restart your entire system to do this. Do some research on the "apache2ctl" or the "service apache2"  commands which can be used to do this.
<li> In order to fully enable the PHP-MySQL module you should restart your Apache webserver. You don't need to restart your entire system to do this. Do some research on the "apache2ctl" or the "service apache2"  commands which can be used to do this.
<li> At this point we will test to verify that php is working properly with Apache. Create a new file named ''phptest.php'', and then open it in a text editor.</li>
<li> At this point we will test to verify that php is working properly with Apache. Create a new file named ''phptest.php'', and then open it in a text editor.</li>
: '' Note: you may have to use sudo to create and edit files in this directory.  as your standard user account may not have privilidges to create and edit files in this directory.
:<code>sudo touch phptest.php</code>
:<code>sudo nano phptest.php</code>
<li>Enter the following into the file:
<li>Enter the following into the file:
<pre><?php
<pre><?php
phpinfo();
phpinfo();
?></pre></li>
?></pre></li>
<li>Now save the file, and on your local machine go to xxx.xxx.xxx.xxx/phptest.php in a web browser where xxx.xxx.xxx.xxx is your VM's static IP. You should now be on a page that shows your PHP Version, and system information. Use the "View Source" option in your browser to see what the HTML source code is for the page you're viewing is. Is it the same or different than the .php file you created? Why? How is this different than a standard .html file?</li>
<li>Now save the file, and on your local machine go to xxx.xxx.xxx.xxx/phptest.php in a web browser where xxx.xxx.xxx.xxx is your VM's static IP. You should now be on a page that shows your PHP Version, and system information.</li>
:[[File:Lab5_php_info.png|500px]]
:[[Media:Lab5_php_info.png|Click here for a larger image]]
<li>Use the "View Source" option in your browser to see what the HTML source code is for the page you're viewing is. Is it the same or different than the .php file you created? Why? How is this different than a standard .html file?</li>
:[[File:Lab5_php_info_devtab.png|500px]]
:[[Media:Lab5_php_info_devtab.png|Click here for a larger image]]
</ol>
</ol>


==Experiment with Website PHP==
==Experiment with Website PHP==


'''''[https://www.youtube.com/watch?v=m_sUrt_quX4&feature=youtu.be Video tutorial - Experiment with Website PHP v3]'''''
'''''[https://www.youtube.com/watch?v=m_sUrt_quX4&feature=youtu.be Video Tutorial - Experiment with Website PHP]'''''
# In a previous lab we learned about editing the ''/var/www/index.html'' file to change the default web page displayed by your server. In a future lab you will install some PHP/MySQL based software which powers many Internet sites with forums, blogs, etc. Before we can do that we need to learn a little more about PHP and about databases. Like most software on Linux the Apache webserver configuration files are stored in the ''/etc'' directory. Specifically, you can find several of them which work together and are called from each other in ''/etc/apache2/'' take a look in the ''/etc/apache2/sites-enabled/'' directory. See how symlinks are used to point to configuration files which actually reside in the ''/etc/apache2/sites-available/'' directory? This allows us to turn off and on various sites by creating or removing a symlink rather than by deleting the actual configuration file, a handy thing if we just want to temporarily disable a site. In fact Apache provides a utility to automatically add and remove these links called '''a2ensite''' and '''a2dissite'''. Check the '''man''' pages for these commands to see how they work.
<ol>
# As you might have guessed the default site for your system is configured by the file linked to at ''/etc/apache2/sites-enabled/000-default'' If you open this file in your favorite text editor you will see a series of what Apache calls "directives" which explain how the webserver should function, what port it should listen on, where the website files will reside (''/var/www/html''), etc. Going into all of the different Apache directives is outside of the scope of this course but you will find a lot of documentation about them on the Internet.
<li> Explore Apache symlinks</li>
# In addition to configuring Apache directly through it's configuration files you can also configure it through an interface like Webmin which we installed in a previous lab. Bring up the Webmin configuration page for Apache and take a look at the settings for the default site. Note how the things you saw directly in the configuration file match up with what you see in Webmin. One of the things you may be interested in seeing is how the webserver knows to display the index.html file from a directory if it exists and no specific file is requested in the URL. Take a look on the "Directory Indexing" page for the default virtualhost and see if you can find this information. You'll notice that there is a list of files, not just index.html which the server will display.
:In a previous lab we learned about editing the ''/var/www/index.html'' file to change the default web page displayed by your server. In a future lab you will install some PHP/MySQL based software which powers many Internet sites with forums, blogs, etc. Before we can do that we need to learn a little more about PHP and about databases. Like most software on Linux the Apache webserver configuration files are stored in the ''/etc'' directory. Specifically, you can find several of them which work together and are called from each other in ''/etc/apache2/'' take a look in the ''/etc/apache2/sites-enabled/'' directory. See how symlinks are used to point to configuration files which actually reside in the ''/etc/apache2/sites-available/'' directory? This allows us to turn off and on various sites by creating or removing a symlink rather than by deleting the actual configuration file, a handy thing if we just want to temporarily disable a site. In fact Apache provides a utility to automatically add and remove these links called '''a2ensite''' and '''a2dissite'''. Check the '''man''' pages for these commands to see how they work.
#* Note: You may need to refresh your Webmin modules or "scan for new modules" if you haven't done that since installing Apache and Webmin. Webmin scans your system for compatible server software packages (like Apache) when it is installed but needs to re-scan it's modules after you install new server software before you can configure it through Webmin.
<li>Explore '''/etc/apche2/sites-enabled'''</li>
# In another section of this lab you created a basic ''phptest.php'' file and saw how the server executed the PHP code and turned it into an HTML page your browser could display. Because PHP offers an easy way to write web applications it powers much of the Internet. To prepare us for setting up some PHP applications in a future lab it will be helpful to know a little about how PHP scripts work. Read through the [http://www.w3schools.com/php/default.asp W3 Schools PHP Basics Tutorial] (PHP Intro through PHP Superglobals) and try creating some PHP scripts on your own Linux server like the examples given in the tutorial pages to see if you can get them to run, try modifying them a little bit and see what the results are. There are many great Internet resources devoted to understanding how to do things with PHP so take some time to see how this language can be easily integrated in websites. You should definitely be familiar with editing PHP files to change variables, echo statements, etc. work which are all skills you'll need when installing PHP based website software.
:As you might have guessed the default site for your system is configured by the file linked to at ''/etc/apache2/sites-enabled/000-default'' If you open this file in your favorite text editor you will see a series of what Apache calls "directives" which explain how the webserver should function, what port it should listen on, where the website files will reside (''/var/www/html''), etc.  
# Try writing some simple PHP scripts on your own using variables and echo statements and make them available through your webserver. Edit your ''index.html'' file (or create a new ''index.php'' file which will be loaded instead if it exists) so there is a list and links to the various scripts you have created as part of your PHP experimentation.
:'' NOTE: Going into all of the different Apache directives is outside of the scope of this course but you will find a lot of documentation about them on the Internet.
#* Note: You will need to be familiar with the very basics of editing HTML files in order to create links, etc. If you haven't done this before there are many introductory HTML tutorials available online.
<li>Explore the Apache configuration files in Webmin</li>
:In addition to configuring Apache directly through it's configuration files you can also configure it through an interface like Webmin which we installed in a previous lab. Bring up the Webmin configuration page for Apache and take a look at the settings for the default site. Note how the things you saw directly in the configuration file match up with what you see in Webmin. One of the things you may be interested in seeing is how the webserver knows to display the index.html file from a directory if it exists and no specific file is requested in the URL. Take a look on the "Directory Indexing" page for the default virtualhost and see if you can find this information. You'll notice that there is a list of files, not just index.html which the server will display.
:'' NOTE: You may need to refresh your Webmin modules or "scan for new modules" if you haven't done that since installing Apache and Webmin. Webmin scans your system for compatible server software packages (like Apache) when it is installed but needs to re-scan it's modules after you install new server software before you can configure it through Webmin.
<li>Expiriment with and learn PHP basics</li>
:In another section of this lab you created a basic ''phptest.php'' file and saw how the server executed the PHP code and turned it into an HTML page your browser could display. Because PHP offers an easy way to write web applications it powers much of the Internet. To prepare us for setting up some PHP applications in a future lab it will be helpful to know a little about how PHP scripts work. Read through the [http://www.w3schools.com/php/default.asp W3 Schools PHP Basics Tutorial] (PHP Intro through PHP Superglobals) and try creating some PHP scripts on your own Linux server like the examples given in the tutorial pages to see if you can get them to run, try modifying them a little bit and see what the results are. There are many great Internet resources devoted to understanding how to do things with PHP so take some time to see how this language can be easily integrated in websites. You should definitely be familiar with editing PHP files to change variables, echo statements, etc. work which are all skills you'll need when installing PHP based website software.
<li>Create your own PHP files</li>
:*Try writing some simple PHP scripts on your own using variables and echo statements and make them available through your webserver.
:*A good place to create these would be in your home directory, using your favorite text editor.
<li>Create your own "home" page by editing your ''index.html'' file located in the '''/var/www/html''' directory.</li>
:''NOTE: You will need to be familiar with the very basics of editing HTML files in order to create links, etc. If you haven't done this before there are many introductory HTML tutorials available online.  A good starting place is [https://www.w3schools.com/html/html_basic.asp W3 Schools HTML Basics Examples]
:'' NOTE:It is importatnt to know that an '''html''' file must include the <!DOCTYPE HTML> tag.  Additionally, you will notice that all tags have an opening tag and a closing tag.  The information between the tags is what will have the features of the tag
:*Backup your ''index.html'' file by copying to a new file name.
:<code>sudo cp /var/www/html/index.html /var/www/html/index.html.bak</code>
:*Edit your ''index.html'' file (or create a new ''index.php'' file which will be loaded instead if it exists) so there is a list and links to the various scripts you have created as part of your PHP experimentation.
:* Open your index.html file in your favorite text editor,
:<code>sudo nano /var/www/html/index.html</code>
:*Since you have backed up the original index.html file I would edit this file to only include the basics.
<pre>
<!DOCTYPE HTML>
<html>
<body>
<h1>My ITC-2480 Server</h1>
<p>This is a server on Pod-R in NetLab at 172.17.50.XX</p>
<br>
<p>Here are some of the Projects I am working on:</p>
<a href="http://172.17.50.XX/phptest.php">PHP Version Information</a>
</body>
</html>
</pre>
:'' NOTE: Make sure to replace the XX in the IP with the IP address of your server.
:
:[[File:Lab5_basic_html.png|link=https://wiki.ihitc.net/mediawiki/images/2/29/Lab5_basic_html.png|500px]]
:[[Media:Lab5_basic_html.png|Click here for a larger image]]
:A first look for a basic page
:[[File:Lab5_initial_home.png|link=https://wiki.ihitc.net/mediawiki/images/9/92/Lab5_initial_home.png|500px]]
:[[Media:Lab5_initial_home.png|Click for a larger image]]
<li>Try to add additional features to your "home" page.</li>
:''NOTE: You may get some ideas of features you might like to add by looking at the original ''index.html'' file that we backed up in step 6 of this lab.
:*As you complete your labs consider what you can add to this page.
:*Add links to the PHP scripts you created
:*A few good chalanges would be to add a title, change font size, style or color.
 
</ol>


==Experiment with Databases==
==Experiment with Databases==


'''''[https://www.youtube.com/watch?v=1zYXiWLoGdM&feature=youtu.be Video tutorial - Experiment with Databases v3]'''''
'''''[https://www.youtube.com/watch?v=1zYXiWLoGdM&feature=youtu.be Video Tutorial - Experiment with Databases]'''''
# In order to do really powerful things with a website we need a database where we can store dynamic content which can be queried to automatically build site pages from a template. One of the most common databases is an SQL database, and MySQL has long been the most prevalent Linux SQL software. These days MariaDB, which we installed, has been taking some MySQL marketshare because it is a community developed program instead of MySQL which is now owned by Oracle. The MariaDB software and MySQL software are essentially equivalent, especially for small sites/databases, and the same commands are used interchangably including using the '''mysql''' command to access them. SQL itself is a standard language for interacting with a specific type of database called a relational database, we'll see why it's called a relational database in a minute. Each database is a collection of data stored in tables. You can think of a table as something like a spreadsheet with rows and columns, except we call the columns fields and the rows records (sometimes we actually call them rows too). The different tables are often related to one another, of course they are usually being used by the same web application, but usually the relationship goes deeper than that as we'll see in a little bit.
<ol>
# To jumpstart our exploration of databases we'll download a pre-built sample database with a few tables and many records already in it and import it into our MySQL database server. Download the employees database file [https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2 employees_db-full-1.0.6.tar.bz2] (about 26MB) and get it into your home directory. Instead of using the '''links''' browser to do this or downloading to your computer and then pushing this large file back across the Internet with an SCP/SFTP client like FileZilla a better choice might be to learn about the '''wget''' program which can be used like "'''wget https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2'''" to download the file into your current working directory.
: In order to do really powerful things with a website we need a database where we can store dynamic content which can be queried to automatically build site pages from a template. One of the most common databases is an SQL database, and MySQL has long been the most prevalent Linux SQL software. These days MariaDB, which we installed, has been taking some MySQL marketshare because it is a community developed program instead of MySQL which is now owned by Oracle. The MariaDB software and MySQL software are essentially equivalent, especially for small sites/databases, and the same commands are used interchangeably including using the '''mysql''' command to access them. SQL itself is a standard language for interacting with a specific type of database called a relational database, we'll see why it's called a relational database in a minute. Each database is a collection of data stored in tables. You can think of a table as something like a spreadsheet with rows and columns, except we call the columns fields and the rows records (sometimes we actually call them rows too). The different tables are often related to one another, of course they are usually being used by the same web application, but usually the relationship goes deeper than that as we'll see in a little bit.
# Figure out how to use the '''tar''' program to unpack this .tar.bz2 file. Note that this file uses bz2 compression which takes longer to compress than gzip but can compress the files more. Once unpacked you should have an "employees_db" folder.
<li>Download a sample database</li>
# Inside the ''employees_db'' folder you should have a number of ''.sql'' script files and dump files which can be used to restore the database to your system. Take a look at the ''employees.sql'' file, it's just a text file, which describes the format of the database. Also take a look at the ''load_employees.dump'' file, also just a text file, which contains all of the records from the employees table of the database. Note there are 300,000 some employee records in this database so you probably don't want to just use '''cat''' to view these files as it could take a while to print them out to your screen. Using the '''less''' program would be a better choice as it will allow you to exit viewing the file by pressing q at any time.
: To jumpstart our exploration of databases we'll download a pre-built sample database with a few tables and many records already in it and import it into our MySQL database server. Download the employees database file [https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2 employees_db-full-1.0.6.tar.bz2] (about 26MB) and get it into your home directory. Instead of using the '''links''' browser to do this or downloading to your computer and then pushing this large file back across the Internet with an SCP/SFTP client like FileZilla a better choice might be to learn about the '''wget''' program which can be used like "'''wget https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2'''" to download the file into your current working directory.
# We can interact with the MySQL/MariaDB server in many different ways. The first way we'll take a look at is through the command line '''mysql''' program. Run the "'''sudo mysql'''" command. Note that we are running this command with system administrator permissions which allows us to log in to the MySQL server with database administrator permissions as well. The MySQL program actually stores it's own usernames and passwords as a MySQL database itself, you can create and modify these permissions from the mysql command line but we'll learn more about modifying them through the Webmin interface later.
<code>wget https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2</code>
# MySQL has it's own command line language the you can use through the "mysql>" prompt you are now running. For example, if we want to see a list of databases on the system we can run "'''SHOW databases;'''" run the command and note the databases that already exist by default.
<li>Unpack the database file</li>
#* Note: Each MySQL/MariaDB statement ends with a semicolon (;) if you do not put the semicolon at the end of the line the command will not execute when you press enter and you will be able to continue to add more commands to the statement before finishing with a semicolon. If you accidentally press enter without ending with a semicolon you can just enter a semicolon on the next line and press enter.
: Figure out how to use the '''tar''' program to unpack this .tar.bz2 file. Note that this file uses bz2 compression which takes longer to compress than gzip but can compress the files more. Once unpacked you should have an "employees_db" folder.
# To import the employee database we can use the "'''source employees.sql;'''" command (assuming we were already working in the directory which contains the employees.sql file when we launched mysql. Import the employee database and verify the system now has an employees database.
<li>Explore the files included with sample database</li>
# If we want to see all of the tables in the employees database we must first select the database we want to work on using the "'''USE employees;'''" command, run this followed by the "'''SHOW tables;'''" command to see a list of tables.
: Inside the ''employees_db'' folder you should have a number of ''.sql'' script files and dump files which can be used to restore the database to your system.
# To see a list of all the records in a table we need to run a SELECT query "'''SELECT * from ''<tablename>'''''" like "'''SELECT * from employees'''" which will list all the records in the ''employees'' table of the employees database and display them. Remember that we previously selected to USE the ''employees'' database, this will be remembered until we USE a different database. Try displaying the records in a couple of different tables like employees and departments.
:*Take a look at the ''employees.sql'' file, it's just a text file, which describes the format of the database.
# There is much more that we can do from the MySQL/MariaDB command line but that would be a whole class or more itself. The MySQL/MariaDB programs are well documented on the Internet and with these basic skills you should be able to figure out most of it. Remember, the vast majority of what you read about MySQL will also work in MariaDB and vice-versa.
:*Also take a look at the ''load_employees.dump'' file, also just a text file, which contains all of the records from the employees table of the database. :'' NOTE: there are 300,000 some employee records in this database so you probably don't want to just use '''cat''' to view these files as it could take a while to print them out to your screen. Using the '''less''' program would be a better choice as it will allow you to exit viewing the file by pressing q at any time.
# Now that you have learned a bit about MySQL databases from the command line let's see how they look in the Webmin web management interface. Log back in to your Webmin interface through a browser on your host system.
<li>Open the MySQL (MariaDB) command line</li>
# Because MySQL was not installed when we first setup Webmin you need to "scan for new modules" first. Once that process has finished refresh the page to see that MySQL has been added to the server section of Webmin.
: We can interact with the MySQL/MariaDB server in many different ways. The first way we'll take a look at is through the command line '''mysql''' program. Run the "'''sudo mysql'''" command.  
# Take a look at the Webmin MySQL module and see how MySQL users and permissions are configured.
:'' NOTE: that we are running this command with system administrator permissions which allows us to log in to the MySQL server with database administrator permissions as well. The MySQL program actually stores it's own usernames and passwords as a MySQL database itself, you can create and modify these permissions from the mysql command line but we'll learn more about modifying them through the Webmin interface later.
# Try browsing through the ''employees'' database using the Webmin interface including viewing tables and records inside of the tables.
:<code>sudo MySQL</code>
# Go back to your MySQL command line window and run the command "'''DROP DATABASE employees;'''" to delete the entire database. Obviously the DROP command is one to be careful with as you can easily wipe out a huge database with one line! Type "'''exit;'''" to quit the MySQL program and return to a Linux shell.
<br>
:[[File:Lab5_mariadb_cli.png|link=https://wiki.ihitc.net/mediawiki/images/4/4f/Lab5_mariadb_cli.png|500px]]
:[[Media:Lab5_mariadb_cli.png|Click here for a larger image]]
<li>Explore MySQL (MariaDB) Commands</li>
: MySQL has it's own command line language the you can use through the "mysql>" prompt you are now running. For example, if we want to see a list of databases on the system we can run "'''SHOW databases;'''" run the command and note the databases that already exist by default.
<code>SHOW databases;</code>
:'' NOTE: Each MySQL/MariaDB statement ends with a semicolon (;) if you do not put the semicolon at the end of the line the command will not execute when you press enter and you will be able to continue to add more commands to the statement before finishing with a semicolon. If you accidentally press enter without ending with a semicolon you can just enter a semicolon on the next line and press enter.
<li>Import the sample database</li>
: To import the employee database we can use the "'''source employees.sql;'''" command (assuming we were already working in the directory which contains the employees.sql file when we launched mysql. Import the employee database and verify the system now has an employees database.
<code>SOURCE employees.sql;</code>
<li>Use the employees database</li>
<code>USE employees;</code>
: If we want to see all of the tables in the employees database we must first select the database we want to work on using the "'''USE employees;'''" command.
<li>Explore the employee table</li>
: Use the "'''SHOW tables;'''" command to see a list of tables.
<code>SHOW tables;</code>
<li>View a list of employees in table</li>
: To see a list of all the records in a table we need to run a SELECT query "'''SELECT * from ''<tablename>'''''" like "'''SELECT * from employees'''" which will list all the records in the ''employees'' table of the employees database and display them.
<code>SELECT * from employees;</code>
<br>
:[[File:Lab5_mariadb_show_tables.png|link=https://wiki.ihitc.net/mediawiki/images/d/d5/Lab5_mariadb_show_tables.png|350px]]
:[[Media:Lab5_mariadb_show_tables.png|Click here for a larger image]]
:* Remember that we previously selected to USE the ''employees'' database, this will be remembered until we USE a different database. Try displaying the records in a couple of different tables like employees and departments.
:'' NOTE: The output can be broken by using CTRL-C on your local machine if you do not wish to wait for all of the records to be displayed.
:
: There is much more that we can do from the MySQL/MariaDB command line but that would be a whole class or more itself. The MySQL/MariaDB programs are well documented on the Internet and with these basic skills you should be able to figure out most of it. Remember, the vast majority of what you read about MySQL will also work in MariaDB and vice-versa.
<li>Explore the MySQL interface in Webmin
: Now that you have learned a bit about MySQL databases from the command line let's see how they look in the Webmin web management interface. Log back in to your Webmin interface through a browser on your host system.
:* Because MySQL was not installed when we first setup Webmin you need to "scan for new modules" first. Once that process has finished refresh the page to see that MySQL has been added to the server section of Webmin.
:* Take a look at the Webmin MySQL module and see how MySQL users and permissions are configured.
:* Try browsing through the ''employees'' database using the Webmin interface including viewing tables and records inside of the tables.
<li>Drop database and exit MySQL (MariaDB)</li>
:* Go back to your MySQL command line window and run the command "'''DROP DATABASE employees;'''" to delete the entire database.
:* Type "'''exit;'''" to quit the MySQL program and return to a Linux shell.
 
:'' NOTE: Obviously, the DROP command is one to be careful with as you can easily wipe out a huge database with one line!  
</ol>


==View Logfiles==
==View Logfiles==


'''''[https://www.youtube.com/watch?v=cxIOQF-eMy0&feature=youtu.be Video tutorial - Tutorial 5 View Logfiles v3]'''''
'''''[https://www.youtube.com/watch?v=cxIOQF-eMy0&feature=youtu.be Video Tutorial - Tutorial 5 View Logfiles]'''''
# Linux stores most of log files in the ''/var/log'' directory so change your working directory to ''/var/log''.
 
# Some log files are viewable by all users on the system but other log files may be restricted and you will need to use '''sudo''' to view them.
<ol>
# Linux log files are typically standard text files and can be read with standard text file utilities like cat or less. One additional utility which comes in hand is the '''tail''' program which displays the last 10 lines (by default) of a text file and which can be configured to display more or less lines using a command like '''tail -20 /var/log/syslog''' which will display the last 20 lines of the main system log file. The manual page for the '''tail''' command contains additional information like how to use the ''-f'' option to view a log in realtime (exit with Ctrl-C).
: Linux stores most of log files in the ''/var/log'' directory so change your working directory to ''/var/log''.
# Take a look at some of the log files on your system and see what kinds of information they contain specifically you should probably look at ''syslog'', ''auth.log'', ''kern.log'', ''apache2/access.log'', and ''apache2/error.log''  
: Some log files are viewable by all users on the system but other log files may be restricted and you will need to use '''sudo''' to view them.
# In order to prevent log files from completely filling your drive old log files are eventually rotated on the system and compressed and then finally deleted. Take a look at some of the rotated log files like ''/var/log/syslog.1'' or ''/var/log/kern.log.1'' which will contain slightly older log entries.
<li>View last 20 lines in '''/var/log/syslog'''</li>
# Note: After the first rotated file other files are usually compressed with gzip. You can decompress and view these on the fly using the '''zcat''' program.
:<code>tail -20 /var/log/syslog</code>
:[[File:Lab5_tail.png|link=https://wiki.ihitc.net/mediawiki/images/8/88/Lab5_tail.png|500px]]
:[[Media:Lab5_tail.png|Click here for a larger image]]
: Linux log files are typically standard text files and can be read with standard text file utilities like cat or less. One additional utility which comes in hand is the '''tail''' program which displays the last 10 lines (by default) of a text file and which can be configured to display more or less lines using a command like '''tail -20 /var/log/syslog''' which will display the last 20 lines of the main system log file. The manual page for the '''tail''' command contains additional information like how to use the ''-f'' option to view a log in realtime (exit with Ctrl-C).
<li>Save the tail output to a file</li>
: Often we may want to save the output from a '''tail''' command to share with others or document an issue.  This can easily be done using redirection we learned in a previous lab.  Experiment with how to save some tail commands.  Keep in mind you may need to escalate privileges for some files to be redirected.  Try the following:
: <code>sudo tail -20 /var/log/syslog > logtail.txt</code>
<li>Explore some other log files</li>
: Take a look at some of the log files on your system and see what kinds of information they contain specifically you should probably look at ''syslog'', ''auth.log'', ''kern.log'', ''apache2/access.log'', and ''apache2/error.log''  
:* Use the tail command to explore these logs
: In order to prevent log files from completely filling your drive old log files are eventually rotated on the system and compressed and then finally deleted. Take a look at some of the rotated log files like ''/var/log/syslog.1'' or ''/var/log/kern.log.1'' which will contain slightly older log entries.
:'' Note: After the first rotated file other files are usually compressed with gzip. You can decompress and view these on the fly using the '''zcat''' program.
</ol>
==Checking Your Work==
<ol>
<li>Ping your assigned IP for your pod</li>
    <ul>Your ping should return a response.</ul>
<li>Open a browser on your own PC and navigate to your IP address.</li>
    <ul>Your custom link page should appear in your browser window.</ul>
<li>Check your home directory for the logtail.txt file you created.</li>
    <ul>The logtail.txt file should be in your home directory.</ul>
<li> Automatically check your results by running this command:</li>
<code><nowiki>
curl https://raw.githubusercontent.com/mnjk-inver/Linux-2480-Rebuild/main/lab_05_test.py | python3
</nowiki></code>
</ol>
<br><br>
=Web App=
You can check your progress on any of the labs in the ITC-2480 course from a webapp from this link:  <br>
[http://webcheck.itc2480.campus.ihitc.net webcheck.itc2480.campus.ihitc.net]<br>
You must be logged into the campus VPN to use this application.

Latest revision as of 01:14, 20 April 2021

Introduction

In this lab you will perform the following tasks:

  • Learn about static network configuration of Debian Linux systems
  • Install PHP
  • Install MariaDB on your server, this is an open-source MySQL alternative
  • Experiment with websites and databases
  • Explore the standard log files on your system.

You will be introduced to the following commands:

You will also use the following MySQL (MariaDB) commands:

Lab Procedure

Prerequisites

  1. Open an SSH console to your Linux system using the PuTTY software, login with your standard user account
  2. Make a note of the static IP address information for your particular system in the table below, it is based on the system name identification letter in vmWare. These IP addresses will ONLY be valid in the ITC vmWare Linux class subnet. If you are working on a local VirtualBox system you will need to select an available static IP from the correct subnet for your system or skip the static IP addressing section. All vmWare systems will use a gateway address of 172.17.50.1 and a subnet mask of 255.255.255.0 with two nameservers 172.17.139.11 and 172.17.139.111
System ID Static IP
A 172.17.50.11
B 172.17.50.12
C 172.17.50.13
D 172.17.50.14
E 172.17.50.15
F 172.17.50.16
G 172.17.50.17
H 172.17.50.18
I 172.17.50.19
J 172.17.50.20
K 172.17.50.21
L 172.17.50.22
M 172.17.50.23
N 172.17.50.24
O 172.17.50.25
P 172.17.50.26
Q 172.17.50.27
R 172.17.50.28
S 172.17.50.29
T 172.17.50.30
U 172.17.50.31
V 172.17.50.32
W 172.17.50.33
X 172.17.50.34
Y 172.17.50.35
Z 172.17.50.36


Set a static IP

Video Tutorial - Setting a Static IP Address

An important first step is to learn how to diferenciate between network interfaces. Take a look at this site and this site to understand how network interface names in Debian 9 and later are identified.
  1. Set a static IP for your server
    • Using your text editor of choice, open up the file /etc/network/interfaces
    • Notice how it is currently set to dhcp for the ens192 interface.
    • To set a static IP, you will need to change iface ens192 inet dhcp to iface ens192 inet static.
    • Now, under the iface line you just edited, you will need to enter the address, netmask, and gateway for the static network.
    Reminder: it is common practice to indent (tab) static network configuration information in the interfaces file.
    Note: Your configuration should be similar to this:
    allow-hotplug ens192
    iface ens192 inet static
    	address xxx.xxx.xxx.xxx
    	netmask 255.255.255.0
    	gateway 172.17.50.1
    	dns-nameservers 172.17.139.11 172.17.139.111
    • Now save the file, and exit your file editor.
  2. Apply your static IP address
    • Now we are going to apply the static IP change. Try using ip address show to view your active configuration now and you should see that your old address is still active.
    NOTE: You should only make network configuration changes when you have physical access to a machine. This way, if you mess up your configuration you will be able to fix it from a local console.' If you lose working SSH access to your system after making these changes you'll need to connect in through the NetLab console interface (which is equivalent to physical access) and find and correct your configuration issues.
    • Using the ifdown and ifup command, we are going to restart the network interface, this step is required to apply the change.
    Note: A good method to watch this change is to have to ping windows open on your local machine ping both your old IP address and your new IP address with a ping 172.17.50.xx -t this will allow you to see how quickly the change will happen. The first image below is with the DHCP address, the second is after the static address is applied.
    Lab5 ping ifup ifdown.png
    Click here for a larger image
    • In a SSH terminal, run sudo ifdown ens192 && sudo ifup ens192. Notice the two && symbols. This tells the Linux shell that it should run the second command right after the first. If we do not define this, then we would be left with a machine that has its networking turned off.
    sudo ifdown ens192 && sudo ifup ens192
    Note: you will lose your SSH connection because the IP your SSH session is connected to is no longer in use by your VM.
    • At this point, your machine should now be using a static address. You may receive an error message indicating that an address cannot be assigned which is related to your old DHCP address still being on the interface as well. Use ip address show to verify the new IP address is assigned to the interface.
  3. Verify the new static IP address
    • Used a ping command from your local PC to try pinging both the old DHCP address as well as your new static address of your VM.
    • Once you have verified the VM is responding on the new static IP address reboot the system to ensure the old DHCP address is removed by restarting your VM with sudo shutdown -r now Note you will lose your SSH connection because the IP your SSH session is connected to is no longer in use by your VM.
  4. Reconnect through SSH to your new IP address and verify it is now applied using the ip address show command.

Install PHP & MySQL/MariaDB

Video Tutorial - Install PHP and MySQL MariaDB

  1. Install packages
    • Using apt, install the php and mariadb-server packages as well as the php-mysql package which provides the link between php and mysql and the libapache2-mod-php package which provides a copy of PHP which allows the Apache webserver to run website PHP scripts.
    Note: Remember to do an apt update before installing packages to make sure you get the latest versions Note: Maria-DB is a fully compatible replacement for MySQL that isn't controlled by Oracle.
    • After the dependencies are found, go ahead and accept them to continue the install.
  2. After the install is complete, cd to /var/www/html which is the root directory of your webserver and so where all website files are served from.
  3. In order to fully enable the PHP-MySQL module you should restart your Apache webserver. You don't need to restart your entire system to do this. Do some research on the "apache2ctl" or the "service apache2" commands which can be used to do this.
  4. At this point we will test to verify that php is working properly with Apache. Create a new file named phptest.php, and then open it in a text editor.
  5. Note: you may have to use sudo to create and edit files in this directory. as your standard user account may not have privilidges to create and edit files in this directory.
    sudo touch phptest.php
    sudo nano phptest.php
  6. Enter the following into the file:
    <?php
    phpinfo();
    ?>
  7. Now save the file, and on your local machine go to xxx.xxx.xxx.xxx/phptest.php in a web browser where xxx.xxx.xxx.xxx is your VM's static IP. You should now be on a page that shows your PHP Version, and system information.
  8. Lab5 php info.png
    Click here for a larger image
  9. Use the "View Source" option in your browser to see what the HTML source code is for the page you're viewing is. Is it the same or different than the .php file you created? Why? How is this different than a standard .html file?
  10. Lab5 php info devtab.png
    Click here for a larger image

Experiment with Website PHP

Video Tutorial - Experiment with Website PHP

  1. Explore Apache symlinks
  2. In a previous lab we learned about editing the /var/www/index.html file to change the default web page displayed by your server. In a future lab you will install some PHP/MySQL based software which powers many Internet sites with forums, blogs, etc. Before we can do that we need to learn a little more about PHP and about databases. Like most software on Linux the Apache webserver configuration files are stored in the /etc directory. Specifically, you can find several of them which work together and are called from each other in /etc/apache2/ take a look in the /etc/apache2/sites-enabled/ directory. See how symlinks are used to point to configuration files which actually reside in the /etc/apache2/sites-available/ directory? This allows us to turn off and on various sites by creating or removing a symlink rather than by deleting the actual configuration file, a handy thing if we just want to temporarily disable a site. In fact Apache provides a utility to automatically add and remove these links called a2ensite and a2dissite. Check the man pages for these commands to see how they work.
  3. Explore /etc/apche2/sites-enabled
  4. As you might have guessed the default site for your system is configured by the file linked to at /etc/apache2/sites-enabled/000-default If you open this file in your favorite text editor you will see a series of what Apache calls "directives" which explain how the webserver should function, what port it should listen on, where the website files will reside (/var/www/html), etc.
    NOTE: Going into all of the different Apache directives is outside of the scope of this course but you will find a lot of documentation about them on the Internet.
  5. Explore the Apache configuration files in Webmin
  6. In addition to configuring Apache directly through it's configuration files you can also configure it through an interface like Webmin which we installed in a previous lab. Bring up the Webmin configuration page for Apache and take a look at the settings for the default site. Note how the things you saw directly in the configuration file match up with what you see in Webmin. One of the things you may be interested in seeing is how the webserver knows to display the index.html file from a directory if it exists and no specific file is requested in the URL. Take a look on the "Directory Indexing" page for the default virtualhost and see if you can find this information. You'll notice that there is a list of files, not just index.html which the server will display.
    NOTE: You may need to refresh your Webmin modules or "scan for new modules" if you haven't done that since installing Apache and Webmin. Webmin scans your system for compatible server software packages (like Apache) when it is installed but needs to re-scan it's modules after you install new server software before you can configure it through Webmin.
  7. Expiriment with and learn PHP basics
  8. In another section of this lab you created a basic phptest.php file and saw how the server executed the PHP code and turned it into an HTML page your browser could display. Because PHP offers an easy way to write web applications it powers much of the Internet. To prepare us for setting up some PHP applications in a future lab it will be helpful to know a little about how PHP scripts work. Read through the W3 Schools PHP Basics Tutorial (PHP Intro through PHP Superglobals) and try creating some PHP scripts on your own Linux server like the examples given in the tutorial pages to see if you can get them to run, try modifying them a little bit and see what the results are. There are many great Internet resources devoted to understanding how to do things with PHP so take some time to see how this language can be easily integrated in websites. You should definitely be familiar with editing PHP files to change variables, echo statements, etc. work which are all skills you'll need when installing PHP based website software.
  9. Create your own PHP files
    • Try writing some simple PHP scripts on your own using variables and echo statements and make them available through your webserver.
    • A good place to create these would be in your home directory, using your favorite text editor.
  10. Create your own "home" page by editing your index.html file located in the /var/www/html directory.
  11. NOTE: You will need to be familiar with the very basics of editing HTML files in order to create links, etc. If you haven't done this before there are many introductory HTML tutorials available online. A good starting place is W3 Schools HTML Basics Examples
    NOTE:It is importatnt to know that an html file must include the <!DOCTYPE HTML> tag. Additionally, you will notice that all tags have an opening tag and a closing tag. The information between the tags is what will have the features of the tag
    • Backup your index.html file by copying to a new file name.
    sudo cp /var/www/html/index.html /var/www/html/index.html.bak
    • Edit your index.html file (or create a new index.php file which will be loaded instead if it exists) so there is a list and links to the various scripts you have created as part of your PHP experimentation.
    • Open your index.html file in your favorite text editor,
    sudo nano /var/www/html/index.html
    • Since you have backed up the original index.html file I would edit this file to only include the basics.
     
    <!DOCTYPE HTML>
    <html>
    <body>
    <h1>My ITC-2480 Server</h1>
    <p>This is a server on Pod-R in NetLab at 172.17.50.XX</p>
    <br>
    <p>Here are some of the Projects I am working on:</p>
    <a href="http://172.17.50.XX/phptest.php">PHP Version Information</a>
    </body>
    </html>
    
    NOTE: Make sure to replace the XX in the IP with the IP address of your server.
    Lab5 basic html.png
    Click here for a larger image
    A first look for a basic page
    Lab5 initial home.png
    Click for a larger image
  12. Try to add additional features to your "home" page.
  13. NOTE: You may get some ideas of features you might like to add by looking at the original index.html file that we backed up in step 6 of this lab.
    • As you complete your labs consider what you can add to this page.
    • Add links to the PHP scripts you created
    • A few good chalanges would be to add a title, change font size, style or color.

Experiment with Databases

Video Tutorial - Experiment with Databases

    In order to do really powerful things with a website we need a database where we can store dynamic content which can be queried to automatically build site pages from a template. One of the most common databases is an SQL database, and MySQL has long been the most prevalent Linux SQL software. These days MariaDB, which we installed, has been taking some MySQL marketshare because it is a community developed program instead of MySQL which is now owned by Oracle. The MariaDB software and MySQL software are essentially equivalent, especially for small sites/databases, and the same commands are used interchangeably including using the mysql command to access them. SQL itself is a standard language for interacting with a specific type of database called a relational database, we'll see why it's called a relational database in a minute. Each database is a collection of data stored in tables. You can think of a table as something like a spreadsheet with rows and columns, except we call the columns fields and the rows records (sometimes we actually call them rows too). The different tables are often related to one another, of course they are usually being used by the same web application, but usually the relationship goes deeper than that as we'll see in a little bit.
  1. Download a sample database
  2. To jumpstart our exploration of databases we'll download a pre-built sample database with a few tables and many records already in it and import it into our MySQL database server. Download the employees database file employees_db-full-1.0.6.tar.bz2 (about 26MB) and get it into your home directory. Instead of using the links browser to do this or downloading to your computer and then pushing this large file back across the Internet with an SCP/SFTP client like FileZilla a better choice might be to learn about the wget program which can be used like "wget https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2" to download the file into your current working directory.
    wget https://launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-full-1.0.6.tar.bz2
  3. Unpack the database file
  4. Figure out how to use the tar program to unpack this .tar.bz2 file. Note that this file uses bz2 compression which takes longer to compress than gzip but can compress the files more. Once unpacked you should have an "employees_db" folder.
  5. Explore the files included with sample database
  6. Inside the employees_db folder you should have a number of .sql script files and dump files which can be used to restore the database to your system.
    • Take a look at the employees.sql file, it's just a text file, which describes the format of the database.
    • Also take a look at the load_employees.dump file, also just a text file, which contains all of the records from the employees table of the database. : NOTE: there are 300,000 some employee records in this database so you probably don't want to just use cat to view these files as it could take a while to print them out to your screen. Using the less program would be a better choice as it will allow you to exit viewing the file by pressing q at any time.
  7. Open the MySQL (MariaDB) command line
  8. We can interact with the MySQL/MariaDB server in many different ways. The first way we'll take a look at is through the command line mysql program. Run the "sudo mysql" command.
    NOTE: that we are running this command with system administrator permissions which allows us to log in to the MySQL server with database administrator permissions as well. The MySQL program actually stores it's own usernames and passwords as a MySQL database itself, you can create and modify these permissions from the mysql command line but we'll learn more about modifying them through the Webmin interface later.
    sudo MySQL

    Lab5 mariadb cli.png
    Click here for a larger image
  9. Explore MySQL (MariaDB) Commands
  10. MySQL has it's own command line language the you can use through the "mysql>" prompt you are now running. For example, if we want to see a list of databases on the system we can run "SHOW databases;" run the command and note the databases that already exist by default.
    SHOW databases;
    NOTE: Each MySQL/MariaDB statement ends with a semicolon (;) if you do not put the semicolon at the end of the line the command will not execute when you press enter and you will be able to continue to add more commands to the statement before finishing with a semicolon. If you accidentally press enter without ending with a semicolon you can just enter a semicolon on the next line and press enter.
  11. Import the sample database
  12. To import the employee database we can use the "source employees.sql;" command (assuming we were already working in the directory which contains the employees.sql file when we launched mysql. Import the employee database and verify the system now has an employees database.
    SOURCE employees.sql;
  13. Use the employees database
  14. USE employees;
    If we want to see all of the tables in the employees database we must first select the database we want to work on using the "USE employees;" command.
  15. Explore the employee table
  16. Use the "SHOW tables;" command to see a list of tables.
    SHOW tables;
  17. View a list of employees in table
  18. To see a list of all the records in a table we need to run a SELECT query "SELECT * from <tablename>" like "SELECT * from employees" which will list all the records in the employees table of the employees database and display them.
    SELECT * from employees;
    Lab5 mariadb show tables.png
    Click here for a larger image
    • Remember that we previously selected to USE the employees database, this will be remembered until we USE a different database. Try displaying the records in a couple of different tables like employees and departments.
    NOTE: The output can be broken by using CTRL-C on your local machine if you do not wish to wait for all of the records to be displayed.
    There is much more that we can do from the MySQL/MariaDB command line but that would be a whole class or more itself. The MySQL/MariaDB programs are well documented on the Internet and with these basic skills you should be able to figure out most of it. Remember, the vast majority of what you read about MySQL will also work in MariaDB and vice-versa.
  19. Explore the MySQL interface in Webmin
    Now that you have learned a bit about MySQL databases from the command line let's see how they look in the Webmin web management interface. Log back in to your Webmin interface through a browser on your host system.
    • Because MySQL was not installed when we first setup Webmin you need to "scan for new modules" first. Once that process has finished refresh the page to see that MySQL has been added to the server section of Webmin.
    • Take a look at the Webmin MySQL module and see how MySQL users and permissions are configured.
    • Try browsing through the employees database using the Webmin interface including viewing tables and records inside of the tables.
  20. Drop database and exit MySQL (MariaDB)
    • Go back to your MySQL command line window and run the command "DROP DATABASE employees;" to delete the entire database.
    • Type "exit;" to quit the MySQL program and return to a Linux shell.
    NOTE: Obviously, the DROP command is one to be careful with as you can easily wipe out a huge database with one line!

View Logfiles

Video Tutorial - Tutorial 5 View Logfiles

    Linux stores most of log files in the /var/log directory so change your working directory to /var/log.
    Some log files are viewable by all users on the system but other log files may be restricted and you will need to use sudo to view them.
  1. View last 20 lines in /var/log/syslog
  2. tail -20 /var/log/syslog
    Lab5 tail.png
    Click here for a larger image
    Linux log files are typically standard text files and can be read with standard text file utilities like cat or less. One additional utility which comes in hand is the tail program which displays the last 10 lines (by default) of a text file and which can be configured to display more or less lines using a command like tail -20 /var/log/syslog which will display the last 20 lines of the main system log file. The manual page for the tail command contains additional information like how to use the -f option to view a log in realtime (exit with Ctrl-C).
  3. Save the tail output to a file
  4. Often we may want to save the output from a tail command to share with others or document an issue. This can easily be done using redirection we learned in a previous lab. Experiment with how to save some tail commands. Keep in mind you may need to escalate privileges for some files to be redirected. Try the following:
    sudo tail -20 /var/log/syslog > logtail.txt
  5. Explore some other log files
  6. Take a look at some of the log files on your system and see what kinds of information they contain specifically you should probably look at syslog, auth.log, kern.log, apache2/access.log, and apache2/error.log
    • Use the tail command to explore these logs
    In order to prevent log files from completely filling your drive old log files are eventually rotated on the system and compressed and then finally deleted. Take a look at some of the rotated log files like /var/log/syslog.1 or /var/log/kern.log.1 which will contain slightly older log entries.
    Note: After the first rotated file other files are usually compressed with gzip. You can decompress and view these on the fly using the zcat program.

Checking Your Work

  1. Ping your assigned IP for your pod
    • Your ping should return a response.
  2. Open a browser on your own PC and navigate to your IP address.
    • Your custom link page should appear in your browser window.
  3. Check your home directory for the logtail.txt file you created.
    • The logtail.txt file should be in your home directory.
  4. Automatically check your results by running this command:
  5. curl https://raw.githubusercontent.com/mnjk-inver/Linux-2480-Rebuild/main/lab_05_test.py | python3



Web App

You can check your progress on any of the labs in the ITC-2480 course from a webapp from this link:
webcheck.itc2480.campus.ihitc.net
You must be logged into the campus VPN to use this application.