Nginx综合实验lnmp(1)
Nginx综合实验lnmp(1)

Nginx综合实验lnmp(1)

Nginx代理:

10.0.0.201
#脚本编译安装Nginx
[root@Ubuntu2004 ~]#apt -y install nginx
#修改nginx配置文件
[root@Ubuntu2004 conf.d]#vim ../nginx.conf
user  www-data;
http{
...
    include       /apps/nginx/conf/confd/*.conf
...
}

[root@Ubuntu2004 ~]#vim /etc/nginx/conf.d/proxy-www.shuhong.com.conf
upstream webservers {
    server 10.0.0.202:80 ;
    server 10.0.0.203:80;
}

server {
    listen 80;
    server_name www.shuhong.com;
    root /data/www/pc;
#    return 302 https://$server_name$request_uri;
    location / {
        proxy_pass http://webservers;
        proxy_set_header Host $http_host;
    }

}
#    server {
#    listen 443 ssl http2;
#    server_name www.shuhong.com;
#    ssl_certificate /apps/nginx/ssl/www.shuhong.com.pem;
#    ssl_certificate_key /apps/nginx/ssl/www.shuhong.com.key;
#    ssl_session_cache shared:sslcache:20m;
#    ssl_session_timeout 10m;
#    location / {
#        proxy_pass http://webservers;
#        proxy_set_header Host $http_host;
#    }
#}
                                                                                    
[root@Ubuntu2004 confd]#nginx -s reload

有道云安装

10.0.0.202(203)
[root@ubuntu2004 ~]#apt -y install nginx
[root@Ubuntu2004 ~]#apt -y install php7.4-fpm php7.4-mysql php7.4-json php7.4-xml php7.4-mbstring php7.4-zip php7.4-gd php7.4-curl php-redis

#下载有道云
[root@Ubuntu2004 ~]#wget https://static.kodcloud.com/update/download/kodbox.1.34.zip
[root@Ubuntu2004 ~]#mkdir /data/kodbox -p
[root@Ubuntu2004 ~]#unzip kodbox.1.34.zip -d /data/kodbox/

[root@Ubuntu2004 data]#mkdir -pv /data/php1
[root@Ubuntu2004 data]#vim /etc/nginx/conf.d/kod.conf
server {
    listen 80;
    server_name www.shuhong.com;
    root /data/php1;
    client_max_body_size 200M;
    location / {
        index index.php index.html;
    }
    location ~ \.php$|ping|php-status {
        root /data/php1;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

[root@Ubuntu2004 data]#vim /etc/php/7.4/fpm/pool.d/www.conf 
[root@Ubuntu2004 data]#egrep -v "^;|^$" /etc/php/7.4/fpm/pool.d/www.conf
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.status_path = /php-status
ping.path = /ping
ping.response = daodao
access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
slowlog = log/$pool.log.slow

[root@Ubuntu2004 data]#systemctl restart php7.4-fpm.service 
[root@Ubuntu2004 data]#cd /data/kodbox/
[root@Ubuntu2004 kodbox]#ll
总用量 192
drwxr-xr-x  7 root root   4096 9月  19 20:02 ./
drwxr-xr-x  5 root root   4096 9月  19 20:08 ../
drwxr-xr-x  8 root root   4096 9月  14 18:02 app/
-rw-r--r--  1 root root 162237 9月  14 18:01 Changelog.md
drwxr-xr-x  3 root root   4096 9月  14 18:02 config/
drwxr-xr-x  3 root root   4096 9月  14 18:02 data/
-rw-r--r--  1 root root    139 9月  14 18:01 index.php
drwxr-xr-x 17 root root   4096 9月  14 18:01 plugins/
drwxr-xr-x  6 root root   4096 9月  14 18:01 static/
[root@Ubuntu2004 kodbox]#mv * /data/php1/

[root@Ubuntu2004 data]#chown -R www-data. php1/

安装数据库Mysql和Redis

[root@ubuntu2004 ~]#apt-get update
[root@ubuntu2004 ~]#apt -y install mysql-server
[root@ubuntu2004 ~]#vim /etc/mysql/mysql.conf.d/mysqld.cnf 
#bind-address       = 127.0.0.1
#mysqlx-bind-address    = 127.0.0.1
[root@ubuntu2004 ~]#systemctl restart mysql.service 

[root@ubuntu2004 ~]#mysql
mysql> create user kmod@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> create database kmod;
Query OK, 1 row affected (0.01 sec)

mysql> grant all on kmod.* to kmod@'10.0.0.%';
Query OK, 0 rows affected (0.00 sec)

[root@ubuntu2004 ~]#apt -y install redis

前端配置MySQL和Redis数据库连接

配置另一台nginx+php服务器10.0.0.203实现负载

#10.0.0.203
[root@ubuntu2004 ~]#mkdir /data

#10.0.0.202
[root@Ubuntu2004 ~]#rsync -a /data/php1   root@10.0.0.203:/data/
[root@Ubuntu2004 ~]#rsync -a /etc/nginx/conf.d/kod.conf    root@10.0.0.203:/etc/nginx/conf.d/kod.conf
[root@Ubuntu2004 ~]#rsync -a /etc/php/7.4/fpm/pool.d/www.conf     root@10.0.0.203:/etc/php/7.4/fpm/pool.d/www.conf

#10.0.0.203
[root@ubuntu2004 ~]#systemctl restart nginx.service 
[root@ubuntu2004 ~]#systemctl restart php7.4-fpm.service 

创建NFS共享目录并挂载上传文件

[root@ubuntu2004 ~]#apt -y install nfs-kernel-server
[root@ubuntu2004 ~]#vim /etc/exports 
/data/www   *(rw)

[root@ubuntu2004 ~]#exportfs -r
exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/data/www".
  Assuming default behaviour ('no_subtree_check').
  NOTE: this default has changed since nfs-utils version 1.0.x

[root@ubuntu2004 ~]#exportfs -v
/data/www     	<world>(rw,wdelay,root_squash,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)


#10.0.0.202和10.0.0.203
[root@ubuntu2004 ~]#apt -y install nfs-common 
[root@ubuntu2004 ~]#showmount -e 10.0.0.204
Export list for 10.0.0.204:
/data/www *
[root@Ubuntu2004 ~]#vim /etc/fstab 
nfs.shuhong.com:/data/www/  /data/php1/data/files/  nfs  _netdev 0 0

测试上传文件

在两台web服务器上添加phpMyAdmin

[root@ubuntu2004 ~]#mkdir /data/php2
[root@ubuntu2004 ~]#wget https://files.phpmyadmin.net/phpMyAdmin/5.2.0/phpMyAdmin-5.2.0-all-languages.zip
[root@ubuntu2004 php2]#unzip phpMyAdmin-5.2.0-all-languages.zip -d /data/php2/
[root@ubuntu2004 php2]#mv phpMyAdmin-5.2.0-all-languages/* .
[root@ubuntu2004 data]#chown -R www-data. php2
[root@Ubuntu2004 php2]#cp config.sample.inc.php config.inc.php
[root@Ubuntu2004 php2]#vim config.inc.php 
$cfg['Servers'][$i]['host'] = '10.0.0.204';

[root@Ubuntu2004 data]#vim /etc/php/7.4/fpm/pool.d/www.conf 
php_value[session.save_handler] = redis
php_value[session.save_path] = "tcp://10.0.0.204:6379"
[root@Ubuntu2004 data]#systemctl restart php7.4-fpm.service 

[root@ubuntu2004 conf.d]#vim mydaim.conf 
server {
    listen 80;
    server_name www.shuhong.vip;
    root /data/php2;
    client_max_body_size 200M;
    index index.php index.html;
    location ~ \.php$|ping|php-status {
        root /data/php2;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
[root@ubuntu2004 conf.d]#nginx -s reload

#10.0.0.201修改配置,添加代理和负载均衡
[root@Ubuntu2004 ~]#vim /etc/nginx/conf.d/proxy-www.shuhong.com.conf 
server {
    listen 80;
    server_name www.shuhong.vip;
    root /data/www/pc;
    location / {
        proxy_pass http://webservers;
        proxy_set_header Host $http_host;
    }
}