1.进入/opt目录下创建apache目录,然后创建一个脚本文件和一个存放安装的目录
[root@localhost opt]
apache
[root@localhost opt]
[root@localhost apache]
install.sh soft
[root@localhost apache]
2.下载好apache服务所需要的安装包
wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz wget https://downloads.apache.org/httpd/httpd-2.4.48.tar.gz
root@localhost apache]
[root@localhost soft]
apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.48.tar.gz
[root@localhost soft]
3.编写apache脚本
[root@localhost apache]# ls
install.sh soft
[root@localhost apache]# vim install.sh
#!/bin/bash
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
install_dir=/usr/local/httpd
yum groups mark install "Development Tools" -y
yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make && echo "=====安装开发环境成功!====="
id apache &>/dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin apache && echo "=====创建apache用户成功!====="
fi
rm -rf /usr/src/{apr*,httpd*}
tar xf soft/apr-1.7.0.tar.gz -C /usr/src
tar xf soft/apr-util-1.6.1.tar.gz -C /usr/src
tar xf soft/httpd-2.4.48.tar.gz -C /usr/src && echo "=====解压安装包成功!====="
cd /usr/src/apr-1.7.0
if [ ! -d /usr/local/apr ];then
sed -i '/$RM "$cfgfile"/d' configure && echo "=====删除$RM "$cfgfile"成功!====="
./configure --prefix=/usr/local/apr && make && make install && echo "=====apr-1.7.0编译安装成功!====="
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 && echo "=====apr-1.7.0编译安装成功!====="
fi
cd ../httpd-2.4.48
if [ ! -d $install_dir ];then
./configure --prefix=$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 && echo "=====httpd-2.4.48编译安装成功!====="
fi
sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:80/g' /usr/local/httpd/conf/httpd.conf
echo 'export PATH=/usr/local/httpd/bin:$PATH' > /etc/profile.d/httpd.sh && echo "=====环境变量创建成功!====="
cat > /usr/lib/systemd/system/httpd.service << EOF
[Unit]
Description=The Apache http server
After=network.targe
[Service]
Type=forking
ExecStart=/usr/local/httpd/bin/apachectl start
ExecStop=/usr/local/httpd/bin/apachectl stop
ExecReload=/usr/local/httpd/bin/apachectl restart
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && echo "=====重新加载成功====="
systemctl enable --now httpd && echo "=====apache开机自启成功!====="
netstat -anpt | grep 80
if [ $? -eq 0 ];then
echo "=====Apache服务启动成功====="
else
echo "=====启动失败====="
fi
4.验证效果
[root@localhost apache]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6011 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 [::1]:6010 [::]:*
LISTEN 0 128 [::1]:6011 [::]:*
[root@localhost apache]# curl http://192.168.8.129
<html><body><h1>It works!</h1></body></html>
[root@localhost apache]#
|