nginx负载均衡配置实现动静分离

环境配置

主机ip服务
192.168.149.143nginx
192.168.149.140lnmp
192.168.149.152apache

nginx的安装与配置

  • nginx的安装
创建系统用户nginx
[root@host ~]# useradd -r -M -s /sbin/nologin nginx
安装依赖环境
[root@host ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++  wget make
[root@host ~]# yum -y groups mark install 'Development Tools'
创建日志存放目录
[root@host ~]# mkdir -p /var/log/nginx
[root@host ~]# chown -R nginx.nginx /var/log/nginx
下载并安装nginx
[root@host ~]# cd /usr/src/
[root@host src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@host src]# tar xf nginx-1.20.1.tar.gz 
[root@host src]# cd nginx-1.20.1/
[root@host nginx-1.20.1]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log

[root@host nginx-1.20.1]# make && make install
[root@host nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@host nginx-1.20.1]# . /etc/profile.d/nginx.sh
[root@host nginx-1.20.1]# nginx 
[root@host nginx-1.20.1]# ss -antl
State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       Process       
LISTEN       0            128                      0.0.0.0:22                     0.0.0.0:*                        
LISTEN       0            128                      0.0.0.0:80                     0.0.0.0:*                        
LISTEN       0            128                         [::]:22                        [::]:* 
关闭防火墙
[root@host ~]# systemctl stop firewalld.service 
[root@host ~]# setenforce 0

在这里插入图片描述

apache配置

  • nginx的安装
创建系统用户nginx
[root@host ~]# useradd -r -M -s /sbin/nologin nginx
安装依赖环境
[root@host ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++  wget make
[root@host ~]# yum -y groups mark install 'Development Tools'
创建日志存放目录
[root@host ~]# mkdir -p /var/log/nginx
[root@host ~]# chown -R nginx.nginx /var/log/nginx
下载并安装nginx
[root@host ~]# cd /usr/src/
[root@host src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
[root@host src]# tar xf nginx-1.20.1.tar.gz 
[root@host src]# cd nginx-1.20.1/
[root@host nginx-1.20.1]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log

[root@host nginx-1.20.1]# make && make install
[root@host nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@host nginx-1.20.1]# . /etc/profile.d/nginx.sh
[root@host nginx-1.20.1]# nginx 
[root@host nginx-1.20.1]# ss -antl
State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       Process       
LISTEN       0            128                      0.0.0.0:22                     0.0.0.0:*                        
LISTEN       0            128                      0.0.0.0:80                     0.0.0.0:*                        
LISTEN       0            128                         [::]:22                        [::]:* 
关闭防火墙
[root@host ~]# systemctl stop firewalld.service 
[root@host ~]# setenforce 0

在这里插入图片描述

MySQL配置

  • 创建用户和组
[root@host ~]# useradd -r -M -s /sbin/nologin mysql
  • 修改目录名
[root@host local]# mv mysql-5.7.33-linux-glibc2.12-x86_64/ mysql
[root@host local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  nginx  sbin  share  src
  • 解压
[root@host ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz -C /usr/local
  • 修改目录/usr/local/mysql的属主属组
[root@host local]# chown -R mysql.mysql /usr/local/mysql/
[root@host local]# ll
总用量 0
drwxr-xr-x.  2 root  root    6 5月  18 2020 bin
drwxr-xr-x.  2 root  root    6 5月  18 2020 etc
drwxr-xr-x.  2 root  root    6 5月  18 2020 games
drwxr-xr-x.  2 root  root    6 5月  18 2020 include
drwxr-xr-x.  2 root  root    6 5月  18 2020 lib
drwxr-xr-x.  3 root  root   17 6月  21 12:23 lib64
drwxr-xr-x.  2 root  root    6 5月  18 2020 libexec
drwxr-xr-x.  9 mysql mysql 129 6月  24 06:08 mysql
drwxr-xr-x. 11 root  root  151 6月  23 06:41 nginx
drwxr-xr-x.  2 root  root    6 5月  18 2020 sbin
drwxr-xr-x.  5 root  root   49 6月  21 12:23 share
drwxr-xr-x.  2 root  root    6 5月  18 2020 src
  • 添加环境变量
[root@host local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@host local]# cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@host local]#  . /etc/profile.d/mysql.sh
[root@host local]# which mysql
/usr/local/mysql/bin/mysql
  • 建立数据存放目录
[root@host local]# mkdir /opt/mysql-data
[root@host local]# chown -R mysql.mysql /opt/mysql-data/
[root@host local]# ll /opt
总用量 0
drwxr-xr-x. 2 mysql mysql 6 6月  24 06:17 mysql-data
  • 初始化
[root@host local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/mysql-data/
2021-06-24T10:18:07.479919Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-24T10:18:07.817693Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-06-24T10:18:07.895037Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-06-24T10:18:07.958682Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 787e20f0-d4d5-11eb-b672-000c29b4ec0b.
2021-06-24T10:18:07.959777Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-06-24T10:18:09.090691Z 0 [Warning] CA certificate ca.pem is self signed.
2021-06-24T10:18:09.404916Z 1 [Note] A temporary password is generated for root@localhost: P;smYnqip7&o
  • 编写配置文件
[root@host local]# vim /etc/my.cnf
[mysqld]
datadir = /opt/mysql-data
basedir = /usr/local/mysql
datadir = /opt/mysql-data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysql-data/mysql.pid
user = mysql
skip-name-resolve
  • 配置服务启动脚本
[root@host local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@host local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@host local]#  sed -ri 's#^(datadir=).*#\1/opt/mysql-data#g' /etc/init.d/mysqld
[root@host local]# head -47 /etc/init.d/mysqld |tail -2
basedir=/usr/local/mysql
datadir=/opt/mysql-data
[root@host local]# service mysqld start
Starting MySQL.Logging to '/opt/mysql-data/host.err'.
 SUCCESS! 
  • 头文件和库文件配置
[root@host local]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@host local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@host local]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@host local]# ldconfig
  • 启动并设置密码
[root@host local]# yum -y install ncurses-compat-libs
[root@host local]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.33

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('xialuo123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit;
Bye

php配置

  • 安装并启动php
[root@host ~]# yum -y install php
[root@host ~]# service php-fpm start
Redirecting to /bin/systemctl start php-fpm.service
  • 监听端口并重启服务
[root@host ~]# vim /etc/php-fpm.d/www.conf
......
;listen = /run/php-fpm/www.sock
listen = 0.0.0.0:9000
......
[root@host ~]# systemctl restart php-fpm.service 
[root@host ~]# ss -antl
State        Recv-Q       Send-Q             Local Address:Port              Peer Address:Port       Process       
LISTEN       0            128                      0.0.0.0:9000                   0.0.0.0:*                        
LISTEN       0            128                      0.0.0.0:80                     0.0.0.0:*                        
LISTEN       0            128                      0.0.0.0:22                     0.0.0.0:*                        
LISTEN       0            80                             *:3306                         *:*                        
LISTEN       0            128                         [::]:22                        [::]:* 

标题配置虚拟主机

[root@host ~]# cd /usr/local/nginx/html/
[root@host html]# mv index.html index.php
[root@host html]# vim index.php 
<?php
       phpinfo();
?>

  • 修改nginx的主配置文件
[root@host ~]# vim /usr/local/nginx/conf/nginx.conf
http启用以下几列:
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
                      
server启用以下几列:
    server {
        listen       80;
        server_name  localhost;

        charset koi8-r;

        access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
     server {
        listen       80;
        server_name  192.168.149.152;

        charset koi8-r;

        access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;
            
启用以下几列:
  location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
修改:        
      location ~ \.php$ {
            root          /usr/local/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }  

测试
在这里插入图片描述

apache配置

安装依赖和其他软件

[root@host ~]# yum -y install openssl-devel pcre-devel expat-devel libtool wget make

解压安装包

[root@host src]# wget https://mirrors.bfsu.edu.cn/apache/apr/apr-1.7.0.tar.gz
[root@host src]# wget https://mirrors.bfsu.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
[root@host src]# wget https://mirrors.bfsu.edu.cn/apache/httpd/httpd-2.4.46.tar.gz
[root@host src]# tar xf apr-1.7.0.tar.gz 
[root@host src]# tar xf apr-util-1.6.1.tar.gz 
[root@host src]# tar xf httpd-2.4.46.tar.gz 

编辑,配置,安装

[root@host src]#cd apr-1.7.0/
[root@host apr-1.7.0]# vim configure
# $RM "$cfgfile"  //将此行加上注释,或者删除此行

配置
[root@host apr-1.7.0]# ./configure --prefix=/usr/local/apr
安装
[root@host apr-1.7.0]# make && make install
[root@host apr-1.7.0]# cd /usr/src/apr-util-1.6.1/
配置
[root@host apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
安装
[root@host apr-util-1.6.1]# make && make install
编译安装apache
[root@host src]# cd  httpd-2.4.46 
[root@host httpd-2.4.46]# ./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
[root@host httpd-2.4.46]# make && make install

关闭防火墙

[root@host ~]# systemctl stop firewalld
[root@host ~]# setenforce 0

启动apache

[root@host ~]# /usr/local/apache/bin/apachectl start

测试
在这里插入图片描述
配置nginx主机的nginx配置文件

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

在server上面编写
......
    upstream xialuo {
       server 192.168.149.140;
    }

    upstream xialuo1 {
       server 192.168.149.152;
    }
......
        location / {
            proxy_pass http://xialuo1;
        }
取消注销然后编写        
        location ~ \.php$ {
            proxy_pass   http://xialuo;
        }
......
[root@nginx ~]# nginx -s reload

apache配置

[root@apache ~]# cd /usr/local/apache/htdocs/
[root@apache htdocs]# echo 'xxhh' > index.html

测试
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值