侧边栏壁纸
  • 累计撰写 176 篇文章
  • 累计创建 87 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

一键安装lamp/lnmp脚本

Z先森
2018-11-14 / 0 评论 / 0 点赞 / 18 阅读 / 0 字 / 正在检测是否收录...

####一键安装lamp/lnmp

  • lamp:linux+apache+mysql+php
  • lnmp:linux+nginx+mysql+php

####各组件版本

  • apache 2.4.29
  • nginx 1.14.2
  • mysql 5.6.42
  • redis 4.0.1
  • php 7.2.2

####特点

  • 兼容centos6<后续将加入centos7的兼容>
  • 基于源码编译一键安装lamp或lnmp
  • 可选择组建单独安装
  • nginx添加ngfw防火墙模块
  • php添加redis和tidy等常用扩展
  • 自动添加了80端口iptables通过条例
  • 需手动添加环境变量至/etc/profile
  • 所有源码包均来自官方
  • 自动配置redis和mysql高强度密码

####一键脚本

#!/bin/bash
#一键安装lnmp/lamp脚本<apache nginx mysql redis>
set -e
logdir="/tmp/onekeyinstall/" #临时目录
basedir="/usr/local/" #安装目录

#安装包地址
#----apache----
apache_pac="http://archive.apache.org/dist/httpd/httpd-2.4.29.tar.gz" #apache 2.4.29
apr_pac="https://archive.apache.org/dist/apr/apr-1.5.2.tar.bz2" #apr 1.5.2
apr_util_pac="https://archive.apache.org/dist/apr/apr-util-1.5.4.tar.bz2" #apr-util 1.5.4
pcre_pac="https://ftp.pcre.org/pub/pcre/pcre-8.38.zip" #pcre 8.38
#----nginx----
nginx_pac="http://nginx.org/download/nginx-1.14.2.tar.gz" #nginx 1.14.2
LuaJIT_pac="http://luajit.org/download/LuaJIT-2.0.5.tar.gz" #LuaJIT 2.0.5
ngx_devel_kit_pac="https://codeload.github.com/simpl/ngx_devel_kit/zip/master" #ngx_devel_kit
openresty_pac="https://github.com/openresty/lua-nginx-module/archive/v0.10.14.tar.gz" #openresty
ngx_cache_purge_pac="http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz" #ngx_cache_purge 2.3
ngx_http_substitutions_filter_module="https://github.com/yaoweibin/ngx_http_substitutions_filter_module/archive/master.zip" #ngx_http_substitutions_filter_module
nginx_vts="https://github.com/vozlt/nginx-module-vts/archive/master.zip"
#----mysql----
mysql_pac="https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.42.tar.gz" #mysql 5.6.42
#----redis----
redis_pac="http://download.redis.io/releases/redis-4.0.1.tar.gz" #redis 4.0.1
#----php----
php_pac="http://cl1.php.net/get/php-7.2.2.tar.gz/from/a/mirror" #php 7.2.2
php_redis_pac="http://pecl.php.net/get/redis-4.2.0.tgz" #php-redis 4.2.0扩展


#安装编译环境	
function install_make()
{
	yum install cmake wget epel-release vim perl perl-devel -y
	yum groupinstall "Development Tools" -y
}
install_make
mkdir -p ${logdir}

#生成随机密码
#例(随机生成20位高强度密码): random_passwd 20
function random_passwd()
{
	MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~#$%^&*()_+="
	LENGTH="$1"
	while [ "${n:=1}" -le "$LENGTH" ]
	do
    		PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
    		let n+=1
	done
	echo $PASS
}

#判断是否存在源码包,不存在则下载
function download_pac()
{
	cd ${logdir}
	if [[ `ls | grep -E "tar|zip" | grep $2` -eq 0 ]];then
		wget -c -t 3 "$1" $3
	fi
}

#安装apache
function install_apache()
{
	#卸载残余环境
	#rpm -e `rpm -qa|grep -E "httpd"`
	cd ${logdir}
	download_pac "${apache_pac}" http #apache 2.4.29
	download_pac "${apr_pac}" apr #apr 1.5.2
	download_pac "${apr_util_pac}" apr-util #apr-util 1.5.4
	download_pac "${pcre_pac}" pcre #pcre 8.38
	
	#解压所有安装包
	tar -xvf apr-1.5.2.tar.bz2
	tar -xvf apr-util-1.5.4.tar.bz2
	unzip pcre-8.38.zip
	tar -xvf httpd-2.4.29.tar.gz
	
	#安装apr 1.5.2
	cd ${logdir}apr-1.5.2
	./configure --prefix=${basedir}apr
	make && make install
	
	#安装apr-util 1.5.4
	cd ${logdir}apr-util-1.5.4
	./configure --prefix=${basedir}apr-util --with-apr=${basedir}apr
	make && make install
	
	#安装pcre 8.38
	cd ${logdir}pcre-8.38
	./configure --prefix=${basedir}pcre
	make && make install
	
	#安装apache 2.4.29
	cd ${logdir}httpd-2.4.29
	./configure --prefix=${basedir}apache --with-apr=${basedir}apr --with-apr-util=${basedir}apr-util --with-pcre=${basedir}pcre --enable-so --enable-rewrite --with-mpm=prefork
	make && make install
	
	#修改http配置文件
	sed -i 's/User daemon/User www/g' ${basedir}apache/conf/httpd.conf
	sed -i 's/Group daemon/Group www/g' ${basedir}apache/conf/httpd.conf
	sed -i 's/\#ServerName www.example.com:80/ServerName localhost:80/g' ${basedir}apache/conf/httpd.conf
	useradd -M -s /sbin/nologin www
	cp ${basedir}apache/bin/apachectl /etc/rc.d/init.d/httpd
	echo "${basedir}apache/bin/apachectl -k start" >>/etc/rc.local
	#chkconfig --add httpd
	#chkconfig httpd on
	#启动apache
	service httpd start
	#防火墙添加80端口
	iptables -I INPUT -p tcp --dport 80 -j ACCEPT
	service iptables save
	echo "-----apache安装并启动完毕-----" >${logdir}apache.log
	echo "[http]" >>${logdir}apache.log
	echo "版本:`${basedir}apache/bin/httpd -v | grep version | awk '{print $3}' | awk -F/ '{print $2}'`" >>${logdir}apache.log
	echo "安装目录:${basedir}apache" >>${logdir}apache.log
}

#安装php
function install_php()
{
	#rpm -e `rpm -qa|grep -E "php"` #卸载残余
	yum install libwebp libwebp-devel ImageMagick libxml2-devel.x86_64 openssl.x86_64 openssl-devel.x86_64 bzip2-devel.x86_64 libcurl.x86_64 libcurl-devel.x86_64 libjpeg libpng.x86_64 freetype.x86_64 libjpeg-devel libpng-devel.x86_64 freetype-devel.x86_64 libmcrypt libmcrypt-devel mysql-devel -y #解决依赖
	cd ${logdir}
	download_pac "${php_pac}" php #php 7.2.2
	mv mirror php-7.2.2.tar.gz
	#解压安装包
	tar -xvf php-7.2.2.tar.gz
	
	#安装php
	cd ${logdir}php-7.2.2
	./configure --prefix=${basedir}php --with-libxml-dir --with-apxs2=${basedir}apache/bin/apxs --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-gd --enable-gd-native-ttf --with-jpeg-dir --with-png-dir --with-webp-dir --with-zlib --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
	make && make install
	cp php.ini-development ${basedir}php/lib/php.ini
	#php安全配置
	sed -i 's/disable_classes =/disable_classes = COM,pcntl_exec,putenv,mail,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,popen,dl,syslog,show_source,socket_create/g' ${basedir}php/lib/php.ini
	sed -i 's/disable_functions =/disable_functions = COM,pcntl_exec,putenv,mail,passthru,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,popen,dl,syslog,show_source,socket_create/g' ${basedir}php/lib/php.ini
	echo "-----php安装完毕-----" >${logdir}php.log
	echo "[php]" >>${logdir}php.log
	echo "版本:`${basedir}php/bin/php -v | head -1 | awk '{print $2}'`" >>${logdir}php.log
	echo "安装目录:${basedir}php" >>${logdir}php.log
}

#安装nginx
function install_nginx()
{
#	rpm -e `rpm -qa|grep -E "nginx"` #卸载残余
	yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel
	cd ${logdir}
	download_pac "${nginx_pac}" nginx #nginx 1.14.2
	download_pac "${LuaJIT_pac}" LuaJIT #LuaJIT 2.0.5
	download_pac "${ngx_devel_kit_pac}" ngx_devel_kit "-O ngx_devel_kit.zip" #ngx_devel_kit
	download_pac "${openresty_pac}" openresty "-O openresty.zip" #openresty
	download_pac "${ngx_cache_purge_pac}" ngx_cache_purge #ngx_cache_purge 2.3
	download_pac "${ngx_http_substitutions_filter_module}" ngx_http_substitutions "-O ngx_http_substitutions_filter_module.zip"
	download_pac "${nginx_vts}" nginx-vts "-O nginx-vts.zip"
	#解压安装包
	tar -xvf nginx-1.14.2.tar.gz
	tar -xvf LuaJIT-2.0.5.tar.gz
	unzip ngx_devel_kit.zip
	tar -xvf v0.10.14.tar.gz
	unzip ngx_http_substitutions_filter_module.zip
	unzip nginx-vts.zip
	tar -xvf ngx_cache_purge-2.3.tar.gz
	
	cd ${logdir}LuaJIT-2.0.5
	make && make install
	export LUAJIT_LIB=${basedir}lib
	export LUAJIT_INC=${basedir}include/luajit-2.0
	export LD_LIBRARY_PATH=${basedir}lib:$LD_LIBRARY_PATH
	cd ${logdir}nginx-1.14.2
	./configure --prefix=${basedir}nginx --user=www --group=www --with-mail --with-mail_ssl_module --with-http_ssl_module --with-http_flv_module --with-http_dav_module --with-http_sub_module --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre --add-module=${logdir}ngx_cache_purge-2.3/ --add-module=${logdir}ngx_devel_kit-master/ --add-module=${logdir}lua-nginx-module-0.10.14/ --add-module=${logdir}ngx_http_substitutions_filter_module-master/ --add-module=${logdir}nginx-module-vts-master/
	make && make install
	ln -s ${basedir}nginx/sbin/nginx /usr/bin/nginx
	ln -s ${basedir}lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
	echo -e '#!/bin/sh\n# chkconfig: - 85 15 \n# description: nginx is a World Wide Web server. It is used to serve \n#               proxy and IMAP/POP3 proxy server\n# Description:        Start and Stop Nginx\n# Provides:        nginx\n# Default-Start:    2 3 4 5\n# Default-Stop:        0 1 6\nPATH=${basedir}sbin:${basedir}bin:/sbin:/bin:/usr/sbin:/usr/bin\nNAME=nginx\nNGINX_BIN=${basedir}nginx/sbin/$NAME\nCONFIGFILE=${basedir}nginx/conf/$NAME.conf\nPIDFILE=/home/www/pid/$NAME.pid\nSCRIPTNAME=/etc/init.d/$NAME\ncase "$1" in\nstart)\necho -n "Starting $NAME... "\nif netstat -tnpl | grep -q nginx;then\necho "$NAME (pid `pidof $NAME`) already running."\nexit 1\nfi\n$NGINX_BIN -c $CONFIGFILE\nif [ "$?" != 0 ] ; then\necho " failed"\nexit 1\nelse\necho " done"\nfi\n;;\nstop)\necho -n "Stoping $NAME... "\nif ! netstat -tnpl | grep -q nginx; then\necho "$NAME is not running."\nexit 1\nfi\n$NGINX_BIN -s stop\nif [ "$?" != 0 ] ; then\necho " failed. Use force-quit"\nexit 1\nelse\necho " done"\nfi\n;;\nstatus)\nif netstat -tnpl | grep -q nginx; then\nPID=`pidof nginx`\necho "$NAME (pid $PID) is running..."\nelse\necho "$NAME is stopped"\nexit 0       \nfi\n;;\nforce-quit)\necho -n "Terminating $NAME... "\nif ! netstat -tnpl | grep -q nginx; then\necho "$NAME is not running."\nexit 1\nfi\nkill `pidof $NAME`\nif [ "$?" != 0 ] ; then\necho " failed"\nexit 1\nelse\necho " done"   \nfi\n;;\nrestart)\n$SCRIPTNAME stop\nsleep 1\n$SCRIPTNAME start\n;;\nreload)                                                                                      \necho -n "Reload service $NAME... "\nif netstat -tnpl | grep -q nginx; then\n$NGINX_BIN -s reload\necho " done"\nelse\necho "$NAME is not running, can not reload."\nexit 1\nfi\n;;\nconfigtest)\necho -n "Test $NAME configure files... "\n$NGINX_BIN -t\n;;\n*)\necho "Usage: $SCRIPTNAME {start|stop|force-quit|restart|reload|status|configtest}"\nexit 1\n;;\nesac\n' >/etc/init.d/nginx
	chmod a+x /etc/init.d/nginx
	chkconfig --add nginx
	chkconfig --level 2345 nginx on
	#启动nginx
	service nginx start
	#防火墙添加80端口
	iptables -I INPUT -p tcp --dport 80 -j ACCEPT
	service iptables save
	echo "-----nginx安装并启动完毕-----" >${logdir}nginx.log
	echo "[nginx]" >>${logdir}nginx.log
	echo "版本:1.14.2" >>${logdir}nginx.log
	echo "安装目录:${basedir}nginx" >>${logdir}nginx.log
}

#安装redis
function install_redis()
{
#	rpm -e `rpm -qa|grep -E "redis"` #卸载残余
        cd ${logdir}
	download_pac "${redis_pac}" redis  #redis 4.0.1
	
	#解压安装包
	tar -xvf redis-4.0.1.tar.gz
	
	#安装redis 4.0.1
	cd ${logdir}redis-4.0.1
	make
	cd src
	make install PREFIX=${basedir}redis
	cd ..
	cp ${basedir}redis/redis.conf ${basedir}redis/redis.conf.bak
	#修改redis默认数据目录为/home/data/redis
	sed -i 's/dir .\//dir \/home\/data\/redis/g' ${basedir}redis/redis.conf
	mkdir -p /home/data/redis
	#后台运行
	sed -i 's/daemonize no/daemonize yes/g' ${basedir}redis/redis.conf
	#开机启动
	echo  "${basedir}redis/bin/redis-server ${basedir}redis/redis.conf" >> /etc/rc.local
	#添加随机高安全redis口令
	redis_passwd=`random_passwd 20`
	sed "/SECURITY/a\requirepass ${redis_passwd}" ${basedir}redis/redis.conf
	#启动redis
	${basedir}redis/bin/redis-server ${basedir}redis/redis.conf
	echo "-----redis安装并启动完毕-----" >${logdir}redis.log
        echo "[redis]" >>${logdir}redis.log
        echo "版本:`${basedir}redis/bin/redis-server -v | awk '{print $3}' | awk -F= '{print $2}'`" >>${logdir}redis.log
        echo "安装目录:${basedir}redis" >>${logdir}redis.log
        echo "数据目录:/home/data/redis" >>${logdir}redis.log
	echo "密码:${redis_passwd}" >>${logdir}redis.log
}

#安装mysql
function install_mysql()
{
	rm -fr /etc/my.cnf #删除默认mysql配置文件
	groupadd mysql && useradd -g mysql -s /bin/false mysql #添加用户
        cd ${logdir}
	yum install -y bison bison-devel ncurses ncurses-devel perl
	download_pac "${mysql_pac}" mysql #mysql 5.6.38
	
	#解压安装包
	tar -xvf mysql-5.6.42.tar.gz
	
	#安装mysql 5.6.42
	cd ${logdir}mysql-5.6.42
	#设置mysql默认数据目录为/home/data/mysql
	mkdir -p /home/data/mysql && chown -R mysql.mysql /home/data/mysql
	cmake . -DCMAKE_INSTALL_PREFIX=${basedir}mysql -DMYSQL_DATADIR=/home/data/mysql -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci
	make && make install
	#初始化mysql
	${basedir}mysql/scripts/mysql_install_db --user=mysql --basedir=${basedir}mysql --datadir=/home/data/mysql
	ln -s ${basedir}mysql/my.cnf /etc/my.cnf
	cp ${basedir}mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
	#添加开机启动
	chmod 755 /etc/init.d/mysqld
	chkconfig mysqld on
	#启动mysql
	service mysqld start
	#添加20位随机高安全root密码
	mysql_passwd=`random_passwd 20`
	${basedir}mysql/bin/mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '${mysql_passwd}' WITH GRANT OPTION;"
	${basedir}mysql/bin/mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '${mysql_passwd}' WITH GRANT OPTION;"
	echo "-----mysql安装并启动完毕-----" >${logdir}mysql.log
        echo "[mysql]" >>${logdir}mysql.log
        echo "版本:`${basedir}mysql/bin/mysqld -V | awk '{print $3}'`" >>${logdir}mysql.log
        echo "安装目录:${basedir}mysql" >>${logdir}mysql.log
        echo "数据目录:/home/data/mysql" >>${logdir}mysql.log
	echo "root密码:${mysql_passwd}" >>${logdir}mysql.log
}

#apache添加php支持
function apache_add_php()
{
	sed -i 's/#LoadModule rewrite_module modules/LoadModule rewrite_module modules/g' ${basedir}apache/conf/httpd.conf
	echo -e "<FilesMatch \.php$>\n\tSetHandler application/x-httpd-php\n</FilesMatch>" >>${basedir}apache/conf/httpd.conf
	sed -i "s/DirectoryIndex\ index.html/DirectoryIndex\ index.html\ index.php/g" ${basedir}apache/conf/httpd.conf
	service httpd restart
}

#php安装redis扩展
function install_php_redis()
{
	cd ${logdir}
	download_pac "${php_redis_pac}" redis #php_redis 4.2.0 扩展
	tar -zxvf redis-4.2.0.tgz
	cd ${logdir}redis-4.2.0
	${basedir}php/bin/phpize
	./configure --with-php-config=${basedir}php/bin/php-config
	make && make install
	sed -i 's/\;extension=php_bz2/extension=redis.so\n\;extension=php_bz2/g' ${basedir}php/lib/php.ini
}

#php安装tidy扩展
function install_php_tidy()
{
	yum install libtidy libtidy-devel -y
	cd ${logdir}php-7.1.10
	cd ext
	cd tidy
	${basedir}php/bin/phpize
	./configure --with-php-config=${basedir}php/bin/php-config
	make && make install
	sed -i 's/\;extension=php_bz2/extension=tidy.so\n\;extension=php_bz2/g' ${basedir}php/lib/php.ini
}

echo "################################################"
echo "##                                            ##"
echo "##                                            ##"
echo "##  redis mysql apache nginx php 一键安装脚本 ##"
echo "##                                            ##"
echo "##                                            ##"
echo "################################################"
echo "1.一键安装lamp:<apache mysql php>"
echo "2.一键安装lnmp:<nginx mysql php>"
echo "3.单独安装redis 4.1.0"
echo "4.单独安装mysql 5.6.38"
echo "5.单独安装apache 2.4.29"
echo "6.单独安装nginx 1.15.5"
echo "7.单独安装php 7.1.10"
echo "8.单独安装php-redis扩展"
echo "9.单独安装php-tidy扩展"
echo "10.http添加php解析<如果单独安装php并且本地安装了apache在/usr/local/apache,则需选择此项添加php解析>"

read -t 30 -p "请输入你的选择: 1-7 " choose
case "$choose" in
	"1")
		echo "1.一并安装php扩展redis.so、tidy.so"
		echo "2.不安装php扩展redis.so、tidy.so"
		read -t 30 -p "请输入你的选择: 1或2 " addso
		case "${addso}" in
			"1")
				echo 你的选择是1.一键安装lamp,并安装redis.so,tidy.so
				install_apache
				install_php
				install_mysql
				apache_add_php
				install_php_redis
				install_php_tidy
			;;
			"2")
				echo 你的选择是1.一键安装lamp,不安装redis.so,tidy.so
				install_apache
				install_php
				install_mysql
				apache_add_php
			;;
			"*")
				echo "请输入1或2"
		esac
	;;
	"2")
                echo "1.一并安装php扩展redis.so、tidy.so"
                echo "2.不安装php扩展redis.so、tidy.so"
                read -t 30 -p "请输入你的选择: 1或2 " noaddso
                case "${noaddso}" in
                        "1")
				echo 你的选择是1.一键安装lnmp,并安装redis.so,tidy.so
				install_nginx
				install_php
				install_mysql
				install_php_redis
				install_php_tidy
                        ;;
                        "2")
				echo 你的选择是1.一键安装lnmp,不安装redis.so,tidy.so
				install_nginx
				install_php
				install_mysql
                        ;;
                        "*")
                                echo "请输入1或2"
                esac    
	;;
	"3")
		echo 你的选择是3.单独安装redis 4.1.0
		install_redis
	;;
	"4")
		
		echo 你的选择是4.单独安装mysql 5.6.38
		install_mysql
	;;
	"5")
		echo 你的选择是5.单独安装apache 2.4.29
		install_apache
	;;
	"6")
		echo 你的选择是6.单独安装nginx 1.15.5	
		install_nginx
	;;
	"7")
		echo 你的选择是7.单独安装php 7.1.10	
		echo "1.一并安装php扩展redis.so、tidy.so"
                echo "2.不安装php扩展redis.so、tidy.so"
                read -t 30 -p "请输入你的选择: 1或2 " php
                case "${php}" in
                        "1")
                                echo 你的选择是1.单独安装php,并安装redis.so,tidy.so
                                install_php
                                install_php_redis
                                install_php_tidy
                        ;;
                        "2")
                                echo 你的选择是1.单独安装php,不安装redis.so,tidy.so
                                install_php
                        ;;
                        "*")
				echo "请选择1或2"
		esac
	;;
    "8")
            echo 你的选择是8.单独安装php-redis扩展
            install_php_redis
    ;;
    "9")
            echo 你的选择是9.单独安装php-tidy扩展
            install_php_tidy
    ;;
    "10")
            echo 你的选择是10.http添加php解析
            apache_add_php
    ;;
	"*")
	echo "请输入1-7"
esac

#输出信息
cat ${logdir}*.log

#清除临时文件
cd ${logdir}
rm -fr `ls | grep -E "tar|zip"`

下载一键脚本

0

评论区