一、实验环境
Linux: CentOS release 6.7 (Final)
Apache: httpd-2.4.23.tar.gz
VMware: VMware 10.0
宿主机: Win10 x64
二、Apache介绍
Apache一款 Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中。同时Apache音译为阿帕奇,是北美印第安人的一个部落,叫阿帕奇族。 三、安装包下载及其环境要求
Apache所需依赖环境有 apr、apr-util、pcre,还有编译源码需要的gcc和gcc-c++,下图所示安装包下载地址和我使用的版本 安装包 | | | apache
| http://httpd.apache.org/download.cgi
| httpd-2.4.23.tar.gz
| apr
| http://apr.apache.org/
| apr-1.5.2.tar.gz
| apr-util
| http://apr.apache.org/
| apr-util-1.5.4.tar.gz
| pcre
| http://www.pcre.org/
| pcre-8.39.tar.gz
| 注意:gcc和gcc-c++使用rpm或者yum方式安装,pcre下载的时候一定要看清楚是pcre还是pcre2,因我下载了pcre2,安装了好几次都失败,最后发现是因为安装包下载错误。
强烈建议看一下官方文档:http://httpd.apache.org/docs/2.4/install.html
四、安装过程
1、配置CentOS-Base.repo, 安装gcc、gcc-c++配置网络yum源的方法(http://blog.iyunv.com/uid-23683795-id-3477603.html),配置完成后执行:
[iyunv@localhost~]# yum -y install gcc
[iyunv@localhost~]# yum -y install gcc-c++
2、安装依赖包和Apache HTTPServer
2.1使用FTP工具上传httpd、apr、apr-util、pcre压缩包至/usr/local/mypackages/
[iyunv@localhost~]# cd /usr/local/mypackagers/
[iyunv@localhostmypackagers]# ls
apr-1.5.2.tar.gzapr-util-1.5.4.tar.gz httpd-2.4.23.tar.gz pcre-8.39.tar.gz
2.2安装apr
[iyunv@localhost mypackagers]# tar xvf apr-1.5.2
[iyunv@localhost mypackagers]# cd apr-1.5.2
[iyunv@localhost mypackagers]#./configure --prefix=/usr/local/apr
[iyunv@localhost mypackagers]#make && install
2.3安装apr-util
[iyunv@localhost mypackagers]# tar xvf apr-util-1.5.4.tar.gz
[iyunv@localhost mypackagers]# cd apr-util-1.5.4
[iyunv@localhost mypackagers]#./configure --prefix=/usr/local/apr-util--with-apr=/usr/local/apr
[iyunv@localhost mypackagers]#make && install
2.3安装pcre
[iyunv@localhost mypackagers]# tar xvf pcre-8.39.tar.gz
[iyunv@localhost mypackagers]# cd pcre-8.39
[iyunv@localhost pcre-8.39]# ./configure --prefix=/usr/local/pcre
[iyunv@localhost pcre-8.39]# make && make install
2.4安装httpd
[iyunv@localhost mypackagers]#tar xvf httpd-2.4.23.tar.gz
[iyunv@localhost mypackagers]# cd httpd-2.4.23
[iyunv@localhost httpd-2.4.23]# ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre/
[root@localhosthttpd-2.4.23]#make && make install
五、启动和设置开机自启
1、开放80端口或关闭防火墙,详见下面防火墙配置,启动apache服务:
[iyunv@localhost ~]# /usr/local/apache/bin/apachectl start,浏览器输入:http://IP地址:80,出现It works!证明Apache已经安装成功。
2、设置开机自启动
echo"/usr/local/apache/bin/apachectl start" >> /etc/rc.d/rc.local
六、安装启动过程排错
1、configure:error: APR / APR-util not found. Pleaseread the documentation.表示apr/apr-util没有安装,先安装apr,再安装apr-util,因为apr-util依赖apr,安装apr和apr-util之前需安装gcc工具,否则会提示configure: error: no acceptable C compiler found in $PATH.
2、configure: error: pcre-config forlibpcre not found. PCRE is required and available from http://pcre.org/,是因为pcre没有安装,安装pcre之前需要先安装gcc-c++,否则会提示configure: error: You need aC++ compiler for C++ support.
3、ServerName服务器域名
[iyunv@localhost~]# /usr/local/apache/bin/apachectl start
AH00558:httpd: Could not reliably determine the server's fully qualified domain name,using::1. Set the 'ServerName' directive globally to suppress this message
[iyunv@localhost conf]# cd /usr/local/apache/conf/
[iyunv@localhost conf]# vi httpd.conf
在#ServerName www.example.com:80下增加一条 ServerName localhost:80
4、端口占用
[iyunv@localhost~]# /usr/local/apache/bin/apachectl start
httpd(pid 38477) already running
[iyunv@localhost~]# ps aux | grep httpd
[iyunv@localhost~]# kill -9 38477 //有好几个进程,以此进行kill
[iyunv@localhost~]# /usr/local/apache/bin/apachectl start
5、防火墙开放80端口或直接关闭防火墙
如Apache后台服务已启动,但是浏览器访问不了,可能由于防火墙阻挡,两种方式解决:
5.1开放80端口,推荐使用
[iyunv@localhost ~]# iptables -I INPUT -p tcp --dport 80 -jACCEPT //开放80端口
[iyunv@localhost ~]# service iptables save //保存策略
iptables: Saving firewall rules to/etc/sysconfig/iptables:[ OK ]
[iyunv@localhost ~]# service iptables status //有下面一行证明80端口已开放
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
5.2关闭防火墙,并设为防火墙开启不启动
[iyunv@localhost~]# service iptables stop //关闭防火墙
iptables:Setting chains to policy ACCEPT: filter [ OK ]
iptables:Flushing firewall rules: [ OK ]
iptables:Unloading modules: [ OK ]
[iyunv@localhost~]# chkconfig |grep iptables //2345为on表示开机自启
iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[iyunv@localhost~]# chkconfig iptables off //设置iptables开机不自启
[iyunv@localhost~]# chkconfig |grep iptables //2345为off为开机不自启
iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
|