CentOS 7安装上LAMP(源码安装简单脚本)
这个实我当时为了省多次安装的时间,写了一个没有什么技术含量的脚本,也没考虑到什么复用性,也是用了很多次,没有发生什么错误,如果有错误也很好改,下面是我使用的软件包,可以使用使用然后省点敲代码的时间,也没什么要改的,准备好下面的那几个同版本的安装包就行了(如果其他版本的可以自己进行改源码的验证),最后说一下,如果需要哪些包或者大佬的指教给我留言,还有就是不喜勿喷,只是使用的一个工具。
这个脚本是完整版的,但是我使用的时候有一次遇到了安装PHP那几个依赖的时候,yum源没有的问题,因此我将安装PHP的部分专门抽了出来(用网络YUM源没什么问题)。
#!/bin/bash
#注明:
logFile="./log.txt"
warFile="./error.txt"
waitSec=10s
echo "#这是日志文档" > $logFile
echo "#这是非致命警告文档" > $warFile
#echo "检测网络配置..."
ping -c 1 127.0.0.1 &>> $logFile
if [ "$?" != 0 ];then
echo "网络连接错误,请检查!可尝试 network 脚本修复网络!"
exit 1
fi
echo "网络配置检测完毕!"
#echo "检测基础yum源配置..."
yum -y install net-tools &>> $logFile
if [ "$?" != 0 ];then
echo "YUM源配置有问题,请检查!可尝试 yum 脚本尝试修复"
exit 1
fi
echo "YUM源配置检测完毕!"
service firewalld stop &>> $logFile
systemctl disable firewalld &>> $logFile
if [ "$?" != 0 ];then
echo "警告:防火墙操作失败!"
echo "防火墙关闭以及禁用失败" >> $warFile
fi
setenforce 0
if [ `getenforce` != "Permissive" ];then
echo "SELinux相关有错误!"
echo "SELinux关闭以及禁用失败" >> $warFile
fi
echo "配置防火墙和SELinux完毕,为了安全起见,此处并未永久关闭SELinux,若想永久关闭请运行 selinux 脚本"
#安装几个必须的工具
first_pake_must=(wget vim unzip)
for i in ${first_pake_must[@]}
do
yum install -y $i &>> $logFile
if [ "$?" == 0 ];then
echo "*** $i 安装成功"
else
echo "安装 $i 发生错误,请解决"
exit 1
fi
done
#安装apache的环境包
echo "基础环境完成,下面开始配置Apache环境"
yum -y install gcc-g77 &>> $logFile
if [ "$?" != 0 ];then
echo "gcc-g77安装未成功"
echo "---gcc-g77安装未成功" >> $warFile
fi
echo "***gcc-g77安装成功"
yum -y install gcc++ &>> $logFile
if [ "$?" != 0 ];then
echo "gcc++安装未成功"
echo "---gcc++安装未成功" >> $warFile
fi
echo "***gcc++安装成功"
apache_pake_must=(gcc gcc-c++ libtool autoconf)
for n in ${apache_pake_must[@]}
do
yum install -y $n &>> $logFile
if [ "$?" == 0 ];then
echo "*** $n 安装成功"
else
echo "安装 $n 发生错误,请解决"
exit 1
fi
done
#判断是否存在所需要的那几个必须的文件
if [ -f "./apr-1.5.0.tar.gz" ];then
echo " -1"
else
echo "apr-1.5.0.tar.gz 文件不存在,请 http://oss.aliyuncs.com/aliyunecs/onekey/apache/apr-1.5.0.tar.gz 或者拷贝文件到当前文件夹 "
exit 1
fi
if [ -f "./apr-util-1.5.3.tar.gz" ];then
echo " -2"
else
echo "apr-util-1.5.3.tar.gz 文件不存在,请 http://oss.aliyuncs.com/aliyunecs/onekey/apache/apr-util-1.5.3.tar.gz 或者拷贝文件到当前文件夹 "
exit 1
fi
if [ -f "./httpd-2.4.23.tar.gz" ];then
echo " -3"
else
echo "httpd-2.4.23.tar.gz 文件不存在,请 wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/apache/httpd-2.4.23.tar.gz 或者拷贝文件到当前文件夹 "
exit 1
fi
if [ -f "./mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz" ];then
echo " -4"
else
echo "mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz 文件不存在,请 wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/mysql/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz 或者拷贝文件到当前文件夹 "
exit 1
fi
if [ -f "./php-5.4.45.tar.gz" ];then
echo " -5"
else
echo "php-5.4.45.tar.gz 文件不存在,请 wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/php/php-5.4.45.tar.gz 或者拷贝文件到当前文件夹 "
exit 1
fi
if [ -f "./pcre-8.38.tar.gz" ];then
echo " -6"
else
echo "pcre-8.38.tar.gz 文件不存在,请 ... 或者拷贝文件到当前文件夹 "
exit 1
fi
if [ -f "./phpMyAdmin-4.1.8-all-languages.zip" ];then
echo " -7"
else
echo "phpMyAdmin-4.1.8-all-languages.zip 文件不存在,请 wget http://oss.aliyuncs.com/aliyunecs/onekey/phpMyAdmin-4.1.8-all-languages.zip 或者拷贝文件到当前文件夹 "
exit 1
fi
echo "所需安装包都在,开始下一步操作!"
#安装apr
tar -zxvf apr-1.5.0.tar.gz &>> $logFile
if [ "$?" == 0 ];then
nowSrc=`pwd`
sed -i "s/RM='\$RM'/RM='\$RM -f'/g" $nowSrc/apr-1.5.0/configure
if [ "$?" == 0 ];then
echo "修改配置文件成功!"
cd $nowSrc/apr-1.5.0/ && ./configure --prefix=/usr/local/apr && cd -
echo "脚本将延迟进行,延迟时间由自己设定,这段时间是让你检查编译是否发生不可预期的错误(就只看结尾就行),若有,请终止脚本"
sleep $waitSec
cd $nowSrc/apr-1.5.0 && make && make install && cd -
else
echo "修改失败"
exit 1
fi
else
echo "解压失败,请检查原因"
exit 1
fi
echo "安装apr完毕"
#安装apr-util
tar -zxvf apr-util-1.5.3.tar.gz &>> $logFile
if [ "$?" == 0 ];then
nowSrc=`pwd`
cd $nowSrc/apr-util-1.5.3/ && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && cd -
echo "脚本将延迟进行,延迟时间由自己设定,这段时间是让你检查编译是否发生不可预期的错误(就只看结尾就行),若有,请终止脚本
"
sleep $waitSec
cd $nowSrc/apr-util-1.5.3 && make && make install && cd -
else
echo "解压失败,请检查原因"
exit 1
fi
echo "安装apr-util完毕"
#安装pcre
tar -zxvf pcre-8.38.tar.gz &>> $logFile
if [ "$?" == 0 ];then
nowSrc=`pwd`
cd $nowSrc/pcre-8.38/ && ./configure --prefix=/usr/local/pcre && cd -
echo "脚本将延迟进行,延迟时间由自己设定,这段时间是让你检查编译是否发生不可预期的错误(就只看结尾就行),若有,请终止脚本
"
sleep $waitSec
cd $nowSrc/pcre-8.38 && make && make install && cd -
else
echo "解压失败,请检查原因"
exit 1
fi
echo "安装pcre完毕"
#安装apache
tar -zxvf httpd-2.4.23.tar.gz &>> $logFile
if [ "$?" == 0 ];then
nowSrc=`pwd`
cd $nowSrc/httpd-2.4.23/ && ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-cgi --enable-rewrite --with-zlib --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-mods-shared=most --enable-mpms-shared=all --with-mpm=event && cd -
echo "脚本将延迟进行,延迟时间由自己设定,这段时间是让你检查编译是否发生不可预期的错误(就只看结尾就行),若有,请终止脚本
"
sleep $waitSec
cd $nowSrc/httpd-2.4.23 && make && make install && cd -
else
echo "解压失败,请检查原因"
exit 1
fi
echo "安装apache完毕"
#开始修改HTTP的配置文件
if [ -d "/etc/httpd" ];then
if [ -f "/etc/httpd/httpd.conf" ];then
sed -i '/AllowOverride none/{n;s/denied/granted/}' /etc/httpd/httpd.conf
if [ "$?" == 0 ];then
echo "修改配置文件/etc/httpd/http.conf让接受请求成功!"
else
echo "修改/etc/httpd/http.conf让其接受请求失败"
exit 1
fi
sed -i '/#ServerName/a ServerName localhost:80' /etc/httpd/httpd.conf
if [ "$?" == 0 ];then
echo "修改配置文件/etc/httpd/http.conf的主机默认端口成功!"
else
echo "修改/etc/httpd/http.conf的主机默认端口失败"
exit 1
fi
sed -i "s/ DirectoryIndex index.html/ DirectoryIndex index.php index.html/g" /etc/httpd/httpd.conf
if [ "$?" == 0 ];then
echo "修改配置文件/etc/httpd/http.conf的添加一个index.php成功!"
else
echo "修改/etc/httpd/http.conf的添加一个index.php失败"
exit 1
fi
sed -i "s/User daemon/User www/g" /etc/httpd/httpd.conf
sed -i "s/Group daemon/Group www/g" /etc/httpd/httpd.conf
if [ "$?" == 0 ];then
echo "修改配置文件/etc/httpd/http.conf的用户和用户组成功!"
else
echo "修改/etc/httpd/http.conf的用户和用户组失败"
exit 1
fi
echo 'PidFile "/var/run/httpd.pid"' >> /etc/httpd/httpd.conf
else
echo "配置文件不存在,请检查"
exit 1
fi
else
echo "/etc/httpd 文件夹怎么不存在,可能是apache安装问题,请检查! "
exit 1
fi
groupadd www &>> $logFile
useradd -g www -s /sbin/nologin www &>> $logFile
sh /usr/local/apache/bin/apachectl start
http_status_code=`netstat -nultp | grep 80`
if [ -n "$http_status_code" ];then
echo "Apache启动成功"
else
echo "Apache启动失败"
exit 1
fi
curl localhost | grep 'It works!' &>> $logFile
if [ "$?" != 0 ];then
echo "Apache运行不正常"
echo "运行的Apache可能是不正常的,请检查" >> $warFile
else
echo "*********************"
echo "It works!"
echo "*********************"
echo "Apache运行正常,开始下一步骤"
fi
#echo "如果看到的结果不包含 It works! 请谨慎向下的过程"
sleep $waitSec
#开始设置开机自启
cp /usr/local/apache/bin/apachectl /etc/rc.d/init.d/httpd
sed -i '/\#\!\/bin\/sh/a# chkconfig: 35 70 30 \n# description: Apache' /etc/rc.d/init.d/httpd
chkconfig --add httpd
chkconfig --level 345 httpd on
httpd_chk_status_code=`chkconfig --list | grep httpd`
if [ -n "$httpd_chk_status_code" ];then
echo "Apache设置开机自启成功"
else
echo "Apache设置开机自启失败"
echo "Apache设置开机自启失败" &>> $warFile
fi
mysql_is_install=`rpm -qa | grep mysql`
if [ "$?" != 0 ];then
echo "mysql未安装,所以不用卸载" >> $warFile
else
rpm -e $mysql_is_install
if [ "$?" != 0 ];then
rpm -e --nodeps $mysql_is_install
fi
fi
mariadb_is_install=`rpm -qa | grep mariadb`
if [ "$?" != 0 ];then
echo "mariadb未安装,所以不用卸载" >> $warFile
else
rpm -e $mariadb_is_install
if [ "$?" != 0 ];then
rpm -e --nodeps $mariadb_is_install
fi
fi
yum -y install libaio-* &>> $logFile
#安装MySQL数据库
tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ &>> $logFile
if [ "$?" == 0 ];then
mv /usr/local/mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql
groupadd mysql &>> $logFile
useradd -g mysql -s /sbin/nologin mysql &>> $logFile
/usr/local/mysql/bin/mysqld --initialize-insecure --datadir=/usr/local/mysql/data/ --user=mysql
chown -R mysql:mysql /usr/local/mysql
else
echo "解压失败,请检查原因"
exit 1
fi
echo "安装mysql数据库完毕"
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
if [ "$?" == 0 ];then
service mysqld start
mysql_status_code=`netstat -nultp | grep 3306`
if [ -n "$mysql_status_code" ];then
echo "Mysql启动成功"
else
echo "Mysql启动失败"
exit 1
fi
else
echo "开机自启第一步,向/etc/init.d/ 中做服务文件失败"
exit 1
fi
echo '[Unit]
Description=Mysql
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/mysql/data/localhost.localdomain.pid
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
[Install]
WantedBy=multi-user.target
' > /usr/lib/systemd/system/mysqld.service
systemctl enable mysqld
systemctl start mysqld
systemctl stop mysqld
systemctl restart mysqld
echo "数据库操作完毕"
echo "该 安装PHP 。。。 了但是这里需要的是网络YUM源,如果你的是网络YUM源就输入 yes 继续,否则结束,运行专门的PHP脚本安装!"
while true;do
echo -n "请输入:"
read msg
case $msg in
Y|y|YES|yes)
break;;
N|n|NO|no)
echo "拜拜~"
exit 0
esac
done
echo "开始安装PHP~"
#安装php
php_pake_must=(epel-release libxml2-devel openssl-devel libcurl-devel libpng.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 libjpeg-turbo-devel libmcrypt-devel libmcrypt php-mcrypt mysql-devel bzip2 bzip2-devel libxslt-devel)
php_pake_maybe=(libjpeg.x86_64 freetype.x86_64 libjpeg-devel.x86_64 )
for i in ${php_pake_must[@]}
do
yum install -y $i &>> $logFile
if [ "$?" == 0 ];then
echo "*** $i 安装成功"
else
echo "安装 $i 发生错误,请解决"
exit 1
fi
done
for j in ${php_pake_maybe[@]}
do
yum install -y $j &>> $logFile
if [ "$?" == 0 ];then
echo "*** $j 安装成功"
else
echo "安装 $j 未成功"
fi
done
tar -zxvf php-5.4.45.tar.gz &>> $logFile
if [ "$?" == 0 ];then
nowSrc=`pwd`
cd $nowSrc/php-5.4.45/ && ./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql --with-mysqli --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-jpeg-dir --with-png-dir --with-xmlrpc --with-xsl --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --enable-exif --disable-ipv6 && cd -
echo "脚本将延迟进行,延迟时间由自己设定,这段时间是让你检查编译是否发生不可预期的错误(就只看结>尾就行),若有没有安装的软件,请自行安装下,请终止脚本
"
sleep $waitSec
cd $nowSrc/php-5.4.45 && make && make install && cd -
else
echo "解压失败,请检查原因"
exit 1
fi
echo "请检查这里是否有error!!!"
sleep $waitSec
echo "安装php完毕"
cp php-5.4.45/php.ini-production /etc/php.ini
echo '
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
' >> /etc/httpd/httpd.conf
service httpd restart
echo '
<?php
phpinfo();
?>
' > /usr/local/apache/htdocs/phpinfo.php
#安装phpMyAdmin
mkdir -p /usr/local/apache/htdocs/phpmyadmin
unzip phpMyAdmin-4.1.8-all-languages.zip
mv phpMyAdmin-4.1.8-all-languages/* /usr/local/apache/htdocs/phpmyadmin
echo "结束!安装完成,恭喜 ^_^"
第二个抽出来的脚本:
#!/bin/bash
#注明:
logFile="./log.txt"
warFile="./error.txt"
waitSec=10s
echo "该 安装PHP 。。。 了但是这里需要的是网络YUM源,如果你的是网络YUM源就输入 yes 继续,否则结束,运行专门的PHP脚本安装!"
while true;do
echo -n "请输入:"
read msg
case $msg in
Y|y|YES|yes)
break;;
N|n|NO|no)
echo "拜拜~"
exit 0
esac
done
echo "开始安装PHP~"
#安装php
php_pake_must=(epel-release libxml2-devel openssl-devel libcurl-devel libpng.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 libjpeg-turbo-devel libmcrypt-devel libmcrypt php-mcrypt mysql-devel bzip2 bzip2-devel libxslt-devel)
php_pake_maybe=(libjpeg.x86_64 freetype.x86_64 libjpeg-devel.x86_64 )
for i in ${php_pake_must[@]}
do
yum install -y $i &>> $logFile
if [ "$?" == 0 ];then
echo "*** $i 安装成功"
else
echo "安装 $i 发生错误,请解决"
exit 1
fi
done
for j in ${php_pake_maybe[@]}
do
yum install -y $j &>> $logFile
if [ "$?" == 0 ];then
echo "*** $j 安装成功"
else
echo "安装 $j 未成功"
fi
done
tar -zxvf php-5.4.45.tar.gz &>> $logFile
if [ "$?" == 0 ];then
nowSrc=`pwd`
cd $nowSrc/php-5.4.45/ && ./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql --with-mysqli --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-jpeg-dir --with-png-dir --with-xmlrpc --with-xsl --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --enable-exif --disable-ipv6 && cd -
echo "脚本将延迟进行,延迟时间由自己设定,这段时间是让你检查编译是否发生不可预期的错误(就只看结>尾就行),若有没有安装的软件,请自行安装下,请终止脚本
"
sleep $waitSec
cd $nowSrc/php-5.4.45 && make && make install && cd -
else
echo "解压失败,请检查原因"
exit 1
fi
echo "请检查这里是否有error!!!"
sleep $waitSec
echo "安装php完毕"
cp php-5.4.45/php.ini-production /etc/php.ini
echo '
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
' >> /etc/httpd/httpd.conf
service httpd restart
echo '
<?php
phpinfo();
?>
' > /usr/local/apache/htdocs/phpinfo.php
#安装phpMyAdmin
mkdir -p /usr/local/apache/htdocs/phpmyadmin
unzip phpMyAdmin-4.1.8-all-languages.zip
mv phpMyAdmin-4.1.8-all-languages/* /usr/local/apache/htdocs/phpmyadmin
echo "结束!安装完成,恭喜 ^_^"