目标
实现CentOS 7 编译安装基于 fastcgi 模式的多虚拟主机的wordpress和discuz的LAMP架构
环境准备
两台主机:
一台主机:httpd+php(fastcgi模式) 10.0.0.152
一台主机:mariadb 服务器 10.0.0.162
软件版本:
CentOS 7.9
mariadb-10.2.31-linux-x86_64.tar.gz 通用二进制格式
apr-1.7.0.tar.bz2
apr-util-1.6.1.tar.bz2
httpd-2.4.46.tar.bz2
php-7.4.25.tar.xz
wordpress-5.4.1-zh_CN.tar.gz
Discuz_X3.4_SC_UTF8_20211022.zip
服务器10.0.0.162上:
二进制安装 mariadb
[root@localhost ~]# useradd -r -s /sbin/nologin mysql
[root@localhost ~]# tar xvf mariadb-10.2.31-linux-x86_64.tar.gz -C /usr/local
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mariadb-10.2.31-linux-x86_64/ mysql
‘mysql’ -> ‘mariadb-10.2.31-linux-x86_64/’
[root@localhost local]# cd mysql/
[root@localhost mysql]# chown -R root:root ./*
[root@localhost mysql]# mkdir /data/mysql -p
[root@localhost mysql]# chown -R mysql:mysql /data/mysql
[root@localhost mysql]# mkdir /etc/mysql
[root@localhost mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf
[root@localhost mysql]# vim /etc/mysql/my.cnf
[mysqld]
#加下面行
datadir =/data/mysql
skip_name_resolve = ON
#准备PATH变量
[root@localhost mysql]# vim /etc/profile.d/lamp.sh
PATH=/usr/local/mysql/bin/:$PATH
[root@localhost mysql]# . /etc/profile.d/lamp.sh
[root@localhost mysql]# yum install -y libaio
[root@localhost mysql]# cd /usr/local/mysql;scripts/mysql_install_db –user=mysql –datadir=/data/mysql
[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chkconfig –add mysqld
[root@localhost mysql]# service mysqld start
#为wordprss和discuz应用准备数据库和用户帐号
[root@localhost mysql]# mysql -uroot
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> create database discuz;
MariaDB [(none)]> grant all on wordpress.* to wordpress@'10.0.0.%' identified by “wppass”;
MariaDB [(none)]> grant all on discuz.* to discuz@'10.0.0.%' identified by 'dispass';
MariaDB [(none)]> flush privileges;
10.0.0.152上:
编译安装httpd2.4
#安装相关包
[root@localhost ~]# yum install gcc pcre-devel openssl-devel expat-devel -y
#编译安装httpd
[root@localhost ~]#tar xf apr-1.7.0.tar.bz2
[root@localhost ~]#tar xf apr-util-1.6.1.tar.bz2
[root@localhost ~]# tar xf httpd-2.4.46.tar.bz2
[root@localhost ~]# mv apr-1.7.0 httpd-2.4.46/srclib/apr
[root@localhost ~]# mv apr-util-1.6.1 httpd-2.4.46/srclib/apr-util
[root@localhost ~]# cd httpd-2.4.46
[root@localhost ~]# mkdir /apps
[root@localhost httpd-2.4.46]# ./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
[root@localhost httpd-2.4.46]# make -j2 && make install
#准备PATH变量
[root@localhost ~]# vim /etc/profile.d/lamp.sh
PATH=/apps/httpd/bin:$PATH
[root@localhost ~]# . /etc/profile.d/lamp.sh
#创建和配置用户和组
[root@localhost ~]# useradd -s /sbin/nologin -r -u 88 apache
[root@localhost ~]# 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
#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
[root@localhost ~]#httpd -M |grep mpm
mpm_event_module (shared)
[root@localhost ~]# apachectl start
[root@localhost ~]#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
编译安装 fastcgi 方式的 php 7.4
#安装相关包,依赖EPEL源
#php 7.4 相关包
[root@localhost ~]#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 编译
[root@localhost ~]# tar xf php-7.4.25.tar.xz
[root@localhost ~]# cd php-7.4.25
[root@localhost ~]#./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
[root@localhost ~]#make -j 2 && make install
#准备PATH变量
#php7.4
[root@localhost ~]#vim /etc/profile.d/lamp.sh
PATH=/apps/php/bin:/apps/httpd/bin:$PATH
[root@localhost ~]# . /etc/profile.d/lamp.sh
[root@centos7 ~]#php –version
PHP 7.4.25 (cli) (built: Nov 17 2021 04:04:48) ( ZTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
#准备php配置文件和启动文件
[root@localhost php-7.4.25]# cp php.ini-production /etc/php.ini
[root@localhost php-7.4.25]# cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
[root@localhost php-7.4.25]# cd /apps/php/etc
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cd php-fpm.d/
[root@localhost php-fpm.d]# cp www.conf.default www.conf
#修改进程所有者
[root@localhost php-fpm.d]# vim /apps/php/etc/php-fpm.d/www.conf
user apache
group apache
#支持status和ping页面
pm.status_path = /fpm_status
ping.path = /ping
#支持opcache加速
[root@localhost php-fpm.d]# mkdir /etc/php.d/
[root@localhost php-fpm.d]# vim /etc/php.d/opcache.ini
[opcache]
zend_extension=opcache.so
opcache.enable=1
[root@localhost ~]#systemctl daemon-reload
[root@localhost ~]#systemctl status php-fpm.service
[root@localhost ~]#systemctl enable –now php-fpm.service
[root@localhost ~]#systemctl status php-fpm.service
#修改配置 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.magedu.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.magedu.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>
[root@localhost ~]# httpd -t
AH00112: Warning: DocumentRoot [/data/wordpress] does not exist
AH00112: Warning: DocumentRoot [/data/discuz] does not exist
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@localhost ~]# apachectl restart
AH00112: Warning: DocumentRoot [/data/wordpress] does not exist
AH00112: Warning: DocumentRoot [/data/discuz] does not exist
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
#准备wordpress和discuz! 相关文件
#准备wordpress程序文件
[root@localhost ~]# mkdir /data/
[root@localhost ~]# tar xf wordpress-5.4.1-zh_CN.tar.gz
[root@localhost ~]# mv wordpress/ /data
[root@localhost ~]# setfacl -R -m u:apache:rwx /data/wordpress/
#或者chown –R apache.apache /data/wordpress
#准备discuz!程序文件
[root@localhost ~]# unzip Discuz_X3.4_SC_UTF8_20211022.zip
[root@localhost ~]# mv upload/ /data/discuz
[root@localhost ~]# setfacl -R -m u:apache:rwx /data/discuz/
#测试访问
在windows机器上修改hosts文件解析C:WindowsSystem32driversetchosts文件中添加一行
10.0.0.152 blog.magedu.org forum.magedu.org
打开浏览器访问 http://blog.magedu.org 和http://forum.magedu.org 分别进行初始化和安装
填写对应数据库地址,数据库名,账号,密码。和以上在10.0.0.162主机的数据库中授权的一致。
访问http://blog.magedu.org/fpm_status ; http://blog.magedu.org/ping
#修改成UDS模式
[root@localhost ~]# 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
[root@localhost ~]# systemctl restart php-fpm
[root@localhost ~]# ll /run/php-fpm.sock
srw-rw—- 1 apache apache 0 Nov 17 04:54 /run/php-fpm.sock
[root@localhost ~]#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>
[root@localhost ~]# systemctl restart php-fpm
[root@localhost ~]# systemctl restart httpd
#再次访问也是ok的