Centos7环境一键安装lamp,php-fpm方式实现wordpress

  这个脚本现在只通过了centos7测试,最好自己下载好安装包放到/src目录下,当然也可以自动下载,但是Php的服务器速度非常慢,而且有断掉的可能,所以推荐自己下载放入/src文件夹,文件夹内不要放其他的文件,安装的时候会删除安装包以外的文件,lamp.sh文件可以放进去,已经排除
一定注意版本号,因为脚本里内置的都是精确的版本号,不符合将会自动下载
  安装包版本:mariadb-10.3.13.tar.gz  ,php-7.3.2.tar.bz2  ,httpd-2.4.38.tar.bz2  ,  wordpress-5.0.3-zh_CN.tar.gz   apr-1.6.5.tar.bz2   apr-util-1.6.1.tar.bz2
  httpd 通过service httpd 操作   开机启动已放入/etc/rc.local
  php-fpm 通过service php-fpm 操作  开机启动项通过chkconfig操作
  mysql通过service mysqld操作   开机启动项通过chkconfig操作

 

 脚本下载地址:http://47.101.41.235/script/lamp.sh

 


#!/bin/bash
#Filename:     lamp.sh
#Revion:       1.0
#Language:     bash shell
#Date:         2019-03-06
#Author:       Yong
#QQ:           691475087
#blog:         www.linux123.tech
#Description:  一键安装lamp,基于php-fpm的wordpress,所有软件包为当前最新版


#因为网络原因,下载php-7.3.2非常的慢,所有脚本中所有的安装包都是提前准备保存在/src目录
#mariadb-10.3.13.tar.gz  ,php-7.3.2.tar.bz2  ,httpd-2.4.38.tar.bz2  ,  wordpress-5.0.3-zh_CN.tar.gz   apr-1.6.5.tar.bz2   apr-util-1.6.1.tar.bz2

lease=`cat /etc/centos-release |sed 's/.*ease \([0-9]\)..*/\1/'`
[ "$lease" == "7" ] || { echo "本脚本,仅适用于centos7" ; exit 9 ; }
CPUS=`lscpu|awk -F" " '/^CPU\(s\)/{print $2}'`

#安装编译所需要的所有组件
fun_yum(){
echo "开始重置Yum源"
rm -f /etc/yum.repos.d/C*
cat> /etc/yum.repos.d/base.repo <<OK
[base]
name=CentOS-7 - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/os/x86_64/
        http://mirrors.aliyuncs.com/centos/7/os/x86_64/
        http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[epel]
name=Extra Packages for Enterprise Linux 7 - \$basearch
baseurl=http://mirrors.aliyun.com/epel/7/\$basearch
failovermethod=priority
enabled=1
gpgcheck=0
OK
yum clean all &> /dev/null
yum repolist  &> /dev/null
[ $? -eq 0 ] && echo "yum配置成功"
echo "yum安装组件中,耗时较长...."
yum groupinstall -y "development tools"  &> /dev/null
yum install -y wget bzip2 gnutls-devel ncurses-devel cmake ibaio-devel openssl-devel libxml2-devel mariadb-devel bison bison-devel zlib-devel openssl-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ libevent-devel pcre-devel apr-devel apr-util-devel bzip2-devel libmcrypt-devel &> /dev/null
}



fun_prepare(){
#环境配置
systemctl stop firewalld &> /dev/null
systemctl disable firewalld &> /dev/null
systemctl status firewalld &> /dev/null
[ $? -eq 3 ] && echo "防火墙关闭成功"||echo "防火墙关闭失败"
setenforce 0 &> /dev/null
sed -ri 's/^(SELINUX=).*/\1disabled/' /etc/selinux/config
[[ `getenforce` == "Enforcing" ]] &&echo "SELINUX关闭失败" ||echo "SELINUX关闭成功"
[ -d "/src" ] || mkdir /src
cd /src
lists=("mariadb-10.3.13.tar.gz" "php-7.3.2.tar.bz2" "httpd-2.4.38.tar.bz2" "wordpress-5.0.3-zh_CN.tar.gz" "apr-1.6.5.tar.bz2" "apr-util-1.6.1.tar.bz2")
#检查安装包
echo "检查安装包是否存在.."
for i in ${lists[@]};do
        if [[ -f "$i" ]] ; then
                echo "$i OK"
        else
                echo "$i 不存在,启动自动下载..."
                case $i in
                        mariadb-10.3.13.tar.gz)
                        wget -O /src/mariadb-10.3.13.tar.gz http://ftp.osuosl.org/pub/mariadb//mariadb-10.3.13/source/mariadb-10.3.13.tar.gz
                        [ $? -eq 0 ] && echo " mariadb-10.3.13.tar.gz 下载成功" || { echo " mariadb-10.3.13.tar.gz 下载失败,请手动下
载到/src,注意版本匹配" ; exit 5 ; }
                        ;;
                        php-7.3.2.tar.bz2)
                        wget -O /src/php-7.3.2.tar.bz2  http://jp2.php.net/distributions/php-7.3.2.tar.bz2
                        [ $? -eq 0 ] && echo " php-7.3.2.tar.bz2 下载成功" || { echo " php-7.3.2.tar.bz2 下载失败,请手动下载到/src,
注意版本匹配" ; exit 5 ; }
                        ;;

                        httpd-2.4.38.tar.bz2)
                        wget -O /src/httpd-2.4.38.tar.bz2  https://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.38.tar.bz2
                        [ $? -eq 0 ] && echo " httpd-2.4.38.tar.bz2 下载成功" || { echo " httpd-2.4.38.tar.bz2 下载失败,请手动下载到
/src,注意版本匹配" ; exit 5 ; }
                        ;;
                        wordpress-5.0.3-zh_CN.tar.gz)
                        wget -O /src/wordpress-5.0.3-zh_CN.tar.gz  https://cn.wordpress.org/latest-zh_CN.tar.gz
                        [ $? -eq 0 ] && echo " wordpress-5.0.3-zh_CN.tar.gz 下载成功" || { echo " wordpress-5.0.3-zh_CN.tar.gz 下载失
败,请手动下载到/src,注意版本匹配" ; exit 5 ; }
                        ;;
                        apr-1.6.5.tar.bz2)
                        wget -O /src/apr-1.6.5.tar.bz2 https://www-eu.apache.org/dist//apr/apr-1.6.5.tar.bz2
                         [ $? -eq 0 ] && echo " apr-1.6.5.tar.bz2 下载成功" || { echo " apr-1.6.5.tar.bz2 下载失败,请手动下载到/src>,注意版本
匹配" ; exit 5 ; }
                        ;;
                        apr-util-1.6.1.tar.bz2)
                        wget -O /src/apr-util-1.6.1.tar.bz2 https://www-eu.apache.org/dist//apr/apr-util-1.6.1.tar.bz2
                        [ $? -eq 0 ] && echo " apr-util-1.6.1.tar.bz2 下载成功" || { echo " apr-util-1.6.1.tar.bz2 下载失败,请手动下
载到/src>,注意版本匹配" ; exit 5 ; }
                        ;;
                        *)
                        echo "程序变量错误,请检查lists变量"
                        ;;
                esac
        fi
done
echo "校验安装包.."
cat > shapkg <<EOF
810de74ea3ee59ff3205f2a46436fc1dcce4e4ab  httpd-2.4.38.tar.bz2
08467885412184e99b835732913d445fd2c4b1b3  mariadb-10.3.13.tar.gz
32c5c8aeb7b02db89bdb8b2c5bc1012e7ed9a32a  wordpress-5.0.3-zh_CN.tar.gz
4cc73bc36ca697419f555476f2fc1c63df6069f4  apr-util-1.6.1.tar.bz2
6fdedcd21de9da806d0a01769b8be7ba0c495145  apr-1.6.5.tar.bz2
cc5a9e43261d2e71bdffca8f150bf08c2ba0f4f2  php-7.3.2.tar.bz2
EOF
sha1sum -c shapkg
[ $? -eq 0 ] && echo "安装包校验通过" || { echo "安装包校验失败,请手动下载安装包到/src目录,注意版本号" ; exit ; }
echo "解包中...,将自动删除/src目录下所有非安装包文件"
rm -rf `ls |grep -v ".gz"|grep -v ".bz2" | grep -v "lamp.sh"`
for pkg in `ls *.{gz,bz2}`;do
        echo "开始解压$pkg...."
        tar xf $pkg
        echo "$pkg解压完成"
done
}

fun_mysql(){
#创建数据库系统账号
cd /src/mariadb-10.3.13
echo "开始编译MYSQL..."
cmake . \
-DCMAKE_INSTALL_PREFIX=/app/mysql \
-DMYSQL_DATADIR=/mysqldb/ \
-DSYSCONFDIR=/etc \
-DMYSQL_USER=mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DMYSQL_UNIX_ADDR=/app/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 &> /dev/null
make -j$CPUS &> /dev/null
echo "MYSQL编译安装开始..."
make install &> /dev/null
echo "MYSQL编译完成"
id mysql  &> /dev/null || useradd -r -s /sbin/nologin mysql
#配置变量路径
echo 'PATH=/app/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
#安装数据库
echo "开始安装数据库文件"
/app/mysql/scripts/mysql_install_db --datadir=/mysqldb/ --user=mysql  &> /dev/null
[ -d /mysqldb ] && echo "安装数据库成功" || { echo "安装数据库失败..退出,请检查" ; exit 8 ; }
#配置文件
sed -ri 's/^(datadir=).*/\1\/mysqldb/' /etc/my.cnf
sed -ri  's/^(socket=).*/\1\/app\/mysql\/mysql.sock/' /etc/my.cnf
#mysql权限设置
chown -R mysql.mysql /mysqldb
chgrp -R mysql /app/mysql/
chmod 775 /app/mysql/
#准备启动脚本
cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
ss -ntl |awk -F" " '{print $4}'|grep ^:::3306$  &> /dev/null
[ $? -eq 0 ] && echo "MYSQL服务正常启动" || echo "MYSQL服务启动失败.."
echo "执行数据库安全策略.."
mysql -u root -e "UPDATE mysql.user SET Password=PASSWORD('123123') WHERE User='root';"
mysql -u root -e "DELETE FROM mysql.user WHERE User='';"
mysql -u root -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
mysql -u root -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
mysql -u root -e "FLUSH PRIVILEGES;"
echo "数据库安全策略执行完毕"
echo "创建wordpress数据库"
mysql -h127.0.0.1 -uroot -p123123 -e "CREATE DATABASE wpdb;"
mysql -h127.0.0.1 -uroot -p123123 -e "CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'centos';"
mysql -h127.0.0.1 -uroot -p123123 -e "GRANT ALL ON wpdb.* TO 'wpuser'@'localhost';"
echo "wordpress数据库创建完成"
}


fun_httpd(){
cd /src
echo "HTTPD准备编译..."
cp -av apr-util-1.6.1 httpd-2.4.38/srclib/apr-util  &> /dev/null
cp -av apr-1.6.5 httpd-2.4.38/srclib/apr   &> /dev/null
cd httpd-2.4.38
echo "HTTPD开始编译..."
./configure --prefix=/app/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$CPUS  &> /dev/null
make install   &> /dev/null
[ $? -eq 0 ] && echo "HTTPD编译完成" ||echo "HTTPD编译故障"
id apache  &> /dev/null || useradd -r -s /sbin/nologin apache
echo '修改配置文件'
sed -ri 's/^(User) .*/\1 apache/'  /app/httpd24/conf/httpd.conf
sed -ri 's/^(Group) .*/\1 apache/'  /app/httpd24/conf/httpd.conf
sed -ri 's/^\#(Serverame)/\1/' /app/httpd24/conf/httpd.conf
sed -ri 's/^#(LoadModule proxy_module modules)/\1/' /app/httpd24/conf/httpd.conf
sed -ri 's/^#(LoadModule proxy_fcgi_module modules)/\1/' /app/httpd24/conf/httpd.conf
sed -ri 's/(DirectoryIndex )index.html$/\1 index.php index.html/' /app/httpd24/conf/httpd.conf
sed -ri 's/#(ServerName)/\1/' /app/httpd24/conf/httpd.conf
echo "AddType application/x-httpd-php .php"  >> /app/httpd24/conf/httpd.conf
echo "AddType application/x-httpd-php-source .phps"  >> /app/httpd24/conf/httpd.conf
echo "ProxyRequests Off"  >> /app/httpd24/conf/httpd.conf
echo "ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/$1"  >> /app/httpd24/conf/httpd.conf
echo "PATH=/app/httpd24/bin:$PATH" > /etc/profile.d/http24.sh
source /etc/profile.d/http24.sh
echo "/app/httpd24/apachectl" >> /etc/rc.local
echo "配置文件修改完毕,启动服务..."
apachectl
cp /app/httpd24/bin/apachectl  /etc/rc.d/init.d/httpd
ss -ntl |awk -F" " '{print $4}'|grep ^:::80$    &> /dev/null
[ $? -eq 0 ] && echo "HTTP服务正常启动" || echo "HTTP服务启动失败.."
}




fun_php(){
echo "开始编译Php..."
cd /src/php-7.3.2
./configure --prefix=/app/php \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-openssl \
--with-pdo-mysql=mysqlnd \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--enable-sockets \
--enable-fpm \
--with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d \
--enable-maintainer-zts \
--disable-fileinfo   &> /dev/null
make -j$CPUS &> /dev/null
make install  &> /dev/null
echo "PHP编译完成"
echo "开始制作配置文件.."
cp /src/php-7.3.2/php.ini-production /etc/php.ini
cp /src/php-7.3.2/sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
cd /app/php/etc
cp php-fpm.conf.default  php-fpm.conf
cp php-fpm.d/www.conf.default  php-fpm.d/www.conf
sed -ri 's/^(pdo_mysql.default_socket=)/\1\/app\/mysql\/mysql.sock/'  /etc/php.ini
sed -ri 's/^(mysqli.default_socket =)/\1 \/app\/mysql\/mysql.sock/' /etc/php.ini
echo "配置文件制作完成,开始启动php-fpm服务"
chkconfig --add php-fpm
chkconfig php-fpm on
service php-fpm start
ss -ntl |grep "\b127.0.0.1:9000\b"  &> /dev/null
[ $? -eq 0 ] && echo "PHP启动成功"  || echo "PHP启动失败 "
}

fun_wordpress(){
echo "开始安装wordpress.."
cd /src/wordpress
rm -f /app/httpd24/htdocs/index.html
mv * /app/httpd24/htdocs/
cd /app/httpd24/htdocs
cp wp-config-sample.php wp-config.php
sed -ri 's/database_name_here/wpdb/' /app/httpd24/htdocs/wp-config.php
sed -ri 's/username_here/wpuser/' /app/httpd24/htdocs/wp-config.php
sed -ri 's/password_here/centos/' /app/httpd24/htdocs/wp-config.php
echo "wordpress安装完毕..."
}



fun_yum
fun_prepare
fun_mysql
fun_httpd
fun_php
fun_wordpress
echo "一键lamp脚本运行完毕"
echo "数据库root账号密码123123,wordpress使用数据库wpdb,登陆数据库名wpuser,密码centos"
echo "谢谢使用"

unset  CPUS lease

 

上一篇:debian 卸载/安装php 安装swoole扩展


下一篇:Linux常用命令