1、简述CGI与FASTCGI区别
CGI:Common Gateway Interface 公共网关接口
web服务器根据这次请求的内容,然后会 fork 一个新进程来运行外部的 C 程序或者bash,perl脚本等,这个进程会把处理完的数据返回给web服务器,最后web服务器把内容发送给用户,刚才fork的进程也随之退出。 如果下次用户还请求改动态脚本,那么web服务器又再次fork一个新进程,周而复始的进行。
请求流程
Client -- (http协议) --> httpd -- (cgi协议) --> application server (program file) -- (mysql协议) --> mysq
fscgi
web服务器收到一个请求时,不会重新fork一个进程(因为这个进程在web服务器启动时就开启了,而且不会退出),web服务器直接把内容传递给这个进程(进程间通信,但fastcgi使用了别的方式,tcp方式通信),这个进程收到请求后进行处理,把结果返回给web服务器,最后自己接着等待下一个请求的到来,而不是退出。
请求流程
Client -- (http协议) --> httpd -- (fastcgi协议) --> fastcgi服务器 -- (mysql协议) --> mysql
2、 编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构
准备两台主机
主机1:192.168.1.14 安装:php(fscgi模式)
php-7.4.7.tar.xz 或 php-7.3.10.tar.bz2
httpd-2.4.43.tar.gz 、apr-1.7.0.tar.bz2、 apr-util-1.6.1.tar.bz2
Discuz_X3.4_SC_UTF8【20191201】.zip
wordpress-5.4.2-zh_CN.tar.gz
主机2:192.168.1.15 安装:Mariadb
mariadb-10.2.27-linux-x86_64.tar.gz 通用二进制格式
(1)192.168.1.14安装httpd
安装相关包
yum install gcc pcre-devel openssl-devel expat-devel -y
编译安装httpd
tar xvf apr-1.7.0.tar.bz2
tar xvf apr-util-1.6.1.tar.bz2
tar xf httpd-2.4.43.tar.gz
将apr和apr-util放在httpd目录下
mv apr-1.7.0 httpd-2.4.43/srclib/apr
mv apr-util-1.6.1 httpd-2.4.43/srclib/apr-util
cd httpd-2.4.43/
./configure \
--prefix=/apps/httpd \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=event
make && make install
准备path变量
vim /etc/profile.d/lamp.sh
PATH=/apps/httpd/bin:$PATH
. /etc/profile.d/lamp.sh
创建和配置用户和组
useradd -s /sbin/nologin -r -u 88 apache
vim /apps/httpd/conf/httpd.conf
user apache
group apache
修改为event模式,编译时已指定,此项不再需修改,可选项
vim /apps/httpd/conf/httpd.conf
LoadModule mpm_event_module modules/mod_mpm_event.so
查看evevt模块
httpd -M |grep mpm
mpm_event_module (shared)
配置service文件
vim /usr/lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)
[Service]
Type=forking
#EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/apps/httpd/bin/apachectl start
#ExecStart=/apps/httpd/bin/httpd $OPTIONS -k start
ExecReload=/apps/httpd/bin/apachectl graceful
#ExecReload=/apps/httpd/bin/httpd $OPTIONS -k graceful
ExecStop=/apps/httpd/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
(2)192.168.1.14安装php7.4
安装相关包
yum -y install gcc libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel
编译安装php7.4
tar xvf php-7.4.7.tar.xz
cd php-7.4.7/
./configure \
--prefix=/apps/php \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-openssl \
--with-zlib \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--enable-mbstring \
--enable-xml \
--enable-sockets \
--enable-fpm \
--enable-maintainer-zts \
--disable-fileinfo
make -j 4 && make install
准备PATH变量
vim /etc/profile.d/lamp.sh
PATH=/apps/php/bin:/apps/httpd/bin:$PATH
. /etc/profile.d/lamp.sh
php --version
PHP 7.4.7 (cli) (built: Jul 1 2020 09:59:57) ( ZTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
准备php配置文件和启动文件
cp php.ini-production /etc/php.ini
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
cd /apps/php/etc
cp php-fpm.conf.default php-fpm.conf
cd php-fpm.d/
cp www.conf.default www.conf
修改进程所有者
vim /apps/php/etc/php-fpm.d/www.conf
user apache
group apache
支持status和ping页面
pm.status_path = /fpm_status
ping.path = /ping
支持opcache加速
mkdir /etc/php.d/
vim /etc/php.d/opcache.ini
[opcache]
zend_extension=opcache.so
opcache.enable=1
systemctl daemon-reload
systemctl status php-fpm.service
systemctl enable --now php-fpm.service
(3)修改配置httpd支持php-fpm
vim /apps/httpd/conf/httpd.conf
#取消下面两行的注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
#修改下面行
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
#加下面三行
AddType application/x-httpd-php .php
#AddType application/x-httpd-php-source .phps
ProxyRequests Off
#实现第一个虚拟主机
<virtualhost *:80>
servername blog.shenqiqi.org
documentroot /data/wordpress
<directory /data/wordpress>
require all granted
</directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/wordpress/$1
#实现status和ping页面
ProxyPassMatch ^/(fpm_status|ping)$ fcgi://127.0.0.1:9000/$1
CustomLog "logs/access_wordpress_log" common
</virtualhost>
#第二个虚拟主机
<virtualhost *:80>
servername forum.shenqiqi.org
documentroot /data/discuz
<directory /data/discuz/>
require all granted
</directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/discuz/$1
CustomLog "logs/access_discuz_log" common
</virtualhost>
启动httpd
systemctl start httpd
(4)准备wordpress和discuz相关文件
准备wordpress程序文件
tar xvf wordpress-5.4.2-zh_CN.tar.gz
mv wordpress/ /data
setfacl –R –m u:apache:rwx /data/wordpress/
#或者chown –R apache.apache /data/wordpress
#准备discuz!程序文件
unzip Discuz_X3.4_SC_UTF8【20191201】.zip
mv DiscuzX/upload/ /data/discuz
setfacl -R -m u:apache:rwx /data/discuz/
(5)192.168.1.15二进制安装marisdb数据库
useradd -r -s /sbin/nologin mysql
tar xvf mariadb-10.2.27-linux-x86_64.tar.gz -C /usr/local
创建软连接
cd /usr/local
ln -sv mariadb-10.2.27-linux-x86_64 mysql
修改文件的权限,编辑配置文件
cd mysql
chown -R root.root ./*
mkdir /data/mysql -p
chown -R mysql.mysql /data/mysql
mkdir /etc/mysql
cp support-files/my-huge.cnf /etc/mysql/my.cnf
vim /etc/mysql/my.cnf
[mysqld]
#加下面行
datadir =/data/mysql
skip_name_resolve = ON
#准备PATH变量
vim /etc/profile.d/lamp.sh
PATH=/usr/local/mysql/bin/:$PATH
. /etc/profile.d/lamp.sh
安装相关包、数据库初始化、设置开机自启动
yum install libaio -y
cd /usr/local/mysql;scripts/mysql_install_db --user=mysql --datadir=/data/mysql
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
service mysqld start
为wordprss和discuz应用准备数据库和用户帐号
mysql -uroot
mysql> create database wordpress;
mysql> create database discuz;
mysql> grant all on wordpress.* to wordpress@'192.168.1.%' identified by "wppass";
mysql> grant all on discuz.* to discuz@'192.168.1.%' identified by 'dispass';
(6)测试访问,在客户端主机hosts添加
vim /etc/hosts
192.168.1. 14 blog.shenqiqi.org forum.shenqiqi.org
打开浏览器访问 http://blog.shenqiqi.org 和http://forum.shenqiqi.org 分别进行初始化和安装
3、通过loganalyzer展示数据库中的日志
准备三台主机:
192.168.1.81 配置rsyslog服务器
192.168.1.82 配置数据库服务器
192.168.1.83 配置httpd、php并安装oganalyzer展示web图形
(1)192.168.1.81 配置rsyslog服务
vim /etc/rsyslog.conf
# Provides UDP syslog reception
# for parameters see http://www.rsyslog.com/doc/imudp.html
module(load="imudp") # needs to be done just once
input(type="imudp" port="514")
# Provides TCP syslog reception
# for parameters see http://www.rsyslog.com/doc/imtcp.html
module(load="imtcp") # needs to be done just once
input(type="imtcp" port="514")
在rsyslog服务器上安装连接mysql模块相关的程序包
yum -y install rsyslog-mysql
rpm -ql rsyslog-mysql
/usr/lib/.build-id
/usr/lib/.build-id/b1
/usr/lib/.build-id/b1/435a976b2dfddfb19d0d1517964f615d510402
/usr/lib64/rsyslog/ommysql.so
/usr/share/doc/rsyslog/mysql-createDB.sql
将sql脚本复制到数据库服库上
scp /usr/share/doc/rsyslog/mysql-createDB.sql @192.168.1.82:/root/
(2)192.168.1.82 安装数据库
yum -y install mariadb-server
[root@datasrv ~]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
导入数据库
MariaDB [(none)]> source /root/mysql-createDB.sql
Query OK, 1 row affected (0.001 sec)
Database changed
Query OK, 0 rows affected (0.004 sec)
Query OK, 0 rows affected (0.003 sec)
MariaDB [Syslog]>
创建用户,并授权rsyslog能连接至当前服务器
MariaDB [Syslog]> GRANT ALL ON Syslog.* TO 'rsyslog'@'10.0.0.%' IDENTIFIED BY '12345678';
Query OK, 0 rows affected (0.000 sec)
MariaDB [Syslog]>
(2)192.168.1.81 配置将日志发送到指定的数据库
vim /etc/rsyslog.conf
#在 MODULES 语言下面,如果是 CentOS 8 加下面行
module(load="ommysql")
#在 MODULES 语言下面,如果是 CentOS 7,6 加下面行
$ModLoad ommysql
#在RULES语句块加下面行的格式
#facility.priority :ommysql:DBHOST,DBNAME,DBUSER, PASSWORD
*.info :ommysql:192.168.1.82,Syslog,rsyslog,12345678
(3)192.168.1.83 安装httpd, php和相关软件包
yum -y install httpd php-fpm php-mysqlnd php-gd
tar xvf loganalyzer-4.1.10.tar.gz
mv loganalyzer-4.1.10/src/ /var/www/html/log
参考文件
[root@phpsrv loganalyzer-4.1.12]#cat contrib/
configure.sh secure.sh
[root@phpsrv loganalyzer-4.1.12]#cat contrib/configure.sh
#!/bin/sh
touch config.php
chmod 666 config.php
创建文件
touch /var/www/html/log/config.php
chmod 666 /var/www/html/log/config.php
(3)测试
访问http://192.168.1.83/log 实现初始化