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.回車