编译安装基于 FASTCGI 模式LAMP架构多 虚拟主机WEB应用

1.目标
实现CentOS 7 编译安装基于 fastcgi 模式的多虚拟主机的wordpress和discuz的LAMP架构
2.环境准备

 两台主机:关闭防火墙,selinux=disabled

一台主机:httpd+php(fastcgi模式)

一台主机:mariadb 服务器
 
软件版本:
CentOS 7.8
mariadb-10.2.27-linux-x86_64.tar.gz 通用二进制格式
apr-1.7.0.tar.bz2
apr-util-1.6.1.tar.bz2
httpd-2.4.43.tar.gz
php-7.4.7.tar.xz 或 php-7.3.10.tar.bz2
wordpress-5.4.2-zh_CN.tar.gz
Discuz_X3.4_SC_UTF8【20191201】.zip
 
3 .实现步骤
(1) 二进制安装 mariadb
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变量(export PATH=$PATH:/usr/local/mysql/bin)
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@'10.0.0.%' identified by "wppass";
mysql> grant all on discuz.* to discuz@'10.0.0.%' identified by 'dispass';
(2)编译安装 httpd 2.4
#安装相关包
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
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变量(export PATH=$PATH:/apps/httpd/bins)
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 apaches
 
#修改为event模式,编译时已指定,此项不再需修改,可选项
vim /apps/httpd/conf/httpd.conf
LoadModule mpm_event_module modules/mod_mpm_event.so                            
     
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
 
httpd -M |grep mpm
mpm_event_module (shared)
 
apachectl start
 
[root@centos7 ~]#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
 
(3)编译安装 fastcgi 方式的 php 7.4
#安装相关包,依赖EPEL源
#php 7.4 相关包
yum -y install gcc libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel
oniguruma-devel
#php 7.3 相关包
yum -y install gcc libxml2-devel bzip2-devel libmcrypt-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
#编译安装php 7.3
tar xvf php-7.3.10.tar.bz2
cd php-7.3.10
./configure --prefix=/apps/php
--enable-mysqlnd
--with-mysqli=mysqlnd
--with-pdo-mysql=mysqlnd
--with-openssl
--with-freetype-dir
--with-jpeg-dir
--with-png-dir
--with-zlib
--with-libxml-dir=/usr
--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变量
#php7.4(export PATH=$PATH:/apps/php/bin:/apps/httpd/bin)
vim /etc/profile.d/lamp.sh
PATH=/apps/php/bin:/apps/httpd/bin:$PATH
. /etc/profile.d/lamp.sh
 
#php7.3(export PATH=$PATH:/apps/php/bin:/apps/httpd/bin)
vim /etc/profile.d/lamp.sh
PATH=/apps/php/bin:/apps/httpd/bin:$PATH
. /etc/profile.d/lamp.sh
 
[root@centos7 ~]#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
[root@websrv ~]#/apps/php/bin/php --version
PHP 7.3.12 (cli) (built: Dec 14 2019 10:20:40) ( ZTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.12, Copyright (c) 1998-2018 Zend Technologies
 
#准备php配置文件和启动文件
cp php.ini-production /etc/php.ini
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
cd /apps/php74/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
(4)修改配置 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 www.sdnpc.top
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 www.snpec.top
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>
 
apachectl restart
vim /etc/hosts
IP地址(xxx.xxx.xxx.xxx)    www.snpec.top  www.sdnpc.top
 
打开浏览器访问 http://www.snpec.top和http://www.sdnpc.top 分别进行初始化和安装
 
(5)修改成UDS模式
vim /apps/php/etc/php-fpm.d/www.conf
;listen = 127.0.0.1:9000
listen = /run/php-fpm.sock
listen.owner = apache
listen.group = apache                                                          
listen.mode = 0660
 
systemctl restart php-fpm
 
ll /run/php-fpm.sock
srw-rw---- 1 apache apache 0 Dec 14 11:11 /run/php-fpm.sock
 
vim /apps/httpd/conf/httpd.conf
<virtualhost *:80>
servername blog.magedu.org
documentroot /data/wordpress
<directory /data/wordpress>
require all granted
</directory>
#ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/data/wordpress/$1
ProxyPassMatch ^/(.*.php)$ "unix:/run/php
fpm.sock|fcgi://localhost/data/wordpress/"
#ProxyPassMatch ^/(fpm_status|ping)$ fcgi://127.0.0.1:9000/$1
ProxyPassMatch ^/(fpm_status|ping)$ "unix:/run/php-fpm.sock|fcgi://localhost/"  
CustomLog "logs/access_wordpress_log" common
</virtualhost>
 
<virtualhost *:80>
servername forum.magedu.org
documentroot /data/discuz
<directory /data/discuz>
require all granted
</directory>
#ProxyPassMatch ^/(.*.php)$ fcgi://127.0.0.1:9000/data/discuz/$1
ProxyPassMatch ^/(.*.php)$ "unix:/run/php
fpm.sock|fcgi://localhost/data/discuz/"
#ProxyPassMatch ^/(fpm_status|ping)$ fcgi://127.0.0.1:9000/$1
ProxyPassMatch ^/(fpm_status|ping)$ "unix:/run/php-fpm.sock|fcgi://localhost/"  
CustomLog "logs/access_discuz_log" common
</virtualhost>
 
systemctl restart httpd
 
原文地址:https://www.cnblogs.com/syk-1994/p/14508563.html