測試說明:本測試重點考察在linux環境下搭建LAMP虛擬主機環境配置和調試排錯的能力。請按任務要求在虛擬機上完成操做任務,並在實驗報告上記錄主要操做步驟和配置文件中編輯的內容。測試環境爲虛擬機centos6.6,測試時間爲100分鐘。
所需軟件包能夠從ftp://172.18.74.252
下載,yum源爲ftp://172.18.74.252/centos6.6
php
注意:爲方便測試關閉防火牆(#services iptables stop
),關閉SElonux(# setenforce 0
)html
任務1:配置虛擬機網絡地址,使之與主機互相ping通。 (20分)mysql
要求:設置虛擬機網卡爲NAT模式,啓動登陸虛擬機,配置其網卡地址爲靜態地址,與虛擬機vmnet8在同一網段,最後三位數爲「學號後三位mod254」,虛擬機主機名爲姓名全拼。linux
檢查點:虛擬機與主機可以互相ping通,虛擬機可以訪問互聯網。web
提示:編輯/etc/sysconfig/network-scripts/ifcfg-eth*
文件,/etc/sysconfig/network
文件,重啓network
服務;或者直接使用ifconfig
命令修改IP地址。sql
任務2:安裝bind軟件包,使之可以提供域名解析服務。 (20分)shell
要求:使用yum方式安裝bind軟件包,設立「姓名全拼.com」和「學號.com」兩個正向區域,並分別設置www主機A記錄地址爲虛擬機地址。數據庫
檢查點:主機ping「www.[姓名全拼].com
」和「www.[學號].com
」能夠與虛擬主機通訊。apache
提示:編輯/etc/named.conf
文件和/var/named/
區域數據文件,使用named-checkconf
和named-checkzone
進行檢查,改變區域數據文件所屬組,重啓named
服務或rndc reload
。注意檢查主機dns服務器配置爲虛擬機地址。vim
任務3:安裝apache軟件,配置基於名字的虛擬主機。 (20分)
要求:使用源碼方式安裝apache,配置兩個基於名字的虛擬主機,「www.[姓名全拼].com
」和「www.[學號].com
」,虛擬主機目錄在/var/wwwroot
下,目錄名爲域名。
檢查點:從主機訪問「www.[姓名全拼].com
」和「www.[學號].com
」站點的默認主頁能夠分別看到「this’s 姓名 site」和「this’s 學號 site」。
提示:編輯/usr/local/apache/conf/httpd.conf
文件配置虛擬主機,注意配置虛擬主機目錄的訪問權限;在/var/wwwroot
創建虛擬主機目錄,編輯index.html文件。
任務4:安裝mysql,建立數據庫和數據表並輸入數據。 (10分)
要求:使用yum方式安裝mysql-server
,改變root
用戶登陸密碼爲123456
,建立數據庫project
。
/* 建立表 */ create table news ( id int unsigned auto_increment primary key, title varchar(60) not null, content text not null, addtime timestamp default current_timestamp not null ); /*插入測試數據*/ insert into news (title, content) values('新聞標題1', '新聞內容1'), ('新聞標題2','新聞內容2');
檢查點:select * from news;
顯示所有記錄,注意mysql數據庫的編碼格式。
提示:改密碼 #mysqladmin –u root –p password
新密碼字符
任務5:安裝PHP環境,編寫運行php腳本。 (10分)
要求:使用yum方式安裝PHP,編輯index.php文件放到「學號」虛擬主機,使之可以鏈接mysql數據庫,讀取顯示數據表的內容。
檢查點:在主機瀏覽器訪問http://www.[學號].com/index.php
,顯示數據表news的內容。
提示:拷貝libphp 5.so
,安裝php_mysql
軟件包,編輯httpd.conf
,注意要重啓apache
任務6:安裝vsftp軟件包,配置虛擬主機的ftp環境。 (10分)
要求:使用yum方式安裝vsftpd。創建用戶「姓名全拼」和「學號」,設定用戶主目錄爲/var/wwwroot/[姓名全拼].com
和/var/wwwroot/[學號].com
,密碼都爲123
。
檢查點:從主機能夠訪問ftp站點,並上傳網站內容。
任務7:使用ab性能測試軟件,優化虛擬主機性能。 (10分)
要求:使用apache自帶的ab軟件測試虛擬主機性能,並記錄最優結果。
提示:/usr/local/apache/bin/ab –c [併發數] –n [訪問次數] http://www.[學號].com/index.php
Linux shell 腳本以下:
#!/bin/bash #-------------------------------------------- # 這是一個註釋 # author: tong707 # site: https://blog.51cto.com/tong707/ # slogan: 學的不只是技術,更是夢想! #-------------------------------------------- # # You can enter your information following command: # ./install.sh -n [name] -s [student number] # # You can also configure your information from # here ( your_name, your_stu_number ), # then, enter the following command: # # ./install.sh -y # # your name your_name='zhangsan' # your student number your_stu_number='201522450123' # use local ftp yum source uselocal="true" # enable both local and internet yum sources enabledTogether="false" # modify eth0 to ethx modifyeth0="true" # ethx's name cd /root/ ifconfig >> ifconfig.txt if_str=$(cat ifconfig.txt | sed -n '1p') ethxname=`expr substr "${if_str}" 1 4` rm -rf /root/ifconfig.txt if [ $ethxname == "eth0" ] then modifyeth0="false" fi # define function: How to use this script? showHelp(){ echo -e "\033[32m How to Use this Shell Script?\033[0m" echo -e "\033[32m # $0 -n [name] -s [student number]\033[0m" echo -e "" exit } # receive parameters if [ $# == 4 ] then if [ $1 == "-n" -a $3 == "-s" ] then # echo "OK" your_name=$2 your_stu_number=$4 elif [ $1 == "-s" -a $3 == "-n" ] then # echo "OK" your_name=$4 your_stu_number=$2 else # echo "Help" showHelp fi elif [ $# == 1 ] then if [ $1 == "-y" ] then # pass echo -e "" else # echo "Help" showHelp fi else # echo "Help" showHelp fi # show user info echo -e "" date time1=`date +%s` echo -e "" echo -e " [ \033[32mINFO\033[0m ] your name is: \033[32m${your_name}\033[0m " echo -e " [ \033[32mINFO\033[0m ] your student number is: \033[32m${your_stu_number}\033[0m " echo -e "" sleep 2s # student number after 3 your_stu_num=`expr substr ${your_stu_number} 10 3` # the 4th part of the IP address your_ip_4=`expr ${your_stu_num} % 254` # the 3rd part of the IP address cd /root/ ifconfig >> ifconfig.txt if_str=$(cat ifconfig.txt | sed -n '2p') your_ip_3=`echo ${if_str} | cut -d '.' -f 3` rm -rf /root/ifconfig.txt # note #vim /etc/udev/rules.d/70-persistent-net.rules # turn off the firewall echo -e "\033[32m [ INFO ] Turning Off the Firewall ... \033[0m" service iptables stop setenforce 0 #echo -e "\033[32m [ INFO ] [ Firewall Closed Successfully ] \033[0m" echo -e "\n\n" # use ftp as the yum source yum install gcc -y if [ ${uselocal} == "true" ] then echo -e "\033[32m [ INFO ] Using FTP Server As the Yum Source ... \033[0m" mkdir /etc/yum.repos.d/bak mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/bak/ echo -e "[FTP]" > /etc/yum.repos.d/ftp.repo echo -e "name=CentOS6.6" >> /etc/yum.repos.d/ftp.repo echo -e "baseurl=ftp://172.18.72.252/centos6.6/" >> /etc/yum.repos.d/ftp.repo echo -e "gpgcheck=0" >> /etc/yum.repos.d/ftp.repo if [ ${enabledTogether} == "true" ] then # test START mv /etc/yum.repos.d/bak/CentOS-* /etc/yum.repos.d/ yum install yum-plugin-priorities.noarch -y echo -e "priority=1" >> /etc/yum.repos.d/ftp.repo #echo -e "priority=10" >> /etc/yum.repos.d/CentOS-* # END fi yum list | tail -n 20 echo -e "\033[32m [ INFO ] Set FTP Source Successfully \033[0m" echo -e "\n\n" fi # modify the host name echo -e "\033[32m [ INFO ] Modify the Host Name ... \033[0m" echo -e "NETWORKING=yes" > /etc/sysconfig/network echo -e "HOSTNAME=${your_name}" >> /etc/sysconfig/network hostname ${your_name} hostname echo -e "Hello, ${your_name}" #echo -e "\033[32m [ INFO ] [ Modify the Host Name Successfully ] \033[0m" echo -e "\n\n" # modify the host IP address echo -e "\033[32m [ INFO ] Modify the Host IP Address ... \033[0m" service network restart sed -i '/ONBOOT/d' /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i '/BOOTPROTO/d' /etc/sysconfig/network-scripts/ifcfg-eth0 echo -e "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo -e "BOOTPROTO=static" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo -e "DNS1=192.168.${your_ip_3}.${your_ip_4}" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo -e "DNS2=202.206.192.33" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo -e "IPADDR=192.168.${your_ip_3}.${your_ip_4}" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo -e "NETMASK=255.255.255.0" >> /etc/sysconfig/network-scripts/ifcfg-eth0 echo -e "GATEWAY=192.168.${your_ip_3}.2" >> /etc/sysconfig/network-scripts/ifcfg-eth0 if [ ${modifyeth0} == "true" ] then # test START sed -i '/HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0 sed -i "s/DEVICE=eth0/DEVICE=${ethxname}/g" /etc/sysconfig/network-scripts/ifcfg-eth0 mv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-${ethxname} # END fi ifconfig sed -i '$d' /etc/resolv.conf echo -e "nameserver 192.168.${your_ip_3}.${your_ip_4}" >> /etc/resolv.conf echo -e "nameserver 202.206.192.33" >> /etc/resolv.conf ping 127.0.0.1 -c 4 service network restart ping 127.0.0.1 -c 4 ifconfig echo -e " [\033[32m INFO \033[0m] Please SSH this IP address:\033[31m 192.168.${your_ip_3}.${your_ip_4} \033[0m" echo -e "\033[32m [ INFO ] Modify the Host IP Address Successfully \033[0m" echo -e "\n\n" sleep 2s # install the bind package echo -e "\033[32m [ INFO ] Install the Bind Package ... \033[0m" yum install bind -y # configure the named configuration file echo -e "\033[32m [ INFO ] Configure the named.conf ... \033[0m" cd /etc/ sed -i "s/127.0.0.1/any/g" named.conf sed -i "s/localhost/any/g" named.conf sed -i "/listen-on-v6/d" named.conf sed -i '/include "/d' named.conf echo -e "zone \"${your_name}.com\" IN {" >> named.conf echo -e " type master;" >> named.conf echo -e " file \"${your_name}.com.zone\";" >> named.conf echo -e "};" >> named.conf echo -e "" echo -e "zone \"${your_stu_number}.com\" IN {" >> named.conf echo -e " type master;" >> named.conf echo -e " file \"${your_stu_number}.com.zone\";" >> named.conf echo -e "};" >> named.conf echo -e "" >> named.conf echo -e "include \"/etc/named.rfc1912.zones\";" >> named.conf echo -e "include \"/etc/named.root.key\";" >> named.conf echo -e "" >> named.conf cd ~ # configure the zone file echo -e "\033[32m [ INFO ] Configure the Zone File ... \033[0m" cd /var/named/ cp named.localhost ${your_name}.com.zone cp named.localhost ${your_stu_number}.com.zone sed -i '$d' ${your_name}.com.zone sed -i '$d' ${your_name}.com.zone sed -i '$d' ${your_stu_number}.com.zone sed -i '$d' ${your_stu_number}.com.zone echo -e " A 192.168.${your_ip_3}.${your_ip_4}" >> ${your_name}.com.zone echo -e "www IN A 192.168.${your_ip_3}.${your_ip_4}" >> ${your_name}.com.zone echo -e " A 192.168.${your_ip_3}.${your_ip_4}" >> ${your_stu_number}.com.zone echo -e "www IN A 192.168.${your_ip_3}.${your_ip_4}" >> ${your_stu_number}.com.zone # modify the limit echo -e "\033[32m [ INFO ] Modify the Limit ... \033[0m" chown root:named /var/named/*.com.zone named-checkconf service named restart ping www.${your_name}.com -c 4 echo -e "" ping www.${your_stu_number}.com -c 4 rm -rf /var/run/yum.pid echo -e "\033[32m [ INFO ] Install the Bind Package Successfully \033[0m" echo -e "\n\n" # install apache web server echo -e "\033[32m [ INFO ] Install Apache Web Server ... \033[0m" echo -e "\033[32m [ INFO ] Install Apache Web Server ( install gcc ) ... \033[0m" yum install gcc -y cd ~ echo -e "\033[32m [ INFO ] Install Apache Web Server ( download httpd package ) ... \033[0m" wget ftp://172.18.72.252/httpd-2.2.34.tar.gz sleep 2s tar -zxvf httpd-2.2.34.tar.gz cd httpd-* echo -e "\033[32m [ INFO ] Install Apache Web Server ( configure apache2 ) ... \033[0m" ./configure --prefix=/usr/local/apache2 echo -e "\033[32m [ INFO ] Install Apache Web Server ( make install apache2 ) ... \033[0m" make && make install cd /usr/local/apache2/bin/ echo -e "\033[32m [ INFO ] Install Apache Web Server ( start apache2 ) ... \033[0m" ./apachectl start cd ~ wget http://localhost:80/index.html cat index.html sleep 2s cd ~ ls # create files and folders echo -e "\033[32m [ INFO ] Create Files and Folders ... \033[0m" mkdir /var/wwwroot/ cd /var/wwwroot/ mkdir ${your_name}.com mkdir ${your_stu_number}.com ls echo -e "<h1>This is wwwroot</h1>" >> /var/wwwroot/index.html echo -e "<?php phpinfo(); ?>" >> /var/wwwroot/info.php echo -e "<h1>This's ${your_name} site</h1>" >> /var/wwwroot/${your_name}.com/index.html echo -e "<h1>This's ${your_stu_number} site</h1>" >> /var/wwwroot/${your_stu_number}.com/index.html echo -e "<?php phpinfo(); ?>" >> /var/wwwroot/${your_name}.com/info.php echo -e "<?php phpinfo(); ?>" >> /var/wwwroot/${your_stu_number}.com/info.php cd ~ # configure apache virtual host echo -e "\033[32m [ INFO ] Configure Apache Virtual Host ... \033[0m" cd /usr/local/apache2/conf/ sed -i 's/\/usr\/local\/apache2\/htdocs/\/var\/wwwroot\//g' httpd.conf sed -i '/httpd-vhosts/s/^#//' httpd.conf cd /usr/local/apache2/conf/extra/ sed -i '27,$d' httpd-vhosts.conf echo -e "" >> httpd-vhosts.conf echo -e "" >> httpd-vhosts.conf echo -e "<VirtualHost *:80>" >> httpd-vhosts.conf echo -e " DocumentRoot \"/var/wwwroot/${your_name}.com/\"" >> httpd-vhosts.conf echo -e " ServerName www.${your_name}.com" >> httpd-vhosts.conf echo -e "</VirtualHost>" >> httpd-vhosts.conf echo -e "" >> httpd-vhosts.conf echo -e "<VirtualHost *:80>" >> httpd-vhosts.conf echo -e " DocumentRoot \"/var/wwwroot/${your_stu_number}.com/\"" >> httpd-vhosts.conf echo -e " ServerName www.${your_stu_number}.com" >> httpd-vhosts.conf echo -e "</VirtualHost>" >> httpd-vhosts.conf echo -e "" >> httpd-vhosts.conf echo -e "" >> httpd-vhosts.conf cd /usr/local/apache2/bin/ ./apachectl restart # test visit webpage echo -e "\033[32m [ INFO ] Test Visit Website ... \033[0m" cd ~ mkdir /root/view cd /root/view/ wget http://www.${your_name}.com/index.html mv index.html ${your_name}.com.html wget http://www.${your_stu_number}.com/index.html mv index.html ${your_stu_number}.com.html cd ~ echo -e "${your_name}.com.html" cat /root/view/${your_name}.com.html cd ~ sleep 2s echo -e "" cd ~ echo -e "${your_stu_number}.com.html" cat /root/view/${your_stu_number}.com.html cd ~ sleep 2s echo -e "" cd ~ rm -rf /var/run/yum.pid echo -e "\033[32m [ INFO ] Install Apache Web Server Successfully \033[0m" echo -e "\n\n" # install MySQL echo -e "\033[32m [ INFO ] Install MySQL Server ... \033[0m" yum install mysql-server -y #mysqladmin -u root -p password 123456 sed -i "7 i\skip-grant-tables" /etc/my.cnf service mysqld restart mysql -s -e "USE mysql; UPDATE user SET Password = password('123456') WHERE User='root';" sed -i '/skip-grant-tables/d' /etc/my.cnf service mysqld restart # modify data echo -e "\033[32m [ INFO ] Insert Data to Database ... \033[0m" echo -e "" mysql -uroot -p123456 -s -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; FLUSH PRIVILEGES;" mysql -uroot -p123456 -s -e "CREATE DATABASE IF NOT EXISTS project;" mysql -uroot -p123456 -s -e "SET NAMES 'UTF8'; USE project; CREATE TABLE IF NOT EXISTS news( id INT unsigned auto_increment primary key, title VARCHAR(60) not null, content TEXT not null, addtime timestamp default current_timestamp not null )DEFAULT CHARSET=UTF8;" mysql -uroot -p123456 -s -e "SET NAMES 'UTF8'; USE project; INSERT INTO news(title, content) VALUES('新聞標題1', '新聞內容1'), ('新聞標題2','新聞內容2');" echo -e "\033[32m [ INFO ] Show Data from Database ... \033[0m" mysql -uroot -p123456 -e "SET NAMES 'UTF8'; USE project; SELECT * FROM news WHERE 1;" sleep 2s rm -rf /var/run/yum.pid echo -e "\033[32m [ INFO ] Install MySQL Server Successfully \033[0m" echo -e "\n\n" # install php echo -e "\033[32m [ INFO ] Install PHP ... \033[0m" yum install php php-mysql -y cp /usr/lib64/httpd/modules/libphp5.so /usr/local/apache2/modules/ echo -e "" >> /usr/local/apache2/conf/httpd.conf echo -e "Include /etc/httpd/conf.d/php.conf" >> /usr/local/apache2/conf/httpd.conf echo -e "AddType application/x-httpd-php .php" >> /usr/local/apache2/conf/httpd.conf echo -e "DirectoryIndex index.php index.html" >> /usr/local/apache2/conf/httpd.conf echo -e "" >> /usr/local/apache2/conf/httpd.conf cd /usr/local/apache2/bin/ ./apachectl restart echo -e "<?php header('Content-Type:text/html; charset=utf-8'); \$con = mysql_pconnect('localhost', 'root', '123456', true); mysql_query(\"SET NAMES 'UTF8'\", \$con); mysql_select_db('project'); \$str = 'SELECT * FROM news WHERE 1;'; \$result = mysql_query(\$str, \$con); \$arr = array(); \$i = 0; if(\$result) { while(\$row = mysql_fetch_array(\$result)) { \$arr[\$i] = array( 'id' => \$row['id'], 'title' => \$row['title'], 'content' => \$row['content'], 'addtime' => \$row['addtime'] ); \$i++; } print_r(\$arr); } else { echo 'Connecting MySQL ERROR.'; } ?>" > /var/wwwroot/${your_stu_number}.com/index.php cd /usr/local/apache2/bin/ ./apachectl restart cd /root/view/ echo -e "" wget http://www.${your_stu_number}.com/index.php mv index.php ${your_stu_number}.com.php cd ~ echo -e "${your_stu_number}.com.php" cat /root/view/${your_stu_number}.com.php sleep 2s cd ~ rm -rf /var/run/yum.pid cd /usr/local/apache2/bin/ ./apachectl restart echo -e "" echo -e "\033[32m [ INFO ] Install PHP Successfully \033[0m" echo -e "\n\n" # install vsftpd echo -e "\033[32m [ INFO ] Install vsftpd ... \033[0m" yum install vsftpd -y sed -i "s/anonymous_enable=YES/anonymous_enable=NO/g" /etc/vsftpd/vsftpd.conf useradd -d /var/wwwroot/${your_name}.com/ -p '$6$sUx1urDQ$KRzEMQ4VvIonSoAQDtN4KJKjU1qcn6VVtYwxHuhSkNk6vxaevjyV9bIUTh77kdxW6CehQNggUSqKO5K2fjPHh/' ${your_name} useradd -d /var/wwwroot/${your_stu_number}.com/ -p '$6$sUx1urDQ$KRzEMQ4VvIonSoAQDtN4KJKjU1qcn6VVtYwxHuhSkNk6vxaevjyV9bIUTh77kdxW6CehQNggUSqKO5K2fjPHh/' ${your_stu_number} chown -R ${your_name}:${your_name} /var/wwwroot/${your_name}.com chown -R ${your_stu_number}:${your_stu_number} /var/wwwroot/${your_stu_number}.com chmod -R 777 /var/wwwroot/${your_name}.com chmod -R 777 /var/wwwroot/${your_stu_number}.com service vsftpd start cd ~ echo -e "\033[32m [ INFO ] Install vsftpd Successfully \033[0m" echo -e "\033[32m [ INFO ] All the installation is completed !!! \033[0m" echo -e "\n\n" # use ab to test virtual host performance echo -e "" echo -e "\033[32m [ INFO ] Use ab to Test Virtual Host Performance ... \033[0m" echo -e "" echo -e " [ \033[32mINFO\033[0m ] TEST START ..." echo -e "\n\n" # TEST START echo -e " [ \033[32mINFO\033[0m ] requests=10000 concurrency=100" echo -e " --------------------------------------- \n" /usr/local/apache2/bin/ab -n 10000 -c 100 http://www.${your_stu_number}.com/index.php echo -e "\n\n" echo -e " [ \033[32mINFO\033[0m ] requests=10000 concurrency=150" echo -e " --------------------------------------- \n" /usr/local/apache2/bin/ab -n 10000 -c 150 http://www.${your_stu_number}.com/index.php echo -e "\n\n" echo -e " [ \033[32mINFO\033[0m ] requests=10000 concurrency=200" echo -e " --------------------------------------- \n" /usr/local/apache2/bin/ab -n 10000 -c 200 http://www.${your_stu_number}.com/index.php echo -e "\n\n" echo -e " [ \033[32mINFO\033[0m ] requests=15000 concurrency=100" echo -e " --------------------------------------- \n" /usr/local/apache2/bin/ab -n 15000 -c 100 http://www.${your_stu_number}.com/index.php echo -e "\n\n" echo -e " [ \033[32mINFO\033[0m ] requests=15000 concurrency=200" echo -e " --------------------------------------- \n" /usr/local/apache2/bin/ab -n 15000 -c 200 http://www.${your_stu_number}.com/index.php # END # install sl START clear mkdir /root/epel/ cd /root/epel/ wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm rpm -ivh epel-release* yum repolist -y yum install sl -y clear sl cd ~ # install sl END # Successfully clear echo -e "" time2=`date +%s` duration=`expr ${time2} - ${time1}` nowDate=`date` echo -e " [\033[32m INFO \033[0m] The time is now: \033[31m${nowDate}\033[0m" echo -e "" echo -e " [\033[32m INFO \033[0m] The installation takes a total of \033[31m${duration}\033[0m seconds." echo -e "" echo -e " [\033[32m INFO \033[0m] Please SSH this IP address:\033[31m 192.168.${your_ip_3}.${your_ip_4} \033[0m" echo -e "" echo -e " [\033[32m INFO \033[0m] The Script Has Finished Running, Good Luck !!!" echo -e "" #END