nginx负载均衡读写分离


nginx负载均衡读写分离

环境说明

主机名 IP地址 服务 系统
nginx 192.168.34.130 nginx centos8
lnmp 192.168.34.131 lnmp centos8
http 192.168.34.140 http centos8

nginx主机:源码部署nginx

[root@nginx ~]# systemctl  stop firewalld
[root@nginx ~]# getenforce 
Disabled
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel make gd-devel gcc gcc-c++
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx/
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@nginx src]# tar xf nginx-1.22.0.tar.gz -C /usr/local/
[root@nginx src]# cd /usr/local/nginx-1.22.0/
[root@nginx nginx-1.22.0]# ./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@nginx nginx-1.22.0]# make && make install 
[root@nginx nginx-1.22.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx nginx-1.22.0]#  source /etc/profile.d/nginx.sh 
[root@nginx nginx-1.22.0]# nginx
[root@nginx nginx-1.22.0]# nginx -s stop
[root@nginx nginx-1.22.0]# vim /usr/lib/systemd/system/nginx.service
[root@nginx nginx-1.22.]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target 
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP \$MAINPID
 
[Install]
WantedBy=multi-user.target
[root@nginx nginx-1.22.0]# systemctl daemon-reload
[root@nginx nginx-1.22.0]# systemctl enable --now nginx
修改配置文件实现动静分离
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
   upstream php.com {
        server 192.168.34.131:80;
    }

    upstream http.com {
        server 192.168.34.131:81;
        server 192.168.34.140:80;
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://http.com;
        }
        location ~ \.php$ {
            proxy_pass http://php.com;
        }
     }
     server {
        listen          81;
        server_name     localhost;
        location / {
            index    index.html;
       }

        }

lnmp主机

源码安装nginx
[root@lnmp ~]# systemctl  stop firewalld
[root@lnmp ~]# cat /etc/sysconfig/selinux
SELINUX=disabled
[root@lnmp ~]# yum -y install pcre-devel openssl openssl-devel make gd-devel gcc gcc-c++ vim wget 
[root@lnmp yum.repos.d]# yum -y install pcre-devel openssl openssl-devel make gd-devel gcc gcc-c++ vim wget 
[root@lnmp src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@lnmp src]# tar xf nginx-1.22.0.tar.gz -C /usr/local/
[root@lnmp src]# cd /usr/local/nginx-1.22.0/
[root@lnmp nginx-1.22.0]# ./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@lnmp nginx-1.22.0]# make && make install
[root@lnmp nginx-1.22.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp nginx-1.22.0]# source /etc/profile.d/nginx.sh 
[root@lnmp nginx-1.22.0]# nginx
[root@lnmp nginx-1.22.0]# nginx -s stop
[root@lnmp nginx-1.22.0]# vim /usr/lib/systemd/system/nginx.service
[root@lnmp nginx-1.22.0]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target 
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP \$MAINPID
 
[Install]
WantedBy=multi-user.target
[root@lnmp nginx-1.22.0]# systemctl daemon-reload
[root@lnmp nginx-1.22.0]# systemctl enable --now nginx
安装mysql
[root@lnmp ~]# yum -y install ncurses-devel ncurses-compat-libs  openssl-devel openssl cmake mariadb-devel
[root@lnmp ~]# groupadd -r -g 306 mysql
[root@lnmp ~]#  useradd -rMs /sbin/nologin -g 306 -u 306 mysql
[root@lnmp ~]#  cd /usr/src/
[root@lnmp src]# ls
debug    mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz  php-8.1.11.tar.gz
kernels  nginx-1.22.0.tar.gz
[root@lnmp src]# tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@lnmp src]# cd /usr/local/
[root@lnmp local]# ln -s mysql-8.0.28-linux-glibc2.12-x86_64/ mysql
[root@lnmp local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lnmp local]# source /etc/profile.d/mysql.sh 
[root@lnmp local]#  ln -s /usr/local/mysql/include /usr/include/mysql
[root@lnmp local]# vim /etc/man_db.conf 		//添加以下内容
MANDATORY_MANPATH                       /usr/local/mysql/man
[root@lnmp local]#  mkdir /opt/data
[root@lnmp local]# chown -R mysql.mysql /opt/data/
[root@lnmp local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
[root@lnmp local]# cat /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/data
socket=/tmp/mysql.sock
port=3306
pid-file=/opt/data/mysql.pid
user=mysql
skip-name-resolve
[root@lnmp local]#  cd /usr/local/mysql
[root@lnmp mysql]# cd support-files/
[root@lnmp support-files]# cp mysql.server /etc/init.d/mysqld
[root@lnmp support-files]# vim /etc/init.d/mysqld
basedir=/usr/local/mysqld
datadir=/opt/data
[root@lnmp support-files]#  chmod +x /etc/init.d/mysqld
[root@lnmp support-files]# service mysqld start
[root@lnmp support-files]# mysql -uroot -p'1<ipz5lNWrXi'
mysql> alter user 'root'@'localhost' identified by '123456';
安装php
[root@lnmp ~]#  yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@lnmp ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@lnmp ~]#  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 http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm sqlite-devel openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ libsqlite3x-devel libzip-devel
[root@lnmp ~]# cd /usr/src/
[root@lnmp src]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
[root@lnmp src]# tar xf php-8.1.11.tar.gz -C /usr/local/
[root@lnmp src]# cd /usr/local/php-8.1.11/
[root@lnmp php-8.1.11]# ./configure --prefix=/usr/local/php  --with-config-file-path=/etc  --enable-fpm    --disable-debug  --disable-rpath  --enable-shared  --enable-soap  --with-openssl  --enable-bcmath  --with-iconv  --with-bz2  --enable-calendar  --with-curl  --enable-exif   --enable-ftp  --enable-gd  --with-jpeg  --with-zlib-dir  --with-freetype  --with-gettext    --enable-mbstring  --enable-pdo  --with-mysqli=mysqlnd  --with-pdo-mysql=mysqlnd  --with-readline  --enable-shmop  --enable-simplexml  --enable-sockets  --with-zip  --enable-mysqlnd-compression-support  --with-pear  --enable-pcntl  --enable-posix
[root@lnmp php-8.1.11]# make && make install
[root@lnmp php]# cd /usr/local/php
[root@lnmp php]# cp etc/php-fpm.conf.default etc/php-fpm.conf 
[root@lnmp php]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
[root@lnmp php]# echo 'export PATH=/usr/local/php/bin:$PATH' > /etc/profile.d/php.sh
[root@lnmp php]# source /etc/profile.d/php.sh
[root@lnmp php]# vim  /etc/ld.so.conf.d/php.conf
[root@lnmp php]# cat /etc/ld.so.conf.d/php.conf
/usr/local/php/lib
[root@lnmp php]# ln -s /usr/local/php/include/ /usr/include/php
[root@lnmp php]# vim /lib/systemd/system/php.service
[root@lnmp php]# systemctl start php.service 
[root@lnmp php]# systemctl enable php.service 
[root@lnmp html]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen      81;
        server_name  localhost;
        location / {
            root  html;
            index index.html;
        }
    }
    server {
        listen       80;
        server_name  localhost;

        #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 $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }


添加php访问页面
[root@lnmp html]# cd /usr/local/nginx/html/
[root@lnmp html]# cat index.php 
<?php    
   phpinfo();
?>

http主机

安装http
[root@http ~]# dnf -y install httpd
[root@http ~]# cd /var/www/html/
[root@http html]# echo 'http' > index.html
[root@http html]# cat index.html
http

测试动静分离

posted @ 2022-10-19 23:51  Tqing  阅读(57)  评论(0编辑  收藏  举报