lnmp架构之一键搭建wordpress博客网站脚本(适用于centos7和centos8)

#!/bin/sh
set -e
. /etc/init.d/functions
#定义变量
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
"
ML=/root
PHPPAGE=php-7.4.12.tar.bz2
cudirectory=/root
php_file=/apps/php74
phpversion=php-7.4.12
COLOR="echo -e \\E[1;32m"
COLOR1="echo -e \\E[1;31m"
END="\\E[0m"
nginx_page=nginx-1.18.0.tar.gz
nginx_ml=/apps/nginx
mysql_ml=/data/mysql

#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
install_nginx() {
    #创建用户
    id nginx &> /dev/null || useradd -s /sbin/nologin nginx
    
    #下载源码包
    ${COLOR}"正在下载源码包,请稍等!"${END}
    cd /usr/local/src/
    [ -e $nginx_page ] || wget http://nginx.org/download/$nginx_page &> /dev/null
    tar xf $nginx_page
    
    #创建安装目并修改权限
    [ -d ${nginx_ml}/run ] || mkdir -p ${nginx_ml}/run
    chown -R nginx.nginx ${nginx_ml}
    
    #编译
    ${COLOR}"正在编译安装,请稍等!"${END}
    cd  nginx-1.18.0/
    ./configure --prefix=${nginx_ml} \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_stub_status_module \
    --with-http_gzip_static_module \
    --with-pcre \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_realip_module

    #安装
    make -j `lscpu | awk '/^CPU\(s\)/{print $2}'` && make install

    #修改配置文件
    [ -e ${nginx_ml}/conf/nginx.conf ] && sed -i 's@^#pid.*@pid /apps/nginx/run/nginx.pid;@' ${nginx_ml}/conf/nginx.conf

    #修改配置
    [ -e ${nginx_ml}/conf/nginx.conf ] && sed -i '/#gzip  on;/a\    include \/apps\/nginx\/conf\.d\/\*\.conf;' ${nginx_ml}/conf/nginx.conf
    
    #创建配置文件
    cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID

[Install]
WantedBy=multi-user.target
EOF
    
    #配置nginx支持fastcgi
    mkdir -p ${nginx_ml}/conf.d
    cat > ${nginx_ml}/conf.d/test.conf <<EOF
server {
    listen       80;
    server_name www.magedu.org;
    server_tokens off;
    location / {
           root   /data/nginx/wordpress;
           index index.php index.html index.htm;
        }
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
           root   html;
        }
        location ~ \.php$ {
           root           /data/nginx/wordpress;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
           include       fastcgi_params;
       fastcgi_hide_header X-Powered-By;
        }
        location ~ ^/(ping|status)$ {
           include fastcgi_params;
           fastcgi_pass 127.0.0.1:9000;
           fastcgi_param PATH_TRANSLATED \$document_root\$fastcgi_script_name;
        } 
}
EOF
    
    #命令创建
    ln -s ${nginx_ml}/sbin/nginx /usr/sbin/nginx
    
    #启动服务
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null && action "nginx安装启动完成!" || action "nginx启动失败,请检查配置文件!" false 
}

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

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

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

    #解压二进制程序
    tar xf 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
install_php() {
    . /etc/init.d/functions
    
    #判断是否在包文件目录
    [[ `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 = nginx/' ${php_file}/etc/php-fpm.d/www.conf
    sed -i 's/^group.*/group = nginx/' ${php_file}/etc/php-fpm.d/www.conf

    #支持status和ping页面
    sed -i '/;pm.status_path = \/status/a\pm.status_path = \/status' ${php_file}/etc/php-fpm.d/www.con
    sed -i '/;ping.path = \/ping/a\ping.path = \/ping' ${php_file}/etc/php-fpm.d/www.con

    #设置支持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 nginx | awk '/Active/{print $2}'` == active ]] ;then
    action "nginx已安装过,跳过"
else
    install_nginx && action "nginx安装完成" || action "nginx安装失败,请检查配置文件" 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
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/nginx
setfacl -R -m u:nginx:rwx /data/nginx/wordpress/
}

install_wordpress && action "wordpress配置完成" || action "wordpress配置失败,请检查文件" false

 

posted @ 2020-11-30 21:07  yt丶独自  阅读(279)  评论(0编辑  收藏  举报