博客,一键安装实现基于fastcgi方式LAMP架构
一键安装实现基于fastcgi方式LAMP架构
[root@centos7 ~]#cat fastcgi_LAMP.sh
#!/bin/bash
#
#********************************************************************
#Author: wei
#QQ:
#Date: 2020-11-1
#FileName: fastcgi_lamp.sh
#URL:
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
. /etc/init.d/functions
set -e
echo -e '\e[1;32m
请准备如下包放到当前目录下:
apr-1.7.0.tar.gz && wget https://mirror.bit.edu.cn/apache//apr/apr-1.7.0.tar.gz
apr-util-1.6.1.tar.gz && wget https://mirror.bit.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
Discuz_X3.4_SC_UTF8【20191201.zip && wget http://download.comsenz.com/DiscuzX/3.3/Discuz_X3.4_SC_UTF8.zip
httpd-2.4.46.tar.gz && wget https://mirrors.bfsu.edu.cn/apache//httpd/httpd-2.4.46.tar.gz
mariadb-10.5.5-linux-systemd-x86_64.tar.gz && wget https://downloads.mariadb.org/interstitial/mariadb-10.5.5/source/mariadb-10.5.5.tar.gz/from/https%3A//mirrors.tuna.tsinghua.edu.cn/mariadb/php-7.4.12.tar.bz2 && wget https://www.php.net/distributions/php-7.4.12.tar.bz2
wordpress-5.4.2-zh_CN.tar.gzi && wget https://cn.wordpress.org/latest-zh_CN.zip
\e[0m'
function install_mysql {
echo -e ''
#编译安装二进制mariadb-10.5.5
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
pwd1=/usr/local/
pwd2=$pwd1/mysql/
name=mariadb-10.5.5-linux-systemd-x86_64
[ -e ${name}.tar.gz ] && tar xf ${name}.tar.gz -C $pwd1 || { echo -e '\e[1;31m压缩包不存在,安装失败!\e[0m';exit 3; }
yum -y install ncurses-compat-libs ncurses-libs libaio*
install_mariadb (){
echo -e '\e[1;33m开始安装MariaDB,请稍后...\e[0m'
getent passwd mysql || { groupadd -r mysql;useradd -r -g mysql -s /sbin/nologin mysql;echo -e '\e[1;32mmysql用户创建成功成功!\e[0m'; }
cd $pwd1
ln -s $name mysql
chown -R root.root $pwd2
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
[mysql]
auto-rehash
prompt="\\u@\\h [\\d]>"
EOF
cd $pwd2
./scripts/mysql_install_db --user=mysql --basedir=$pwd2 --datadir=/data/mysql &> /dev/null && echo -e '\e[1;32m数据库初始化成功!\e[0m'
ln -s ${pwd2}bin/* ${pwd1}bin/
cp ${pwd2}support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
echo -e '\e[1;32mMariaDB安装成功!\e[0m'
}
install_mariadb
#创建数据库用户及密码并授权
mysql -uroot -e 'create database wordpress'
mysql -uroot -e 'create database discuz'
mysql -uroot -e "grant all on wordpress.* to wordpress@'10.0.0.%' identified by '123456'"
mysql -uroot -e "grant all on discuz.* to discuz@'10.0.0.%' identified by '123456'"
}
function install_httpd {
#二进制安装httpd-2.4.46
cd /root
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
cpu=`lscpu | sed -rn 's/^CPU\(s\)[^0-9]+([0-9]+)/\1/p'`
apr_version=apr-1.7.0.tar.gz
apr_util_version=apr-util-1.6.1.tar.gz
httpd_version=httpd-2.4.46.tar.gz
apr_name=${apr_version%.tar*}
apr_util_name=${apr_util_version%.tar*}
httpd_name=${httpd_version%.tar.*}
echo -e '\e[1;32m开始安装httpd,请稍候...\e[0m'
# 安装依赖包
echo -e '\e[1;32m正在安装相关依赖包...\e[0m'
yum -y install gcc make pcre-devel openssl-devel expat-devel wget &> /dev/null || { action "依赖包安装失败,请检查网络和yum源!" false; exit ; }
# 解压
echo -e '\e[1;32m开始解压...\e[0m'
[ -e ${apr_version} ] && tar xf ${apr_version} || { action "没有找到${apr_version},安装失败!" false; exit ; }
[ -e ${apr_util_version} ] && tar xf ${apr_util_version} || { action "没有找到${apr_util_version},安装失败!" false; exit ; }
[ -e ${httpd_version} ] && tar xf ${httpd_version} || { action "没有找到${httpd_version},安装失败!" false; exit ; }
# 合并源码,共同编译安装
echo -e '\e[1;32m开始编译Httpd...\e[0m'
mv ${apr_name} ${httpd_name}/srclib/apr
mv ${apr_util_name} ${httpd_name}/srclib/apr-util
cd ${httpd_name}
./configure \
--prefix=/apps/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-included-apr \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork &> /dev/null
make -j $cpu &> /dev/null && action "Httpd 编译成功!" || { action "Httpd 编译安装失败!" false; exit; }
make install &> /dev/null
echo -e '\e[1;32m开始配置Httpd相关设置...\e[0m'
# 创建apache账户
useradd -r -s /sbin/nologin apache
# 修改配置文件
sed -ri 's/^(User ).*/\1apache/' /apps/httpd24/conf/httpd.conf
sed -ri 's/^(Group ).*/\1apache/' /apps/httpd24/conf/httpd.conf
# 配置环境变量
echo 'PATH=/apps/httpd24/bin:$PATH' > /etc/profile.d/httpd.sh
# 配置man帮助
echo 'MANDATORY_MANPATH /apps/httpd24/man' >> /etc/man_db.conf
# 创建service文件
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
ExecStart=/apps/httpd24/bin/apachectl start
ExecReload=/apps/httpd24/bin/apachectl graceful
ExecStop=/apps/httpd24/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 开机启动
systemctl enable --now httpd.service &> /dev/null && action "Httpd 编译安装完成!!!"
}
function install_php {
#编译安装fastcgi方式的php7.4
cd /root
yum -y install epel-release yum-utils
yum -y install gcc gcc-c++ make autoconf bzip2 bzip2-devel libpng libpng-devel freetype-devel gmp-devel readline-devel curl-devel libxml2-devel libjpeg-devel bison openssl-devel uw-imap-devel libc-client sqlite-devel libicu-devel libedit-devel libxslt-devel oniguruma oniguruma-devel
tar xvf php-7.4.12.tar.bz2
mkdir -p /apps/
cd php-7.4.12/
./configure \
--prefix=/apps/php74 \
--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 2 && make install
#准备php配置文件
cp /root/php-7.4.12/php.ini-production /etc/php.ini
cp /apps/php74/etc/php-fpm.conf.default /apps/php74/etc/php-fpm.conf
cp /apps/php74/etc/php-fpm.d/www.conf.default /apps/php74/etc/php-fpm.d/www.conf
#复制启动服务文件到系统文件中
cp /root/php-7.4.12/php.ini-production /etc/php.ini
cp /root/php-7.4.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cp /root/php-7.4.12/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start php-fpm.service
systemctl enable --now php-fpm.service
#准备php的PATH变量
echo 'PATH=/apps/php74/bin:/apps/httpd24/bin:$PATH' >> /etc/profile.d/php.sh
source /etc/profile.d/php.sh
sed -i.bak 's/^user = .*/user = apache/' /apps/php74/etc/php-fpm.d/www.conf
sed -i 's/^group = .*/group = apache/' /apps/php74/etc/php-fpm.d/www.conf
echo 'listen = 0.0.0.0:9000' >> /apps/php74/etc/php-fpm.d/www.conf
echo 'pm.status_path = /pm_status' >> /apps/php74/etc/php-fpm.d/www.conf
echo 'ping.path = /ping' >> /apps/php74/etc/php-fpm.d/www.conf
mkdir -p /apps/php74/etc/php.d
cat > /apps/php74/etc/php.d/opcache.ini <<EOF
zend_extension=opcache.so
opcache.enable=1
EOF
systemctl enable --now php-fpm.service
echo 'LoadModule proxy_module modules/mod_proxy.so' >> /apps/httpd24/conf/httpd.conf
echo 'LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so' >> /apps/httpd24/conf/httpd.conf
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' /apps/httpd24/conf/httpd.conf
cat >> /apps/httpd24/conf/httpd.conf <<EOF
AddType application/x-httpd-php .php
ProxyRequests Off
#实现第一个虚拟主机
<virtualhost *:80>
servername blog.weirui.cn
documentroot /data/wordpress
<directory /data/wordpress>
require all granted
</directory>
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/data/wordpress/\$1
#实现status和ping页面
ProxyPassMatch ^/(pm_status|ping)$ fcgi://127.0.0.1:9000/\$1
CustomLog "logs/access_wordpress_log" common
</virtualhost>
#第二个虚拟主机
<virtualhost *:80>
servername forum.weirui.cn
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
systemctl restart httpd.service
}
function install_p {
#准备wordpress和discuz
#准备wordpress程序文件
cd /root
mkdir /data/
tar xvf wordpress-5.4.2-zh_CN.tar.gz
mv /root/wordpress/ /data
setfacl -R -m u:apache:rwx /data/wordpress
}
function install_d {
#准备discuz!程序文件
yum -y install unzip
mkdir -p /data/discuz
setfacl -R -m u:apache:rwx /data/discuz
unzip Discuz_X3.4_SC_UTF8【20191201】.zip
mv /root/DiscuzX/upload/* /data/discuz/
setfacl -R -m u:apache:rwx /data/discuz/
}
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和discuz
if [ -f "/data/wordpress/index.php" ] ;then
action "wordpress已配置"
else
install_p action "wordpress配置成功" || action "wordpress配置失败" false
fi
if [ -f "/data/discuz/index.php" ];then
action "discuz已配置"
else
install_d action "discuz配置成功" || action "discuz配置失败" false
fi
#打开浏览器访问 http://blog.weirui.cn 和http://forum.weirui.cn 分别进行初始化和安装
echo '打开浏览器访问 http://blog.weirui.cn 和http://forum.weirui.cn 分别进行初始化和安装'
执行结果
#需在本机C:\Windows\System32\drivers\etc\hosts中添加
10.0.0.17 blog.weirui.cn
10.0.0.17 forum.weirui.cn