第14周-2022-03-09

1、简述CGI与FASTCGI区别

  • CGI
    web服务器会根据这次请求的内容,然后会 fork 一个新进程来运行外部的 C 程序或者bash,perl脚本等,这个进程会把处理完的数据返回给web服务器,最后web服务器把内容发送给用户,刚才fork的进程也随之退出。 如果下次用户还请求改动态脚本,那么web服务器又再次fork一个新进程,周而复始的进行。
  • FASTCGI
    web服务器收到一个请求时,不会重新fork一个进程(因为这个进程在web服务器启动时就开启了,而且不会退出),web服务器直接把内容传递给这个进程(进程间通信,但fastcgi使用了别的方式,tcp方式通信),这个进程收到请求后进行处理,把结果返回给web服务器,最后自己接着等待下一个请求的到来,而不是退出。

2、 编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构

环境:
MySQL8.0:centos01
httpd2.4、PHP7.4:centos02
域名(通过hosts文件解析):
wordpress.mylamp.com
discuz.mylampcom

运行以下脚本安装mysql数据库:
bash install_mysql80.sh

[root@centos01 ~]# cat install_mysql80.sh 
#!/bin/bash
yum -y install libaio numactl-libs
id -g mysql &> /dev/null || groupadd mysql
id mysql &> /dev/null || useradd -r -g mysql -s /bin/nologin mysql
mkdir -p /data/mysql && chown -R mysql.mysql /data/mysql
[ -f mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz ] || wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
tar xf mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
mv /usr/local/mysql-8.0.27-linux-glibc2.12-x86_64 /usr/local/mysql
chown -R mysql.mysql /usr/local/mysql/
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
cat > /etc/my.cnf <

创建wordpress和discuz数据库账号:

[root@centos01 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

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

mysql> create user wordpress@'192.168.184.%' identified by "123456";
Query OK, 0 rows affected (0.00 sec)

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

mysql> create user discuz@'192.168.184.%' identified by "123456";
Query OK, 0 rows affected (0.00 sec)

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

编译安装http2.4

#安装相关包
[root@centos02 ~]# yum install gcc pcre-devel openssl-devel expat-devel -y

#下载安装包
[root@centos02 ~]# curl -O https://dlcdn.apache.org/httpd/httpd-2.4.52.tar.bz2
[root@centos02 ~]# curl -O https://dlcdn.apache.org/apr/apr-1.7.0.tar.bz2
[root@centos02 ~]# curl -O https://dlcdn.apache.org/apr/apr-util-1.6.1.tar.bz2

#编译安装
[root@centos02 ~]# tar xf httpd-2.4.52.tar.bz2 
[root@centos02 ~]# tar xf apr-1.7.0.tar.bz2 
[root@centos02 ~]# tar xf apr-util-1.6.1.tar.bz2 
[root@centos02 ~]# mv apr-1.7.0 httpd-2.4.52/srclib/apr
[root@centos02 ~]# mv apr-util-1.6.1 httpd-2.4.52/srclib/apr-util
[root@centos02 ~]# cd httpd-2.4.52
[root@centos02 httpd-2.4.52]# ./configure \
--prefix=/usr/local/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
[root@centos02 httpd-2.4.52]# make && make install

#配置PATH变量
[root@centos02 ~]# echo 'PATH=/usr/local/httpd/bin:$PATH' > /etc/profile.d/lamp.sh
[root@centos02 ~]# . /etc/profile.d/lamp.sh

#创建配置用户和组
[root@centos02 ~]# useradd -s /sbin/nologin -r apache
[root@centos02 ~]# sed -i 's/^User daemon/User apache/' /usr/local/httpd/conf/httpd.conf
[root@centos02 ~]# sed -i 's/^Group daemon/Group apache/' /usr/local/httpd/conf/httpd.conf

#开机启动脚本
[root@centos02 ~]# vi /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=/usr/local/httpd/bin/apachectl start
#ExecStart=/usr/local/httpd/bin/httpd $OPTIONS -k start
ExecReload=/usr/local/httpd/bin/apachectl graceful
#ExecReload=/usr/local/httpd/bin/httpd $OPTIONS -k graceful
ExecStop=/usr/local/httpd/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target

#启动httpd
[root@centos02 ~]# systemctl daemon-reload
[root@centos02 php]# systemctl enable --now httpd.service

编译安装fastcgi模式PHP7.4

#安装依赖包(需要EPEL源)
[root@centos02 conf]# yum -y install gcc libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma oniguruma-devel

#下载安装包
[root@centos02 ~]# wget https://www.php.net/distributions/php-7.4.28.tar.bz2

#编译安装
[root@centos02 ~]# tar xf php-7.4.28.tar.bz2
[root@centos02 ~]# cd php-7.4.28
[root@centos02 php-7.4.28]# ./configure \
--prefix=/usr/local/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
[root@centos02 php-7.4.28]# make -j 4 && make install

#配置PATH变量
[root@centos02 php-7.4.28]# echo 'PATH=/usr/local/php/bin:/usr/local/httpd/bin:$PATH' > /etc/profile.d/lamp.sh 
[root@centos02 php-7.4.28]# . /etc/profile.d/lamp.sh
[root@centos02 php-7.4.28]# php --version
PHP 7.4.28 (cli) (built: Mar  8 2022 10:47:52) ( ZTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

#准备php配置文件和启动文件
[root@centos02 php-7.4.28]# cp php.ini-production /etc/php.ini
[root@centos02 php-7.4.28]# cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
[root@centos02 php-7.4.28]# cd /usr/local/php/etc/
[root@centos02 etc]# cp php-fpm.conf.default php-fpm.conf
[root@centos02 etc]# cd php-fpm.d/
[root@centos02 php-fpm.d]# cp www.conf.default www.conf
[root@centos02 php-fpm.d]# sed -i 's/ProtectSystem=full/ProtectSystem=false/' /usr/lib/systemd/system/php-fpm.service

#修改进程所有者
[root@centos02 etc]# sed -i 's/user = nobody/user = apache/' /usr/local/php/etc/php-fpm.d/www.conf
[root@centos02 etc]# sed -i 's/group = nobody/group = apache/' /usr/local/php/etc/php-fpm.d/www.conf

#支持opcache加速
[root@centos02 etc]# mkdir /etc/php.d/
[root@centos02 etc]# vi /etc/php.d/opcache.ini
[opcache]
zend_extension=opcache.so
opcache.enable=1

#启动PHP
[root@centos02 fpm]# systemctl daemon-reload
[root@centos02 fpm]# systemctl enable --now php-fpm.service

#修改配置 httpd 支持 php-fpm
[root@centos02 var]# vi /usr/local/httpd/conf/httpd.conf
#取消下面两行的注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
#修改下面行

DirectoryIndex index.php index.html

#加下面三行
AddType application/x-httpd-php .php
#AddType application/x-httpd-php-source .phps
ProxyRequests Off
#虚拟主机配置

servername wordpress.mylamp.com
documentroot /data/wordpress
DirectoryIndex index.php index.htm

require all granted

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/wordpress/$1
CustomLog "logs/access_wordpress_log" common



servername discuz.mylamp.com
documentroot /data/discuz
DirectoryIndex index.php index.htm

require all granted

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/discuz/$1
CustomLog "logs/access_discuz_log" common

[root@centos02 var]# systemctl restart httpd

#准备wordpress程序文件
[root@centos02 ~]# mkdir /data
[root@centos02 ~]# unzip wordpress-5.9.1-zh_CN.zip
[root@centos02 ~]# mv wordpress /data/
[root@centos02 ~]# setfacl -R -m u:apache:rwx /data/wordpress/
#准备discuz!程序文件
[root@centos02 Discuz]# unzip Discuz_X3.4_SC_UTF8_20220131.zip
[root@centos02 Discuz]# mv upload/ /data/discuz
[root@centos02 Discuz]# setfacl -R -m u:apache:rwx /data/discuz/

#wordpress站点配置
浏览器打开http://wordpress.mylamp.com




#discuz站点配置
浏览器打开http://discuz.mylamp.com


3、通过loganalyzer展示数据库中的日志

环境:
mysql:centos01(192.168.184.147)
loganalyzer、lamp:centos02(192.168.184.129)
rsyslog:centos03(192.168.184.134)

#日志服务器上安装:
[root@centos03 ~]# yum install rsyslog-mysql
#将数据库初始化脚本拷贝的mysql服务器上:
[root@centos03 ~]# scp /usr/share/doc/rsyslog-8.24.0/mysql-createDB.sql 192.168.184.147:~

#mysql服务器导入建库sql
[root@centos01 ~]# mysql < mysql-createDB.sql
#创建rsyslog账号
mysql> create user rsyslog@'192.168.184.%' identified with mysql_native_password by '123456';
mysql> grant all on Syslog.* to rsyslog@'192.168.184.%';

#在日志服务器上配置加入模块:
[root@centos03 ~]#vi /etc/rsyslog.conf
#在 MODULES 语言下面加下面行(centos7)
$ModLoad ommysql
#在RULES语句块加下面行的格式
#facility.priority  :ommysql:DBHOST,DBNAME,DBUSER, PASSWORD
*.info :ommysql:10.0.0.28,Syslog,rsyslog,123456

#重启rsyslog服务
[root@centos03 ~]# systemctl restart rsyslog.service

通过loganalyzer展示数据库中的日志

下载地址:https://download.adiscon.com/loganalyzer/loganalyzer-4.1.12.tar.gz
在centos02上安装LAP环境:

#配置yum源:
[root@centos02 ~]# yum install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm

#安装php-fpm7.4版和httpd
[root@centos02 ~]# yum install httpd php74-php-fpm php74-php-mysqlnd php74-php-opcache php74-php-json php74-php-gd

#修改文件配置:
[root@centos02 ~]# vi /etc/httpd/conf.d/fcgi.conf
DirectoryIndex index.php
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
#ProxyPassMatch ^/(fpm_status|ping) fcgi://127.0.0.1:9000

#启动httpd和PHP
[root@centos02 ~]# systemctl enable --now httpd php74-php-fpm

#测试PHP:
[root@centos02 ~]# cd /var/www/html
[root@centos02 ~]# vim info.php

浏览器访问:http://192.168.184.129/info.php

#loganalyzer配置
[root@centos02 ~]# tar xf loganalyzer-4.1.12.tar.gz
[root@centos02 ~]# mv loganalyzer-4.1.12/src/ /var/www/html/log

#创建loganalyzer配置文件并加权限:
[root@centos02 html]# touch /var/www/html/log/config.php
[root@centos02 html]# chmod 666 /var/www/html/log/config.php

后续在浏览器中配置:
http://192.168.184.129/log






最终效果

#最后将文件恢复成644
[root@centos02 log]# cd /var/www/html/log/
[root@centos02 log]# chmod 644 config.php

你可能感兴趣的:(第14周-2022-03-09)