Shell代码
#!/bin/sh
#检查一下liunx系统
if grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
DISTRO='CentOS'
fi
version=$(/usr/bin/lsb_release -a |grep Description |awk -F : '{print $2}' |sed 's/^[ \t]*//g'|sed -r 's/.* ([0-9]+[.][0-9]+)\..*/\1/')
if [ $DISTRO != CentOS ] || [ "${version}" != 7.4 ];then
echo "========================该脚本只适合 Linux Centos7.4 ================================="; exit 1
fi
#检测安装环境
if [ $UID -ne 0 ];then
echo "========================请以超级管理员身份运行该脚本================================="; exit 1
fi
if [ ! `rpm -qa yum` ];then
echo "==============================请先安装YUM============================================"; exit 1
fi
echo "========================先清理一下,原来的软件(mysql,apache)================================";
if [ `rpm -qa mysql` ];then
echo "============================正在删除原有mysql=========================================";
yum -y remove mysql
rm -f /etc/my.cnf
fi
if [ `rpm -qa httpd` ];then
echo "==========================正在删除原有apache========================================";
yum -y remove httpd
fi
echo "=======================================安装apache中============================================";
yum -y install httpd #安装apache
if [ ! `rpm -qa httpd` ];then
echo "=============================apache安装失败请重新执行脚本========================================="; exit 1
fi
yum -y install httpd-manual mod_ssl mod_perl mod_auth_mysql #安装apache扩展
systemctl start httpd.service
if [ ! $? ];then
echo "===========================apache启动失败请重新执行脚本================================"; exit 1
fi
systemctl enable httpd.service #设置开机自启动apache
if [ ! $? ];then
echo "=======================apache设置开机自启动失败请重新执行脚本=========================="; exit 1
fi
echo "=======================================安装apache成功!============================================";
echo "=======================================安装PHP5.6中=============================================";
yum -y install epel-release #配置yum源,以下是CentOS 7.0的源。
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum -y install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof #安装PHP5.6
yum -y install --enablerepo=remi --enablerepo=remi-php56 php-fpm #安装PHP-fpm
yum -y install php-gd --enablerepo=remi-php56 #安装php-gd扩展
systemctl restart httpd.service
if [ ! $? ];then
echo "===========================apache启动失败请重新执行脚本================================"; exit 1
fi
echo "=======================================安装PHP5.6成功!============================================";
echo "====================================安装mysql-5.7.16中=======================================";
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm #使用wget下载官方yum源的mysql-rpm包
rpm -ivh mysql57-community-release-el7-11.noarch.rpm #安装mysql-rpm包
yum install -y mysql-server #使用yum来安装mysql-server
systemctl start mysqld #安装完成后,启动mysqld服务
if [ ! $? ];then
echo "===========================mysql启动失败请重新执行脚本================================"; exit 1
fi
systemctl enable mysqld #设置mysqld服务开机自启动
if [ ! $? ];then
echo "=======================mysql设置开机自启动失败请重新执行脚本=========================="; exit 1
fi
rm -f /mysql57-community-release-el7-11.noarch.rpm;
echo "=======================================安装mysql-5.7成功!============================================";
echo "====================================配置apache中=======================================";
if [ ! -e "/etc/httpd/conf/httpd.conf" ];then
echo "========================找不到配置文件,请重置系统后执行脚本================================="; exit 1
fi
sed -i 's/Options Indexes FollowSymLinks/Options Includes ExecCGI FollowSymLinks/g' /etc/httpd/conf/httpd.conf #允许服务器执行CGI及SSI,禁止列出目录
sed -i 's/#AddHandler cgi-script .cgi/AddHandler cgi-script .cgi .pl/g' /etc/httpd/conf/httpd.conf #允许扩展名为.pl的CGI脚本运行
sed -i 's/AllowOverride None/AllowOverride All/g' /etc/httpd/conf/httpd.conf #允许.htaccess
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.html index.htm Default.html Default.htm index.php Default.php index.html.var/g' /etc/httpd/conf/httpd.conf #设置默认首页文件,增加index.php
echo "====================================配置apache成功!=======================================";
echo "====================================配置PHP5.6中=======================================";
if [ ! -e "/etc/php.ini" ];then
echo "========================找不到配置文件,请重置系统后执行脚本================================="; exit 1
fi
sed -i 's/;date.timezone =/date.timezone = PRC/g' /etc/php.ini
sed -i 's/disable_functions =/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,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname/g' /etc/php.ini #列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
sed -i 's/expose_php = On/expose_php = Off/g' /etc/php.ini # 禁止显示php版本的信息
#sed -i 's/;open_basedir =/open_basedir =usr\/www\/html:\/tmp\//g' /etc/php.ini #设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站
sed -i 's/;always_populate_raw_post_data/always_populate_raw_post_data/g' /etc/php.ini
sed -i 's/session.auto_start = 0/session.auto_start = 1/g' /etc/php.ini
sed -i 's/display_errors = Off/display_errors = On/g' /etc/php.ini
sed -i 's/error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT/error_reporting = E_ALL | E_STRICT/g' /etc/php.ini
systemctl restart httpd.service
if [ ! $? ];then
echo "===========================apache启动失败请重新执行脚本================================"; exit 1
fi
echo "====================================配置PHP5.6成功!=======================================";
echo "====================================配置MYSQL5.7中=======================================";
if [ ! -e "/etc/my.cnf" ];then
echo "========================找不到配置文件,请重置系统后执行脚本================================="; exit 1
fi
echo 'sql-mode="NO_AUTO_Create_USER,NO_ENGINE_SUBSTITUTION"' >> /etc/my.cnf #防止数据出现MySQL 1364 错误:#1364 – Field “details” doesn’t have a default value。
/bin/systemctl start mysqld.service #设置mysqld重启
if [ ! $? ];then
echo "=======================mysql重启失败请重新执行脚本=========================="; exit 1
fi
echo "====================================配置MYSQL5.7成功!=======================================";
echo "****************************************************************************************";
echo "**************************************恭喜您安装成功!**********************************";
echo "****************************************************************************************";
使用方法
- 将本脚本放到根目录下
- chmod +x ./test.sh(该文件名) #使脚本具有执行权限
- ./test.sh(该文件名) #执行脚本
注意事项
该脚本会先清理一下,原有的mysql,apache
该脚本是Linux Centos7.4 下安装 LAMP环境及配置(php5.6,mysql5.7)的脚本,在其他Linux系统下不会成功
如果执行失败,最好重置系统再执行该脚本
由于MySQL从5.7开始不允许首次安装后,使用空密码进行登录,系统会随机生成一个密码以供管理员首次登录使用,这个密码记录在/var/log/mysqld.log文件中,使用下面的命令可以查看此密码:
cat /var/log/mysqld.log|grep 'A temporary password'
出现下面代码
2017-11-12T13:35:37.013617Z 1 [Note] A temporary password is generated for root@localhost: /htroSQ0srf3
最后一行冒号后面的部分就是初始密码,我的密码就是 /htroSQ0srf3
安装成功后项目目录为:/var/www/html
Shell脚本报错
Shell脚本报错:-bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory
1.首先用vi命令打开文件
[root@localhost /]# vi test.sh
2.在vi命令模式中使用 :set ff=unix
:set ff=unix
3.在vi命令模式中使用 :wq
:wq
4.回车