您现在的位置是:首页 > 电脑学习教程 > Linux > 文章正文 Linux

nginx做负载均衡服务器配置动静分离

2022-09-09 11:35:51 Linux

简介1 题目:后端RS服务器⼀台部署LNMP(nginx1 22+mysql8 0+php8 0),⼀台部署httpd。要求nginx和php使⽤编译安装最后要通过访问nginx负载均

1. 题目:

后端RS服务器⼀台部署LNMP(nginx1.22+mysql8.0+php8.0),⼀台部署
httpd。
要求nginx和php使⽤编译安装
最后要通过访问nginx负载均衡服务器的IP看到动静分离的效果。

2. 环境和提供软件包

2.1 提供软件包

  1. [root@node6 ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz https://www.php.net/distributions/php-8.0.23.tar.gz 
  2. [root@node6 ~]# ls 
  3. anaconda-ks.cfg                             nginx-1.22.0.tar.gz 
  4. mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz  php-8.0.23.tar.gz 
  5. [root@node6 ~]#  

2.2 环境

主机ip安装的服务node6192.168.232.134lnmp,动态资源node3192.168.232.128nginx,静态资源node2192.168.232.129nginx,做负载均衡
  • 关闭防火墙
  1. [root@node6 ~]# systemctl disable --now firewalld 
  2. Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. 
  3. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. 
  4. [root@node6 ~]# vim /etc/selinux/config  
  5. [root@node6 ~]# setenforce 0 
  6.  
  7.  
  8. [root@node3 ~]# systemctl disable --now firewalld 
  9. Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. 
  10. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. 
  11. [root@node3 ~]# setenforce 0 
  12. [root@node3 ~]# vi /etc/selinux/config 
  13.  
  14.  
  15. [root@node2 ~]# systemctl disable --now firewalld 
  16. Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. 
  17. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. 
  18. [root@node2 ~]# setenforce 0 
  19. [root@node2 ~]# vi /etc/selinux/config 

3. 在134主机主机部署lnmp,在动态资源

3.1 源码安装nginx

  1. 创建系统用户nginx 
  2. [root@node6 ~]# useradd -r -M -s /sbin/nologin nginx 
  3. [root@node6 ~]#  
  4.  
  5.  
  6. 安装依赖环境 
  7. [root@node6 ~]# yum -y groups mark install 'Development Tools' 
  8. [root@node6 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make  
  9.  
  10.  
  11. 创建日志存放目录 
  12. [root@node6 ~]# mkdir -p /var/log/nginx 
  13. [root@node6 ~]# chown -R nginx.nginx /var/log/nginx 
  14. [root@node6 ~]# ll -d /var/log/nginx 
  15. drwxr-xr-x. 2 nginx nginx 6 Sep  5 18:43 /var/log/nginx 
  1. 编译安装 
  2. [root@node6 ~]# tar xf nginx-1.22.0.tar.gz  
  3. [root@node6 ~]# cd nginx-1.22.0 
  4. [root@node6 nginx-1.22.0]# ./configure \ 
  5.     --prefix=/usr/local/nginx \ 
  6.     --user=nginx \ 
  7.     --group=nginx \ 
  8.     --with-debug \ 
  9.     --with-http_ssl_module \ 
  10.     --with-http_realip_module \ 
  11.     --with-http_image_filter_module \ 
  12.     --with-http_gunzip_module \ 
  13.     --with-http_gzip_static_module \ 
  14.     --with-http_stub_status_module \ 
  15.     --http-log-path=/var/log/nginx/access.log \ 
  16.     --error-log-path=/var/log/nginx/error.log 
  17.  
  18. [root@node6 nginx-1.22.0]# make  
  19. [root@node6 nginx-1.22.0]# make install 
  20.  
  21.  
  22. nginx安装后配置 
  23. [root@node6 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh 
  24. [root@node6 ~]# source /etc/profile.d/nginx.sh 
  25.  
  26.  
  27.  
  28. 启动nginx 
  29. [root@node6 ~]# nginx  
  30. [root@node6 ~]# ss -antl 
  31. State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process  
  32. LISTEN 0      128           0.0.0.0:22          0.0.0.0:*             
  33. LISTEN 0      128           0.0.0.0:80          0.0.0.0:*             
  34. LISTEN 0      128              [::]:22             [::]:*             
  35. [root@node6 ~]# nginx -s stop 
  36. [root@node6 ~]# ss -antl 
  37. State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process  
  38. LISTEN 0      128           0.0.0.0:22          0.0.0.0:*             
  39. LISTEN 0      128              [::]:22             [::]:*             
  40. [root@node6 ~]#  
  • 可以访问

在这里插入图片描述

3.2 二进制安装MySQL

  1. 安装依赖包,创建用户,并解压 
  2. [root@node6 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs 
  3.  
  4. [root@node6 ~]# useradd -r -M -s /sbin/nologin mysql 
  5.  
  6. [root@node6 ~]# tar xf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz -C /usr/local/ 
  7.  
  8.  
  9.  
  10. 修改属主 
  11. [root@node6 ~]# cd /usr/local/ 
  12. [root@node6 local]# ls 
  13. bin    include  libexec                              sbin 
  14. etc    lib      mysql-8.0.20-linux-glibc2.12-x86_64  share 
  15. games  lib64    nginx                                src 
  16. [root@node6 local]# mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql 
  17. [root@node6 local]# chown -R mysql.mysql mysql 
  18. [root@node6 local]# ll -d mysql 
  19. drwxr-xr-x. 9 mysql mysql 129 Sep  5 19:02 mysql 
  20.  
  21.  
  22.  
  23. 配置环境变量,man文档,lib库,头文件 
  24. [root@node6 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh 
  25. [root@node6 ~]# source /etc/profile.d/mysql.sh 
  26. [root@node6 ~]# vim /etc/ld.so.conf.d/mysql.conf 
  27. [root@node6 ~]# ldconfig  
  28. [root@node6 ~]# cat /etc/ld.so.conf.d/mysql.conf 
  29. /usr/local/mysql/lib 
  30. [root@node6 ~]# ln -s /usr/local/mysql/include /usr/include/mysql 
  31. [root@node6 ~]#  
  32. [root@node6 ~]# vim /etc/man_db.conf 
  33. MANDATORY_MANPATH                       /usr/local/mysql/man 
  34.  
  35.  
  36.  
  37. 建立数据存放目录,并修改属主 
  38. [root@node6 ~]# mkdir -p /opt/data 
  39. [root@node6 ~]# chown -R mysql.mysql /opt/data 
  40. [root@node6 ~]# ll -d /opt/data 
  41. drwxr-xr-x. 2 mysql mysql 6 Sep  5 19:09 /opt/data 
  42. [root@node6 ~]#  
  43.  
  44.  
  45. 初始化数据库,并保存密码 
  46. [root@node6 ~]# mysqld --initialize --user mysql --datadir /opt/data 
  47. 2022-09-05T11:10:42.151293Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 209429 
  48. 2022-09-05T11:10:42.160150Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 
  49. 2022-09-05T11:10:43.610708Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 
  50. 2022-09-05T11:10:44.652264Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: w-ETXrfTV1wE 
  51. [root@node6 ~]# echo 'w-ETXrfTV1wE' > passwd 
  52. [root@node6 ~]# cat passwd 
  53. w-ETXrfTV1wE 
  54. [root@node6 ~]#  
  55.  
  56.  
  57.  
  58. 生成配置文件 
  59. [root@node6 ~]# > /etc/my.cnf 
  60. [root@node6 ~]# vim /etc/my.cnf 
  61. [root@node6 ~]# cat /etc/my.cnf 
  62. [mysqld] 
  63. basedir = /usr/local/mysql 
  64. datadir = /opt/data 
  65. socket = /tmp/mysql.sock 
  66. port = 3306 
  67. pid-file = /opt/data/mysql.pid 
  68. user = mysql 
  69. skip-name-resolve 
  70. [root@node6 ~]#  
  71.  
  72.  
  73.  
  74. 配置服务启动脚本 
  75. [root@node6 ~]# cd /usr/local/mysql/support-files/ 
  76. [root@node6 support-files]# cp mysql.server mysqld 
  77. [root@node6 support-files]# vim mysqld 
  78. basedir=/usr/local/mysql 
  79. datadir=/opt/data 
  80. [root@node6 support-files]# chown -R mysql.mysql mysqld 
  81. [root@node6 support-files]# ll -d mysqld 
  82. -rwxr-xr-x. 1 mysql mysql 10602 Sep  2 19:06 mysqld 
  83. [root@node6 support-files]#  
  84.  
  85.  
  86. 先启动测试一下 
  87. [root@node6 ~]# /usr/local/mysql/support-files/mysqld start 
  88. Starting MySQL.Logging to '/opt/data/node6.err'
  89.  SUCCESS!  
  90. [root@node6 ~]# ss -antl 
  91. State  Recv-Q Send-Q Local Address:Port    Peer Address:Port Process  
  92. LISTEN 0      128          0.0.0.0:22           0.0.0.0:*             
  93. LISTEN 0      128          0.0.0.0:80           0.0.0.0:*             
  94. LISTEN 0      128             [::]:22              [::]:*             
  95. LISTEN 0      70                 *:33060              *:*             
  96. LISTEN 0      128                *:3306               *:*             
  97. [root@node6 ~]# /usr/local/mysql/support-files/mysqld stop 
  98. Shutting down MySQL.. SUCCESS!  
  99. [root@node6 ~]# ss -antl 
  100. State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process  
  101. LISTEN 0      128           0.0.0.0:22          0.0.0.0:*             
  102. LISTEN 0      128           0.0.0.0:80          0.0.0.0:*             
  103. LISTEN 0      128              [::]:22             [::]:*             
  104. [root@node6 ~]#  
  105.  
  106.  
  107. 配置service文件,设置开机自启 
  108. [root@node6 ~]# cd /usr/lib/systemd/system 
  109. [root@node6 system]# cp sshd.service mysqld.service 
  110. [root@node6 system]# ll sshd.service  
  111. -rw-r--r--. 1 root root 456 Jul 12  2021 sshd.service 
  112. [root@node6 system]# ll mysqld.service 
  113. -rw-r--r--. 1 root root 456 Sep  5 19:15 mysqld.service 
  114. [root@node6 system]# vim mysqld.service  
  115. [root@node6 system]# cat mysqld.service 
  116. [Unit] 
  117. Description=mysqld server daemon 
  118. After=network.target sshd-keygen.target 
  119.  
  120. [Service] 
  121. Type=forking 
  122. ExecStart=/usr/local/mysql/support-files/mysqld start 
  123. ExecStop=/usr/local/mysql/support-files/mysqld stop 
  124. ExecReload=/bin/kill -HUP $MAINPID 
  125.  
  126. [Install] 
  127. WantedBy=multi-user.target 
  128. [root@node6 system]# systemctl daemon-reload 
  129.  
  130.  
  131. [root@node6 system]# systemctl enable --now mysqld 
  132. Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service 鈫� /usr/lib/systemd/system/mysqld.service. 
  133. [root@node6 system]# ss -antl 
  134. State  Recv-Q Send-Q Local Address:Port    Peer Address:Port Process  
  135. LISTEN 0      128          0.0.0.0:22           0.0.0.0:*             
  136. LISTEN 0      128          0.0.0.0:80           0.0.0.0:*             
  137. LISTEN 0      128             [::]:22              [::]:*             
  138. LISTEN 0      70                 *:33060              *:*             
  139. LISTEN 0      128                *:3306               *:*             
  140. [root@node6 system]#  
  141.  
  142.  
  143.  
  144. 设置密码 
  145. [root@node6 ~]# mysql -uroot -p'w-ETXrfTV1wE'
  146.  
  147. mysql> alter user 'root'@'localhost' identified by 'run123123'
  148. Query OK, 0 rows affected (0.00 sec) 
  149.  
  150. mysql> quit 
  151. Bye 
  152. [root@node6 ~]# mysql -uroot -p'run123123'
  153. mysql: [Warning] Using a password on the command line interface can be insecure. 
  154. Welcome to the MySQL monitor.  Commands end with ; or \g. 
  155. Your MySQL connection id is 9 
  156. Server version: 8.0.20 MySQL Community Server - GPL 
  157.  
  158. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. 
  159.  
  160. Oracle is a registered trademark of Oracle Corporation and/or its 
  161. affiliates. Other names may be trademarks of their respective 
  162. owners. 
  163.  
  164. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 
  165.  
  166. mysql> quit 
  167. Bye 
  168. [root@node6 ~]#  

3.3 源码安装PHP

  1. 解压 
  2. [root@node6 ~]# tar xf php-8.0.23.tar.gz 
  3.  
  4. 安装依赖包 
  5. [root@node6 ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd sqlite-devel libzip-devel https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm 
  6.  
  7.  
  8.  
  9. 编译安装: 
  10. [root@node6 ~]# cd php-8.0.23 
  11. [root@node6 php-8.0.23]# ./configure --prefix=/usr/local/php7  \ 
  12.   --with-config-file-path=/etc \ 
  13.   --enable-fpm \ 
  14.   --enable-inline-optimization \ 
  15.   --disable-debug \ 
  16.   --disable-rpath \ 
  17.   --enable-shared \ 
  18.   --enable-soap \ 
  19.   --with-openssl \ 
  20.   --enable-bcmath \ 
  21.   --with-iconv \ 
  22.   --with-bz2 \ 
  23.   --enable-calendar \ 
  24.   --with-curl \ 
  25.   --enable-exif  \ 
  26.   --enable-ftp \ 
  27.   --enable-gd \ 
  28.   --with-jpeg \ 
  29.   --with-zlib-dir \ 
  30.   --with-freetype \ 
  31.   --with-gettext \ 
  32.   --enable-json \ 
  33.   --enable-mbstring \ 
  34.   --enable-pdo \ 
  35.   --with-mysqli=mysqlnd \ 
  36.   --with-pdo-mysql=mysqlnd \ 
  37.   --with-readline \ 
  38.   --enable-shmop \ 
  39.   --enable-simplexml \ 
  40.   --enable-sockets \ 
  41.   --with-zip \ 
  42.   --enable-mysqlnd-compression-support \ 
  43.   --with-pear \ 
  44.   --enable-pcntl \ 
  45.   --enable-posix 
  46.  
  47.  
  48. [root@node6 php-8.0.23]# make 
  49. [root@node6 php-8.0.23]# make install 
  50.  
  51.  
  52. 配置环境变量,lib,头文件 
  53. [root@node6 ~]# cd /usr/local/php7/ 
  54. [root@node6 php7]# ls 
  55. bin  etc  include  lib  php  sbin  var 
  56. [root@node6 php7]# echo 'export PATH=/usr/local/php7/bin:/usr/local/php7/:sbin:$PATH' > /etc/profile.d/php7.sh 
  57. [root@node6 php7]# source /etc/profile.d/php7.sh 
  58. [root@node6 php7]#  
  59. [root@node6 php7]# ln -s /usr/local/php7/include /usr/include/php 
  60. [root@node6 php7]#  
  61. [root@node6 php7]# vim /etc/ld.so.conf.d/php.conf 
  62. [root@node6 php7]# cat /etc/ld.so.conf.d/php.conf 
  63. /usr/local/php7/lib 
  64. [root@node6 php7]# ldconfig  
  65.  
  66. [root@node6 ~]# php -v 
  67. PHP 7.4.30 (cli) (built: Sep  2 2022 19:31:45) ( NTS ) 
  68. Copyright (c) The PHP Group 
  69. Zend Engine v3.4.0, Copyright (c) Zend Technologies 
  70. [root@node6 ~]#  
  71.  
  72.  
  73.  
  74. 配置php-fpm 
  75. [root@node6 ~]# cd php-8.0.23 
  76. [root@node6 php-8.0.23]# \cp php.ini-production /etc/php.ini 
  77. [root@node6 php-8.0.23]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 
  78. [root@node6 php-8.0.23]# chmod +x /etc/rc.d/init.d/php-fpm 
  79. [root@node6 php-8.0.23]# ll /etc/rc.d/init.d/php-fpm 
  80. -rwxr-xr-x. 1 root root 2402 Sep  5 19:56 /etc/rc.d/init.d/php-fpm 
  81. [root@node6 php-8.0.23]#  
  82. [root@node6 php-8.0.23]# pwd 
  83. /root/php-8.0.23 
  84. [root@node6 php-8.0.23]# cd /usr/local/php7/etc/ 
  85. [root@node6 etc]# ls 
  86. pear.conf  php-fpm.conf.default  php-fpm.d 
  87. [root@node6 etc]# cp php-fpm.conf.default php-fpm.conf 
  88. [root@node6 etc]# cd php-fpm.d/ 
  89. [root@node6 php-fpm.d]# ls 
  90. www.conf.default 
  91. [root@node6 php-fpm.d]# cp www.conf.default www.conf 
  92. [root@node6 php-fpm.d]#  
  93. [root@node6 php-fpm.d]# vim www.conf 
  94. listen = 127.0.0.1:9000 
  95.  
  96.  
  97. 启动 
  98. [root@node6 ~]# cd /etc/init.d 
  99. [root@node6 init.d]# ls 
  100. README  functions  php-fpm 
  101. [root@node6 init.d]# service php-fpm start 
  102. Starting php-fpm  done 
  103. [root@node6 init.d]# service php-fpm stop 
  104. Gracefully shutting down php-fpm . done 
  105. [root@node6 init.d]# service php-fpm start 
  106. Starting php-fpm  done 
  107. [root@node6 init.d]# ss -antl 
  108. State  Recv-Q Send-Q Local Address:Port    Peer Address:Port Process  
  109. LISTEN 0      128          0.0.0.0:22           0.0.0.0:*             
  110. LISTEN 0      128        127.0.0.1:9000         0.0.0.0:*             
  111. LISTEN 0      128          0.0.0.0:80           0.0.0.0:*             
  112. LISTEN 0      128             [::]:22              [::]:*             
  113. LISTEN 0      70                 *:33060              *:*             
  114. LISTEN 0      128                *:3306               *:*             
  115. [root@node6 init.d]#  

3.4 配置nginx

  1. [root@node6 ~]# cd /usr/local/nginx/conf/ 
  2. [root@node6 conf]# vim nginx.conf 
  3. [root@node6 conf]# vim nginx.conf 
  4. [root@node6 conf]# cat nginx.conf 
  5.  
  6. user  nginx; 
  7. worker_processes  1; 
  8.  
  9. error_log  logs/error.log; 
  10. #error_log  logs/error.log  notice; 
  11. #error_log  logs/error.log  info; 
  12.  
  13. pid        logs/nginx.pid; 
  14.  
  15.  
  16. events { 
  17.     worker_connections  1024; 
  18.  
  19.  
  20. http { 
  21.     include       mime.types; 
  22.     default_type  application/octet-stream; 
  23.  
  24.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
  25.     #                  '$status $body_bytes_sent "$http_referer" ' 
  26.     #                  '"$http_user_agent" "$http_x_forwarded_for"'; 
  27.  
  28.     #access_log  logs/access.log  main; 
  29.  
  30.     sendfile        on; 
  31.     #tcp_nopush     on; 
  32.  
  33.     #keepalive_timeout  0; 
  34.     keepalive_timeout  65; 
  35.  
  36.     #gzip  on; 
  37.  
  38.     server { 
  39.         listen       80; 
  40.         server_name  localhost; 
  41.  
  42.         #charset koi8-r; 
  43.  
  44.         #access_log  logs/host.access.log  main; 
  45.  
  46.         location / { 
  47.             root   html; 
  48.             index  index.php index.html index.htm; //在index后面添加index.php,表示优先访问php页面 
  49.         } 
  50.  
  51.         #error_page  404              /404.html; 
  52.  
  53.         # redirect server error pages to the static page /50x.html 
  54.         # 
  55.         error_page   500 502 503 504  /50x.html; 
  56.         location = /50x.html { 
  57.             root   html; 
  58.         } 
  59.  
  60.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
  61.         # 
  62.         #location ~ \.php$ { 
  63.         #    proxy_pass   http://127.0.0.1; 
  64.         #} 
  65.  
  66.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
  67.         # 
  68. //将以下内容取消注释并修改     
  69.         location ~ \.php$ { 
  70.             root           html; 
  71.             fastcgi_pass   127.0.0.1:9000; 
  72.             fastcgi_index  index.php; 
  73.             fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name; 
  74.             include        fastcgi_params; 
  75.         } 
  76.  
  77.         # deny access to .htaccess files, if Apache's document root 
  78.         # concurs with nginx's one 
  79.         # 
  80.         #location ~ /\.ht { 
  81.         #    deny  all; 
  82.         #} 
  83.     } 
  84.  
  85.  
  86.     # another virtual host using mix of IP-, name-, and port-based configuration 
  87.     # 
  88.     #server { 
  89.     #    listen       8000; 
  90.     #    listen       somename:8080; 
  91.     #    server_name  somename  alias  another.alias; 
  92.  
  93.     #    location / { 
  94.     #        root   html; 
  95.     #        index  index.html index.htm; 
  96.     #    } 
  97.     #} 
  98.  
  99.  
  100.     # HTTPS server 
  101.     # 
  102.     #server { 
  103.     #    listen       443 ssl; 
  104.     #    server_name  localhost; 
  105.  
  106.     #    ssl_certificate      cert.pem; 
  107.     #    ssl_certificate_key  cert.key; 
  108.  
  109.     #    ssl_session_cache    shared:SSL:1m; 
  110.     #    ssl_session_timeout  5m; 
  111.  
  112.     #    ssl_ciphers  HIGH:!aNULL:!MD5; 
  113.     #    ssl_prefer_server_ciphers  on; 
  114.  
  115.     #    location / { 
  116.     #        root   html; 
  117.     #        index  index.html index.htm; 
  118.     #    } 
  119.     #} 
  120.  
  121. [root@node6 conf]# nginx -s stop 
  122. [root@node6 conf]# nginx 

3.5 配置PHP网络界面

  1. [root@node6 ~]# cd /usr/local/nginx/html/ 
  2. [root@node6 html]# vi index.php 
  3. [root@node6 html]# cat index.php 
  4.     phpinfo(); 
  5. ?> 
  6. [root@node6 html]#  
  7.  
  8. 重启 
  9. [root@node6 ~]# nginx -s stop 
  10. [root@node6 ~]# nginx 
  • 访问:http://192.168.232.134/

在这里插入图片描述

4. 部署

4.1 在128主机安装httpd,做静态资源

  1. [root@node3 ~]# yum -y install httpd 
  2. [root@node3 ~]# systemctl enable --now httpd 
  3. Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service. 
  4. [root@node3 ~]# ss -antl 
  5. State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process  
  6. LISTEN 0      128           0.0.0.0:22          0.0.0.0:*             
  7. LISTEN 0      128                 *:80                *:*             
  8. LISTEN 0      128              [::]:22             [::]:*             

在这里插入图片描述

4.2 在129主机源码安装nginx并配置负载均衡器,进行调度

  1. [root@node2 ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz 
  2. --2022-09-05 20:47:25--  https://nginx.org/download/nginx-1.22.0.tar.gz 
  3. Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ... 
  4. Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected. 
  5. HTTP request sent, awaiting response... 200 OK 
  6. Length: 1073322 (1.0M) [application/octet-stream] 
  7. Saving to: 'nginx-1.22.0.tar.gz' 
  8.  
  9. nginx-1.22.0.tar. 100%[==========>]   1.02M  29.1KB/s    in 20s      
  10.  
  11. 2022-09-05 20:47:47 (51.5 KB/s) - 'nginx-1.22.0.tar.gz' saved [1073322/1073322] 
  12.  
  13.  
  14. [root@node2 ~]# useradd -r -M -s /sbin/nologin nginx 
  15. [root@node2 ~]# yum -y groups mark install 'Development Tools' 
  16. [root@node2 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make  
  17.  
  18.  
  19. [root@node2 ~]# mkdir -p /var/log/nginx 
  20. [root@node2 ~]# chown -R nginx.nginx /var/log/nginx 
  21. [root@node2 ~]# ll -d /var/log/nginx 
  22. drwxr-xr-x. 2 nginx nginx 6 Sep  5 20:51 /var/log/nginx 
  23. [root@node2 ~]# tar xf nginx-1.22.0.tar.gz  
  24. [root@node2 ~]# cd nginx-1.22.0 
  25. [root@node2 nginx-1.22.0]# ./configure \ 
  26.      --prefix=/usr/local/nginx \ 
  27.      --user=nginx \ 
  28.      --group=nginx \ 
  29.      --with-debug \ 
  30.      --with-http_ssl_module \ 
  31.      --with-http_realip_module \ 
  32.      --with-http_image_filter_module \ 
  33.      --with-http_gunzip_module \ 
  34.      --with-http_gzip_static_module \ 
  35.      --with-http_stub_status_module \ 
  36.      --http-log-path=/var/log/nginx/access.log \ 
  37.      --error-log-path=/var/log/nginx/error.log 
  38.  
  39. [root@node2 nginx-1.22.0]# make 
  40. [root@node2 nginx-1.22.0]# make install 
  41.  
  42.  
  43. [root@node2 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh 
  44. [root@node2 ~]# source /etc/profile.d/nginx.sh 
  45.  
  46.  
  47. [root@node2 ~]# nginx  
  48. [root@node2 ~]# ss -antl 
  49. State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process  
  50. LISTEN 0      128           0.0.0.0:22          0.0.0.0:*             
  51. LISTEN 0      128           0.0.0.0:80          0.0.0.0:*             
  52. LISTEN 0      128              [::]:22             [::]:*             
  53. [root@node2 ~]#  

5. 配置负载均衡,129主机

  1. [root@node6 sbin]# ss -antl 
  2. State  Recv-Q Send-Q Local Address:Port    Peer Address:Port Process  
  3. LISTEN 0      128          0.0.0.0:80           0.0.0.0:*             
  4. LISTEN 0      128          0.0.0.0:22           0.0.0.0:*             
  5. LISTEN 0      128        127.0.0.1:9000         0.0.0.0:*             
  6. LISTEN 0      128             [::]:22              [::]:*             
  7. LISTEN 0      70                 *:33060              *:*             
  8. LISTEN 0      128                *:3306               *:*             
  9. [root@node6 sbin]#  
  10.  
  11.  
  12. [root@node2 ~]# cd /usr/local/nginx/conf/ 
  13. [root@node2 conf]# vim nginx.conf 
  14.  
  15.  
  16.  
  17. #gzip  on; 
  18.  
  19.     upstream backend  #配置负载均衡 
  20.         server 192.168.232.128; 
  21.         server 192.168.232.134; 
  22.     }    
  23.  
  24.     server { 
  25.         listen       80; 
  26.         server_name  localhost; 
  27.  
  28.         #charset koi8-r; 
  29.  
  30.         #access_log  logs/host.access.log  main; 
  31.  
  32.         location / { 
  33.             proxy_pass http://backend;#配置反向代理 
  34.         } 
  35.  
  36.    
  37. [root@node2 conf]# nginx -t 
  38. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 
  39. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 
  40. [root@node2 conf]# nginx -s reload 
  41. [root@node2 conf]# nginx -s stop 
  42. [root@node2 conf]# nginx   
  • 访问:http://192.168.232.129/

在这里插入图片描述

  • 访问:http://192.168.232.129/

在这里插入图片描述

6. 实现动静分离

  1. [root@node2 conf]# pwd 
  2. /usr/local/nginx/conf 
  3. [root@node2 conf]# vim nginx.conf 
  4. [root@node2 conf]# nginx -s reload 
  5. [root@node2 conf]# vim nginx.conf 
  6. [root@node2 conf]# nginx -s reload 
  7.  
  8. #gzip  on; 
  9.  
  10.     upstream static { 
  11.         server 192.168.232.128;#httpd主机的ip 
  12.     } 
  13.  
  14.     upstream dynamic { 
  15.         server 192.168.232.134;#lnmp主机的ip 
  16.     } 
  17.  
  18. server { 
  19.         listen       80; 
  20.         server_name  localhost; 
  21.  
  22.         #charset koi8-r; 
  23.  
  24.         #access_log  logs/host.access.log  main; 
  25.  
  26.         location / { 
  27.             proxy_pass http://static;#访问静态资源会自动跳转到进行访问 
  28.         } 
  29.  
  30.    # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
  31.         # 
  32.         location ~ \.php$ { 
  33.             proxy_pass   http://dynamic;#访问动态资源会自动跳转到进行访问 
  34.         } 
  35. [root@node2 conf]# nginx -s reload 
  36. [root@node2 conf]# nginx -t 
  37. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 
  38. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 

[root@node2 conf]# 
  • 访问静态资源:http://192.168.232.129/

在这里插入图片描述

  • 访问动态资源:http://192.168.232.129/index.php

在这里插入图片描述

站点信息