Linux系统之LAMP实现

Linux系统之LAMP实现,第1张

概述1、部署分离的LAMP,部署到二台服务器上,php加载xcache模块 首先准备LAMP环境,准备两台Linux主机,一台是192.168.0.10 ,这台上面主要跑apache httpd ,mar

1、部署分离的LAMP,部署到二台服务器上,PHP加载xcache模块

   首先准备LAMP环境,准备两台linux主机,一台是192.168.0.10 ,这台上面主要跑apache httpd ,mariadb数据库,另外一台是192.168.0.30 ,这台上面跑PHP-fpm;

  1)在192.168.0.10这台主机上安装httpd和mariadb ,这个安装可以yum安装,也可以编译安装。我这里就yum安装来演示

[root@test-centos7-node1 ~]# ip a s ens332: ens33: <broADCAST,MulTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:f2:82:0c brd ff:ff:ff:ff:ff:ff    inet 192.168.0.10/24 brd 192.168.0.255 scope global ens33       valID_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fef2:820c/64 scope link        valID_lft forever preferred_lft forever[root@test-centos7-node1 ~]# yum install -y httpd mariadb-serverLoaded plugins: fastestmirrormy_base                                                            | 3.6 kB  00:00:00     Loading mirror speeds from cached hostfileepel/x86_64/primary_db                                             | 6.9 MB  00:00:00     Resolving DependencIEs--> Running transaction check---> Package httpd.x86_64 0:2.4.6-67.el7.centos will be installed--> Processing Dependency: httpd-tools = 2.4.6-67.el7.centos for package: httpd-2.4.6-67.el7.centos.x86_64--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-67.el7.centos.x86_64--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-67.el7.centos.x86_64--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-67.el7.centos.x86_64---> Package mariadb-server.x86_64 1:5.5.56-2.el7 will be installed--> Processing Dependency: mariadb(x86-64) = 1:5.5.56-2.el7 for package: 1:mariadb-server-5.5.56-2.el7.x86_64--> Processing Dependency: perl-DBI for package: 1:mariadb-server-5.5.56-2.el7.x86_64--> Processing Dependency: perl-dbd-mysql for package: 1:mariadb-server-5.5.56-2.el7.x86_64--> Processing Dependency: perl(Data::Dumper) for package: 1:mariadb-server-5.5.56-2.el7.x86_64--> Processing Dependency: perl(DBI) for package: 1:mariadb-server-5.5.56-2.el7.x86_64--> Processing Dependency: libaio.so.1(liBAIO_0.4)(64bit) for package: 1:mariadb-server-5.5.56-2.el7.x86_64--> Processing Dependency: libaio.so.1(liBAIO_0.1)(64bit) for package: 1:mariadb-server-5.5.56-2.el7.x86_64--> Processing Dependency: libaio.so.1()(64bit) for package: 1:mariadb-server-5.5.56-2.el7.x86_64--> Running transaction check---> Package apr.x86_64 0:1.4.8-3.el7 will be installed---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed---> Package httpd-tools.x86_64 0:2.4.6-67.el7.centos will be installed---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed---> Package mariadb.x86_64 1:5.5.56-2.el7 will be installed---> Package perl-dbd-mysql.x86_64 0:4.023-5.el7 will be installed---> Package perl-DBI.x86_64 0:1.627-4.el7 will be installed--> Processing Dependency: perl(RPC::PlServer) >= 0.2001 for package: perl-DBI-1.627-4.el7.x86_64--> Processing Dependency: perl(RPC::PlClIEnt) >= 0.2000 for package: perl-DBI-1.627-4.el7.x86_64---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be installed--> Running transaction check---> Package perl-PlRPC.noarch 0:0.2020-14.el7 will be installed--> Processing Dependency: perl(Net::Daemon) >= 0.13 for package: perl-PlRPC-0.2020-14.el7.noarch--> Processing Dependency: perl(Net::Daemon::Test) for package: perl-PlRPC-0.2020-14.el7.noarch--> Processing Dependency: perl(Net::Daemon::Log) for package: perl-PlRPC-0.2020-14.el7.noarch--> Processing Dependency: perl(Compress::Zlib) for package: perl-PlRPC-0.2020-14.el7.noarch--> Running transaction check---> Package perl-IO-Compress.noarch 0:2.061-2.el7 will be installed--> Processing Dependency: perl(Compress::Raw::Zlib) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch--> Processing Dependency: perl(Compress::Raw::Bzip2) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch---> Package perl-Net-Daemon.noarch 0:0.48-5.el7 will be installed--> Running transaction check---> Package perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 will be installed---> Package perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 will be installed--> Finished Dependency ResolutionDependencIEs Resolved========================================================================================== Package                       Arch         Version                   Repository     Size==========================================================================================Installing: httpd                         x86_64       2.4.6-67.el7.centos       my_base       2.7 M mariadb-server                x86_64       1:5.5.56-2.el7            my_base        11 MInstalling for dependencIEs: apr                           x86_64       1.4.8-3.el7               my_base       103 k apr-util                      x86_64       1.5.2-6.el7               my_base        92 k httpd-tools                   x86_64       2.4.6-67.el7.centos       my_base        87 k libaio                        x86_64       0.3.109-13.el7            my_base        24 k mailcap                       noarch       2.1.41-2.el7              my_base        31 k mariadb                       x86_64       1:5.5.56-2.el7            my_base       8.7 M perl-Compress-Raw-Bzip2       x86_64       2.061-3.el7               my_base        32 k perl-Compress-Raw-Zlib        x86_64       1:2.061-4.el7             my_base        57 k perl-dbd-mysql                x86_64       4.023-5.el7               my_base       140 k perl-DBI                      x86_64       1.627-4.el7               my_base       802 k perl-Data-Dumper              x86_64       2.145-3.el7               my_base        47 k perl-IO-Compress              noarch       2.061-2.el7               my_base       260 k perl-Net-Daemon               noarch       0.48-5.el7                my_base        51 k perl-PlRPC                    noarch       0.2020-14.el7             my_base        36 kTransaction Summary==========================================================================================Install  2 Packages (+14 Dependent packages)Total download size: 24 MInstalled size: 120 MDownloading packages:(1/16): apr-util-1.5.2-6.el7.x86_64.rpm                            |  92 kB  00:00:00     (2/16): apr-1.4.8-3.el7.x86_64.rpm                                 | 103 kB  00:00:00     (3/16): httpd-tools-2.4.6-67.el7.centos.x86_64.rpm                 |  87 kB  00:00:00     (4/16): libaio-0.3.109-13.el7.x86_64.rpm                           |  24 kB  00:00:00     (5/16): mailcap-2.1.41-2.el7.noarch.rpm                            |  31 kB  00:00:00     (6/16): httpd-2.4.6-67.el7.centos.x86_64.rpm                       | 2.7 MB  00:00:00     (7/16): mariadb-server-5.5.56-2.el7.x86_64.rpm                     |  11 MB  00:00:02     (8/16): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm             |  32 kB  00:00:00     (9/16): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm              |  57 kB  00:00:00     (10/16): mariadb-5.5.56-2.el7.x86_64.rpm                           | 8.7 MB  00:00:03     (11/16): perl-dbd-mysql-4.023-5.el7.x86_64.rpm                     | 140 kB  00:00:00     (12/16): perl-Data-Dumper-2.145-3.el7.x86_64.rpm                   |  47 kB  00:00:00     (13/16): perl-DBI-1.627-4.el7.x86_64.rpm                           | 802 kB  00:00:00     (14/16): perl-IO-Compress-2.061-2.el7.noarch.rpm                   | 260 kB  00:00:00     (15/16): perl-Net-Daemon-0.48-5.el7.noarch.rpm                     |  51 kB  00:00:00     (16/16): perl-PlRPC-0.2020-14.el7.noarch.rpm                       |  36 kB  00:00:00     ------------------------------------------------------------------------------------------Total                                                     5.2 MB/s |  24 MB  00:00:04     Running transaction checkRunning transaction testTransaction test succeededRunning transaction  Installing : apr-1.4.8-3.el7.x86_64                                                1/16   Installing : apr-util-1.5.2-6.el7.x86_64                                           2/16   Installing : perl-Data-Dumper-2.145-3.el7.x86_64                                   3/16   Installing : httpd-tools-2.4.6-67.el7.centos.x86_64                                4/16   Installing : 1:mariadb-5.5.56-2.el7.x86_64                                         5/16   Installing : libaio-0.3.109-13.el7.x86_64                                          6/16   Installing : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                            7/16   Installing : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                           8/16   Installing : perl-IO-Compress-2.061-2.el7.noarch                                   9/16   Installing : mailcap-2.1.41-2.el7.noarch                                          10/16   Installing : perl-Net-Daemon-0.48-5.el7.noarch                                    11/16   Installing : perl-PlRPC-0.2020-14.el7.noarch                                      12/16   Installing : perl-DBI-1.627-4.el7.x86_64                                          13/16   Installing : perl-dbd-mysql-4.023-5.el7.x86_64                                    14/16   Installing : 1:mariadb-server-5.5.56-2.el7.x86_64                                 15/16   Installing : httpd-2.4.6-67.el7.centos.x86_64                                     16/16   Verifying  : perl-dbd-mysql-4.023-5.el7.x86_64                                     1/16   Verifying  : perl-Net-Daemon-0.48-5.el7.noarch                                     2/16   Verifying  : apr-1.4.8-3.el7.x86_64                                                3/16   Verifying  : perl-Data-Dumper-2.145-3.el7.x86_64                                   4/16   Verifying  : httpd-2.4.6-67.el7.centos.x86_64                                      5/16   Verifying  : mailcap-2.1.41-2.el7.noarch                                           6/16   Verifying  : httpd-tools-2.4.6-67.el7.centos.x86_64                                7/16   Verifying  : apr-util-1.5.2-6.el7.x86_64                                           8/16   Verifying  : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64                           9/16   Verifying  : perl-PlRPC-0.2020-14.el7.noarch                                      10/16   Verifying  : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64                           11/16   Verifying  : 1:mariadb-server-5.5.56-2.el7.x86_64                                 12/16   Verifying  : libaio-0.3.109-13.el7.x86_64                                         13/16   Verifying  : perl-DBI-1.627-4.el7.x86_64                                          14/16   Verifying  : perl-IO-Compress-2.061-2.el7.noarch                                  15/16   Verifying  : 1:mariadb-5.5.56-2.el7.x86_64                                        16/16 Installed:  httpd.x86_64 0:2.4.6-67.el7.centos         mariadb-server.x86_64 1:5.5.56-2.el7        Dependency Installed:  apr.x86_64 0:1.4.8-3.el7                                                                  apr-util.x86_64 0:1.5.2-6.el7                                                             httpd-tools.x86_64 0:2.4.6-67.el7.centos                                                  libaio.x86_64 0:0.3.109-13.el7                                                            mailcap.noarch 0:2.1.41-2.el7                                                             mariadb.x86_64 1:5.5.56-2.el7                                                             perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7                                              perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7                                               perl-dbd-mysql.x86_64 0:4.023-5.el7                                                       perl-DBI.x86_64 0:1.627-4.el7                                                             perl-Data-Dumper.x86_64 0:2.145-3.el7                                                     perl-IO-Compress.noarch 0:2.061-2.el7                                                     perl-Net-Daemon.noarch 0:0.48-5.el7                                                       perl-PlRPC.noarch 0:0.2020-14.el7                                                       Complete![root@test-centos7-node1 ~]# 

  2)启动httpd和mariadb

[root@test-centos7-node1 ~]# systemctl start httpd mariadb[root@test-centos7-node1 ~]# ss -ntlState      Recv-Q Send-Q Local Address:Port               Peer Address:Port              ListEN     0      128               *:22                            *:*                  ListEN     0      100       127.0.0.1:25                            *:*                  ListEN     0      50                *:3306                          *:*                  ListEN     0      128              :::80                           :::*                  ListEN     0      128              :::22                           :::*                  ListEN     0      100             ::1:25                           :::*                  [root@test-centos7-node1 ~]# 

  3)测试httpd和mariadb是否可以正常使用

[root@test-centos7-node1 ~]# echo "this is test page" > /var/www/HTML/index.HTML[root@test-centos7-node1 ~]# curl -I http://192.168.0.10/index.HTMLhttp/1.1 200 OKDate: Mon,27 Jan 2020 11:51:44 GMTServer: Apache/2.4.6 (CentOS)Last-ModifIEd: Mon,27 Jan 2020 11:51:23 GMTETag: "12-59d1db9fd910a"Accept-Ranges: bytesContent-Length: 18Content-Type: text/HTML; charset=UTF-8[root@test-centos7-node1 ~]# MysqLWelcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection ID is 2Server version: 5.5.56-MariaDB MariaDB Servercopyright (c) 2000,2017,Oracle,MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || MysqL              || performance_schema || test               |+--------------------+4 rows in set (0.00 sec)MariaDB [(none)]> \qBye[root@test-centos7-node1 ~]# 

  提示:可以看到httdp和mariadb是可以正常访问的。接下来在192.168.0.30机器上安装PHP-fpm

[root@test-centos7-node3 ~]# ip a s ens332: ens33: <broADCAST,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:4a:bd:74 brd ff:ff:ff:ff:ff:ff    inet 192.168.0.30/24 brd 192.168.0.255 scope global ens33       valID_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe4a:bd74/64 scope link        valID_lft forever preferred_lft forever[root@test-centos7-node3 ~]# yum install -y PHP-fpmLoaded plugins: fastestmirrorbase                                                               | 3.6 kB  00:00:00     epel                                                               | 5.3 kB  00:00:00     extras                                                             | 2.9 kB  00:00:00     updates                                                            | 2.9 kB  00:00:00     (1/4): epel/x86_64/updateinfo                                      | 1.0 MB  00:00:00     (2/4): extras/7/x86_64/primary_db                                  | 159 kB  00:00:00     (3/4): updates/7/x86_64/primary_db                                 | 6.7 MB  00:00:01     (4/4): epel/x86_64/primary_db                                      | 6.9 MB  00:00:02     Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.aliyun.comResolving DependencIEs--> Running transaction check---> Package PHP-fpm.x86_64 0:5.4.16-46.1.el7_7 will be installed--> Processing Dependency: PHP-common(x86-64) = 5.4.16-46.1.el7_7 for package: PHP-fpm-5.4.16-46.1.el7_7.x86_64--> Running transaction check---> Package PHP-common.x86_64 0:5.4.16-46.1.el7_7 will be installed--> Processing Dependency: libzip.so.2()(64bit) for package: PHP-common-5.4.16-46.1.el7_7.x86_64--> Running transaction check---> Package libzip.x86_64 0:0.10.1-8.el7 will be installed--> Finished Dependency ResolutionDependencIEs Resolved========================================================================================== Package             Arch            Version                       Repository        Size==========================================================================================Installing: PHP-fpm             x86_64          5.4.16-46.1.el7_7             updates          1.4 MInstalling for dependencIEs: libzip              x86_64          0.10.1-8.el7                  base              48 k PHP-common          x86_64          5.4.16-46.1.el7_7             updates          565 kTransaction Summary==========================================================================================Install  1 Package (+2 Dependent packages)Total download size: 2.0 MInstalled size: 8.4 MDownloading packages:(1/3): libzip-0.10.1-8.el7.x86_64.rpm                              |  48 kB  00:00:00     (2/3): PHP-common-5.4.16-46.1.el7_7.x86_64.rpm                     | 565 kB  00:00:00     (3/3): PHP-fpm-5.4.16-46.1.el7_7.x86_64.rpm                        | 1.4 MB  00:00:00     ------------------------------------------------------------------------------------------Total                                                     3.1 MB/s | 2.0 MB  00:00:00     Running transaction checkRunning transaction testTransaction test succeededRunning transaction  Installing : libzip-0.10.1-8.el7.x86_64                                             1/3   Installing : PHP-common-5.4.16-46.1.el7_7.x86_64                                    2/3   Installing : PHP-fpm-5.4.16-46.1.el7_7.x86_64                                       3/3   Verifying  : PHP-common-5.4.16-46.1.el7_7.x86_64                                    1/3   Verifying  : libzip-0.10.1-8.el7.x86_64                                             2/3   Verifying  : PHP-fpm-5.4.16-46.1.el7_7.x86_64                                       3/3 Installed:  PHP-fpm.x86_64 0:5.4.16-46.1.el7_7                                                      Dependency Installed:  libzip.x86_64 0:0.10.1-8.el7            PHP-common.x86_64 0:5.4.16-46.1.el7_7           Complete![root@test-centos7-node3 ~]# 

  4)配置PHP-fpm工作在192.168.0.30的9000端口

[root@test-centos7-node3 ~]# rpm -ql PHP-fpm/etc/logrotate.d/PHP-fpm/etc/PHP-fpm.conf/etc/PHP-fpm.d/etc/PHP-fpm.d/www.conf/etc/sysconfig/PHP-fpm/run/PHP-fpm/usr/lib/systemd/system/PHP-fpm.service/usr/lib/tmpfiles.d/PHP-fpm.conf/usr/sbin/PHP-fpm/usr/share/doc/PHP-fpm-5.4.16/usr/share/doc/PHP-fpm-5.4.16/fpm_liCENSE/usr/share/doc/PHP-fpm-5.4.16/PHP-fpm.conf.default/usr/share/fpm/usr/share/fpm/status.HTML/usr/share/man/man8/PHP-fpm.8.gz/var/log/PHP-fpm[root@test-centos7-node3 ~]# grep -i "Listen" /etc/PHP-fpm.d/www.conf;   'ip.add.re.ss:port'    - to Listen on a TCP socket to a specific address on;   'port'                 - to Listen on a TCP socket to all addresses on a;   '/path/to/unix/socket' - to Listen on a unix socket.#Listen = 127.0.0.1:9000Listen = 192.168.0.30:9000; Set Listen(2) backlog. A value of '-1' means unlimited.;Listen.backlog = -1; PHP FCGI (5.2.2+). Makes sense only with a tcp Listening socket. Each address#Listen.allowed_clIEnts = 127.0.0.1;Listen.owner = nobody;Listen.group = nobody;Listen.mode = 0666[root@test-centos7-node3 ~]# 

  5)启动PHP-fpm

[root@test-centos7-node3 ~]# systemctl start PHP-fpm[root@test-centos7-node3 ~]# ss -ntlState      Recv-Q Send-Q Local Address:Port               Peer Address:Port              ListEN     0      128               *:22                            *:*                  ListEN     0      100       127.0.0.1:25                            *:*                  ListEN     0      128    192.168.0.30:9000                          *:*                  ListEN     0      128              :::22                           :::*                  ListEN     0      100             ::1:25                           :::*                  [root@test-centos7-node3 ~]# 

  提示:可以看到PHP-fpm启动起来了,并且监听在192.168.0.30的9000端口上,接下来我们配置httpd,请求.PHP结尾的资源都交给192.168.0.30:9000执行

  6)配置httpd的请求.PHP的资源能够连接至192.168.0.30:9000

[root@test-centos7-node1 ~]# httpd -M |grep fcgiAH00558: httpd: Could not reliably determine the server's fully qualifIEd domain name,using fe80::20c:29ff:fef2:820c. Set the 'Servername' directive globally to suppress this message proxy_fcgi_module (shared)[root@test-centos7-node1 ~]# cat /etc/httpd/conf.d/fcgi.confDirectoryIndex index.PHPProxyRequests OffProxyPassMatch ^/(.*\.PHP)$ fcgi://192.168.0.30:9000/var/www/HTML/[root@test-centos7-node1 ~]# 

  提示:如果没有加载proxy_fcgi_module需要先加载此模块,然后才能够代理fcgi协议

  7)在192.168.0.30上新建一个PHP测试页面,然后访问192.168.0.10/teset.PHP,如果能够将测试页面解析出来,说明httpd和PHP-fpm是连接正常的

[root@test-centos7-node3 ~]# ip a s ens33 2: ens33: <broADCAST,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:4a:bd:74 brd ff:ff:ff:ff:ff:ff    inet 192.168.0.30/24 brd 192.168.0.255 scope global ens33       valID_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe4a:bd74/64 scope link        valID_lft forever preferred_lft forever[root@test-centos7-node3 ~]# ll /var/www/HTMLls: cannot access /var/www/HTML: No such file or directory[root@test-centos7-node3 ~]# mkdir /var/www/HTML -p [root@test-centos7-node3 ~]# cat > /var/www/HTML/test.PHP <?PHPPHPinfo();?>^C[root@test-centos7-node3 ~]# cat /var/www/HTML/test.PHP <?PHPPHPinfo();?>[root@test-centos7-node3 ~]# 

  8)重新启动httpd,然后访问192.168.10/test.PHP

[root@test-centos7-node1 ~]# systemctl restart httpd[root@test-centos7-node1 ~]# curl -I http://192.168.0.10/test.PHPhttp/1.1 200 OKDate: Mon,27 Jan 2020 12:54:56 GMTServer: Apache/2.4.6 (CentOS)X-Powered-By: PHP/5.4.16Content-Type: text/HTML; charset=UTF-8[root@test-centos7-node1 ~]# 

  提示:到此LAP环境已经搭建好了,但是PHP-fpm是不支持xcode的,接下来我们来安装xcache模块

  9)安装xcache模块

[root@test-centos7-node3 ~]# yum install -y PHP-xcacheLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.aliyun.comResolving DependencIEs--> Running transaction check---> Package PHP-xcache.x86_64 0:3.1.1-1.el7 will be installed--> Finished Dependency ResolutionDependencIEs Resolved========================================================================================== Package                Arch               Version                 Repository        Size==========================================================================================Installing: PHP-xcache             x86_64             3.1.1-1.el7             epel              72 kTransaction Summary==========================================================================================Install  1 PackageTotal download size: 72 kInstalled size: 187 kDownloading packages:PHP-xcache-3.1.1-1.el7.x86_64.rpm                                  |  72 kB  00:00:00     Running transaction checkRunning transaction testTransaction test succeededRunning transaction  Installing : PHP-xcache-3.1.1-1.el7.x86_64                                          1/1   Verifying  : PHP-xcache-3.1.1-1.el7.x86_64                                          1/1 Installed:  PHP-xcache.x86_64 0:3.1.1-1.el7                                                         Complete![root@test-centos7-node3 ~]#

  提示:当然xcache这个模块我们可以编译安装。

  10)重启PHP-fpm,然后访问测试页,看看是不是支持xcache

[root@test-centos7-node3 ~]# systemctl restart PHP-fpm[root@test-centos7-node3 ~]# 

   提示:通过访问测试页面,我们看到了PHP-fpm支持了xcache,到此PHP部分就搭建完毕了,接下来我们要来利用PHP连接数据库

  11)安装PHP-MysqL 此包用于PHP连接MysqL数据库

[root@test-centos7-node3 ~]# yum install -y PHP-MysqLLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.aliyun.comResolving DependencIEs--> Running transaction check---> Package PHP-MysqL.x86_64 0:5.4.16-46.1.el7_7 will be installed--> Processing Dependency: PHP-pdo(x86-64) = 5.4.16-46.1.el7_7 for package: PHP-MysqL-5.4.16-46.1.el7_7.x86_64--> Running transaction check---> Package PHP-pdo.x86_64 0:5.4.16-46.1.el7_7 will be installed--> Finished Dependency ResolutionDependencIEs Resolved========================================================================================== Package             Arch             Version                     Repository         Size==========================================================================================Installing: PHP-MysqL           x86_64           5.4.16-46.1.el7_7           updates           101 kInstalling for dependencIEs: PHP-pdo             x86_64           5.4.16-46.1.el7_7           updates            99 kTransaction Summary==========================================================================================Install  1 Package (+1 Dependent package)Total download size: 201 kInstalled size: 424 kDownloading packages:(1/2): PHP-MysqL-5.4.16-46.1.el7_7.x86_64.rpm                      | 101 kB  00:00:00     (2/2): PHP-pdo-5.4.16-46.1.el7_7.x86_64.rpm                        |  99 kB  00:00:00     ------------------------------------------------------------------------------------------Total                                                     513 kB/s | 201 kB  00:00:00     Running transaction checkRunning transaction testTransaction test succeededRunning transaction  Installing : PHP-pdo-5.4.16-46.1.el7_7.x86_64                                       1/2   Installing : PHP-MysqL-5.4.16-46.1.el7_7.x86_64                                     2/2   Verifying  : PHP-MysqL-5.4.16-46.1.el7_7.x86_64                                     1/2   Verifying  : PHP-pdo-5.4.16-46.1.el7_7.x86_64                                       2/2 Installed:  PHP-MysqL.x86_64 0:5.4.16-46.1.el7_7                                                    Dependency Installed:  PHP-pdo.x86_64 0:5.4.16-46.1.el7_7                                                      Complete![root@test-centos7-node3 ~]# 

  12)在192.168.0.10的mariadb数据库里添加一个帐号,用于PHP测试连接数据库用

[root@test-centos7-node1 ~]# MysqLWelcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection ID is 4Server version: 5.5.56-MariaDB MariaDB Servercopyright (c) 2000,MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> grant all on *.* to 'PHP_test'@'192.168.0.%' IDentifIEd by 'admin';query OK,0 rows affected (0.00 sec)MariaDB [(none)]> flush privileges;query OK,0 rows affected (0.00 sec)MariaDB [(none)]> \qBye[root@test-centos7-node1 ~]# 

  13)在PHP-fpm服务器上添加一个测试页,用于测试PHP-fpm是否能够连接数据库

[root@test-centos7-node3 HTML]# pwd/var/www/HTML[root@test-centos7-node3 HTML]# lltotal 8-rw-r--r-- 1 root root 159 Feb 14 07:21 test_conn_MysqL.PHP-rw-r--r-- 1 root root  20 Feb 14 06:40 test.PHP[root@test-centos7-node3 HTML]# cat test_conn_MysqL.PHP <?PHP$MysqLi=new MysqLi("192.168.0.10","PHP_test","admin");if(MysqLi_connect_errno()){echo "Failure";$MysqLi=null;exit;}echo "OK\n";$MysqLi->close();?>[root@test-centos7-node3 HTML]# 

  14)重启PHP-fpm,然后再访问http://192.168.0.10/test_conn_MysqL.PHP

[root@test-centos7-node3 HTML]# curl http://192.168.0.10/test_conn_MysqL.PHPOK[root@test-centos7-node3 HTML]# 

  提示:可以看到我们访问测试连接数据库的脚本文件能够正常连接到数据库,到此LAMP的环境就搭建完毕了。

2、部署wordpress论坛,并实现正常访问登录论坛。

   1)下载wordpress源码包,然后把源码放到我们刚才搭建的LAMP环境中。

[root@test-centos7-node1 ~]# ip a s ens332: ens33: <broADCAST,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:f2:82:0c brd ff:ff:ff:ff:ff:ff    inet 192.168.0.10/24 brd 192.168.0.255 scope global ens33       valID_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fef2:820c/64 scope link        valID_lft forever preferred_lft forever[root@test-centos7-node1 ~]# cd /var/www/HTML/[root@test-centos7-node1 HTML]# ls[root@test-centos7-node1 HTML]# rzrz waiting to receive. zmodem trl+C ȡ  100%    7989 KB 7989 KB/s 00:00:01       0 Errorstar.gz...Transferring wordpress-4.5.1-zh_CN.tar.gz...[root@test-centos7-node1 HTML]# lswordpress-4.5.1-zh_CN.tar.gz[root@test-centos7-node1 HTML]# tar xf wordpress-4.5.1-zh_CN.tar.gz [root@test-centos7-node1 HTML]# 

  提示:在192.168.0.10上放一份源码的原因是httpd的静态页面是不会往后面的PHP-fpm服务器转的,所以我们放程序源码除了httpd上要放一份外,PHP-fpm服务器上也要放一份

[root@test-centos7-node3 ~]# ip a s ens332: ens33: <broADCAST,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 00:0c:29:4a:bd:74 brd ff:ff:ff:ff:ff:ff    inet 192.168.0.30/24 brd 192.168.0.255 scope global ens33       valID_lft forever preferred_lft forever    inet6 fe80::20c:29ff:fe4a:bd74/64 scope link        valID_lft forever preferred_lft forever[root@test-centos7-node3 ~]# cd /var/www/HTML/[root@test-centos7-node3 HTML]# lspdo.PHP  test_conn_MysqL.PHP  test.PHP[root@test-centos7-node3 HTML]# rm -rf *[root@test-centos7-node3 HTML]# ls[root@test-centos7-node3 HTML]# rzrz waiting to receive. zmodem trl+C ȡ  100%    7989 KB 7989 KB/s 00:00:01       0 Errorstar.gz...[root@test-centos7-node3 HTML]# tar xf wordpress-4.5.1-zh_CN.tar.gz [root@test-centos7-node3 HTML]# lswordpress  wordpress-4.5.1-zh_CN.tar.gz[root@test-centos7-node3 HTML]# 

  提示:放好源码文件后,我们要在数据库里创建一个库来存放WordPress程序的数据

  2)在数据库里创建一个数据库用于存放wordpress的各种数据

[root@test-centos7-node1 HTML]# MysqLWelcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection ID is 36Server version: 5.5.56-MariaDB MariaDB Servercopyright (c) 2000,MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || MysqL              || performance_schema || test               |+--------------------+4 rows in set (0.00 sec)MariaDB [(none)]> create database wordpress;query OK,1 row affected (0.00 sec)MariaDB [(none)]> grant all on  wordpress.* to wordpress@'192.168.0.%' IDentifIEd by 'admin';query OK,0 rows affected (0.01 sec)MariaDB [(none)]> \qBye[root@test-centos7-node1 HTML]# 

  提示:为了安全,我们在数据库里专门创建了一个用户用于WordPress程序连接数据库用

  接下来我们就可以通过浏览器来安装WordPress

   提示:它提示不能写入wp-config.php,我们需要对httpd运行的用户来进行授权,让其对wordpress目录有写 *** 作即可

[root@test-centos7-node3 HTML]# setfacl -R -m u:apache:rwx wordpress [root@test-centos7-node3 HTML]# getfacl wordpress# file: wordpress# owner: nobody# group: 65534user::rwxuser:apache:rwxgroup::r-xmask::rwxother::r-x[root@test-centos7-node3 HTML]# 

  提示:我们授权其实在PHP-fpm上面授权即可。授权后我们就可以刷新刚才的安装页面,进行安装

    到此wordpress就搭建完毕了,后续就可以通过以上界面管理wordpress

3、收集apache访问日志,并实现图形化展示。

   首先说一下实现过程吧,要想图形化展示日志,我们可以利用rsyslog的前端展示工具loganalyzer来展示。用这个工具它是基于LAMP架构来实现的,在这之前我们先把apache的访问日志交给rsyslog,rsyslog再把对应的日志写到数据库里,然后通过日志展示工具loganalyzer去数据库把日志对出来,然后展示出来。

  1)配置httpd的日志,把错误日志传给syslog:local1,把访问日志通过管道传给logger,然后logger再把它传给 local2

[root@test-centos7-node1 ~]# grep -E "^Error" /etc/httpd/conf/httpd.confErrorLog syslog:local1[root@test-centos7-node1 ~]# grep -E "^[[:space:]]*CustomLog" /etc/httpd/conf/httpd.conf         CustomLog "|/usr/bin/logger -p local2.info" combined[root@test-centos7-node1 ~]# 

  提示:以上配置就是把httpd的访问日志传给local1 ,把访问日志传给local2,local1和local2是rsyslog的自定义日志设施

  2)安装rsyslog-MysqL,这个包主要是用于rsyslog连接数据库的一个模块,除此以外还有一个创建数据库和表的sql脚本,用此脚本可以创建存放日志的库和表

[root@test-centos7-node1 ~]# yum install -y rsyslog-MysqLLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfileResolving DependencIEs--> Running transaction check---> Package rsyslog-MysqL.x86_64 0:8.24.0-12.el7 will be installed--> Finished Dependency ResolutionDependencIEs Resolved================================================================================================================================== Package                           Arch                       Version                           Repository                   Size==================================================================================================================================Installing: rsyslog-MysqL                     x86_64                     8.24.0-12.el7                     my_base                      35 kTransaction Summary==================================================================================================================================Install  1 PackageTotal download size: 35 kInstalled size: 20 kDownloading packages:rsyslog-MysqL-8.24.0-12.el7.x86_64.rpm                                                                     |  35 kB  00:00:00     Running transaction checkRunning transaction testTransaction test succeededRunning transaction  Installing : rsyslog-MysqL-8.24.0-12.el7.x86_64                                                                             1/1   Verifying  : rsyslog-MysqL-8.24.0-12.el7.x86_64                                                                             1/1 Installed:  rsyslog-MysqL.x86_64 0:8.24.0-12.el7                                                                                            Complete![root@test-centos7-node1 ~]# rpm -ql rsyslog-MysqL/usr/lib64/rsyslog/omMysqL.so/usr/share/doc/rsyslog-8.24.0/MysqL-createDB.sql[root@test-centos7-node1 ~]#

  3)导入数据库和表,然后在其数据库里创建一个用于rsyslog连接数据库的专有账号

[root@test-centos7-node1 ~]# rpm -ql rsyslog-MysqL/usr/lib64/rsyslog/omMysqL.so/usr/share/doc/rsyslog-8.24.0/MysqL-createDB.sql[root@test-centos7-node1 ~]# MysqL < /usr/share/doc/rsyslog-8.24.0/MysqL-createDB.sql[root@test-centos7-node1 ~]# MysqLWelcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection ID is 60Server version: 5.5.56-MariaDB MariaDB Servercopyright (c) 2000,MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || Syslog             || MysqL              || performance_schema || test               || wordpress          |+--------------------+6 rows in set (0.00 sec)MariaDB [(none)]> use SyslogReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [Syslog]> show tables;+------------------------+| tables_in_Syslog       |+------------------------+| SystemEvents           || SystemEventsPropertIEs |+------------------------+2 rows in set (0.00 sec)MariaDB [Syslog]> grant all on Syslog.* to rsyslog@'192.168.0.%' IDentifIEd by 'admin';query OK,0 rows affected (0.00 sec)MariaDB [Syslog]> flush privileges;query OK,0 rows affected (0.00 sec)MariaDB [Syslog]> \qBye[root@test-centos7-node1 ~]# 

  4)配置rsyslog加载omMysqL模块将local1、local2收集的日志存入数据库中

[root@test-centos7-node1 ~]# grep "omMysqL" /etc/rsyslog.conf$ModLoad omMysqLlocal1.*                                                :omMysqL:192.168.0.10,Syslog,rsyslog,adminlocal2.*                                                :omMysqL:192.168.0.10,admin[root@test-centos7-node1 ~]# 

  5)重启rsyslog和httpd后访问httpd服务,然后在其数据库里查看对应的表里是否存有日志

[root@test-centos7-node1 ~]# systemctl restart rsyslog httpd      [root@test-centos7-node1 ~]# curl -I http://192.168.0.10/wordpresshttp/1.1 301 Moved PermanentlyDate: Mon,27 Jan 2020 17:08:30 GMTServer: Apache/2.4.6 (CentOS)Location: http://192.168.0.10/wordpress/Content-Type: text/HTML; charset=iso-8859-1[root@test-centos7-node1 ~]# MysqLWelcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection ID is 12Server version: 5.5.56-MariaDB MariaDB Servercopyright (c) 2000,MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> show databases;+--------------------+| Database           |+--------------------+| information_schema || Syslog             || MysqL              || performance_schema || test               || wordpress          |+--------------------+6 rows in set (0.00 sec)MariaDB [(none)]> use SyslogReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [Syslog]> show tables;+------------------------+| tables_in_Syslog       |+------------------------+| SystemEvents           || SystemEventsPropertIEs |+------------------------+2 rows in set (0.00 sec)MariaDB [Syslog]> select * from SystemEvents;+----+------------+---------------------+---------------------+----------+----------+--------------------+--------------------------------------------------------------------------------------------------+------------+------------+-------------+-----------+---------------+---------+-----------------+--------------+-----------+----------+----------+------------+-----------+--------------+-----------------+----------+| ID | CustomerID | ReceivedAt          | DeviceReportedTime  | Facility | Priority | FromHost           | Message                                                                                          | NTSeverity | importance | EventSource | EventUser | Eventcategory | EventID | EventBinaryData | MaxAvailable | CurrUsage | MinUsage | MaxUsage | InfoUnitID | SysLogTag | EventLogType | Genericfilename | SystemID |+----+------------+---------------------+---------------------+----------+----------+--------------------+--------------------------------------------------------------------------------------------------+------------+------------+-------------+-----------+---------------+---------+-----------------+--------------+-----------+----------+----------+------------+-----------+--------------+-----------------+----------+|  1 |       NulL | 2020-01-27 12:08:30 | 2020-01-27 12:08:30 |       18 |        6 | test-centos7-node1 | 192.168.0.10 - - [27/Jan/2020:12:08:30 -0500] "head /wordpress http/1.1" 301 - "-" "curl/7.29.0" |       NulL |       NulL | NulL        | NulL      |          NulL |    NulL | NulL            |         NulL |      NulL |     NulL |     NulL |          1 | logger:   | NulL         | NulL            |     NulL |+----+------------+---------------------+---------------------+----------+----------+--------------------+--------------------------------------------------------------------------------------------------+------------+------------+-------------+-----------+---------------+---------+-----------------+--------------+-----------+----------+----------+------------+-----------+--------------+-----------------+----------+1 row in set (0.00 sec)MariaDB [Syslog]> 

  提示:可以看到数据库里已经有我们刚才的访问日志了。到此收集日志记录到数据库的过程就实现了,接下来我们把前端展示工具搭建好就可以把数据库里的日志展示出来

  6)上传loganalyzer到/var/ww/HTML/下,并解压(注意在httpd服务器上放一份的同时PHP-fpm服务器上也放一份)

[root@test-centos7-node1 HTML]# lswordpress[root@test-centos7-node1 HTML]# rzrz waiting to receive. zmodem trl+C ȡ  100%    1022 KB 1022 KB/s 00:00:01       0 Errorsgz...[root@test-centos7-node1 HTML]# tar xf loganalyzer-3.6.5.tar.gz [root@test-centos7-node1 HTML]# lsloganalyzer-3.6.5  loganalyzer-3.6.5.tar.gz  wordpress[root@test-centos7-node1 HTML]# cd loganalyzer-3.6.5[root@test-centos7-node1 loganalyzer-3.6.5]# lsChangeLog  contrib  copYING  doc  INSTALL  src[root@test-centos7-node1 loganalyzer-3.6.5]# cp -a src ../log[root@test-centos7-node1 loganalyzer-3.6.5]# cd ..[root@test-centos7-node1 HTML]# lslog  loganalyzer-3.6.5  loganalyzer-3.6.5.tar.gz  wordpress[root@test-centos7-node1 HTML]# scp -r log 192.168.0.30:/var/www/HTML/root@192.168.0.30's password: install.PHP                                                                                     100%   41KB   1.0MB/s   00:00    export.PHP                                                                                      100%   12KB 309.3KB/s   00:00    searchhelpers.Js                                                                                100% 6181     1.5MB/s   00:00    common.Js                                                                                       100% 9434     2.0MB/s   00:00    reports.PHP                                                                                     100% 4146   110.2KB/s   00:00    details.PHP                                                                                     100%   16KB   2.6MB/s   00:00    defaults.CSS                                                                                    100% 2039   521.7KB/s   00:00    highlight.CSS                                                                                   100% 1032   314.3KB/s   00:00    menu.CSS                                                                                        100% 1194    25.9KB/s   00:00    msgparser.class.PHP                                                                             100% 2425    73.3KB/s   00:00    logstreamconfigmongodb.class.PHP                                                                100% 2573   420.8KB/s   00:00    logstreamconfig.class.PHP                                                                       100% 5439   904.0KB/s   00:00    msgparser.apache2common.class.PHP                                ……省略部分内容

  7)用浏览器访问192.168.0.10/log安装loganalyzer

   提示:上面安装界面提示我们没有config.PHP文件,需要我们新建一个这样的文件,并且权限得是666的权限

[root@test-centos7-node3 ~]# cd /var/www/HTML/[root@test-centos7-node3 HTML]# lslog  wordpress[root@test-centos7-node3 HTML]# cd log/[root@test-centos7-node3 log]# lsadmin               classes      details.PHP  images       Js                   reports.PHP     themesasktheoracle.PHP    convert.PHP  doc          include      lang                 search.PHP      userchange.PHPBitstreamVeraFonts  cron         export.PHP   index.PHP    login.PHP            statistics.PHPchartgenerator.PHP  CSS          favicon.ico  install.PHP  reportgenerator.PHP  templates[root@test-centos7-node3 log]# touch config.PHP[root@test-centos7-node3 log]# chmod 666 config.PHP[root@test-centos7-node3 log]# 

  提示:把需要的文件创建好,授好权后,我们点击recheck

    提示:安装完毕后,它提示我们错误500,我们知道错误500是服务器内部错误,我们需要在PHP-fpm上面找原因,我们先看看PHP-fpm的错误日志吧

[root@test-centos7-node3 log]# cd /var/log/PHP-fpm/[root@test-centos7-node3 PHP-fpm]# lserror.log  www-error.log[root@test-centos7-node3 PHP-fpm]# tail www-error.log [14-Feb-2020 16:00:06 UTC] PHP Notice:  Undefined index: VIEwStringCharacterlimit in /var/www/HTML/log/install.PHP on line 800[14-Feb-2020 16:00:06 UTC] PHP Notice:  Undefined index: VIEwEntrIEsPerPage in /var/www/HTML/log/install.PHP on line 801[14-Feb-2020 16:00:06 UTC] PHP Notice:  Undefined index: VIEwEnableDetailPopups in /var/www/HTML/log/install.PHP on line 802[14-Feb-2020 16:00:06 UTC] PHP Notice:  Undefined index: EnableIPAddressResolve in /var/www/HTML/log/install.PHP on line 803[14-Feb-2020 16:00:06 UTC] PHP Notice:  Undefined index: UserDBEnabled in /var/www/HTML/log/install.PHP on line 904[14-Feb-2020 16:00:06 UTC] PHP Warning:  UnkNown: open(/var/lib/PHP/session/sess_b7f7ckbngimv44nhs20dpl7807,O_RDWR) Failed: No such file or directory (2) in UnkNown on line 0[14-Feb-2020 16:00:06 UTC] PHP Warning:  UnkNown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/PHP/session) in UnkNown on line 0[14-Feb-2020 16:00:48 UTC] PHP Parse error:  Syntax error,unexpected ';' in /var/www/HTML/log/config.PHP on line 86[14-Feb-2020 16:00:48 UTC] PHP Warning:  UnkNown: open(/var/lib/PHP/session/sess_b7f7ckbngimv44nhs20dpl7807,O_RDWR) Failed: No such file or directory (2) in UnkNown on line 0[14-Feb-2020 16:00:48 UTC] PHP Warning:  UnkNown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/PHP/session) in UnkNown on line 0[root@test-centos7-node3 PHP-fpm]# 

  说明:PHP-fpm错误日志提示我们说/var/www/HTML/log/config.PHP的第86行语法错误

    提示:前三个值是我们设置安装loganalyzer的第三步,它这里没有写进来,我们需要手动填一下即可

@H_133_301@

   提示:修改好后,我们在用浏览器访问下192.168.0.10/log看看问题解决没有

   提示:还是告诉我们错误500,我们再来看看PHP-fpm的错误日志,看看它提示我们那里错了

   提示:它又告诉我们99行有错误,我们去看看呢

   提示:这个错误和上面一样,没有给值,我们可以从后面的解释看看,这个表示什么;它说如果启用,将自动解析IP地址内联消息,并将结果添加到IP后面的括号{}中;那说明这选项要么启动,要么关闭,那我们就填一个1启用嘛

   提示:我们改完配置文件后,保存在用浏览器访问192.168.0.10/log看看现在是否可以访问了

   提示:可以看到是可以访问了,我们看到我们访问httpd的日志都有记录,并显示在我们搭建的loganalyzer上面

  我们点击statistics可能没有图标展示,这是因为我们的服务器上没有安装PHP-gd这个包,安装好了,重启PHP-fpm后,在刷新就有了

[root@test-centos7-node3 log]# yum install PHP-gd -y[root@test-centos7-node3 log]# systemctl restart PHP-fpm

  提示:我们才搭建好数据量很少,所以看到画的图也不是很好。我们可以把其他主机的日志也接入到数据库中,当数据量比较大时,我们看统计图就可以看到那个主机的日志占比等等信息。如下

    到此我们就把httpd的访问日志用rsyslog给收集到数据库中,然后通过loganalyzer 给图形化展示出来了。 

总结

以上是内存溢出为你收集整理的Linux系统之LAMP实现全部内容,希望文章能够帮你解决Linux系统之LAMP实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1014646.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存