LAMP环境一键部署脚本:
#!/bin/bash#用于安装LAMP环境VHOST_COM=lzxlinux.com #虚拟主机域名[ ! -d /software/ ] && mkdir /software#功能选择菜单menu(){ echo -e "\033[31;32m LAMP编译安装脚本 \033[0m" echo -e "\033[31;32m================================================================================\033[0m" echo -e "\033[34m 此脚本能直接在rhel7.5、Centos 7.5上成功执行 \033[0m" echo -e "\033[33m 安装包版本: httpd:2.4.39 mysql5.7.24 php7.1.29 \033[0m" echo " 相关依赖包:pcre-devel、openssl-devel、freetype-devel、libmcrypt-devel、bzip2 " echo " perl-devel、libpng-devel、libxml2-devel bzip2-devel libcurl-devel " echo -e "\033[47;34m------------安装需联网下载软件包,若下载地址失效,需自行更新下载地址------------\033[0m" echo -e "\033[31;32m================================================================================\033[0m" echo -e "\033[34m请注意:\033[0m" echo -e "\033[33m httpd安装目录:/usr/local/httpd,日志目录:/usr/local/httpd/logs \033[0m" echo -e "\033[33m mysql安装目录:/usr/local/mysql,数据存放目录:/data/mysql \033[0m" echo -e "\033[33m php安装目录:/usr/local/php7 \033[0m" echo -e "\033[31;32m================================================================================\033[0m" echo -e "\033[34m请选择:\033[0m" echo -e "\033[36m0、安装依赖包 1、安装httpd 2、源码编译安装mysql 3、安装php \033[0m" echo -e "\033[36m4、整合httpd和php 5、启动httpd、mysql服务 \033[0m" echo -e "\033[36m6、一键安装并部署lamp 7、退出脚本 \033[0m" echo -e "\033[31;32m================================================================================\033[0m" echo read -p "请输入数字:0-5[单独安装](单独安装需要先安装依赖包),6[一键安装],7[退出脚本]: " num}#(0)安装依赖包install_package(){ cd /software yum install -y vim-enhanced lrzsz net-tools gcc rsync epel-release wget unzip bzip2 curl bash-completion yum install -y pcre-devel perl-Data-Dumper bzip2-devel openssl-devel gnutls-devel gcc gcc-c++ cmake libaio-devel libxml2-devel freetype-devel yum install -y autoconf libcurl-devel libevent libevent-devel expat-devel numactl libpng-devel libjpeg-devel libmcrypt-devel if [ $? -eq 0 ] then sleep 3 echo -e "\033[36m==========依赖包安装成功==========\033[0m" else echo -e "\033[31m**********安装依赖包失败,请检查**********\033[0m" fi}#(1)编译安装httpdinstall_httpd(){ cd /software #1、安装依赖包apr和apr-util APR_VERS=apr-1.7.0 UTIL_VERS=apr-util-1.6.1 if [ ! -f $APR_VERS.tar.gz ] then echo -e "\033[34m==========正在下载apr依赖包==========\033[0m" curl -O http://mirrors.cnnic.cn/apache/apr/$APR_VERS.tar.gz fi if [ ! -f $UTIL_VERS.tar.gz ] then echo -e "\033[34m==========正在下载apr-util依赖包==========\033[0m" curl -O http://mirrors.cnnic.cn/apache/apr/$UTIL_VERS.tar.gz fi [ ! -d $APR_VERS ] && tar zxf $APR_VERS.tar.gz cd $APR_VERS ./configure --prefix=/usr/local/apr && make && make install if [ $? -eq 0 ] then sleep 3 echo -e "\033[36m==========apr依赖包编译安装成功==========\033[0m" else echo -e "\033[31m**********apr依赖包编译安装失败,请检查**********\033[0m" exit 1 fi cd /software [ ! -d $UTIL_VERS ] && tar zxf $UTIL_VERS.tar.gz cd $UTIL_VERS ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install if [ $? -eq 0 ] then sleep 3 echo -e "\033[36m==========apr-util依赖包编译安装成功==========\033[0m" else echo -e "\033[31m**********apr-util依赖包编译安装失败,请检查**********\033[0m" exit 1 fi #2、定义httpd安装的版本 HTTPD_VERS=httpd-2.4.39 #httpd版本 #3、下载httpd cd /software if [ ! -f $HTTPD_VERS.tar.gz ] then echo -e "\033[34m==========正在下载httpd源码包==========\033[0m" curl -O https://mirrors.cnnic.cn/apache/httpd/$HTTPD_VERS.tar.gz fi #4、解压下载包 [ ! -d $HTTPD_VERS ] && tar zxf $HTTPD_VERS.tar.gz #5、进入httpd解压后的目录 cd $HTTPD_VERS #6、编译安装 ./configure --prefix=/usr/local/httpd \ --with-apr=/usr/local/apr \ --with-apr-util=/usr/local/apr-util \ --enable-so \ --enable-mods-shared=most && make && make install if [ $? -eq 0 ] then sleep 3 echo -e "\033[36m==========httpd编译安装成功==========\033[0m" else echo -e "\033[31m**********httpd编译安装失败,请检查**********\033[0m" exit 1 fi ln -s /usr/local/httpd/bin/apachectl /usr/bin/httpd}#(2)源码编译安装mysqlinstall_mysql(){ cd /software VERSION=5.7.24 #mysql版本 if [ ! -f mysql-$VERSION.tar.gz ] then #下载mysql echo -e "\033[34m==========下载mysql源码包==========\033[0m" curl -o mysql-$VERSION.tar.gz https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-$VERSION-linux-glibc2.12-x86_64.tar.gz fi # 1、创建mysql用户 id mysql &> /dev/null [ $? -ne 0 ] && useradd -r -s /sbin/nologin mysql #2、解压mysql echo -e "\033[34m==========解压mysql...==========\033[0m" if [ -f mysql-$VERSION.tar.gz ] then tar xvf mysql-$VERSION.tar.gz if [ ! -d /usr/local/mysql ] then mv mysql-$VERSION-*/ /usr/local/mysql else rm -rf /usr/local/mysql mv mysql-$VERSION-*/ /usr/local/mysql fi fi #3、创建数据库数据存放目录、安装目录 [ ! -d /data/mysql/ ] && mkdir -pv /data/mysql/ [ ! -f /usr/local/mysql/log/error.log ] && mkdir -pv /usr/local/mysql/log/ && touch /usr/local/mysql/log/error.log chown -R mysql:mysql /data/mysql chown -R mysql:mysql /usr/local/mysql #4、初始化 cd /usr/local/mysql if [ $? -eq 0 ] then echo -e "\033[34m==========数据库开始初始化==========\033[0m" ./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql if [ $? -eq 0 ] then echo -e "\033[36m==========数据库初始化成功==========\033[0m" else echo -e "\033[31m**********编译安装错误!初始化失败**********\033[0m" exit 1 fi fi #5、配置数据库配置文件 cat > /etc/my.cnf << EOF [client] port = 3306 socket = /usr/local/mysql/tmp/mysql.sock [mysql] default-character-set=utf8 [mysqld] default-storage-engine=INNODB character_set_server=utf8 explicit_defaults_for_timestamp basedir=/usr/local/mysql datadir=/data/mysql socket=/usr/local/mysql/tmp/mysql.sock log_error = /usr/local/mysql/log/error.log sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION EOF [ ! -f /etc/init.d/mysql ] && \cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql sed -i "46s/basedir=/basedir=\/usr\/local\/mysql/" /etc/init.d/mysql sed -i "47s/datadir=/datadir=\/data\/mysql/" /etc/init.d/mysql chmod +x /etc/init.d/mysql #6、配置环境变量 n=`egrep "/usr/local/mysql/bin" /etc/profile |wc -l` if [ $n -eq 0 ] then echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile source /etc/profile else source /etc/profile fi #7、创建日志目录、/var/run/mysqld/ [ ! -d /var/log/mysql/ ] && mkdir /var/log/mysql/ && chown -R mysql:mysql /var/log/mysql/ [ ! -d /var/run/mysqld/ ] && mkdir /var/run/mysqld/ && chown -R mysql:mysql /var/run/mysqld/ [ ! -d /usr/local/mysql/tmp/ ] && mkdir /usr/local/mysql/tmp/ && chown -R mysql:mysql /usr/local/mysql/ #8、配置成mysqld服务并启动 cat > /usr/lib/systemd/system/mysqld.service << EOF [Unit] Description=MySQL Server After=network.target After=syslog.target [Install] WantedBy=multi-user.target [Service] User=mysql Group=mysql ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf #连接数限制 LimitNOFILE=65535 LimitNPROC=65535 #Restart配置可以在进程被kill掉之后,让systemctl产生新的进程,避免服务挂掉 #Restart=always PrivateTmp=false EOF systemctl daemon-reload && systemctl enable mysqld && systemctl start mysqld if [ $? -eq 0 ] then echo -e "\033[36m==========mysql安装成功并启动==========\033[0m" fi mysql -uroot -D mysql -e "UPDATE user SET authentication_string=PASSWORD("123456789") WHERE user='root';" mysql -uroot -e "FLUSH PRIVILEGES;" mysql -uroot -p123456789 -e "grant all privileges on *.* to root@'%' identified by '123456789';" if [ $? -eq 0 ] then echo -e "\033[36m==========数据库root密码修改为123456789成功==========\033[0m" else echo -e "\033[31m**********数据库root密码修改为123456789失败,为空**********\033[0m" fi }#(3)编译安装phpinstall_php(){ cd /software PHPVERS=7.1.29 #php版本 DLOAD_PHP=https://www.php.net/distributions/php-$PHPVERS.tar.bz2 #PHP下载地址 #1、下载php [ ! -f php-$PHPVERS.tar.bz2 ] && echo -e "\033[36m正在下载php源码包...\033[0m" && curl -O $DLOAD_PHP #2、解压php [ ! -d php-$PHPVERS/ ] && tar jxvf php-$PHPVERS.tar.bz2 echo -e "\033[36m编译安装php需要很长时间,请慢慢等待...\033[0m" sleep 3 cd php-$PHPVERS/ #3、编译安装 ./configure --prefix=/usr/local/php7 \ --with-apxs2=/usr/local/httpd/bin/apxs \ --with-config-file-path=/usr/local/php7/etc \ --with-mysql=/usr/local/mysql \ --with-pdo-mysql \ --with-mysqli \ --with-libxml-dir \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-iconv-dir \ --with-zlib-dir \ --with-bz2 \ --with-openssl \ --with-mcrypt \ --enable-soap \ --enable-gd-native-ttf \ --enable-mbstring \ --enable-sockets \ --enable-exif && make -j 2 && make -j 2 install if [ $? -eq 0 ] then echo -e "\033[36m==========php编译安装成功==========\033[0m" else echo -e "\033[31m**********php编译安装失败,请检查**********\033[0m" exit 1 fi #4、配置php服务文件 [ ! -d /usr/local/php7/etc/ ] && mkdir /usr/local/php7/etc/ \cp php.ini-production /usr/local/php7/etc/php.ini echo -e "\033[36m==========PHP安装步骤完成==========\033[0m"}#(4)整合httpd和phpconfig_lamp(){ cd /usr/local/httpd #1、修改httpd配置文件 sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:80/' conf/httpd.conf sed -i '205s/Require all denied/Require all granted/' conf/httpd.conf sed -i 's/DirectoryIndex index.html/DirectoryIndex index.html index.php/' conf/httpd.conf if [ `grep "AddType application/x-httpd-php .php" conf/httpd.conf |wc -l` -eq 0 ] then sed -i '/AddType application\/x-gzip .gz .tgz/a\ AddType application\/x-httpd-php .php' conf/httpd.conf fi #sed -i 's/#Include conf\/extra\/httpd-vhosts.conf/Include conf\/extra\/httpd-vhosts.conf/' conf/httpd.conf #2、配置虚拟主机 cat > conf/extra/httpd-vhosts.conf << EOF ## ServerAdmin webmaster@$VHOST_COM# DocumentRoot "/usr/local/httpd/docs/$VHOST_COM" # ServerName $VHOST_COM# ServerAlias www.$VHOST_COM# ErrorLog "logs/$VHOST_COM-error_log" # CustomLog "logs/$VHOST_COM-access_log" common #EOF [ ! -d /usr/local/httpd/docs/$VHOST_COM ] && mkdir -pv /usr/local/httpd/docs/$VHOST_COM #3、关闭SELinux、防火墙 setenforce 0 && sed -i 's/enforcing/disabled/g' /etc/selinux/config systemctl stop firewalld && systemctl disable firewalld #4、创建测试页,备份httpd默认的页面 cat > htdocs/1.php << EOF EOF if [ `httpd -M |grep "php7_module (shared)" |wc -l` -eq 1 ] then echo -e "\033[36m==========整合httpd和php成功==========\033[0m" else echo -e "\033[31m**********整合httpd和php失败,请检查**********\033[0m" fi}#(5)启动httpd、mysql服务start_service(){ /usr/bin/httpd start #启动httpd if [ $? -eq 0 ] then echo -e "\033[36m==========httpd服务启动成功==========\033[0m" else echo -e "\033[31m**********httpd服务启动失败**********\033[0m" fi systemctl start mysqld if [ $? -eq 0 ] then sleep 3 echo -e "\033[36m==========mysql服务启动成功==========\033[0m" echo -e "\033[33m查看端口启用情况:\033[0m" ss -tnl echo -e "\033[33m端口:80、3306已启动!\033[0m" echo -e "\033[36m==========编译安装lamp已完成==========\033[0m" echo -e "\033[36m--------打开浏览器输入你的ip/1.php,看看测试页--------\033[0m" else echo -e "\033[31m**********mysql服务启动失败**********\033[0m" fi}#脚本运行入口run_install(){ while true;do menu case $num in "0") #0、安装依赖包 echo -e "\033[34m==========安装依赖包==========\033[0m" install_package ;; "1") #1、编译安装httpd echo -e "\033[34m==========编译安装httpd==========\033[0m" install_httpd ;; "2") #2、编译安装mysql echo -e "\033[34m==========编译安装mysql==========\033[0m" install_mysql ;; "3") #3、编译安装php echo -e "\033[34m==========编译安装php==========\033[0m" install_php ;; "4") #4、整合httpd和php echo -e "\033[34m==========整合httpd和php==========\033[0m" config_lamp ;; "5") #5、启动httpd、mysql服务 echo -e "\033[34m==========启动httpd、mysql服务==========\033[0m" start_service ;; "6") #6、一键编译安装lamp echo -e "\033[34m==========一键编译安装并配置lamp==========\033[0m" install_package install_httpd install_mysql install_php config_lamp start_service exit 0 ;; "7") #7、退出脚本 exit 0 ;; *) ;; esac done}#调用脚本运行入口run_install
手动单独安装可以参考这里:https://blog.csdn.net/miss1181248983/article/details/80831289