当前位置:   article > 正文

web 服务搭建_搭建web服务

搭建web服务

目录

环境搭建

一、在Nginx服务器上搭建LNMP服务,并且能够对外提供Discuz论坛服务,在Web1、Web2服务器上搭建Tomcat 服务

(一)Nginx服务器上搭建LNMP服务

①编译安装nginx

②编译安装mysql

③ 编译安装php软件

④配置 Nginx支持PHP解析

验证数据库工作是否正常

⑤安装论坛

(二)Web1、Web2服务器上搭建Tomcat 服务

①安装Oracle JDK(即部署java环境)

②安装tomcat

二、为nginx服务配置虚拟主机,新增两个域名 www.kgc.com 和 www.benet.com,使用http://www.kgc.com/index.php可访问上一题的Discuz论坛页面。使用http://www.benet.com则访问/var/www/html目录中的index.html文件的内容,内容自定义

①nginx服务配置虚拟主机

②创建对应文件夹

③真机配置:

④检测:

三、对基于www.benet.com域名的虚拟机主机的nginx服务调优:隐藏nginx版本号,缓存静态图片网页时间为1天,设置防盗链功能

①配置文件

②客户机检测:

③防盗链检测:

四、网关服务器搭建NFS服务,提供的文件系统使用LVM类型,共享目录名称为/opt/nfs;要求根据日期对Discuz论坛服务的访问日志进行日志分割,要求每天生成一份日志文件,保存到NFS服务共享的目录内。

①文件系统使用LVM类型

方法一:新加盘做一个LVM类型

方法二:因为我们根本身就是逻辑卷;所以就不另外添加磁盘了,就在根下面做

②设置共享目录

③nginx 服务挂载共享目录

④日志分割

五、要求配置location匹配请求地址http://www.kgc.com/test/XXXX,使用户访问该路径下的文件时返回/var/share/nginx/html/目录下的文件内容。要求使用rewrite将使用域名www.benet.com 请求以 .php 结尾的访问都跳转到域名www.kgc.com上,而且后面的参数保持不变,比如访问http://www.benet.com/bbs/index.php跳转到http://www.kgc.com/bbs/index.php

①Nginx服务配置

②创建被访问文件内容

③检测:

六、在Nginx服务器上对基于www.benet.com域名的虚拟机主机设置动静分离由nginx提供静态页面服务,将对 .jsp文件的动态页面请求转发到Tomcat 服务器处理,并实现负载均衡

①Nginx服务器配置

②配置Tomcat服务器

③浏览器检测:

访问动态页面:

访问静态页面:

七、在网关服务器上设置SNAT/DNAT,使client使用网关服务器的ens36接口的IP地址访问也可实现上一题的效果。

①客户机地址配置

②网关服务器配置

③做SNAT与DNAT

④客户机浏览器检测


拓扑图:

环境搭建


Centos7-5作为Client(12.0.0.12/24);Centos7-1作为网关服务器(配置两块网卡ens33 192.168.246.7/24,ens36 12.0.0.1/24);Centos7-2作为Web1(192.168.246.8/24 提供web1服务);Centos7-3作为Web2(192.168.246.9/24 提供web2服务);Centos7-4作为Nginx服务器(192.168.246.10/24)

五台机器都关闭防火墙、防护

7-1网关服务器配置双网卡

  1. [root@localhost ~]#ifconfig
  2. [root@localhost ~]#cd /etc/sysconfig/network-scripts/
  3. [root@localhost network-scripts]#ls
  4. ifcfg-ens33 ifdown-ipv6 ifdown-TeamPort ifup-ippp ifup-routes network-functions
  5. ifcfg-lo ifdown-isdn ifdown-tunnel ifup-ipv6 ifup-sit network-functions-ipv6
  6. ifdown ifdown-post ifup ifup-isdn ifup-Team
  7. ifdown-bnep ifdown-ppp ifup-aliases ifup-plip ifup-TeamPort
  8. ifdown-eth ifdown-routes ifup-bnep ifup-plusb ifup-tunnel
  9. ifdown-ib ifdown-sit ifup-eth ifup-post ifup-wireless
  10. ifdown-ippp ifdown-Team ifup-ib ifup-ppp init.ipv6-global
  11. [root@localhost network-scripts]#cp ifcfg-ens33 ifcfg-ens36
  12. [root@localhost network-scripts]#vim ifcfg-ens36

一、在Nginx服务器上搭建LNMP服务,并且能够对外提供Discuz论坛服务,在Web1、Web2服务器上搭建Tomcat 服务

(一)Nginx服务器上搭建LNMP服务

①编译安装nginx

  1. [root@zzzcentos4 ~]#yum -y install pcre-devel zlib-devel gcc gcc-c++ make
  2. [root@zzzcentos4 ~]#cd /opt
  3. [root@zzzcentos4 opt]#ls
  4. rh
  5. [root@zzzcentos4 opt]#rz -E
  6. rz waiting to receive.
  7. [root@zzzcentos4 opt]#rz -E
  8. rz waiting to receive.
  9. [root@zzzcentos4 opt]#rz -E
  10. rz waiting to receive.
  11. [root@zzzcentos4 opt]#rz -E
  12. rz waiting to receive.
  13. [root@zzzcentos4 opt]#ls
  14. Discuz_X3.4_SC_UTF8.zip nginx-1.22.0.tar.gz rh
  15. mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2
  16. [root@zzzcentos4 opt]#tar xf nginx-1.22.0.tar.gz
  17. [root@zzzcentos4 opt]#cd nginx-1.22.0/
  18. [root@zzzcentos4 nginx-1.22.0]#useradd -M -s /sbin/nologin nginx
  19. [root@zzzcentos4 nginx-1.22.0]#./configure \
  20. > --prefix=/usr/local/nginx \
  21. > --user=nginx \
  22. > --group=nginx \
  23. > --with-http_stub_status_module
  24. [root@zzzcentos4 nginx-1.22.0]#make -j 2
  25. [root@zzzcentos4 nginx-1.22.0]#make install
  1. [root@zzzcentos4 nginx-1.22.0]#echo $PATH
  2. /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  3. [root@zzzcentos4 nginx-1.22.0]#
  4. [root@zzzcentos4 nginx-1.22.0]#ln -s /usr/local/nginx/sbin/nginx /usr/bin
  5. #为了使用nginx命令可以补全 (做到$PATH下面就行)
  6. [root@zzzcentos4 nginx-1.22.0]#tee /lib/systemd/system/nginx.service <<eof
  7. > [Unit]
  8. > Description=nginx
  9. > After=network.target
  10. > [Service]
  11. > Type=forking
  12. > PIDFile=/usr/local/nginx/logs/nginx.pid
  13. > ExecStart=/usr/local/nginx/sbin/nginx
  14. > ExecReload=/bin/kill -1 $MAINPID
  15. > ExecStop=/bin/kill -3 $MAINPID
  16. > PrivateTmp=true
  17. > [Install]
  18. > WantedBy=multi-user.target
  19. > eof
  20. [Unit]
  21. Description=nginx
  22. After=network.target
  23. [Service]
  24. Type=forking
  25. PIDFile=/usr/local/nginx/logs/nginx.pid
  26. ExecStart=/usr/local/nginx/sbin/nginx
  27. ExecReload=/bin/kill -1
  28. ExecStop=/bin/kill -3
  29. PrivateTmp=true
  30. [Install]
  31. WantedBy=multi-user.target
  32. [root@zzzcentos4 nginx-1.22.0]#systemctl daemon-reload
  33. [root@zzzcentos4 nginx-1.22.0]#systemctl enable --now nginx.service
  34. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
  35. [root@zzzcentos4 nginx-1.22.0]#systemctl start nginx
  36. [root@zzzcentos4 nginx-1.22.0]#systemctl status nginx

②编译安装mysql

  1. [root@zzzcentos4 nginx-1.22.0]#cd /opt
  2. [root@zzzcentos4 opt]#ls
  3. Discuz_X3.4_SC_UTF8.zip nginx-1.22.0 php-7.1.10.tar.bz2
  4. mysql-boost-5.7.20.tar.gz nginx-1.22.0.tar.gz rh
  5. [root@zzzcentos4 opt]#yum -y install \
  6. > ncurses \
  7. > ncurses-devel \
  8. > bison \
  9. > cmake
  10. [root@zzzcentos4 opt]#yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel
  11. [root@zzzcentos4 opt]#useradd -M -s /sbin/nologin mysql
  12. [root@zzzcentos4 opt]#ls
  13. Discuz_X3.4_SC_UTF8.zip nginx-1.22.0 php-7.1.10.tar.bz2
  14. mysql-boost-5.7.20.tar.gz nginx-1.22.0.tar.gz rh
  15. [root@zzzcentos4 opt]#tar xf mysql-boost-5.7.20.tar.gz
  16. [root@zzzcentos4 opt]#ls
  17. Discuz_X3.4_SC_UTF8.zip mysql-boost-5.7.20.tar.gz nginx-1.22.0.tar.gz rh
  18. mysql-5.7.20 nginx-1.22.0 php-7.1.10.tar.bz2
  19. [root@zzzcentos4 opt]#cd mysql-5.7.20/
  20. [root@zzzcentos4 mysql-5.7.20]#
  21. [root@zzzcentos4 mysql-5.7.20]#cmake \
  22. > -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  23. > -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
  24. > -DSYSCONFDIR=/etc \
  25. > -DSYSTEMD_PID_DIR=/usr/local/mysql \
  26. > -DDEFAULT_CHARSET=utf8 \
  27. > -DDEFAULT_COLLATION=utf8_general_ci \
  28. > -DWITH_EXTRA_CHARSETS=all \
  29. > -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  30. > -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  31. > -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  32. > -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
  33. > -DMYSQL_DATADIR=/usr/local/mysql/data \
  34. > -DWITH_BOOST=boost \
  35. > -DWITH_SYSTEMD=1

  1. [root@zzzcentos4 mysql-5.7.20]#vim /etc/my.cnf
  2. [client]
  3. port = 3306
  4. socket=/usr/local/mysql/mysql.sock
  5. [mysqld]
  6. user = mysql
  7. basedir=/usr/local/mysql
  8. datadir=/usr/local/mysql/data
  9. port = 3306
  10. character-set-server=utf8
  11. pid-file = /usr/local/mysql/mysqld.pid
  12. socket=/usr/local/mysql/mysql.sock
  13. bind-address = 0.0.0.0
  14. skip-name-resolve
  15. max_connections=2048
  16. default-storage-engine=INNODB
  17. max_allowed_packet=16M
  18. server-id = 1
  19. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,
  20. NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
  21. [root@zzzcentos4 mysql-5.7.20]#chown -R mysql:mysql /usr/local/mysql/
  22. [root@zzzcentos4 mysql-5.7.20]#chown mysql:mysql /etc/my.cnf
  23. [root@zzzcentos4 mysql-5.7.20]#
  24. [root@zzzcentos4 mysql-5.7.20]#echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
  25. [root@zzzcentos4 mysql-5.7.20]#source /etc/profile
  26. [root@zzzcentos4 mysql-5.7.20]#cd /usr/local/mysql/bin/
  27. [root@zzzcentos4 bin]#./mysqld \
  28. > --initialize-insecure \
  29. > --user=mysql \
  30. > --basedir=/usr/local/mysql \
  31. > --datadir=/usr/local/mysql/data
  32. [root@zzzcentos4 bin]#cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
  33. [root@zzzcentos4 bin]#systemctl daemon-reload
  34. [root@zzzcentos4 bin]#systemctl start mysqld.service
  35. [root@zzzcentos4 bin]#systemctl status mysqld.service

③ 编译安装php软件

  1. [root@zzzcentos4 bin]#cd /opt
  2. [root@zzzcentos4 opt]#ls
  3. Discuz_X3.4_SC_UTF8.zip mysql-boost-5.7.20.tar.gz nginx-1.22.0.tar.gz rh
  4. mysql-5.7.20 nginx-1.22.0 php-7.1.10.tar.bz2
  5. [root@zzzcentos4 opt]#tar xf php-7.1.10.tar.bz2
  6. [root@zzzcentos4 opt]#cd php-7.1.10/
  7. [root@zzzcentos4 php-7.1.10]#yum -y install gd \
  8. > libjpeg libjpeg-devel \
  9. > libpng libpng-devel \
  10. > freetype freetype-devel \
  11. > libxml2 libxml2-devel \
  12. > zlib zlib-devel \
  13. > curl curl-devel \
  14. > openssl openssl-devel
  15. [root@zzzcentos4 php-7.1.10]#./configure \
  16. > --prefix=/usr/local/php \
  17. > --with-mysql-sock=/usr/local/mysql/mysql.sock \
  18. > --with-mysqli \
  19. > --with-zlib \
  20. > --with-curl \
  21. > --with-gd \
  22. > --with-jpeg-dir \
  23. > --with-png-dir \
  24. > --with-freetype-dir \
  25. > --with-openssl \
  26. > --enable-fpm \
  27. > --enable-mbstring \
  28. > --enable-xml \
  29. > --enable-session \
  30. > --enable-ftp \
  31. > --enable-pdo \
  32. > --enable-tokenizer \
  33. > --enable-zip
  34. [root@zzzcentos4 php-7.1.10]#make -j 2
  35. [root@zzzcentos4 php-7.1.10]#make install

  1. [root@zzzcentos4 etc]#ls
  2. pear.conf php-fpm.conf php-fpm.conf.default php-fpm.d
  3. [root@zzzcentos4 etc]#cd php-fpm.d/
  4. [root@zzzcentos4 php-fpm.d]#ls
  5. www.conf.default
  6. [root@zzzcentos4 php-fpm.d]#cp www.conf.default www.conf
  7. [root@zzzcentos4 php-fpm.d]#ls
  8. www.conf www.conf.default
  9. [root@zzzcentos4 php-fpm.d]#ln -s /usr/local/php/bin/* /usr/local/bin/
  10. [root@zzzcentos4 php-fpm.d]#ln -s /usr/local/php/sbin/* /usr/local/sbin/
  11. [root@zzzcentos4 php-fpm.d]#
  12. [root@zzzcentos4 php-fpm.d]#cd /opt/php-7.1.10/sapi/fpm
  13. [root@zzzcentos4 fpm]#cp php-fpm.service /usr/lib/systemd/system/php-fpm.service
  14. [root@zzzcentos4 fpm]#systemctl daemon-reload
  15. [root@zzzcentos4 fpm]#systemctl start php-fpm.service
  16. [root@zzzcentos4 fpm]#systemctl status php-fpm.service

④配置 Nginx支持PHP解析

[root@zzzcentos4 fpm]#vim /usr/local/nginx/conf/nginx.conf

  1. [root@zzzcentos4 fpm]#nginx -s reload
  2. [root@zzzcentos4 fpm]#systemctl restart nginx.service
  3. [root@zzzcentos4 fpm]#cd /usr/local/nginx/html/
  4. [root@zzzcentos4 html]#vim /usr/local/nginx/html/index.php
  5. [root@zzzcentos4 html]#cat /usr/local/nginx/html/index.php
  6. <?php
  7. phpinfo();
  8. ?>
  9. [root@zzzcentos4 html]#
验证数据库工作是否正常
  1. [root@zzzcentos4 html]#mysql -uroot -pabc123
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 5
  5. Server version: 5.7.20 Source distribution
  6. Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> CREATE DATABASE bbs;
  12. Query OK, 1 row affected (0.02 sec)
  13. mysql> GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
  14. Query OK, 0 rows affected, 1 warning (0.03 sec)
  15. mysql> GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
  16. Query OK, 0 rows affected, 2 warnings (0.00 sec)
  17. mysql> flush privileges;
  18. Query OK, 0 rows affected (0.01 sec)
  19. mysql> quit
  20. Bye
  21. [root@zzzcentos4 html]#vim /usr/local/nginx/html/index.php
  22. <?php
  23. $link=mysqli_connect('192.168.246.10','bbsuser','admin123');
  24. if($link) echo "<h1>Success!!</h1>";
  25. else echo "Fail!!";
  26. ?>

⑤安装论坛

  1. [root@zzzcentos4 opt]#ls
  2. dir_SC_UTF8 mysql-5.7.20 nginx-1.22.0 php-7.1.10 rh
  3. Discuz_X3.4_SC_UTF8.zip mysql-boost-5.7.20.tar.gz nginx-1.22.0.tar.gz php-7.1.10.tar.bz2 说明.htm
  4. [root@zzzcentos4 opt]#cd dir_SC_UTF8/
  5. [root@zzzcentos4 dir_SC_UTF8]#ls
  6. readme upload utility
  7. [root@zzzcentos4 dir_SC_UTF8]#cp -r upload/ /usr/local/nginx/html/bbs/
  8. [root@zzzcentos4 dir_SC_UTF8]#cd /usr/local/nginx/html/bbs/
  9. [root@zzzcentos4 bbs]#ls
  10. admin.php archiver crossdomain.xml forum.php index.php member.php portal.php source uc_client
  11. api config data group.php install misc.php robots.txt static uc_server
  12. api.php connect.php favicon.ico home.php m plugin.php search.php template
  13. [root@zzzcentos4 bbs]#chown -R nginx.nginx ./config/
  14. [root@zzzcentos4 bbs]#chown -R nginx.nginx ./data/
  15. [root@zzzcentos4 bbs]#chown -R nginx.nginx ./uc_client/
  16. [root@zzzcentos4 bbs]#chown -R nginx.nginx ./uc_server/
  17. [root@zzzcentos4 bbs]#chmod -R 777 ./config/
  18. [root@zzzcentos4 bbs]#chmod -R 777 ./data/
  19. [root@zzzcentos4 bbs]#chmod -R 777 ./uc_client/
  20. [root@zzzcentos4 bbs]#chmod -R 777 ./uc_server/
  21. [root@zzzcentos4 bbs]#

(二)Web1、Web2服务器上搭建Tomcat 服务

①安装Oracle JDK(即部署java环境)

  1. [root@zzzcentos2 ~]#cd /opt/
  2. [root@zzzcentos2 opt]#ls
  3. rh
  4. [root@zzzcentos2 opt]#rz -E
  5. rz waiting to receive.
  6. [root@zzzcentos2 opt]#rz -E
  7. rz waiting to receive.
  8. [root@zzzcentos2 opt]#ls
  9. apache-tomcat-9.0.16.tar.gz jdk-8u291-linux-x64.tar.gz rh
  10. [root@zzzcentos2 opt]#ls
  11. apache-tomcat-9.0.16.tar.gz jdk-8u291-linux-x64.tar.gz rh
  12. [root@zzzcentos2 opt]#systemctl stop firewalld
  13. [root@zzzcentos2 opt]#setenforce 0
  14. setenforce: SELinux is disabled
  15. [root@zzzcentos2 opt]#java -version
  16. openjdk version "1.8.0_131"
  17. OpenJDK Runtime Environment (build 1.8.0_131-b12)
  18. OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
  19. [root@zzzcentos2 opt]#tar xf jdk-8u291-linux-x64.tar.gz -C /usr/local/
  20. [root@zzzcentos2 opt]#cd /usr/local/
  21. [root@zzzcentos2 local]#ls
  22. bin etc games include jdk1.8.0_291 lib lib64 libexec sbin share src
  23. [root@zzzcentos2 local]#ln -s jdk1.8.0_291/ jdk
  24. [root@zzzcentos2 local]#vim /etc/profile.d/jdk.sh
  25. [root@zzzcentos2 local]#cat /etc/profile.d/jdk.sh
  26. export JAVA_HOME=/usr/local/jdk
  27. export PATH=$JAVA_HOME/bin:$PATH
  28. export JRE_HOME=$JAVA_HOME/jre
  29. export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
  30. [root@zzzcentos2 local]#source /etc/pro
  31. profile profile.d/ protocols
  32. [root@zzzcentos2 local]#source /etc/profile.d/jdk.sh
  33. [root@zzzcentos2 local]#java -version
  34. java version "1.8.0_291"
  35. Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
  36. Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
  37. [root@zzzcentos2 local]#

②安装tomcat

  1. [root@zzzcentos2 local]#cd /opt/
  2. [root@zzzcentos2 opt]#ls
  3. apache-tomcat-9.0.16.tar.gz jdk-8u291-linux-x64.tar.gz rh
  4. [root@zzzcentos2 opt]#tar xf apache-tomcat-9.0.16.tar.gz
  5. [root@zzzcentos2 opt]#ls
  6. apache-tomcat-9.0.16 apache-tomcat-9.0.16.tar.gz jdk-8u291-linux-x64.tar.gz rh
  7. [root@zzzcentos2 opt]#cp -r apache-tomcat-9.0.16 /usr/local/
  8. [root@zzzcentos2 opt]#cd /usr/local/
  9. [root@zzzcentos2 local]#ls
  10. apache-tomcat-9.0.16 bin etc games include jdk jdk1.8.0_291 lib lib64 libexec sbin share src
  11. [root@zzzcentos2 local]#ln -s apache-tomcat-9.0.16/ tomcat
  12. [root@zzzcentos2 local]#useradd -s /sbin/nologin -M tomcat
  13. [root@zzzcentos2 local]#chown -R tomcat:tomcat tomcat/
  14. [root@zzzcentos2 local]#vim /usr/lib/systemd/system/tomcat.service
  15. [root@zzzcentos2 local]#cat /usr/lib/systemd/system/tomcat.service
  16. [Unit]
  17. Description=Tomcat
  18. After=syslog.target network.target
  19. [Service]
  20. Type=forking
  21. ExecStart=/usr/local/tomcat/bin/startup.sh
  22. ExecStop=/usr/local/tomcat/bin/shutdown.sh
  23. RestartSec=3
  24. PrivateTmp=true
  25. User=tomcat
  26. Group=tomcat
  27. [Install]
  28. WantedBy=multi-user.target
  29. [root@zzzcentos2 local]#systemctl daemon-reload
  30. [root@zzzcentos2 local]#systemctl start tomcat
  31. [root@zzzcentos2 local]#systemctl status tomcat

二、为nginx服务配置虚拟主机,新增两个域名 www.kgc.com 和 www.benet.com,使用http://www.kgc.com/index.php可访问上一题的Discuz论坛页面。使用http://www.benet.com则访问/var/www/html目录中的index.html文件的内容,内容自定义

①nginx服务配置虚拟主机

[root@zzzcentos4 ~]#vim /usr/local/nginx/conf/nginx.conf

搭好论坛会生成它,注意题目访问www.kgc.com跳到论坛,注意位置,在www.kgc.com域名下面

再编辑域名www.benet.com

  1. server {
  2. listen 80;
  3. server_name www.benet.com;
  4. root /var/www/html;
  5. }

②创建对应文件夹

  1. [root@zzzcentos4 conf]#mkdir -p /var/www/html
  2. [root@zzzcentos4 conf]#cd /var/www/html/
  3. [root@zzzcentos4 html]#ls
  4. [root@zzzcentos4 html]#echo hello /var/www/html/ > index.html
  5. [root@zzzcentos4 html]#cat index.html
  6. hello /var/www/html/

③真机配置:

C:\Windows\System32\drivers\etc

④检测:

使用http://www.benet.com则访问/var/www/html/目录中的index.html文件的内容

使用http://www.kgc.com/index.php可访问上一题的Discuz论坛页面

三、对基于www.benet.com域名的虚拟机主机的nginx服务调优:隐藏nginx版本号,缓存静态图片网页时间为1天,设置防盗链功能

①配置文件

[root@zzzcentos4 ~]#vim /usr/local/nginx/conf/nginx.conf
  1. server {
  2. listen 80;
  3. server_name www.benet.com;
  4. root /var/www/html;
  5. expires 1d;
  6. server_tokens off;
  7. location ~* \.(jpg|gif|jepg|bmp|png)$ {
  8. valid_referers none bloaced *.benet.com benet.com;
  9. if ( $invalid_referer ) {
  10. return 403;
  11. }
  12. }
  13. }

②客户机检测:

③防盗链检测:

使用另一台机器检测,就随意选7-2吧

  1. [root@zzzcentos2 ~]#yum install httpd -y
  2. [root@zzzcentos2 ~]#cd /var/www/html/
  3. [root@zzzcentos2 html]#systemctl start httpd
  4. [root@zzzcentos2 html]#vim index.html
  5. [root@zzzcentos2 html]#cat index.html #检测页面
  6. <html>
  7. <body>
  8. <h1>this is yun</h1>
  9. <img src="http://www.benet.com/a.jpg"/>
  10. </body>
  11. </html>
  12. [root@zzzcentos2 html]#systemctl restart httpd.service
  13. [root@zzzcentos2 html]#rz -E
  14. rz waiting to receive.
  15. [root@zzzcentos2 html]#ls
  16. c38f51c57937c53c60ebba856b53cc3.png index.html
  17. [root@zzzcentos2 html]#mv c38f51c57937c53c60ebba856b53cc3.png a.jpg
  18. [root@zzzcentos2 html]#ls
  19. a.jpg index.html
  20. [root@zzzcentos2 html]#

没设置反盗链的时候,是可以访问到图片的,设置放盗链后再次访问,返回设置的403

四、网关服务器搭建NFS服务,提供的文件系统使用LVM类型,共享目录名称为/opt/nfs;要求根据日期对Discuz论坛服务的访问日志进行日志分割,要求每天生成一份日志文件,保存到NFS服务共享的目录内。

Centos7-1作为网关服务器

文件系统使用LVM类型

方法一:新加盘做一个LVM类型

  1. [root@localhost ~]#lsblk
  2. [root@localhost ~]#echo "- - -" > /sys/class/scsi_host/host0/scan;echo "- - -" > /sys/class/scsi_host/host1/scan;echo "- - -" > /sys/class/scsi_host/host2/scan
  3. [root@localhost ~]#lsblk
  4. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  5. sda 8:0 0 60G 0 disk
  6. ├─sda1 8:1 0 5G 0 part /boot
  7. └─sda2 8:2 0 54G 0 part
  8. ├─centos-root 253:0 0 50G 0 lvm /
  9. └─centos-swap 253:1 0 4G 0 lvm [SWAP]
  10. sdb 8:16 0 20G 0 disk
  11. sdc 8:32 0 20G 0 disk
  12. sr0 11:0 1 4.2G 0 rom
  13. [root@localhost ~]#pvcreate /dev/sdb /dev/sdc
  14. #建物理卷
  15. Physical volume "/dev/sdb" successfully created.
  16. Physical volume "/dev/sdc" successfully created.
  17. [root@localhost ~]#vgcreate vg /dev/sdb /dev/sdc
  18. #建卷组
  19. Volume group "vg" successfully created
  20. [root@localhost ~]#lvcreate -n lvm -L 10G /dev/vg
  21. #建逻辑卷 指定名称lvm 指定大小30G 存放在/dev/vg下
  22. Logical volume "lvm" created.
  23. [root@localhost ~]#mkfs.xfs /dev/vg/lvm
  24. meta-data=/dev/vg/lvm isize=512 agcount=4, agsize=655360 blks
  25. = sectsz=512 attr=2, projid32bit=1
  26. = crc=1 finobt=0, sparse=0
  27. data = bsize=4096 blocks=2621440, imaxpct=25
  28. = sunit=0 swidth=0 blks
  29. naming =version 2 bsize=4096 ascii-ci=0 ftype=1
  30. log =internal log bsize=4096 blocks=2560, version=2
  31. = sectsz=512 sunit=0 blks, lazy-count=1
  32. realtime =none extsz=4096 blocks=0, rtextents=0
  33. [root@localhost ~]#mount /dev/vg/lvm /opt
  34. [root@localhost ~]#mkdir /opt/nfs
  35. [root@localhost ~]#vim /etc/exports
  36. /opt/nfs *
  37. [root@localhost ~]#cat /etc/exports
  38. /opt/nfs *
  39. [root@localhost ~]#exportfs -r
  40. exportfs: No options for /opt/nfs *: suggest *(sync) to avoid warning
  41. [root@localhost ~]#exportfs -v
  42. /opt/nfs <world>(ro,sync,wdelay,hide,no_subtree_check,sec=sys,secure,root_squash,no_all_squash)
  43. [root@localhost ~]#hostname zzzcentos1
  44. [root@localhost ~]#su
  45. [root@zzzcentos1 ~]#

方法二:因为我们根本身就是逻辑卷;所以就不另外添加磁盘了,就在根下面做

②设置共享目录

  1. [root@zzcentos1 ~]#mkdir /opt/nfs
  2. [root@zzcentos1 ~]#vim /etc/exports
  3. [root@zzcentos1 ~]#cat /etc/exports
  4. /opt/nfs *
  5. [root@zzcentos1 ~]#exportfs -r
  6. exportfs: No options for /opt/nfs *: suggest *(sync) to avoid warning
  7. [root@zzcentos1 ~]#exportfs -v
  8. /opt/nfs <world>(ro,sync,wdelay,hide,no_subtree_check,sec=sys,secure,root_squash,no_all_squash)
  9. [root@zzcentos1 ~]#

③nginx 服务挂载共享目录

④日志分割

编辑脚本:

  1. #!/bin/bash
  2. pid=`cat /usr/local/nginx/logs/nginx.pid`
  3. cd /opt
  4. mv benet.log /mnt/`date +%F`
  5. touch benet.log
  6. kill -USR1 ${pid}

再去页面访问:

编写crontab计划

绝对路径加脚本有执行权限,如上图就可以执行,到此结束

方法二:脚本也可以如下写法

  1. #!/bin/bash
  2. day=`date "+%Y-%m-%d"`
  3. log="/usr/local/nginx/logs"
  4. pid=`cat /usr/local/nginx/logs/nginx.pid`
  5. mv /${log}/access.log /opt/${day}
  6. kill -USR1 ${pid}
  7. sed -i '/.*bbs.*/!p' /opt/${day}

五、要求配置location匹配请求地址http://www.kgc.com/test/XXXX,使用户访问该路径下的文件时返回/var/share/nginx/html/目录下的文件内容。
要求使用rewrite将使用域名www.benet.com 请求以 .php 结尾的访问都跳转到域名www.kgc.com上,而且后面的参数保持不变,比如访问http://www.benet.com/bbs/index.php跳转到http://www.kgc.com/bbs/index.php

(1)要求配置location匹配请求地址http://www.kgc.com/test/XXXX,使用户访问该路径下的文件时返回/var/share/nginx/html/目录下的文件内容

①Nginx服务配置

  1. location /test {
  2. alias /var/share/nginx/html;
  3. }

②创建被访问文件内容

③检测:

(2)要求使用rewrite将使用域名www.benet.com 请求以 .php 结尾的访问都跳转到域名www.kgc.com上,而且后面的参数保持不变,比如访问http://www.benet.com/bbs/index.php跳转到http://www.kgc.com/bbs/index.php

①Nginx服务配置

  1. location ~* \.php$ {
  2. rewrite ^/(.*) http://www.kgc.com/$1 permanent;
  3. }

②去浏览器检测:

成功跳转

六、在Nginx服务器上对基于www.benet.com域名的虚拟机主机设置动静分离由nginx提供静态页面服务,将对 .jsp文件的动态页面请求转发到Tomcat 服务器处理,并实现负载均衡

①Nginx服务器配置

  1. upstream tomcat {
  2. server 192.168.246.8:8080;
  3. server 192.168.246.9:8080;
  4. }

  1. location / {
  2. root /var/www/html;
  3. index index.html index.htm;
  4. }
  5. location ~*\.jsp$ {
  6. proxy_pass http://tomcat;
  7. }

②配置Tomcat服务器

tomcat 7-2配置:

tomcat 7-3配置:

  1. [root@zzzcentos3 ~]#systemctl stop firewalld
  2. [root@zzzcentos3 ~]#setenforce 0
  3. [root@zzzcentos3 ~]#cd /usr/local/tomcat/webapps/ROOT/
  4. [root@zzzcentos3 ROOT]#ls
  5. asf-logo-wide.svg bg-middle.png bg-upper.png index.jsp tomcat.css tomcat.png tomcat.svg
  6. bg-button.png bg-nav.png favicon.ico RELEASE-NOTES.txt tomcat.gif tomcat-power.gif WEB-INF
  7. [root@zzzcentos3 ROOT]#cp index.jsp index.jsp.bak #先备份
  8. [root@zzzcentos3 ROOT]#ls
  9. asf-logo-wide.svg bg-nav.png index.jsp tomcat.css tomcat-power.gif
  10. bg-button.png bg-upper.png index.jsp.bak tomcat.gif tomcat.svg
  11. bg-middle.png favicon.ico RELEASE-NOTES.txt tomcat.png WEB-INF
  12. [root@zzzcentos3 ROOT]#echo tomcat 7-3 > index.jsp #输入内容
  13. [root@zzzcentos3 ROOT]#cat index.jsp
  14. tomcat 7-3
  15. [root@zzzcentos3 ROOT]#

③浏览器检测:

访问动态页面:

访问静态页面:

七、在网关服务器上设置SNAT/DNAT,使client使用网关服务器的ens36接口的IP地址访问也可实现上一题的效果

理解:

我们想要 客户机 用 网关服务器的ens36接口IP地址(12.0.0.1)去访问www.benet.com/index.jsp,从而得到我们在  Tomcat服务器中配置的 动态页面;

所以这是外网(7-5 IP:12.0.0.12)可以访问内网(7-1 ip: 192.168.246.7),所以我们需要配置的是DNAT

①客户机地址配置

②网关服务器配置

  1. [root@zzcentos1 network-scripts]#sysctl -a |grep "ip_forward"
  2. net.ipv4.ip_forward = 0
  3. net.ipv4.ip_forward_use_pmtu = 0
  4. sysctl: reading key "net.ipv6.conf.all.stable_secret"
  5. sctl: reading key "net.ipv6.conf.default.stable_secret"
  6. ?ysctl: reading key "net.ipv6.conf.ens33.stable_secret"
  7. sysctl: reading key "net.ipv6.conf.ens36.stable_secret"
  8. sysctl: reading key "net.ipv6.conf.lo.stable_secret"
  9. sysctl: reading key "net.ipv6.conf.virbr0.stable_secret"
  10. sysctl: reading key "net.ipv6.conf.virbr0-nic.stable_secret"
  11. [root@zzcentos1 network-scripts]#vim /etc/sysctl.conf
  12. [root@zzcentos1 network-scripts]#sysctl -p
  13. net.ipv4.ip_forward = 1
  14. [root@zzcentos1 network-scripts]#

③做SNAT与DNAT

  1. [root@zzcentos1 network-scripts]#iptables -t nat -A POSTROUTING -o ens36 -s 192.168.246.0/24 -j SNAT --to 12.0.0.1
  2. [root@zzcentos1 network-scripts]#
  3. [root@zzcentos1 network-scripts]#iptables -t nat -A PREROUTING -i ens36 -d 12.0.0.1 -p tcp --dport 80 -j DNAT --to 192.168.246.7
  4. [root@zzcentos1 network-scripts]#iptables -t nat -vnL
  5. Chain PREROUTING (policy ACCEPT 1 packets, 71 bytes)
  6. pkts bytes target prot opt in out source destination
  7. 0 0 DNAT tcp -- ens36 * 0.0.0.0/0 12.0.0.1 tcp dpt:80 to:192.168.246.7
  8. Chain INPUT (policy ACCEPT 1 packets, 71 bytes)
  9. pkts bytes target prot opt in out source destination
  10. Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
  11. pkts bytes target prot opt in out source destination
  12. Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
  13. pkts bytes target prot opt in out source destination
  14. 0 0 SNAT all -- * ens36 192.168.246.0/24 0.0.0.0/0 to:12.0.0.1
  15. [root@zzcentos1 network-scripts]#

④客户机浏览器检测:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/985522
推荐阅读
相关标签
  

闽ICP备14008679号