lamp架构之一键编译安装lamp搭建wordpress和discuz脚本(基于centos)

源码包准备:

apr-1.7.0.tar.bz2

apr-util-1.6.1.tar.bz2

wordpress-5.4.2-zh_CN.zip

Discuz_X3.4_SC_UTF8【20191201】.zip

httpd-2.4.46.tar.bz2

mariadb-10.2.31-linux-systemd-x86_64.tar.gz

oniguruma5php-6.9.5+rev1-4.el8.remi.x86_64.rpm

oniguruma5php-devel-6.9.5+rev1-4.el8.remi.x86_64.rpm

php-7.4.12.tar.bz2

#!/bin/sh
. /etc/init.d/functions
set -e

#定义变量
PACKAGE="
gcc
make
pcre-devel
openssl-devel
expat-devel
bzip2
wget
openssl
bzip2-devel
unzip
perl-Data-Dumper
libaio-devel
ncurses-compat-libs
ncurses-libs
libaio
ncurses-c++-libs
libxml2-devel
libmcrypt-devel
sqlite-devel
oniguruma-devel
"
http_file=/apps/httpd
ML=/root
PHPPAGE=php-7.4.12.tar.bz2
cudirectory=/root
php_file=/apps/php74
phpversion=php-7.4.12

#php特定包,需事先放在root家目录下
yum -y install oniguruma5php-6.9.5+rev1-4.el8.remi.x86_64.rpm && action "oniguruma5php-6.9.5安装完成" || { action "oniguruma5php-6.9.5安装失败,请检查包是否存在!" false;exit; }
yum -y install oniguruma5php-devel-6.9.5+rev1-4.el8.remi.x86_64.rpm && action "oniguruma5php-devel-6.9.5安装完成"
for PACK in $PACKAGE;do
    rpm -q $PACK &> /dev/null || yum -y -q install $PACK
done

#安装httpd
function install_httpd {
#判断是否在源码包目录
[[ `pwd` == /root ]] || cd
#下载相关源码包
[ -f /root/apr-1.7.0.tar.bz2 ] || wget https://downloads.apache.org//apr/apr-1.7.0.tar.bz2 &> /dev/null
[ -f /root/apr-util-1.6.1.tar.bz2 ] || wget https://downloads.apache.org//apr/apr-util-1.6.1.tar.bz2 &> /dev/null
[ -f /root/httpd-2.4.46.tar.bz2 ] || wget https://downloads.apache.org//httpd/httpd-2.4.46.tar.bz2 &> /dev/null

#解压相关源码包
tar xf httpd-2.4.46.tar.bz2
tar xf apr-1.7.0.tar.bz2 && mv apr-1.7.0 ${ML}/httpd-2.4.46/srclib/apr
tar xf apr-util-1.6.1.tar.bz2 && mv ${ML}/apr-util-1.6.1 httpd-2.4.46/srclib/apr-util

#编译
[ -d $http_file ] || mkdir -p $http_file
cd ${ML}/httpd-2.4.46
./configure --prefix=$http_file --enable-so --with-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-included-apr --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork  &> /dev/null && action "编译完成" || { action "编译失败" false; exit; }

#安装
make -j `lscpu | awk '/^CPU(s)/{print $2}'` &> /dev/null  && { make install &> /dev/null; action "httpd安装完成"; }

#创建用户
id apache &> /dev/null || useradd -s /sbin/nologin -r apache

#修改配置文件中的启动用户
sed -i 's/^User.*/User apache/' ${http_file}/conf/httpd.conf
sed -i 's/^Group.*/Group apache/' ${http_file}/conf/httpd.conf

#配置httpd支持php-fpm
cat >> ${http_file}/conf/httpd.conf <<EOF
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
AddType application/x-httpd-php .php
ProxyRequests Off
IncludeOptional conf.d/*.conf
EOF
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' ${http_file}/conf/httpd.conf
[ -d ${http_file}/conf.d ] || mkdir -p ${http_file}/conf.d
[ -f ${http_file}/conf.d/server.conf ] || touch ${http_file}/conf.d/server.conf
cat > ${http_file}/conf.d/server.conf << EOF
<virtualhost *:80>
servername www.blog.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 www.forum.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> 
EOF

#配置环境变量
echo 'PATH=/apps/httpd/bin:$PATH' > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh

#配置man帮助
echo 'MANDATORY_MANPATH          /apps/httpd/man' >> /etc/man_db.conf

#创建服务启动文件
cat > /usr/lib/systemd/system/httpd.service <<EOF
[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
EOF

#启动服务
systemctl daemon-reload
systemctl enable --now httpd.service &> /dev/null && action "httpd服务启动成功" || action "httpd服务启动失败,请检查配置文件" false
}

#安装数据库
function install_mysql {
#判断是否在源码包目录
[[ `pwd` == /root ]] || cd

#创建mysql用户
id mysql &> /dev/null || { useradd -r -d /data/mysql -s /sbin/nologin mysql ; action "创建用户成功"; }

#创建mysql的家目录文件夹
mkdir -pv /data/mysql && chown mysql:mysql  /data/mysql

#解压二进制程序
tar xvf mariadb-10.2.31-linux-systemd-x86_64.tar.gz -C /usr/local

#将原文件创建为软链接
ln -sv /usr/local/mariadb-10.2.31-linux-systemd-x86_64 /usr/local/mysql
chown -R root:root /usr/local/mysql/

#环境变量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh

#准备配置文件
cp -b /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
cat > /etc/my.cnf <<EOF
[mysqld]
datadir = /data/mysql
innodb_file_per_table = on
skip_name_resolve = on
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

[client]
port=3306 
socket=/data/mysql/mysql.sock
EOF

#创建数据库文件
/usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysql --user=mysql

#服务启动文件
cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
#创建wordpress和discuz数据库
/usr/local/mysql/bin/mysql -e "create database wordpress;"
/usr/local/mysql/bin/mysql -e "create database discuz;"
/usr/local/mysql/bin/mysql -e "grant all on wordpress.* to wordpress@'10.0.0.%' identified by '123456';"
/usr/local/mysql/bin/mysql -e "grant all on discuz.* to discuz@'10.0.0.%' identified by '123456';"
}

#安装php
function install_php {
#判断是否在包文件目录
[[ `pwd` == /root ]] || cd

#解压文件
tar -jxvf $PHPPAGE  &> /dev/null && action "php源码包解压完成"
[ -d $php_file ] || mkdir -p $php_file

#编译
cd ${cudirectory}/$phpversion
./configure 
    --prefix=$php_file 
    --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 `lscpu | awk '/^CPU(s)/{print $2}'` && make install

#准备环境变量
echo "PATH=${php_file}/bin:$PATH" > /etc/profile.d/php.sh
source /etc/profile.d/php.sh

#准备配置文件
cp $cudirectory/${phpversion}/php.ini-production /etc/php.ini
cp ${php_file}/etc/php-fpm.conf.default ${php_file}/etc/php-fpm.conf
cp ${php_file}/etc/php-fpm.d/www.conf.default ${php_file}/etc/php-fpm.d/www.conf 

#准备服务启动文件
cp $cudirectory/${phpversion}/sapi/fpm/php-fpm.service /usr/lib/systemd/system/

#修改进程所有者
sed -i 's/^user.*/user = apache/' ${php_file}/etc/php-fpm.d/www.conf
sed -i 's/^group.*/group = apache/' ${php_file}/etc/php-fpm.d/www.conf

#支持status和ping页面
echo 'pm.status_path = /fpm_status' >> ${php_file}/etc/php-fpm.d/www.conf
echo 'ping.path = /ping' >> ${php_file}/etc/php-fpm.d/www.conf

#设置支持opcache加速
[ -d /etc/php.d ] || mkdir -p /etc/php.d/
cat > /etc/php.d/opcache.ini <<EOF
[opcache]
zend_extension=opcache.so              
opcache.enable=1
EOF

#启动服务
systemctl daemon-reload
systemctl enable --now php-fpm.service && action "php-fpm服务启动成功" || action "php-fpm服务启动失败,请检查配置文件" false
}

#安装mysql
if [[ `systemctl status mysql | awk '/Active/{print $2}'` == active ]] ;then
    action "数据库已安装过,跳过"
else
    install_mysql && action "数据库安装完成" || action "数据库安装失败,请检查配置文件" false
fi

#安装apache
if [[ `systemctl status httpd | awk '/Active/{print $2}'` == active ]] ;then
    action "apache已安装过,跳过"
else
    install_httpd && action "apache安装完成" || action "apache安装失败,请检查配置文件" false
fi

#安装php
if [[ `systemctl status php-fpm | awk '/Active/{print $2}'` == active ]] ;then
    action "apache已安装过,跳过"
else
    install_php action "php安装完成" || action "php安装失败,请检查配置文件" false
fi

#配置wordpress
function install_wordpress {
[[ `pwd` == /root ]] || cd
rpm -q unzip || yum -y install unzip &> /dev/null
[ -f /root/wordpress-5.4.2-zh_CN.zip ] && unzip wordpress-5.4.2-zh_CN.zip || action "wordpress源码包不存在"
mv /root/wordpress /data
setfacl -R -m u:apache:rwx /data/wordpress/
}

#配置discuz
function install_discuz {
[[ `pwd` == /root ]] || cd
[ -f Discuz_X3.4_SC_UTF8【20191201】.zip ] && unzip Discuz_X3.4_SC_UTF8【20191201】.zip || action "discuz源码包不存在"
[ -d /data/discuz ] || mkdir -p /data/discuz
mv /root/DiscuzX/upload/* /data/discuz
setfacl -R -m u:apache:rwx /data/discuz/
}

install_wordpress && action "wordpress配置完成" || action "wordpress配置失败,请检查文件" false
install_discuz && action "discuz配置完成" || action "discuz配置失败,请检查文件" false
原文地址:https://www.cnblogs.com/nj-duzi/p/13909568.html