1、下载

wget https://archive.apache.org/dist/httpd/httpd-2.4.54.tar.gz

2.解压下载压缩包

tar -zxvf httpd-2.4.54.tar.gz

cd httpd-2.4.54 

3、安装httpd所需要的依赖

yum groupinstall "Development Tools" -y

4.配置httpd

./configure --prefix=/usr/local/httpd \

--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi \
--with-zlib \

--with-pcre \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork 

5、编译安装httpd

make -j 4 && make install

6、创建Apache用户和组

sudo groupadd -r apache
sudo useradd -r -g apache -s /sbin/nologin apache

7、修改/usr/local/httpd/conf/httpd.conf 文件:

将 User daemon 改为 User apache:

将 Group daemon 改为 Group apache:

.将 DocumentRoot "/usr/local/httpd/htdocs" 改为 DocumentRoot "/var/www":

将 <Directory "/usr/local/httpd/htdocs"> 改为 <Directory "/var/www">:

8、创建系统服务管理脚本

vim /etc/init.d/httpd:

#!/bin/bash
. /etc/rc.d/init.d/functions
 
apachectl=/usr/local/httpd/bin/apachectl
httpd=/usr/local/httpd/bin/httpd
prog=httpd
start() {
    echo -n $"Starting $prog: "
    daemon $httpd $OPTIONS
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/httpd
    return $RETVAL
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $httpd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpd
    return $RETVAL
}
 
# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status $httpd
    ;;
  restart)
    stop
    start
    ;;
  condrestart)
    if [ -f /var/lock/subsys/httpd ]; then
        stop
        start
    fi
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|condrestart|status}"
    exit 1
esac
 
exit $?

9、赋予权限

chmod +x /etc/init.d/httpd 

10、将 httpd 服务添加到系统服务管理中,并设置开机启动

sudo chkconfig --add httpd
sudo chkconfig httpd on 

11、启动服务并查看状态

service httpd start 
service httpd status

更多推荐