shell脚本安装apache服务
安装httpd步骤:
创建一个系统用户
下载依赖包
下载软件Apr、Apr-util、httpd软件包、并解压
编译apr、Apr-util、httpd
设置环境变量、头文件、man文档
设置开机自启
apache的官网:https://downloads.apache.org/
部署什么就创建什么目录,跟它相对应。
[root@localhost ~]# cd /usr/src/
[root@localhost src]# mkdir apache
[root@localhost src]# cd apache/
创建files目录存放文件或软件等
[root@localhost apache]# mkdir files
[root@localhost apache]# cd files/
[root@localhost files]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@localhost files]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@localhost files]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
[root@localhost files]# ls
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.54.tar.gz
[root@localhost files]#
编写安装apache脚本
安装脚本内容
[root@localhost apache]# cat httpd.sh
#!/bin/bash
# 在使用脚本安装之前需手动安装一遍
if [ $UID -ne 0 ];then
echo "Please execute this script uses root user."
exit 250
fi
apache_install_dir=/usr/local/apache
# 安装依赖包
dnf -yq install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make
id apache &> /dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin apache
fi
# 解压软件包
if [ ! -d /usr/src/apr-1.7.0 ] && [ ! -d /usr/src/apr-util-1.6.1 ];then
tar xf files/apr-1.7.0.tar.gz -C /usr/src/
tar xf files/apr-util-1.6.1.tar.gz -C /usr/src/
tar xf files/httpd-2.4.54.tar.gz -C /usr/src/
fi
cd /usr/src/apr-1.7.0
if [ ! -d /usr/local/apr ];then
sed -i '/$RM "$cfgfile"/d' configure
./configure --prefix=/usr/local/apr && \
make && make install
fi
cd ../apr-util-1.6.1
if [ ! -d /usr/local/apr-util ];then
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
make && make install
fi
cd ../httpd-2.4.54
if [ ! -d $apache_install_dir ];then
./configure --prefix=$apache_install_dir \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork && \
make && make install
fi
echo "export PATH=$apache_install_dir/bin:\$PATH" > /etc/profile.d/apache.sh
ln -s $apache_install_dir/include /usr/include/apache &> /dev/null
# 这里的 \t 表示一个tab
grep "$apache_install_dir/man" /etc/man_db.conf
if [ $? -ne 0 ];then
sed -i "/^MANDATORY_MANPATH.*local\/share\/man/a MANDATORY_MANPATH\t\t\t$apache_install_dir/man" /etc/man_db.conf
fi
if [ ! -f /usr/lib/systemd/system/httpd.service ];then
cat > /usr/lib/systemd/system/httpd.service << EOF # 配置service文件
[Unit]
Description=httpd server daemon
After=network.target
[Service]
Type=forking
ExecStart=$apache_install_dir/bin/apachectl start
ExecStop=$apache_install_dir/bin/apachectl stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
EOF
fi
systemctl daemon-reload # 刷新程序
systemctl enable --now httpd # 开机自启
# 给执行权限
[root@localhost apache]# chmod +x httpd.sh
# 执行
[root@localhost apache]# bash -x httpd.sh
....执行过程省略
配置虚拟主机文件
[root@localhost apache]# cat vhosts.sh
#!/bin/bash
apache_install_dir=$(grep '^apache_install_dir' httpd.sh | awk -F'=' '{print $2}')
grep 'Include conf/extra/vhosts.conf' $apache_install_dir/conf/httpd.conf
if [ $? -ne 0 ];then
sed -i '/vhosts.conf/a Include conf/extra/vhosts.conf' $apache_install_dir/conf/httpd.conf
fi
mkdir -p $apache_install_dir/htdocs/login.example.com
# 把网站传到相应目录下
cp -a files/qadmin/qadmin-html/* $apache_install_dir/htdocs/login.example.com
cat > $apache_install_dir/conf/extra/vhosts.conf <<EOF
<VirtualHost *:80>
DocumentRoot "$apache_install_dir/htdocs/login.example.com"
ServerName login.example.com
ErrorLog "logs/login.example.com-error_log"
CustomLog "logs/login.example.com-access_log" common
</VirtualHost>
EOF
systemctl restart httpd
# 给执行权限
[root@localhost apache]# chmod +x vhosts.sh
[root@localhost apache]# bash -x vhosts.sh
....执行过程省略
访问测试
ip
访问
域名访问