#/!bin/bash#+++++++++++++++++++++++++++++++++++++++ 脚本信息 +++++++++++++++++++++++++++++++++++++++#################################################### ## System: centos7 ## Version: v1.0 ## Description: System init and install LNMP ## Author: Sand ## Mail: sandbenene@163.com ## Date: 2022.4.16 ## ##################################################################################################################### 注意点:运行脚本需要在脚本所在路径,系统初始环境必须干净### 安装包存放位置 :~/package### 配置文件存放位置 :~/config### test : sh -vx system_init.sh### grep -v '^$|^;' etc/php.ini #################### 要求 ######################################## 1,JDK 1.8.0_91### 2,mysql 5.7.30 不对外访问### 3,redis 5.0.14 不对外访问### 4,php 7.3.32### 5,nginx 1.20.2###### JDK 网络下载地址 : https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html### nginx 网络获取安装包: wget http://nginx.org/download/nginx-1.20.2.tar.gz### php 网络获取安装包: wget https://www.php.net/distributions/php-7.3.32.tar.gz### mysql 网络下载地址 : wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz### redis 网络下载安装包:wget https://download.redis.io/releases/redis-5.0.14.tar.gz### ################################################# [root@localhost ~]# tree auto### auto### ├── info.sh### ├── init.sh### ├── mian.sh### ├── config### │ ├── java### │ │ └── java.sh### │ ├── mysql### │ │ ├── my.cnf### │ │ └── mysqld.service### │ ├── nginx### │ │ ├── default.conf### │ │ ├── nginx.conf### │ │ └── nginx.service### │ └── redis### │ ├── redis.conf### │ └── redis.service### └── packages### ├── jdk-8u91-linux-x64.tar.gz### ├── mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz### ├── nginx-1.22.0.tar.gz### ├── php-7.3.30.tar.gz### └── redis-5.0.14.tar.gz#=======================================脚本通用变量=======================================#filedir=$(cd `dirname $0`;pwd)packages=$filedir/work/packagesconfig=$filedir/work/confignginx_version=nginx-1.22.0nginx_install_path=/usr/local/nginxmysql_version=mysql-5.7.36mysql_install_path=/usr/local/mysqlredis_version=redis-5.0.14redis_install_path=/usr/local/redisphp_version=php-7.3.30php_install_path=/usr/local/php#========================================脚本插件=======================================##################### 插件一:字体 ######################### 设置文件输出警告色,info/warning/error "你要输出信息"### 使用方法### info "This is a info!"### warnning "This is a warn"### error "This is a error"function info(){ # 32 绿色字 SAND=${1:-'没有传参'} echo -e "\033[1;32mINFO: $SAND\033[0m"}function warnning(){ # 33 黄色字 SAND=${1:-'没有传参'} echo -e "\033[1;33mWARN: $SAND\033[0m"}function error(){ # 31 红色字 SAND=${1:-'没有传参'} echo -e "\033[1;5;41;37mERROR: $SAND\033[0m"}#################### 插件二:判断服务是否安装 ######################function judge_server_install(){ NO_install=0 if [ $SERVER == 'docker' ]; then if [ -f /bin/docker ]; then warnning "docker已安装" NO_install=1 fi elif [ $SERVER == 'mysql' ]; then if [ -f /usr/local/mysql/bin/mysqld ]; then warnning "mysql已安装" NO_install=1 fi elif [ $SERVER == 'php' ]; then if [ -f /usr/local/php/sbin/php-fpm ]; then warnning "php已安装" NO_install=1 fi elif [ $SERVER == 'nginx' ]; then if [ -f /usr/local/nginx/sbin/nginx ]; then warnning "nginx已安装" NO_install=1 fi elif [ $SERVER == 'zabbix-agent' ]; then if [ -f /etc/zabbix/zabbix_agent2.conf ]; then warnning "zabiix-agent2已安装" NO_install=1 fi elif [ $SERVER == 'redis' ]; then if [ -f /usr/local/redis/bin/redis-server ]; then warnning "redis已安装" NO_install=1 fi fi}#========================================功能模块=======================================#function install_docker(){ SERVER=docker judge_server_install if [ ! $NO_install -eq 1 ]; then info +++++++++++++++++++++++安装docker20.10.8+++++++++++++++++++++++ info "1.卸载部分已安装软件" yum -y remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine &>/dev/null info "2.安装yum-config-manager和yum-utils" yum install -y yum-utils >&/dev/null yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo &>/dev/null sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo info "3.安装20.10版本Docker" yum -y install docker-ce-20.10.8-3.el7 docker-ce-cli-20.10.8-3.el7 containerd.io &>/dev/null info "4.配置加速源" [ ! -d /etc/docker ] && mkdir /etc/docker && touch /etc/docker/daemon.json echo -e "{ \n \"registry-mirrors\": [\n \"https://hub-mirror.c.163.com\",\n \"https://mirror.baidubce.com\" \n ]\n}" >/etc/docker/daemon.json info "5.安装Docker-Compose" if [ ! -f /usr/local/bin/docker-compose ]; then curl -L https://download.fastgit.org/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m`> /usr/local/bin/docker-compose &>/dev/null chmod +x /usr/local/bin/docker-compose fi info "6.启动" systemctl enable --now docker &>/dev/null if [ ps -ef |grep dockerd |wc -l -gt 1 ] ; then info "+++++docker以启动 √+++++" fi fi}function install_zabbix_agent(){ SERVER='zabbix-agent' judge_server_install if [ ! $NO_install -eq 1 ]; then info +++++++++++++++++++++++安装zabbix-agent2+++++++++++++++++++++++ info "1.获取zabbix源,请耐心等待..." rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm &>/dev/null sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#g' /etc/yum.repos.d/zabbix.repo sed -i 's/enabled=0/enabled=1/' /etc/yum.repos.d/zabbix.repo info "2 安装zabbix-agent2" yum -y install zabbix-agent2 &>/dev/null info "3.开启zabbix-agent2" systemctl enable --now zabbix-agent2 &>/dev/null if ss -antl |grep :10050 &>/dev/null ; then info "+++++zabbix-agent以启动 √+++++" fi fi}function install_nginx(){ SERVER=nginx judge_server_install if [ ! $NO_install -eq 1 ];then info "+++++++++++++++++++++++安装$nginx_version+++++++++++++++++++++++" info "1. 创建nginx用户" if ! grep www /etc/passwd &>/dev/null ;then useradd -r -s /sbin/nologin -M www fi info "2. 解决依赖" yum install -y gcc gcc-c++ glibc glibc-devel pcre pcre-devel zlib zlib-devel openssl openssl-devel wget &>/dev/null info "3. 下载nginx" if [ ! -f $packages/$nginx_version.tar.gz ]; then wget -O $packages/$nginx_version.tar.gz https://nginx.org/download/$nginx_version.tar.gz &>/dev/null fi info "4. 解压" cd $packages && tar -xf $packages/$nginx_version.tar.gz info "5. 编译nginx" cd $packages/$nginx_version ./configure --prefix=$nginx_install_path --user=www --with-http_stub_status_module --with-http_ssl_module &>/dev/null make &>/dev/null && make install &>/dev/null info "6. 设置systemd管理" ln -s $nginx_install_path/sbin/* /sbin/ \cp -f $config/nginx/nginx.service /usr/lib/systemd/system/ info "7. 配置nginx" \cp -f $config/nginx/nginx.conf $nginx_install_path/conf/ mkdir -p $nginx_install_path/conf/vhost/ && \cp -f $config/nginx/default.conf $nginx_install_path/conf/vhost/ info "8. 启动nginx" systemctl enable --now nginx if ss -antl |grep :80 &>/dev/null ; then info "+++++nginx以启动 √+++++" fi fi}function install_mysql(){ SERVER=mysql judge_server_install if [ ! $NO_install -eq 1 ];then info "1. 解决依赖" yum -y remove `rpm -qa | grep mariadb` &>/dev/null yum install -y libaio-* &>/dev/null info "2. 创建mysql用户" groupadd mysql && useradd -M -s /sbin/nologin -r -g mysql mysql info "3. 解压,安装" cd $packages tar -xf $packages/$mysql_version-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ mv /usr/local/$mysql_version-linux-glibc2.12-x86_64/ $mysql_install_path # 设置mysql目录权限 chown -R mysql.mysql $mysql_install_path info "4. 设置systemd管理" ln -s /usr/local/mysql/bin/* /bin/ echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile && source /etc/profile cd /usr/lib/systemd/system && touch mysqld.service && chmod 644 mysqld.service cp $config/mysql/mysqld.service /etc/systemd/system/mysqld.service cd /usr/lib/tmpfiles.d && touch mysql.conf && chmod 644 mysql.conf echo 'd /usr/local/mysql/data 0750 mysql mysql -' > /usr/lib/tmpfiles.d/mysql.conf info "5. 配置服务器启动选项,初始化数据目录" cp $config/mysql/my.cnf /etc/my.cnf mkdir -p /data/mysql/mysql3306/{data,logs,tmp} chown -R mysql:mysql /data/mysql/ info "6. 初始化MySQL" cd /usr/local/mysql ./bin/mysqld --defaults-file=/etc/my.cnf --initialize &>/dev/null echo "----请及时修改密码,密码:【SET PASSWORD = PASSWORD('lnmp@123');】" grep "temporary password" /data/mysql/mysql3306/data/error.log info "7. 启动mysql" systemctl enable --now mysqld &>/dev/null if ss -antl |grep :3306 &>/dev/null ; then info "+++++mysql以启动 √+++++" fi fi}function install_redis(){ SERVER=redis judge_server_install if [ ! $NO_install -eq 1 ];then info +++++++++++++++++++++++安装redis-5.0.14+++++++++++++++++++++++ info "1. 创建redis用户" groupadd redis && useradd -M -s /sbin/nologin -r -g redis redis info "2. 解压redis安装包..." tar -xf /work/packages/$redis_version.tar.gz -C /usr/local/ mv /usr/local/$redis_version $redis_install_path cd $redis_install_path info "3. 解决依赖" yum -y install gcc gcc-c++ &>/dev/null info "4. 安装redis..." make &>/dev/null && make install PREFIX=$redis_install_path &>/dev/null info "5. 设置systemd管理" cp $redis_install_path/src/redis-server /usr/local/bin/ cp $redis_install_path/src/redis-cli /usr/local/bin/ \cp -f $config/redis/redis.service /etc/systemd/system/redis.service info "6. 配置启动redis..." \cp -f $config/redis/redis.conf $redis_install_path/ systemctl enable --now redis &>/dev/null if ss -antl |grep :6379 &>/dev/null ; then info "+++++redis以启动 √+++++" fi fi}function install_php(){ SERVER=php judge_server_install if [ ! $NO_install -eq 1 ];then # 1. 解决依赖 yum -y install libxslt-devel bxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel libzip-devel pcre-devel &>/dev/null # 2. 创建www用户 useradd -M -s /sbin/nologin www # 3. 解压安装包 cd $packages && tar -xf $php_version.tar.gz # 4. 正在安装PHP... cd $packages/php-7.3.30 ./configure --prefix=$php_install_path --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-pdo-mysql --with-openssl --with-pcre-regex --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml >&/dev/null # 5. 正在编译php7 make -j 5 &>/dev/null && make install &>/dev/null # 6. 设置命令路径优化 ln -s $php_install_path/sbin/php-fpm /usr/sbin/ ln -s $php_install_path/bin/* /bin/ cp $php_install_path/etc/{php-fpm.conf.default,php-fpm.conf} cp $php_install_path/etc/php-fpm.d/{www.conf.default,www.conf} # 7. 配置php7 cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/ cp php.ini-production $php_install_path/etc/php.ini sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#' /usr/local/php/etc/php.ini sed -i 's#max_execution_time = .*#max_execution_time = 300#' /usr/local/php/etc/php.ini sed -i 's#post_max_size =.*#post_max_size = 32M#' /usr/local/php/etc/php.ini sed -i 's#max_input_time = .*#max_input_time = 300#' /usr/local/php/etc/php.ini # 8. 启动php-fpm systemctl enable --now php-fpm &>/dev/null if ss -antl |grep :9000 &>/dev/null ; then info "+++++php以启动 √+++++" fi fi}function install_jdk(){ if [ ! -f /etc/profile.d/java.sh ]; then info +++++++++++++++++++++++安装jdk1.8+++++++++++++++++++++++ info "1. 安装jdk1.8" cd $packages && tar -xf jdk-8u91-linux-x64.tar.gz mv jdk1.8.0_91/ /usr/local/java info "2. 配置环境变量" echo "export JAVA_HOME=/usr/local/java" >>/etc/profile.d/java.sh echo "export PATH=$PATH:$JAVA_HOME/bin" >>/etc/profile.d/java.sh echo "export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar" >>/etc/profile.d/java.sh echo "export JRE_HOME=$JAVA_HOME/jre" >>/etc/profile.d/java.sh warnning "你需要手动执行一条命令【source /etc/profile】" source /etc/profile if java -version |grep java &>/dev/null ; then info "+++++jdk以安装 √+++++" fi fi}function install_lnmp(){ install_nginx install_mysql install_redis install_php}function install_all(){ install_zabbix_agent install_docker install_nginx install_mysql install_redis install_php}function mian(){ INSTALL=${1:-'lnmp'} # 默认值 # 判断输入安装服务 if [ $INSTALL == '1' -o $INSTALL == 'init' ]; then source $filedir/init.sh elif [ $INSTALL == '2' -o $INSTALL == 'docker' ]; then install_docker elif [ $INSTALL == '3' -o $INSTALL == 'java' -o $INSTALL == 'jdk' ]; then install_jdk elif [ $INSTALL == '4' -o $INSTALL == 'zabbix_agent' ]; then install_zabbix_agent elif [ $INSTALL == '5' -o $INSTALL == 'nginx' ]; then install_nginx elif [ $INSTALL == '6' -o $INSTALL == 'mysql' ]; then install_mysql elif [ $INSTALL == '7' -o $INSTALL == 'redis' ]; then install_redis elif [ $INSTALL == '8' -o $INSTALL == 'php' ]; then install_php elif [ $INSTALL == '9' -o $INSTALL == 'lnmp' ]; then install_lnmp elif [ $INSTALL == '10' -o $INSTALL == 'all' ]; then install_all else source $filedir/info.sh info "请输入" info "1.init 2.docker 3.java 4.zabbix_agent 5.nginx" info "6.mysql 7.redis 8.php 9.lnmp 10.all" fi}mian $1