LAMP一键安装

本文介绍了一种在CentOS 7上一键部署LAMP环境的方法,包括Mariadb、Apache、PHP及WordPress的安装配置过程,并提供了自动化脚本。

author:JevonWei
版权声明:原创作品


    #!/bin/bash

定义变量

    export MDB=$(rpm -qa *mariadb*)
    export HTT=$(rpm -qa *httpd*)
    export MDB_USER=$(getent passwd mysql)
    export HTT_USER=$(getent passwd apache)

配置yum源

fun_yum() {
    if [ -d /etc/yum.repos.d/backup ]; then
    mv -f /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
else 
    mkdir /etc/yum.repos.d/backup && mv -f /etc/yum.repos.d/*.repo /etc/repos.d/backup
fi
    cat > /etc/yum.repos.d/lamp.repo    <<EOF
[base]
name=danran
baseurl=https://mirrors.aliyun.com/centos/7.3.1611/os/x86_64/
gpgcheck=0
enable=1
   
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
gpgcheck=0
enable=1
EOF
    
    yum clean all
}

前期准备

fun_prepare() {
    systemctl stop firewalld
    systemctl disable firewalld
iptables -F
    setenforce 0
    sed -ri.bak 's/(^SELINUX=).*/\1permissive/' /etc/selinux/config
    echo "export PATH=/usr/local/apache24/bin:/usr/local/mysql/bin:$PATH" > /etc/profile.d/lamp.sh
source /etc/profile.d/lamp.sh
    if [ -n "$MDB" ];then
            yum -y remove *mariadb*
    fi
    if [ -n "$HTT" ];then
            yum -y remove *httpd*
    else
            useradd -r -s /sbin/nologin apache -m
    fi
    yum -y install bzip2 gzip wget pcre-devel openssl-devel libxml2-devel bzip2-devel libmcrypt-devel libaio*
    yum -y groupinstall "development tools"
 
#     cd /usr/local/src         
#         ls /usr/local/src | xargs -n1 tar xf
    if [ -e /usr/local/src ];then
            cd /usr/local/src
            if [[ ! -e /usr/local/src/apr-1.5.2.tar.bz2 ]];then
            wget http://archive.apache.org/dist/apr/apr-1.5.2.tar.bz2 
                tar xf apr-1.5.2.tar.bz2
            else
                    tar xf apr-1.5.2.tar.bz2
            fi
            
            if [[ ! -e /usr/local/src/apr-util-1.5.4.tar.bz2 ]];then
                wget http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.bz2 
            tar xvf apr-util-1.5.4.tar.bz2
            else
        tar xvf apr-util-1.5.4.tar.bz2
    fi

            if [[ ! -e /usr/local/src/httpd-2.4.27.tar,bz2 ]];then
                    wget http://apache.fayea.com/httpd/httpd-2.4.27.tar.bz2
                    tar xvf httpd-*.tar.bz2
            else#                        tar xvf httpd-*.tar.bz2
                    
            fi

            if [[ ! -e /usr/local/src/mariadb-10.2.7-linux-x86_64.tar.gz ]];then
                    wget http://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.2.7/bintar-linux-x86_64/mariadb-10.2.7-linux-x86_64.tar.gz
                   tar xvf mariadb-10.2.7-linux-x86_64.tar.gz
            else
                    tar xvf mariadb-10.2.7-linux-x86_64.tar.gz
            fi

            if [[ ! -e /usr/local/src/php-7.1.7.tar.bz2 ]];then
                    wget http://cn2.php.net/distributions/php-7.1.7.tar.bz2
                    tar xvf php-7.1.7.tar.bz2
            else
                    tar xvf php-7.1.7.tar.bz2
            fi

            if [[ ! -e /usr/local/src/wordpress-4.8-zh_CN.tar.gz ]];then
                    wget wget https://cn.wordpress.org/wordpress-4.8.1-zh_CN.tar.gz
                    tar xvf wordpress-4.8-zh_CN.tar.gz
            else
                    tar xvf wordpress-4.8-zh_CN.tar.gz
            fi
    else
            mkdir /usr/local/src -p
            cd /usr/local/src
            wget http://archive.apache.org/dist/apr/apr-1.5.2.tar.bz2
            wget http://archive.apache.org/dist/apr/apr-util-1.5.4.tar.bz2
            wget http://apache.fayea.com/httpd/httpd-2.4.27.tar.bz2
            wget http://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.2.7/bintar-linux-x86_64/mariadb-10.2.7-linux-x86_64.tar.gz
            wget http://cn2.php.net/distributions/php-7.1.7.tar.bz2
            wget https://cn.wordpress.org/wordpress-4.8.1-zh_CN.tar.gz
            ls | xargs -n1 tar xf
    fi

}

httpd编译安装

fun_httpd() {
    cd /usr/local/src
    mv apr-1.5.2 httpd-2.4.27/srclib/apr
    mv apr-util-1.5.4 httpd-2.4.27/srclib/apr-util
    cd httpd-2.4.27/
    ./configure --prefix=/usr/local/apache24 --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
    make && make install
    if [ -z "$HTT_USER" ];then
        useradd -r apache -s /sbin/nologin
    fi
    sed -ri 's/(User )daemon/\1apache/' /usr/local/apache24/conf/httpd.conf
    sed -ri 's/(Group )daemon/\1apache/' /usr/local/apache24/conf/httpd.conf
    apachectl start

}

mariadb二进制安装

fun_mariadb() {
    cd /usr/local/src
    if [ -z "$MDB_USER" ];then
        useradd -r mysql -s /sbin/nologin -d /usr/local/mysqldb -m
    fi
    mv mariadb-10.2.7-linux-x86_64  /usr/local/mysql
    chgrp -R mysql /usr/local/mysql
    cd /usr/local/mysql
    scripts/mysql_install_db --datadir=/usr/local/mysqldb --user=mysql
    [ ! -e /etc/mysql ] && mkdir /etc/mysql
    cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf

    sed -ri '/\[mysqld\]/a\datadir =/usr/local/mysqldb\ninnodb_file_per_table = ON\nskip_name_resolve = ON' /etc/mysql/my.cnf 
    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    chkconfig --add mysqld
    service mysqld start
    mysql -e "create database blog;grant all on blog.* to 'blog'@172.%.%.%' identified by 'blog';"
    . /etc/profile.d/lamp.sh

}

PHP编译安装

fun_php() {
    cd /usr/local/src/php-7.1.7
    ./configure --prefix=/usr/local/php --enable-mysqlnd  --with-mysqli=mysqlnd   --with-openssl --enable-mbstring --with-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache24/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
    make && make install
    cp php.ini-production /etc/php.ini

    sed -ri '/IfModule mime_module/a\    AddType application/x-httpd-php .php\nAddType application/x-httpd-php-source .phps' /usr/local/apache24/conf/httpd.conf
    sed -ri 's/(^[[:space:]]+DirectoryIndex).*/\1 index.html index.php/' /usr/local/apache24/conf/httpd.conf
}

wordpress安装

fun_wordpress() {
    cd /usr/local/src
    cp -a wordpress /usr/local/apache24/htdocs/blog
    chown -R apache /usr/local/apache24/htdocs/blog

    setfacl -m u:daemon:rwx /app/httpd24/htdocs/blog/
#   或
#   cp /usr/local/apache24/htdocs/blog/wp-config-sample.php /usr/local/apache24/htdocs/blog/wp-config.php
#   sed -ri 's/database_name_here/blog/' /usr/local/apache24/htdocs/blog/wp-config.php
#   sed -ri 's/username_here/blog/' /usr/local/apache24/htdocs/blog/wp-config.php
#   sed -ri 's/password_here/blog/' /usr/local/apache24/htdocs/blog/wp-config.php
#   rm -f /usr/local/apache24/htdocs/index.html

}

fun_yum
fun_prepare
fun_httpd
fun_mariadb
fun_php
fun_wordpress
apachectl stop
apachectl start
service mysqld restart
echo "安装完毕"
echo "账号密码为blog"
echo "请尽快登录验证"

unset MDB HTT MDB_USER HTT_USER

exit

转载于:https://www.cnblogs.com/JevonWei/p/7325310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值