Linux相关知识的第二十二回合

Linux相关知识的第二十二回合

1、部署分离的LAMP,部署到二台服务器上,php加载xcache模块
2、部署wordpress论坛,并实现正常访问登录论坛
3、收集apache访问日志,并实现图形化展示

部署分离的LAMP

部署说明:
应用服务器:192.168.168.66
数据库服务器:192.168.168.211

部署应用服务器

# 安装apache、php、xcache
yum -y install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
yum -y install httpd php56-php php56-php-devel php56-php-mysql gcc
cd /data/soft
# 上传xcache包,并解压
wget -c https://github.com/lighttpd/xcache/archive/refs/tags/3.2.0.tar.gz
tar -zxvf xcache-3.2.0.tar.gz
cd xcache-3.2.0/
/opt/remi/php56/root/usr/bin/phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
echo $?
0
./configure --enable-xcache --with-php-config=/opt/remi/php56/root/usr/bin/php-config
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /opt/remi/php56/root/usr
checking for PHP includes... -I/opt/remi/php56/root/usr/include/php -I/opt/remi/php56/root/usr/include/php/main -I/opt/remi/php56/root/usr/include/php/TSRM -I/opt/remi/php56/root/usr/include/php/Zend -I/opt/remi/php56/root/usr/include/php/ext -I/opt/remi/php56/root/usr/include/php/ext/date/lib
checking for PHP extension directory... /opt/remi/php56/root/usr/lib64/php/modules
checking for PHP installed headers prefix... /opt/remi/php56/root/usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... gawk
checking for XCache support... yes, shared
checking for XCache handle of compile time constant... yes
checking for XCache optimizer... no
checking for XCache coverager... no
checking for XCache assembler... no
checking for XCache disassembler... no
checking for XCache encoder... no
checking for XCache decoder... no
checking for XCache self test... no
checking for XCache self test... no
checking for gawk... /bin/gawk
checking for m4... /bin/m4
checking for grep... (cached) /bin/grep
checking for sed... (cached) /bin/sed
checking for indent... no
checking for cat... /bin/cat
checking how to print strings... printf
checking for a sed that does not truncate output... (cached) /bin/sed
checking for fgrep... /bin/grep -F
checking for ld used by cc... /bin/ld
checking if the linker (/bin/ld) is GNU ld... yes
checking for BSD- or MS-compatible name lister (nm)... /bin/nm -B
checking the name lister (/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 1572864
checking whether the shell understands some XSI constructs... yes
checking whether the shell understands "+="... yes
checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop
checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
checking for /bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for dlltool... no
checking how to associate runtime and link libraries... printf %s\n
checking for ar... ar
checking for archiver @FILE support... @
checking for strip... strip
checking for ranlib... ranlib
checking for gawk... (cached) gawk
checking command to parse /bin/nm -B output from cc object... ok
checking for sysroot... no
checking for mt... no
checking if : is a manifest tool... no
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC -DPIC
checking if cc PIC flag -fPIC -DPIC works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking if cc supports -c -o file.o... (cached) yes
checking whether the cc linker (/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
configure: creating ./config.status
config.status: creating config.h
config.status: executing libtool commands

echo $?
0
make -j$(echo `nproc` - 1 |bc) && make install
...省略...
Installing shared extensions:     /opt/remi/php56/root/usr/lib64/php/modules/

cp -rp /opt/remi/php56/root/etc/php.ini{,.`datebak`}
cat xcache.ini >> /opt/remi/php56/root/etc/php.ini

# 启动apache服务
systemctl enable httpd --now

部署数据库服务器

# 安装MySQL
# 删除系统自带的mariadb
yum remove -y mariadb-libs
# 上传安装包,并安装
cd /data/soft
yum install -y mysql-community-common-5.7.24-1.el7.x86_64.rpm 
yum install -y mysql-community-libs-5.7.24-1.el7.x86_64.rpm 
yum install -y mysql-community-client-5.7.24-1.el7.x86_64.rpm 
yum install -y mysql-community-server-5.7.24-1.el7.x86_64.rpm

# 查看mysql版本
mysql --version
mysql  Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using  EditLine wrapper

# 修改mysql数据目录
cp -rp /etc/my.cnf{,.`date +"%F_%H-%M-%S"`}
cat /dev/null > /etc/my.cnf
vim /etc/my.cnf
####################################################
[mysqld]
# 按照实际物理内存大小的60%修改
innodb_buffer_pool_size = 2G
datadir=/data/mysql/data
socket=/data/mysql/data/mysql.sock
character-set-server=utf8
symbolic-links=0
# 指定相关日志文件位置
general_log_file=/data/mysql/logs/mysqld.log
log-error=/data/mysql/logs/mysqld.error
slow_query_log_file=/data/mysql/logs/mysqlslow.log
# 设置相关bin-log参数
server-id=1
binlog_format=MIXED
log-bin=/data/mysql/logs/mysql-bin
log-bin-index=/data/mysql/logs/mysql-bin.index
binlog_cache_size=1M
max_binlog_size=500M
pid-file=/var/run/mysqld/mysqld.pid
# 添加客户端socket的信息
[client]
port=3306
socket=/data/mysql/data/mysql.sock
####################################################
# 创建目录数据目录及日志目录
mkdir -p /data/mysql/{data,logs}
chown -R mysql:mysql /data/mysql
ll /data

# 启动mysql并设置开机启动
systemctl enable mysqld.service --now

# 查看端口
ss -tnl|grep 3306

部署wordpress

下载wordpress代码到应用服务器

# 登录应用服务器
# 将代码部署到web站点目录下
cd /data/soft
wget https://cn.wordpress.org/wordpress-5.8-zh_CN.tar.gz
tar -zxvf wordpress-5.8-zh_CN.tar.gz -C /var/www/html
chown apache.apache -R /var/www/html/wordpress

创建数据库及用户

mycli -u'root' -p'2Fkq7eTPNK#K'
MySQL 5.7.24
mycli 1.24.1
Home: http://mycli.net
Bug tracker: https://github.com/dbcli/mycli/issues
Thanks to the contributor - Johannes Hoff
MySQL root@localhost:(none)> create database wordpress;
Query OK, 1 row affected
Time: 0.027s
MySQL root@localhost:(none)> create user 'wpuser'@'192.168.168.%' identified by 'wpP@ss!123';
Query OK, 0 rows affected
Time: 0.020s
MySQL root@localhost:(none)> grant all on wordpress.* to 'wpuser'@'192.168.168.%' identified by 'wpP@ss!123';
Query OK, 0 rows affected
Time: 0.023s
MySQL root@localhost:(none)> flush privileges;
Query OK, 0 rows affected
Time: 0.015s
MySQL root@localhost:(none)> \q
Goodbye!

重启apache服务

apachectl -t
Syntax OK
systemctl enable httpd.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
ss -tnlp |grep 80

浏览器登录并设置相关信息
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
用户名 hooper 密码 %@rRabxjT*wWnnn31d

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

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

利用MySQL数据库存储日志信息

应用服务器上安装rsyslog-mysql

# 在应用服务器上安装rsyslog连接MySQL数据库相关的程序包
yum install -y rsyslog-mysql
# 将sql脚本复制到MySQL服务器
scp -rpP 10022 /usr/share/doc/rsyslog-8.24.0/mysql-createDB.sql kfx@192.168.168.211:/home/kfx/

MySQL服务器导入sql数据

cp home/kfx/mysql-createDB.sql /data/
mycli -u'root' -p'2Fkq7eTPNK#K'
MySQL 5.7.24
mycli 1.24.1
Home: http://mycli.net
Bug tracker: https://github.com/dbcli/mycli/issues
Thanks to the contributor - Jakub Boukal
MySQL root@localhost:(none)> source /data/mysql-createDB.sql
Query OK, 1 row affected
Time: 0.090s

You are now connected to database "Syslog" as user "root"
Time: 0.000s

Query OK, 0 rows affected
Time: 0.220s

Query OK, 0 rows affected
Time: 0.104s
MySQL root@localhost:(none)> create user 'loguser'@'192.168.168.%' identified by 'logP@ss!123';
Query OK, 0 rows affected
Time: 0.034s
MySQL root@localhost:(none)> grant all on Syslog.* to 'loguser'@'192.168.168.%' identified by 'logP@ss!123';
Query OK, 0 rows affected
Time: 0.003s
MySQL root@localhost:(none)> flush privileges;
Query OK, 0 rows affected
Time: 0.028s

配置日志服务器将日志发送到指定数据库

cp -rp /etc/rsyslog.conf{,.`datebak`}
vim /etc/rsyslog.conf
#######################################################################################
#### MODULES ####
# MODULES语句块下面添加:
### add by hooper start
$ModLoad ommysql
### add by hooper end
..........
#### RULES ####
# RULES语句块下面添加:
..........
### add by hooper start
*.info                          :ommysql:192.168.168.211,Syslog,loguser,logP@ss!123
### add by hooper end
..........
#######################################################################################

# 重启rsyslog服务
systemctl restart rsyslog.service

测试

# 在MySQL数据库上查看信息数量
MySQL root@localhost:(none)> select count(*) from Syslog.SystemEvents;
+----------+
| count(*) |
+----------+
| 13       |
+----------+
1 row in set
Time: 0.009s

# 在应用服务器输入
$ logger "This is a test log by hooper."

# 再次查看MySQL数据库上的信息数量
MySQL root@localhost:(none)> select count(*) from Syslog.SystemEvents;
+----------+
| count(*) |
+----------+
| 14       |
+----------+
1 row in set
Time: 0.009s

至此MySQL数据库已经可以保存日志信息了;下一步就是将信息展示出来

安装loganalyzer

# 安装依赖包
yum -y install php56-php-gd php56-php-fpm
# 启动php-fpm服务
systemctl enable php56-php-fpm.service --now
systemctl status php56-php-fpm.service
● php56-php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php56-php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-08-17 13:25:44 CST; 7s ago
 Main PID: 4535 (php-fpm)
   Status: "Ready to handle connections"
   CGroup: /system.slice/php56-php-fpm.service
           ├─4535 php-fpm: master process (/opt/remi/php56/root/etc/php-fpm.conf)
           ├─4536 php-fpm: pool www
           ├─4537 php-fpm: pool www
           ├─4538 php-fpm: pool www
           ├─4539 php-fpm: pool www
           └─4540 php-fpm: pool www

Aug 17 13:25:44 magedu-demo systemd[1]: Starting The PHP FastCGI Process Manager...
Aug 17 13:25:44 magedu-demo systemd[1]: Started The PHP FastCGI Process Manager.
# 检查端口
ss -tnlp|grep 9000
LISTEN     0      32768  127.0.0.1:9000                     *:*                   users:(("php-fpm",pid=4540,fd=0),("php-fpm",pid=4539,fd=0),("php-fpm",pid=4538,fd=0),("php-fpm",pid=4537,fd=0),("php-fpm",pid=4536,fd=0),("php-fpm",pid=4535,fd=8))

# 下载代码
cd /data/soft
wget -c https://download.adiscon.com/loganalyzer/loganalyzer-4.1.12.tar.gz
tar -zxvf loganalyzer-4.1.12.tar.gz

# 将loganalyzer的代码复制到web站点目录下
cp -rp loganalyzer-4.1.12/src /var/www/html/
mv /var/www/html/src /var/www/html/log
# cp -rp /data/soft/loganalyzer-4.1.12/contrib/* /var/www/html/log/
chown -R apache.apache /var/www/html/log
ll /var/www/html
total 8.0K
drwxrwxr-x 13 apache apache 4.0K Apr 29 14:38 log
drwxr-xr-x  5 apache apache 4.0K Aug 17 12:32 wordpress

cd /var/www/html/log
touch config.php
chmod 666 config.php

浏览器设置

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

在这里插入图片描述
选择:MySQL NativeSyslog FieldsMonitorWare
输入数据库信息: 库名:Syslog 、 表名:SystemEvents (注意大小写区分)、 用户名及密码: loguser|logP@ss!123
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

修改apache的配置文件及rsyslog的配置文件

修改apache的配置文件,将访问日志的配置,重定向至rsyslog
修改rsyslog的配置文件,添加apache的访问日志

# 修改apache配置文件
cp -rp /etc/httpd/conf/httpd.conf{,.`datebak`}
vim /etc/httpd/conf/httpd.conf
#######################################################################################
#    CustomLog "logs/access_log" common
    CustomLog "|/usr/bin/logger -p local6.info" combined
#######################################################################################

apachectl -t
Syntax OK
systemctl restart httpd.service

# 修改rsyslog配置文件
cp -rp /etc/rsyslog.conf{,.`datebak`}
vim /etc/rsyslog.conf
#######################################################################################
### add by hooper start
local6.*                                                /var/log/httpd/access_log
local6.*                                                :ommysql:192.168.168.66,Syslog,loguser,logP@ss!123
### add by hooper end
#######################################################################################

# 重启rsyslog服务
systemctl restart rsyslog.service

刷新浏览器,查看是否有apache的访问日志

在这里插入图片描述

参考文献

rsyslog记录于MySQL数据库中
三分鐘就可以 CentOS 7 安裝 LogAnalyzer
HOW TO SETUP LOGANALYZER WITH RSYSLOG ON CENTOS 7 / RHEL 7
centos 7.2往rsyslog服务器端发送apache日志
实现lamp架构及统一日志管理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值