Lab 8 Overview
Apache Web Server
SCRIPTSLinux Tricks
Scripts
Small programs to help the maintaining and configuring of an operating system
Executed by the shellSyntax dependent on shell
Typical use:Create a bunch of new usersConfigure a service
De facto extension: .sh
Change terminal color
#!/bin/bash# script to turn the screen bluesetterm -background blueecho It is a blue day
Line 1: specifies which shell should be used to interpret the commands in the script.Line 2: is a comment (has no effect when the script is executed).Line 3: sets the background colour.Line 4: displays a message.
Assuming the file is executable:
To execute if the PWD has this file:
Assume a file named changecolor:
./changecolor
If in another directory use the fully qualified name:
/home/mydir/utils/changecolor
Simple Menu Script
#!/bin/bash OPTIONS="Hello Quit" select opt in $OPTIONS; do if [ "$opt" = "Quit" ]; then
echo done exit
elif [ "$opt" = "Hello" ]; then echo Hello World else
clear echo bad option
fi done
These can be as complex as needed with conditional and loop controls (among many other things)
Running Scripts Basics
Make sure the first line is a directive of which shell to use (if needed) Starts with a shebang: #! For Debian it is the bash shell
/bin/bash
Make sure it is executable rwxr-xr–-
User can run, edit and view Group can run and view World can view
chmod 754 script.sh
If the shell is in a directory defined in the PATH echo $PATH
Will show the directories
type: filename
If it is not in PATH but it is in the PWD type: ./filename
If not in PATH and not in the PWD type full filename starting with the root: /home/ajkombol/Desktop/script.sh
Script one: netconfig.sh
#!/bin/bashNETCONFIGFILE=/etc/network/interfacesRESOLVECONF=/etc/resolv.confifdown $1vi $NETCONFIGFILE#vi $RESOLVECONFifup $1echo 'nameserver 172.16.1.254' >> $RESOLVECONFecho 'nameserver 172.16.1.250' >> $RESOLVECONF
myprompt#./netconfig.sh eth0
Desired action: • stop the NIC• open the interfaces file to edit• restart the NIC• add two nameservers to the resolve.conf fileThe script:
To execute the script:
Quick script introParameters passed to a script are
denoted by $1, $2, $3, … $1 is the first parameter, $2 is the second, etc.$# is the number of parameters passed
Variables are case sensitiveEnvironment variables
Used by the system in generalAre UPPER case by convention
Quick script intro (cont.) Conditionals are done by an if…elif…else…fi structure
if and elif are followed by a command that evaluates to true or false elif and else are optional
elif can be repeatedOnly one else may be used, if needed
if is closed with the fi statement To stop in the middle of a script use exit n
n = 0 is a normal exit• If n is not specified it is assumed 0
n = 1 is an error exit• Actually any non 0 value is an error
If a value needs to be checked the test command is usedNumbers use conditionals
• e.g. –gt, -lt, and –eq greater than, less than, equal
• EX: test 1 –gt 2
Strings use operators• e.g. = or !=
equal and not equal• EX: test $1 = "opt1"
There are other comparisons that can be done, check the internet
Script two: go
#!/bin/bashIFILE=/etc/network/interfacesifdown eth0if test $1 = "static" ; then cp $IFILE.static $IFILE echo "Static interfaces loaded!"elif test $1 = "dhcp" ; then cp $IFILE.dhcp $IFILE echo "DHCP interfaces loaded!"else echo "Parameter must be static or dhcp"fiifup eth0
Desired action: • stop the NIC, • check which option • copy the proper template to interfaces,• restart the NIC
To execute the script:myprompt#./go static…myprompt#./go dhcp
Today's labs scripts summaryChanging network configuration
Script one – editing the interfaces filestop NICedit interfacesadd some additional routing informationstart NIC
Script two – alternating interfaces templatesMake two interfaces templatesstop NICCopy the desired templatestart NIC
You may wish to keep these scripts Use netconfig.sh to edit the interfaces file
After making appropriate changes
Use go or go2 to switch between DHCP addressesStatic addresses
Reminder
ALWAYS make a backup copy of a configuration file BEFORE editing itWill allow the file to be restored
If you mess it upIf something else messes it up
Examples:cp interfaces interfaces.backup cp apache2.conf apache2.conf.orig
Make a copy of a line to be changed in a file and comment it out before changing the originalExample:
# This is the original lineThis is the changed line
APACHEMain Lab
Apache Web Server
Main GoalsInstall ApacheConfigure basic systemConfigure restrictions
Side goalsInstalling packages on a Debian SystemReinforce VM environmentReinforce use of vi editor
Apache Web Server Overview
Install Apache on your Debian VM apt-get
Backup: Synaptic Package ManagerNo credit for Synaptic install
Check if installation workedCreate new directories
“Install” the web applicationConfigure Apache to “find” the new “application”Copy Web page files into proper directories
Hint: assume the Web application is being moved from a different Web server to this machine
Configure Apache for restrictions Allow directory access Deny directory access
Browse the application from another machine
Misc. Notes
Debian required for the Apache Server CentOS recommended for the browsing client
Can be any OS with a browser
Use ifconfig to identify your IP address Web files (.htm and pics) available on hades.lab
Use browser to locate /apachelab
• e.g. lab302-web.hades.lab/classes/apachelab (172.16.1.250)
In /public directory Also available
ON websiteon thumbdrive
You will need to figure out on your own where the images go to be properly displayed All web pages except home have an image A subdirectory is involved!
IMPORTANT!!!!
This Debian VM must be working and saved!
Will be used as the basis for the DNS Lab!
Apache Overview
“Open source” web serverDefault browser “root” directory
NOT the same as the host’s root directory!
To access the web server:Use the IP address or host namePort 80
Bonus:At end of lab add port to browse on port 8080
Document results
Last minute notes:
Check the links to test1.htmltest2.htmlThey should go to the ITIS2110 directory
Watch the IP addresses of your VMDHCP OK this lab
Need to look up address for browsing
If address was assignedBe sure does not conflict with another machine
• Can use machine ID as subnet or host id• Can use your subnet (see listing on post)
Apache Lab
Lab 20 pts