CentOS 6.4 编译安装 Apache 2.4

  部署环境:

CentOS6.4 x86_64位 采用最小化安装,系统经过了基本优化  1G内存,1核cpu

selinux为关闭状态,iptables开放80端口

apache版本:httpd-2.4.29

apr版本:apr-1.6.3

apr-util版本:apr-util-1.6.3

源码包存放位置:~/

源码包编译安装位置:/opt/软件名称

 

1)准备工作

yum groupinstall "Development tools" "Server Platform Development" -y  #安装这两个开发环境的软件包组
yum install pcre* -y #安装pcre兼容的正则表达式
yum install expat-devel

  如果centos中自带有httpd服务,需要先卸载

chkconfig httpd off
chkconfig --del httpd
rm -f /ect/init.d/httpd 

2)下载源代码

cd ~
wget -c http://www.eu.apache.org/dist/httpd/httpd-2.4.29.tar.gz

  apache的源码包,我们可以去apache的官网或者镜像站点进行下载,并且为了安全,我们下载时最好将apache的公钥和数字签名文件同时下载下来,然后对源码包的一致性进行验证,当验证无误后再投入使用。

wget http://www.eu.apache.org/dist/httpd/httpd-2.4.29.tar.gz.asc
wget http://www.apache.org/dist/httpd/KEYS
gpg --import KEYS
gpg --verify httpd-2.4.29.tar.gz.asc

  执行完成后,会输出类似于以下的信息  

   

  接下来解压apache源代码,并下载apr和apr-util源代码,并解药到srclib中

tar -zvxf httpd-2.4.29.tar.gz
cd httpd-2.4.29
wget -c http://www-us.apache.org/dist//apr/apr-1.6.3.tar.gz
wget -c http://www-us.apache.org/dist//apr/apr-util-1.6.1.tar.gz
tar -zxf apr-1.6.3.tar.gz
mv apr-1.6.3 ./srclib/apr
tar -zxf apr-util-1.6.1.tar.gz
mv apr-util-1.6.1 ./srclib/apr-util
rm -f *.tar.gz

3)开是编译安装

./configure --prefix=/opt/apache-2.4.6 \       #配置文件目录
--with-included-apr \
--with-pcre \                            #支持perl的正则表达式
--enable-so \                            #启用动态模块加载
--enable-ssl \                           #基于ssl加密传输
--enable-cgi \                           #开启CGI脚本
--enable-rewrite \                       #允许URL重写
--with-zlib \                            #支持压缩
--enable-deflate \                       #支持网页压缩
--enable-expires \                       #支持网页缓存
--enable-headers \                       #提供允许对HTTP请求头的控制
--enable-modules=most \                  #支持大多数模块
--enable-mpms-shared=all \               #mpm模块的动态切换
--with-mpm=event \                       #默认加载使用的mpm
--with-mpm=worker                        #让apache以worker方式运行
                                        #以上是生产环境中常用的一些编译参数

   命令贴在下面,方便执行

./configure --prefix=/opt/apache-2.4.29 --with-included-apr --with-pcre --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-mpms-shared=all --with-mpm=event --with-mpm=worker --enable-mine-magic

4)配置apache

  首先进入apache安装根目录

cd /opt/apache-2.4.29

  apache安装完成之后,我们要先为编译安装好的目录添加软链接。此操作在生产环境中为重要调优参数,添加这条软链接的目的有两点:

    1、方便人们使用。

    2、便于以后升级版本。

ln -s /opt/apache-2.4.29 /usr/local/apache

  接下来配置apache服务,首先打开配置文件

cd /usr/local/apache
vim conf/httpd.conf

  找到ServerName 这一行,将前面的#去掉,名修改其值为 0.0.0.0:80或者为自己主机的ip

  然后保存并退出,检查一下语法

/usr/local/apache/bin/apachectl -t 

  启动apache

/usr/local/apache/bin/apachectl start

5)将apache添加到系统服务

  首先需要修改 apachectl

cd /usr/local/apache
vim bin/apachectl

  在第二行添加一些内容,修改结果如下

  代码贴在下面,方便粘贴

#Comments to support chkconfig on RedHat Linux
#chkconfig: 2345 90 90
#description:http server

  之后再执行一下命令即可将apache添加到系统服务

cp bin/apachectl /etc/init.d/httpd
chkconfig --add httpd   #将apache添加到系统服务
chkconfig httpd on     #设置开机自启动

  这样就可以用service启动和停止服务了

service httpd start   #启动apache服务
service httpd stop       #停止apache服务
service httpd restart    #重启apache服务
service httpd status   #查看apache服务状态

 

额外提醒:开启apache服务失败

1)未开启80端口

  可以直接关闭防火墙

service iptables stop
chkconfig iptables off

  或者可以开启80端口

vim /etc/sysconfig/iptables

  在里面添加一行  -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT ,如下图所示

  保存并退出,再重启防火墙即可

service iptables restart

 

2)80端口被占用

  1)修改httpd.conf

    将里面Listen 80 这一行里面的80改成其他的端口,比如81,保存并退出以后重启apache即可

  2)杀死占用80端口的进程

netstat -lnp |grep :80

  可以查看到,监听80端口的pid为2786,使用kill指令杀死该进程

kill -9 2786

  之后重新启动apache即可

posted on 2017-12-31 01:29  c_night  阅读(2154)  评论(1编辑  收藏  举报