网络服务阶段——作业
一、准备三台虚拟机
IP分别设置为192.168.x.50、192.168.x.150、192.168.x.250,x自定义
[ipv4]
method=manual
address=192.168.69.50/24[ipv4]
method=manual
address=192.168.69.150/24[ipv4]
method=manual
address=192.168.69.250/24
二、免密登录
请配置三台虚拟机相互间的免密登录,用root用户即可;
注:确保三台虚拟机关闭防火墙,SElinux,配置好本地源
在xshell中打开工具——发送到所有会话,这样就不用重复编写三回了,区别是每次发公钥的时候要给自己也发一下,避免其他两台缺少本台公钥
[root@localhost ~]# ssh-keygen -t rsa -b 2048[root@localhost ~]# ls .ssh/
id_rsa id_rsa.pub[root@localhost ~]# ssh-copy-id root@192.168.69.50Are you sure you want to continue connecting (yes/no/[fingerprint])? yes #确定root@192.168.69.50's password: #输入服务端密码[root@localhost ~]# ssh-copy-id root@192.168.69.150Are you sure you want to continue connecting (yes/no/[fingerprint])? yes #确定root@192.168.69.150's password: #输入服务端密码[root@localhost ~]# ssh-copy-id root@192.168.69.250Are you sure you want to continue connecting (yes/no/[fingerprint])? yes #确定root@192.168.69.250's password: #输入服务端密码
三、搭建apache服务器
请在50机器上搭建Apache服务:
1)请配置基于域名的虚拟主机:
一个虚拟主机:网站名字是www1.2307.com,网页根目录是/data/apache/www1,默认网页文件的内容是www1;
另一个虚拟主机:网站名字是www2.2307.com,网页根目录是/data/apache/www2,默认网页文件内容是www2;
步骤一:首先安装apache服务器,这里我用的是rpm包安装
[root@localhost ~]# dnf -y install httpd
步骤二:查看有没有vhost模块
[root@localhost conf.d]# httpd -M | grep vhost
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using ::1. Set the 'ServerName' directive globally to suppress this messagevhost_alias_module (shared)
步骤三:因为rpm包安装的apache服务没有虚拟主机的子配置文件,因此要找模版复制到conf.d目录下,然后才能进行虚拟主机的配置
[root@localhost conf.d]# cp -a /usr/share/doc/httpd-core/httpd-vhosts.conf .
步骤四:配置虚拟主机
[root@localhost ~]# vi /etc/httpd/conf.d/httpd-vhosts.conf<VirtualHost *:80>ServerAdmin webmaster@dummy-host2.example.comDocumentRoot "/data/apache/www2"ServerName www2.2307.comErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>ServerAdmin webmaster@dummy-host.example.comDocumentRoot "/data/apache/www1"ServerName www1.2307.comServerAlias www.dummy-host.example.comErrorLog "/var/log/httpd/dummy-host.example.com-error_log"CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>
步骤五:开放根目录的权限
[root@localhost ~]# vi /etc/httpd/conf/httpd.conf
<Directory />AllowOverride allRequire all granted
</Directory>
步骤六:创建网页根目录文件和页面
[root@localhost ~]# mkdir -p /data/apache/www1
[root@localhost ~]# mkdir -p /data/apache/www2
[root@localhost ~]# echo "www1" >> /data/apache/www1/index.html
[root@localhost ~]# echo "www2" >> /data/apache/www2/index.html
2)请配置地址跳转功能:
实现在客户端访问www1.2307.com时,自动跳转到www2.2307.com;
步骤一:打开主配置文件,设置虚拟主机的目录权限
[root@localhost ~]# vi /etc/httpd/conf/httpd.conf<Directory "/data/apache/www1">RewriteEngine onRewriteCond %{HTTP_HOST} www1.2307.comRewriteRule ^(.*)$ http://www2.2307.com/$1 [R=301,L]
</Directory>
步骤二:重启服务器,查看浏览器
[root@localhost ~]# systemctl restart httpd
输入www1.2307.com,自动跳转到www2.2307.com
四、搭建tomcat服务器
请在150机器上搭建Tomcat服务:
1)请配置网站域名为:www3.2307.com;
2)请配置网站监听端口:2307
3)请修改默认网页文件内容为:www3;
步骤一:参考之前搭建tomcat的文件,先解包jdk和tomcat,放在/usr/local/目录下
[root@localhost ~]# cd ltmj-r9
[root@localhost ltmj-r9]# ls
apache-tomcat-9.0.48.tar.gz jdk-11.0.11_linux-x64_bin.tar.gz mariadb-10.6.17.tar.gz mypress.war mysql-connector-java-5.1.24.jar[root@localhost ltmj-r9]# tar -xf jdk-11.0.11_linux-x64_bin.tar.gz
[root@localhost ltmj-r9]# cp -a jdk-11.0.11 /usr/local/jkd11
[root@localhost ltmj-r9]# tar -xf apache-tomcat-9.0.48.tar.gz
[root@localhost ltmj-r9]# cp -a apache-tomcat-9.0.48 /usr/local/tomcat9
步骤二:配置java的环境变量
[root@localhost tomcat9]# vi /etc/profileexport JAVA_HOME=/usr/local/jdk11
PATH=$PATH:$JAVA_HOME/bin
步骤三:测试是否配置完成
[root@localhost ~]# java -version
java version "11.0.11" 2021-04-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode)
步骤四:在主配置文件中修改网站域名和端口
[root@localhost ~]# vi /usr/local/tomcat9/conf/server.xml <Connector port="2307" protocol="HTTP/1.1"<Engine name="Catalina" defaultHost="www3.2307.com"><Host name="www3.2307.com" appBase="webapps"unpackWARs="true" autoDeploy="true">
步骤五:修改默认网页文件内容
[root@localhost ~]# echo "www3" > /usr/local/tomcat9/webapps/ROOT/index.jsp
步骤六:启动服务,浏览器测试
[root@localhost tomcat9]# ./bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat9
Using CATALINA_HOME: /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME: /usr/local/jdk11
Using CLASSPATH: /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
五、搭建nginx服务器
请在250机器上搭建nginx服务:
1)请配置网站域名为:nginx.2307.com;
2)请配置反向代理功能,实现可以到www2.2307.com网站和www3.2307.com网站,比例是2:3;
3)请为上述功能实现加密传输,协议为h2;
4)请实现用nginx服务的IP或域名都能访问到上面的代理内容;
步骤一:安装nginx服务,这里我用的还是rpm包
[root@localhost ~]# dnf -y install nginx
步骤二:在主配置文件中修改网站域名
server {listen 80;listen [::]:80;server_name nginx.2307.com;root /usr/share/nginx/html;
步骤三:配置反向代理,为方便代理到www2.2307.com上,还需要在69.50服务器的子配置文件中将该域名所执行的虚拟机提前。
upstream daili {server 192.168.69.50 weight=2;server 192.168.69.150:2307 weight=3;}server {listen 80;listen [::]:80;server_name nginx.2307.com;root /usr/share/nginx/html;#这里我顺便做了加密跳转,只要访问nginx.2307.com,就会跳转到https://nginx.2307.com页面rewrite ^(.*)$ https://nginx.2307.com$1 permanent;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;error_page 404 /404.html;#这里直接匹配根目录,代理跳转到69.50和69.150两台服务器上location / {proxy_pass http://daili;}location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}
步骤四:因为rpm包的nginx直接用的h2加密,所以直接解开加密虚拟主 机,配置好域名和跳转路径就可以了
server {listen 443 ssl http2;listen [::]:443 ssl http2;server_name nginx.2307.com;root /usr/share/nginx/html;ssl_certificate "/etc/nginx/cert.pem";ssl_certificate_key "/etc/nginx/cert.key";ssl_session_cache shared:SSL:1m;ssl_session_timeout 10m;ssl_ciphers PROFILE=SYSTEM;ssl_prefer_server_ciphers on;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {proxy_pass http://daili;}error_page 404 /404.html;location = /40x.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}
步骤五:生成认证
[root@localhost nginx]# openssl genrsa -out cert.key 2048
[root@localhost nginx]# openssl req -new -key cert.key -out cert.csr
[root@localhost nginx]# openssl x509 -req -days 365 -sha256 -in cert.csr -signkey cert.key -out cert.pem
步骤六:开启nginx服务,浏览器测试
六、DNS域名解析
在50机器上搭建DNS服务器实现上述域名的解析(只配正向);
步骤一:安装dns服务器
[root@localhost ~]# dnf -y install bind
步骤二:修改主配置文件,开放监听端口和主机
[root@localhost ~]# vi /etc/named.conf options {listen-on port 53 { any; };allow-query { any; };
步骤三:修改区域配置文件
[root@localhost ~]# vi /etc/named.rfc1912.zones
zone "2307.com" IN {type master;file "named.localhost";allow-update { none; };
};
步骤四:修改数据配置文件
[root@localhost ~]# vi /var/named/named.localhost$TTL 1D
@ IN SOA @ rname.invalid. (0 ; serial1D ; refresh1H ; retry1W ; expire3H ) ; minimumNS dns.2307.com.
dns A 192.168.69.50
www1 A 192.168.69.50
www2 A 192.168.69.50
www3 A 192.168.69.150
nginx A 192.168.69.250
步骤五:启动服务,安装bind-utils包,使用nslookup命令检测效果(记得把dns地址给网卡配上)
[root@localhost ~]# systemctl start named
[root@localhost ~]# nslookup www1.2307.com
Server: 192.168.69.50
Address: 192.168.69.50#53Name: www1.2307.com
Address: 192.168.69.50[root@localhost ~]# nslookup www2.2307.com
Server: 192.168.69.50
Address: 192.168.69.50#53Name: www2.2307.com
Address: 192.168.69.50[root@localhost ~]# nslookup www3.2307.com
Server: 192.168.69.50
Address: 192.168.69.50#53Name: www3.2307.com
Address: 192.168.69.150[root@localhost ~]# nslookup nginx.2307.com
Server: 192.168.69.50
Address: 192.168.69.50#53Name: nginx.2307.com
Address: 192.168.69.250