企业实战分布式LNMP高性能服务器+redis

分布式实战(一):

实战环境:

  • nginx WEB服务器:192.168.2.10
  • PHP服务器:192.168.2.20
  • PHP服务器:192.168.2.30
  • MYCAT服务器:192.168.2.40
  • MYSQL主服务器:192.168.2.50
  • MySQL从服务器:192.168.2.60
  • redis服务器:192.168.2.70

部署流程:

1. 部署nginx:

1.1. 安装nginx:

[root@localhost ~]# yum  -y install nginx

1.2. 下载门户网站文件:

[root@localhost ~]# wget https://wordpress.org/wordpress-4.9.4.zip
[root@localhost ~]# unzip wordpress-4.9.5.zip -d /usr/share/nginx/html/

1.3. 配置nginx

[root@localhost ~]# vim /etc/nginx/nginx.conf
user  nginx;
worker_processes  auto;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  65;
    keepalive_requests 100;
    gzip  on;
    gzip_min_length      1k;
    gzip_types          text/plain   text/xml;
    gzip_comp_level     3;
    charset        utf-8;
    include /etc/nginx/conf.d/*.conf;
}
[root@localhost ~]# cp -a /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/www.wordpress.com.conf
[root@localhost ~]# vim /etc/nginx/conf.d/www.wordpress.com.conf

upstream  cgiwordpress  {
        server  192.168.2.20:9000;
        server  192.168.2.30:9000;
  }
server {
    listen       80;
    server_name  bog.wordpress.com;
    
    access_log  /var/log/nginx/wordpress.access.log  main;
    error_log  /var/log/nginx/wordpress.error.log   warn;
    
    location / {
        root   /usr/share/nginx/html/wordpress;
        index  index.php index.html index.htm;
    }   
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }   
    
    location ~ \.php$ {
        root           /data/nginx/html/wordpress;
        fastcgi_pass   cgiwordpress;
        fastcgi_next_upstream  error  http_500;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;  
    }   
}   

2. 部署PHP:

2.1. 安装PHP:

[root@localhost ~]# yum  -y install php php-fpm php-mysql php-devel
以上命令2.20和2.30服务器都需要执行

2.2. 配置php-fpm:

2.20服务器配置如下:
[root@localhost ~]# vim /etc/php-fpm.d/www.conf 
修改如下:
listen = 192.168.2.20:9000    
listen.allowed_clients = 192.168.2.10
user = nginx
group = nginx
2.30服务器配置如下;
[root@localhost ~]# vim /etc/php-fpm.d/www.conf 
修改如下:
listen = 192.168.2.30:9000    
listen.allowed_clients = 192.168.2.10
user = nginx
group = nginx 
D、E
2.20和2.30服务器配置如下:
[root@localhost ~]# useradd -rs  /sbin/nologin  nginx
[root@localhost ~]# mkdir -p /data/nginx/html
[root@localhost ~]# scp -r 192.168.2.10:/usr/share/nginx/html/wordpress  /data/nginx/html/
[root@localhost ~]# chown nginx. -R  /data/nginx/html/wordpress/
[root@localhost ~]# systemctl restart php-fpm.service 
[root@localhost ~]# netstat -ntlp

3. 配置MySQL主从:

3.1. 安装MySQL:

[root@localhost ~]# yum -y install mysql-community-server.x86_64

3.2. 修改配置文件:

2.20服务器配置:
[root@www ~]# vim /etc/my.cnf
在mysqld指令段添加以下内容:
server-id=1
log-bin=ys.bin
validate_password_policy=0    
validate_password_length=4 
2.30服务器配置:
[root@www ~]# vim /etc/my.cnf
在mysqld指令段添加以下内容:
server-id=2
validate_password_policy=0    
validate_password_length=4 

3.3. 授权从库:

[root@localhost ~]# systemctl start mysqld  启动服务:
[root@localhost ~]# grep "password"  /var/log/mysqld.log
2020-06-12T09:43:19.918327Z 1 [Note] A temporary password is generated for root@localhost: .2Lsr>r?)_CY
[root@localhost ~]# mysql -uroot -p
Enter password: .2Lsr>r?)_CY
mysql> alter user 'root'@'localhost' identified by '1234';   ---修改root密码
Query OK, 0 rows affected (0.01 sec)
mysql> show variables like "%log_bin";   -----查看log_bin和sql_log_bin是否均为on
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
| sql_log_bin   | ON    |
+---------------+-------+
2 rows in set (0.00 sec)
授权从库
mysql> grant replication slave on *.* to "ys"@"192.168.2.60" identified by "1234";
mysql> flush privileges;     ---刷新权限
Query OK, 0 rows affected (0.00 sec)
查看主库状态
mysql> show master status;
+-----------+----------+--------------+------------------+-------------------+
| File      | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------+----------+--------------+------------------+-------------------+
| ys.000001 |      306 |              |                  |                   |
+-----------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3.4. slave端配置:

root@localhost ~]# systemctl start mysqld  启动服务:
[root@localhost ~]# grep "password"  /var/log/mysqld.log
2020-06-12T09:43:19.918327Z 1 [Note] A temporary password is generated for root@localhost: .2Lsr>r?)_CY
[root@localhost ~]# mysql -uroot -p
Enter password: .2Lsr>r?)_CY
mysql> alter user 'root'@'localhost' identified by '1234';   ---修改root密码
Query OK, 0 rows affected (0.01 sec)
mysql>  change master to
    -> master_host="192.168.2.50",
    -> master_user="ys",
    -> master_password="1234",
    -> master_log_file=" ys.000001",
    -> master_log_pos=306;
mysql> start slave;  
查看slave状态:
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.50
                  Master_User: sxy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: ys.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: www-relay-bin.000001
                Relay_Log_Pos: 355
        Relay_Master_Log_File: ys.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 306
              Relay_Log_Space: 761
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 4f2cb611-a2ab-11ea-af22-000c29f12149
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

3.5. 授权mycat:

在主库执行授权信息,从库自动会同步:
mysql> grant all privileges on *.* to 'mycat-proxy'@'192.168.2.40' identified by '1234';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> create database wordpressdb default charset=utf8;
Query OK, 1 row affected (0.00 sec)

3.6. 配置mycat:

# 配置server.xml
[root@localhost ~]# cd /usr/local/mycat/
[root@localhost mycat]# cp conf/server.xml{,.bak}   备份server.xml文件
[root@localhost mycat]# vim conf/server.xml
# 默认管理用户,可读可写:
.....
<user name="mycat" defaultAccount="true">
                <property name="password">1234</property>
                <property name="schemas">wordpress</property>
</user>
#只读用户:
<user name="user">
                <property name="password">user</property>
                <property name="schemas">wordpress</property>
                <property name="readOnly">true</property>
        </user>
....
#配置schema.xml:
[root@localhost mycat]# cp conf/schema.xml{,.bak}
[root@localhost mycat]# vim conf/schema.xml
#设置逻辑库以及数据库节点
......
<schema name="wordpress" checkSQLschema="false" sqlMaxLimit="100" dataNote="dn1">
        </schema>
        <dataNode name="dn1" dataHost="localhost1" database="wordpressdb" />
        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
        				writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
负载均衡类型:
(1)balance="0", 不开启读写分离机制,所有读操作都发送到当前可用的writeHost 上。

(2)balance="1",全部的 readHost 与 stand by writeHost 参与 select 语句的负载均衡,简单的说,当双主双从模式(M1->S1,M2->S2,并且 M1 与 M2 互为主备),正常情况下,M2,S1,S2 都参与 select 语句的负载均衡。

(3)balance="2",所有读操作都随机的在 writeHost、 readhost 上分发。

(4)balance="3",所有读请求随机的分发到 wiriterHost 对应的 readhost 执行,writerHost 不负担读压力,注意 balance=3 只在 1.4 及其以后版本有,1.3 没有。
<heartbeat>select user()</heartbeat>
                <!-- can have multi write hosts -->
                <writeHost host="hostM1" url="192.168.2.50:3306" user="mycat-proxy"
                                   password="1234">
                        <!-- can have multi read hosts -->
                        <readHost host="hostS1" url="192.168.2.60:3306" 
                </writeHost>
                <writeHost host="hostM2" url="192.168.2.60:3306" user="mycat-proxy"
                                   password="1234" />
               
        </dataHost>

3.7. 启动服务:

[root@localhost ~]# mycat start
[root@localhost ~]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      3494/master         
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      3666/java           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1196/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      3494/master         
tcp6       0      0 :::1984                 :::*                    LISTEN      3666/java           
tcp6       0      0 :::8066                 :::*                    LISTEN      3666/java           
tcp6       0      0 :::9066                 :::*                    LISTEN      3666/java           
tcp6       0      0 :::45231                :::*                    LISTEN      3666/java           
tcp6       0      0 :::34869                :::*                    LISTEN      3666/java           
tcp6       0      0 :::22                   :::*                    LISTEN      1196/sshd 

4.验证:

  • 修改hosts的文件:C:\Windows\System32\drivers\etc\hosts
    添加如下:
    192.168.2.10 www.wordpress.com
  • 打开浏览器验证:
    在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

5. 安装redis

[root@localhost ~]# yum  -y install  https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
##修改yum源
[root@localhost ~]# vim /etc/yum.repos.d/remi.repo
修改如下:
[remi]
name=Remi's RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/remi/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/7/remi/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/remi/mirror
enabled=1        这里原本默认是0的修改为1开启
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
##安装redis
[root@localhost ~]# yum  -y install redis-5*

6. 安装php-redis连接驱动;

现在redis安装好了,但是还是没法缓存数据,因为php还没有于redis建立联系

[root@localhost ~]# yum -y install php-redis

7. 安装redis插件:

在wordpress后台,安装Redis Object Cache插件,并且启用,看到如果已连接,表示成功:
在这里插入图片描述

8. 在web服务上指定redis服务器IP地址:

[root@localhost ~]# vim /data/nginx/html/wordpress/wp-content/plugins/redis-cache/includes/object-cache.php
$parameters = array(
     'scheme' => 'tcp',
     'host' => '192.168.2.70',   这里填的时redis的IP地址
     'port' => 6379,
     'timeout' => 5,
     'read_timeout' => 5,
     'retry_interval' => null
   );

9.查看缓存信息:

[root@localhost ~]# redis-cli 
127.0.0.1:6379> SELECT 0
OK
127.0.0.1:6379> KEYS *
 1) "wp:site-transient:update_themes"
 2) "wp:userslugs:admin"
 3) "wp:users:1"
 4) "wp:site-transient:theme_roots"
 5) "wp:default:is_blog_installed"
 6) "wp:options:alloptions"
 7) "wp:options:can_compress_scripts"
 8) "wp:user_meta:1"
 9) "wp:site-transient:update_plugins"
10) "wp:site-transient:update_core"
11) "wp:options:notoptions"
12) "wp:userlogins:admin"
13) "wp:useremail:12345@qq.com"
可以看的出来redis已经连接成功。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
部署LNMP Redis 是一种常见的配置,在以下的点上,我将提供一个简单的指南来帮助部署LNMP Redis。 首先,确保您已经安装了Linux操作系统。您可以选择Ubuntu、CentOS等。 其次,安装Nginx(N)是部署LNMP堆栈的第一步。您可以通过运行适应您操作系统的命令来安装Nginx。例如,在Ubuntu上,您可以使用以下命令: ``` sudo apt-get update sudo apt-get install nginx ``` 然后,您需要安装MySQL(M)来处理数据库请求。您可以使用以下命令在Ubuntu上安装MySQL: ``` sudo apt-get install mysql-server ``` 接下来是PHP(P)的安装。LNMP中的PHP是用于处理后端逻辑的脚本语言。在Ubuntu上,您可以使用以下命令来安装PHP: ``` sudo apt-get install php-fpm php-mysql ``` 现在,您已经安装了LNMP堆栈所需的所有组件。 接下来,我们需要安装RedisRedis是内存数据库,用于高效地处理数据读写。您可以通过以下命令在Ubuntu上安装Redis: ``` sudo apt-get install redis-server ``` 安装完Redis后,您可以通过编辑配置文件来进行必要的更改。配置文件的位置可能会有所不同,但通常在 `/etc/redis/redis.conf` 中。 确保在配置文件中设置了适当的权限和安全性设置,例如允许远程连接、设置密码等。 完成以上步骤后,您需要重新启动相应的服务,使更改生效。您可以使用以下命令: ``` sudo service nginx restart sudo service mysql restart sudo service php-fpm restart sudo service redis-server restart ``` 然后,您可以通过访问nginx默认的HTML页面,确保Nginx和PHP已经正确安装。 总结:部署LNMP Redis可以通过依次安装NginxMySQL、PHP和Redis来实现。确保适当配置和启动相应的服务后,您可以通过访问网页来检查是否成功部署。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值