一、实验目的
全新系统,通过网络(wget http://ftp.wangshuai.tech/script/install.sh && cat install.sh |bash |tee log),实现在线自动编译安装httpd服务,且重启也生效
二、实验过程
(1)准备安装环境
1、关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
2、关闭SELINUX
sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
3、准备用户
groupadd -g 48 apache
useradd -u 48 -g 48 -r -s /sbin/nologin apache
4、配置YUM库
yum install wget -y -q
cd /etc/yum.repos.d/
rm -rf *
wget http://ftp.wangshuai.tech/yum_repo/base-aliyun.repo
wget http://ftp.wangshuai.tech/yum_repo/epel-aliyun.repo
5、源码包安装环境依赖的包
yum install apr-devel apr-util-devel openssl-devel pcre-devel gcc -y
安装开发工具组
yum groupinstall "Development Tools" >/dev/null
6、下载httpd源码包
cd /tmp/
rm -rf *
wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.29.tar.gz
(2)编译安装http服务
1、解压httpd源码包
cd /tmp/
tar -zxvf httpd*
2、编译安装httpd
进入源目录
cd httpd-2.4.29
configure脚本,指定安装位置、指定启用的特性
./configure --prefix=/usr/local/httpd24 --sysconfdir=/etc/httpd/ --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
遇到如下类似画面表示配置成功
按脚本生成的文件去编译安装
make && make install
(3)编译后细节配置
1、配置环境变量并使环境变量生效
echo 'export PATH=/usr/local/httpd24/bin:$PATH' > /etc/profile.d/apache2.sh
. /etc/profile.d/apache2.sh
2、导入帮助手册
编辑文件
vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/httpd24/man //添加此行
如果没出现,更新man数据库
mandb
3、导入库文件路径
编辑创建如下文件,添加新的库文件所在目录至此文件中
vim /etc/ld.so.conf.d/httpd24.conf
/usr/local/httpd24/modules
让系统重新生成缓存:
ldconfig [-v]
(4)准备修改配置文件,以及权限问题
先备份
cp /etc/httpd/httpd.conf /etc/httpd/httpd.conf.bak
修改配置文件
vim /etc/httpd/httpd.conf
User apache
Group apache
创建日志生成目录
默认在/usr/local/httpd24/logs
删除该目录,创建软连接指向自己指定的日志生成目录
cd /usr/local/httpd24/
rm -rf logs
mkdir /var/log/httpd
ln -s /var/log/httpd /usr/local/httpd24/logs
chown -R apache.apache /usr/local/httpd24/
chown -R apache.apache /etc/httpd/
choown -R apache.apache /var/log/httpd
(5)制作启动http服务脚本
vim /etc/rc.d/init.d/httpd
#!/bin/bash
# chkconfig: 2345 11 99
# Description: start/syop httpd service
cmd_path="/usr/local/httpd24/bin"
httpd_pid="/usr/local/httpd24/logs/httpd.pid"
function_start_http()
{
if [ ! -e "{cmd_path}/httpd -k start &> /dev/null
else
printf "http is running...\n"
exit
fi
}
function_stop_http()
{
if [ ! -e "{cmd_path}/httpd -k stop &> /dev/null
fi
}
function_restart_http()
{
printf "Restarting http...\n"
function_stop_http
sleep 2
function_start_http
}
case $1 in
start)
function_start_http
;;
stop)
function_stop_http
;;
restart)
function_restart_http
;;
*)
printf "Usage: service httpd {start|stop|restart}\n"
esac
修改权限并添加SysV的服务脚本
cd /etc/rc.d/init.d/
chmod 750 httpd
chkconfig --add httpd
(6)启动服务设置开机自启
service httpd start
chkconfig httpd on
三、实现脚本自动安装编译
最终脚本如下
#!/bin/bash
# ------------------------------------
# Filename: install45.sh
# Revision: 6.6
# Author: wang shuai
# Date: 2017-12-01
# Description: This is a script
# ------------------------------------
# Copyright: 2017 shuai
# License: NI DA YE
ver=`sed -nr 's/.release ([^.]+)../\1/p' /etc/centos-release`
# 开启自动挂载并开机启动,关闭防火墙
echo "正在开启自动挂载服务并开机启动,关闭防火墙,需要时间,请稍等..."
[[ "$ver" -eq 7 ]] && systemctl stop firewalld && systemctl disable firewalld || { service iptables stop && chkconfig iptables off ; }
# 关闭SELINUX
echo "正在关闭SELINUX"
sed -i.bak 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config
setenforce 0
echo "配置ftp服务器上YUM源,请保证能ping通外网"
# 配置YUM库
cd /etc/yum.repos.d/
rm -rf *
wget http://ftp.wangshuai.tech/yum_repo/base-aliyun.repo
wget http://ftp.wangshuai.tech/yum_repo/epel-aliyun.repo
# 清除yum缓存
yum clean all
# 卸载httpd的rpm安装包
echo "正在卸载旧版本rpm包,如果没有安装,无影响"
rpm -qa |grep http &>/dev/null
[[ $? -eq 0 ]] && rpm -qa |grep http|xargs yum -y remove
ls /usr/local/httpd24 -ld &>/dev/null
[[ $? -eq 0 ]] && \rm -rf /usr/local/httpd24
# 安装开发工具组
echo "安装开发工具组,请稍等..."
yum groupinstall "Development Tools" -y
#源码包安装环境依赖的包
echo "安装httpd源码包环境依赖的包,请稍等..."
yum install apr-devel apr-util-devel openssl-devel pcre-devel gcc -y
#准备用户
groupadd -g 48 apache
useradd -u 48 -g 48 -r -s /sbin/nologin apache
echo "马上下载httpd源码包并解压"
# 下载httpd源码包并解压
cd /tmp/
rm -rf *
wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.29.tar.gz
tar xf httpd*
echo "编译安装httpd,时间较长,请稍等......."
# 编译安装httpd
cd httpd*
./configure --prefix=/usr/local/httpd24 --sysconfdir=/etc/httpd/ --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork
make
make install
echo "配置httpd环境变量"
# 配置环境变量
cat /etc/profile.d/apache2.sh &>/dev/null
[[ PATH' >>/etc/profile.d/apache2.sh && source /etc/profile.d/apache2.sh && echo "配置环境变量成功且已生效" ; }
#导入帮助手册
echo 'MANDATORY_MANPATH /usr/local/httpd24/man' >>/etc/man_db.conf
#导入库文件路径让重新生成缓存
echo '/usr/local/httpd24/modules' >/etc/ld.so.conf.d/httpd24.conf
ldconfig
#准备修改配置文件,以及权限问题
cp /etc/httpd/httpd.conf /etc/httpd/httpd.conf.bak
sed -ir 's/^User deamon/User apache/' /etc/httpd/httpd.conf
sed -ir 's/^Group deamon/Group apache/' /etc/httpd/httpd.conf
#创建日志生成目录
cd /usr/local/httpd24/
rm -rf logs
ls /var/log/httpd -ld &>/dev/null
[[ $? -eq 0 ]] || mkdir /var/log/httpd
ln -s /var/log/httpd /usr/local/httpd24/logs
chown -R apache.apache /usr/local/httpd24/
chown -R apache.apache /etc/httpd/
chown -R apache.apache /var/log/httpd
#下载启动脚本
wget http://ftp.wangshuai.tech/other//httpd -O /etc/rc.d/init.d/httpd
cd /etc/rc.d/init.d/
chmod 750 httpd
chkconfig --add httpd
echo "启动httpd服务"
# 启动http服务
service httpd start
echo "设置开机启动httpd服务"
# 设置开机启动http服务
chkconfig httpd on
echo "安装完成!"