编写脚本实例

本文详细介绍了如何在Linux CentOS系统中编译安装Apache,并提供了优化步骤,包括恢复最小化快照、配置YUM源和关闭防火墙。同时,文章还涉及了MySQL的二进制安装脚本,涵盖了单例和多例的安装,以及如何搭建完整的LAMP环境,实现www.example.com的访问。
摘要由CSDN通过智能技术生成

编译安装apache

[root@localhost ~]# cat httpd.sh 
#!/bin/bash

route=/usr/local
path=/usr/src
#安装开发环境
yum -y groups mark install "Development Tools"
yum -y install openssl-devel pcre-devel expat-devel libtool wget make

#下载源码包
cd $path
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.54.tar.gz

#解压
cd $path
tar xf apr-1.7.0.tar.gz
tar xf apr-util-1.6.1.tar.gz
tar xf httpd-2.4.54.tar.gz

#创建用户和组
id apache
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin apache
fi

#编译apr-1.7.0
cd $path/apr-1.7.0
if [ ! -d apr-1.7.0 ];then
    sed -i '/$RM "$cfgfile"/d' apr-1.7.0/configure
    ./configure --prefix=$route/apr &&\
    make && make install
fi

#编译apr-util-1.6.1
cd $path/apr-util-1.6.1
if [ ! -d apr-util-1.6.1 ];then
    ./configure --prefix=$route/apr-util --with-apr=$route/apr &&\
    make && make install
fi

#编译httpd-2.4.54
cd $path/httpd-2.4.54
if [ ! -d httpd-2.4.54 ];then
    ./configure --prefix=$route/apache \
        --enable-so \
        --enable-ssl \
        --enable-cgi \
        --enable-rewrite \
        --with-zlib \
        --with-pcre \
        --with-apr=$route/apr \
        --with-apr-util=$route/apr-util/ \
        --enable-modules=most \
        --enable-mpms-shared=all \
        --with-mpm=prefork &&\
    make && make install
fi

#设置环境变量
echo 'export PATH=$route/apache/bin:\$PATH' > /etc/profile.d/apache.sh
sed -i "/#ServerName/s/#//" $route/apache/conf/httpd.conf

#映射
ln -s $route/apache/include/ /usr/include/apache

#配置man文档
echo "MANDATORY_MANPATH  $route/apache/man" >> /etc/man_db.conf

#配置service文件
cat > /usr/lib/systemd/system/httpd.service << EOF
[Unit]
Description=httpd server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

#启动
systemctl daemon-reload
systemctl enable --now httpd
systemctl start httpd
systemctl status httpd

#防火墙关闭
systemctl disable --now firewalld
setenforce 0
sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/config

优化

(恢复最小化快照,布置好yum源,关闭防火墙)

[root@localhost apache]# ls
config.sh  files  install.sh
[root@localhost apache]# ls files/
apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.54.tar.gz
[root@localhost apache]# cat install.sh 
#!/bin/bash

if [ $UID -ne 0 ];then
    echo "Please execute this script with root user."
    exit
fi

install_dir=/usr/local/apache

#安装依赖包
echo -e "\033[32minstalling package for depend\033[0m\033[5;32m...\033[0m"
yum -y install wget make openssl-devel pcre-devel expat-devel libtool gcc gcc-c++

id apache &> /dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin apache
fi

#解压软件包
rm -rf /usr/src/*
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/

cd /usr/src/apr-1.7.0
if [ ! -d /usr/local/apr ];then
    sed -i '/$RM "$cfgfile"/d' /usr/src/apr-1.7.0/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 $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
fi

echo "export PATH=$install_dir/bin:\$PATH" > /etc/profile.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值