1比驻、簡(jiǎn)述CGI與FASTCGI區(qū)別
- CGI
web服務(wù)器會(huì)根據(jù)這次請(qǐng)求的內(nèi)容懂酱,然后會(huì) fork 一個(gè)新進(jìn)程來(lái)運(yùn)行外部的 C 程序或者bash,perl腳本等沪摄,這個(gè)進(jìn)程會(huì)把處理完的數(shù)據(jù)返回給web服務(wù)器葵诈,最后web服務(wù)器把內(nèi)容發(fā)送給用戶此叠,剛才fork的進(jìn)程也隨之退出毅待。 如果下次用戶還請(qǐng)求改動(dòng)態(tài)腳本饱溢,那么web服務(wù)器又再次fork一個(gè)新進(jìn)程庭猩,周而復(fù)始的進(jìn)行帅刊。 - FASTCGI
web服務(wù)器收到一個(gè)請(qǐng)求時(shí)纸泡,不會(huì)重新fork一個(gè)進(jìn)程(因?yàn)檫@個(gè)進(jìn)程在web服務(wù)器啟動(dòng)時(shí)就開(kāi)啟了,而且不會(huì)退出)厚掷,web服務(wù)器直接把內(nèi)容傳遞給這個(gè)進(jìn)程(進(jìn)程間通信弟灼,但fastcgi使用了別的方式级解,tcp方式通信),這個(gè)進(jìn)程收到請(qǐng)求后進(jìn)行處理田绑,把結(jié)果返回給web服務(wù)器勤哗,最后自己接著等待下一個(gè)請(qǐng)求的到來(lái),而不是退出掩驱。
2芒划、 編譯安裝基于fastcgi模式的多虛擬主機(jī)的wordpress和discuz的LAMP架構(gòu)
環(huán)境:
MySQL8.0:centos01
httpd2.4、PHP7.4:centos02
域名(通過(guò)hosts文件解析):
wordpress.mylamp.com
discuz.mylampcom
運(yùn)行以下腳本安裝mysql數(shù)據(jù)庫(kù):
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 <<EOF
[mysqld]
datadir=/data/mysql
skip_name_resolve=1
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
創(chuàng)建wordpress和discuz數(shù)據(jù)庫(kù)賬號(hào):
[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
#安裝相關(guān)包
[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
#創(chuàng)建配置用戶和組
[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
#開(kāi)機(jī)啟動(dòng)腳本
[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
#啟動(dòng)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
#準(zhǔn)備php配置文件和啟動(dòng)文件
[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
#修改進(jìn)程所有者
[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
#啟動(dòng)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
#修改下面行
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
#加下面三行
AddType application/x-httpd-php .php
#AddType application/x-httpd-php-source .phps
ProxyRequests Off
#虛擬主機(jī)配置
<virtualhost *:80>
servername wordpress.mylamp.com
documentroot /data/wordpress
DirectoryIndex index.php index.htm
<directory /data/wordpress>
require all granted
</directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/wordpress/$1
CustomLog "logs/access_wordpress_log" common
</virtualhost>
<virtualhost *:80>
servername discuz.mylamp.com
documentroot /data/discuz
DirectoryIndex index.php index.htm
<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>
[root@centos02 var]# systemctl restart httpd
#準(zhǔn)備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/
#準(zhǔn)備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站點(diǎn)配置
瀏覽器打開(kāi)http://wordpress.mylamp.com
#discuz站點(diǎn)配置
瀏覽器打開(kāi)http://discuz.mylamp.com
3欧穴、通過(guò)loganalyzer展示數(shù)據(jù)庫(kù)中的日志
環(huán)境:
mysql:centos01(192.168.184.147)
loganalyzer民逼、lamp:centos02(192.168.184.129)
rsyslog:centos03(192.168.184.134)
#日志服務(wù)器上安裝:
[root@centos03 ~]# yum install rsyslog-mysql
#將數(shù)據(jù)庫(kù)初始化腳本拷貝的mysql服務(wù)器上:
[root@centos03 ~]# scp /usr/share/doc/rsyslog-8.24.0/mysql-createDB.sql 192.168.184.147:~
#mysql服務(wù)器導(dǎo)入建庫(kù)sql
[root@centos01 ~]# mysql < mysql-createDB.sql
#創(chuàng)建rsyslog賬號(hào)
mysql> create user rsyslog@'192.168.184.%' identified with mysql_native_password by '123456';
mysql> grant all on Syslog.* to rsyslog@'192.168.184.%';
#在日志服務(wù)器上配置加入模塊:
[root@centos03 ~]#vi /etc/rsyslog.conf
#在 MODULES 語(yǔ)言下面加下面行(centos7)
$ModLoad ommysql
#在RULES語(yǔ)句塊加下面行的格式
#facility.priority :ommysql:DBHOST,DBNAME,DBUSER, PASSWORD
*.info :ommysql:10.0.0.28,Syslog,rsyslog,123456
#重啟rsyslog服務(wù)
[root@centos03 ~]# systemctl restart rsyslog.service
通過(guò)loganalyzer展示數(shù)據(jù)庫(kù)中的日志
下載地址:https://download.adiscon.com/loganalyzer/loganalyzer-4.1.12.tar.gz
在centos02上安裝LAP環(huán)境:
#配置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
#啟動(dòng)httpd和PHP
[root@centos02 ~]# systemctl enable --now httpd php74-php-fpm
#測(cè)試PHP:
[root@centos02 ~]# cd /var/www/html
[root@centos02 ~]# vim info.php
<?php phpinfo(); ?>
瀏覽器訪問(wèn):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
#創(chuàng)建loganalyzer配置文件并加權(quán)限:
[root@centos02 html]# touch /var/www/html/log/config.php
[root@centos02 html]# chmod 666 /var/www/html/log/config.php
后續(xù)在瀏覽器中配置:
http://192.168.184.129/log
最終效果
#最后將文件恢復(fù)成644
[root@centos02 log]# cd /var/www/html/log/
[root@centos02 log]# chmod 644 config.php