LAMP一键安装

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]/adatadir =/usr/local/mysqldb
innodb_file_per_table = ON
skip_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
AddType 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
danran
原文地址:https://www.cnblogs.com/JevonWei/p/7325310.html