当前位置: > CentOS > CentOS服务器 >

Web服务基础一之Apache源码和YUM安装

时间:2014-03-04 00:11来源:51cto.com 作者:IT网

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源代码的网页服务器,可以在大多数电脑操作系统中运行,由于其跨平台(可以用于UNIX/Linux系统,甚至还可以用于Windows系统)、安全性并且可通过简单的API扩充,将Perl/Python等解释器编译到服务器中被广泛使用,是最流行的Web服务器端软件之一,但是默认只有256个并发连接,运行速度偏慢,效率较低。官方网站http://httpd.apache.org,Apache有两个版本分支:1.x和2.x,目前使用的都是2.x版本,最新版本为2.4.7,在系统中的进程名为httpd。

 


YUM安装Apache

1、查看系统是否安装Apache:

 
[root@justin ~]# rpm -qa|grep httpd
httpd-2.2.15-15.el6_2.1.i686
httpd-tools-2.2.15-15.el6_2.1.i686
[root@justin ~]#

系统默认安装了Apache,版本是2.2.15,发布号为15,el6表示在rhel6系列版本中发布,i686指32位PC架构

2、安装Apache:

 
[root@justin ~]# yum remove httpd* -y
Removed:
httpd.i686 0:2.2.15-15.el6_2.1                                                                      
httpd-tools.i686 0:2.2.15-15.el6_2.1
Dependency Removed:
gnome-user-share.i686 0:2.28.2-3.el6                                                                
Complete!
[root@justin ~]# rpm -qa|grep httpd
[root@justin ~]#

先卸载掉已经安装的

 
[root@justin src]# yum install httpd -y
Installed:
httpd.i686 0:2.2.15-29.el6.centos                                                                  
Dependency Installed:
httpd-tools.i686 0:2.2.15-29.el6.centos                                                            
Complete!
[root@justin src]# rpm -qa|grep httpd
httpd-2.2.15-29.el6.centos.i686
httpd-tools-2.2.15-29.el6.centos.i686
[root@justin src]#

3、启动Apache服务:

 

 
[root@justin ~]# chkconfig --level 35 httpd on
[root@justin ~]# /etc/init.d/httpd start
正在启动 httpd:httpd: apr_sockaddr_info_get() failed forjustin
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1forServerName
[确定]
[root@justin ~]#

4、查看Apache占用的端口:

1
2
3
[root@justin ~]# netstat -antp |grep httpd
tcp        0      0 :::80                       :::*                        LISTEN      4814/httpd
[root@justin ~]#

Apache使用的是80端口,Apache中默认设置了一个站点,此时直接在客户端IE里输入http://ServerIP就可以访问默认站点:http://10.15.72.38

Tips:如果无法访问确认防火墙和SeLinux状态


源码安装Apache

在生产环境中,大都是采用源码编译方式安装Apache,这样可灵活定制各种功能、及时获取软件的最新版本以及便于今后在不同的系统之间移植。

 

 

 
[root@justin src]# cd /usr/local/src/ && wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.7.tar.gz
[root@justin httpd-2.4.7]# tar zxvf httpd-2.4.7.tar.gz
[root@justin httpd-2.4.7]# cd httpd-2.4.7
[root@justin httpd-2.4.7]# mkdir -p /usr/local/apache
[root@justin httpd-2.4.7]# ./configure --prefix=/usr/local/apache/
checking forchosen layout... Apache
checking forworking mkdir-p... yes
checking forgrepthat handles long lines and -e... /bin/grep
checking foregrep... /bin/grep-E
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking forAPR... no
configure: error: APR not found.  Please readthe documentation.
[root@justin httpd-2.4.7]#

 

配置报错configure: error: APR not found.  Please readthe documentation.未发现APR,这个是Apache关联软件,安装和apache版本相符版本,版本过早后面也会提示类似:configure: error: APR version x.x.x or later is required的错误

 

 
[root@justin httpd-2.4.7]# wget http://archive.apache.org/dist/apr/apr-1.5.0.tar.gz -P /usr/local/src/
[root@justin httpd-2.4.7]# cd ..
[root@justin src]# tar zxvf apr-1.5.0.tar.gz
[root@justin src]# cd apr-1.5.0
[root@justin apr-1.5.0]# mkdir -p /usr/local/apr
[root@justin apr-1.5.0]# ./configure --prefix=/usr/local/apr/
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
Configuring APR library
Platform: i686-pc-linux-gnu
checking forworking mkdir-p... yes
APR Version: 1.5.0
checking forchosen layout... apr
checking forgcc... no
checking forcc... no
checking forcl.exe... no
configure: error: in`/usr/local/src/apr-1.5.0':
configure: error: no acceptable C compiler found in$PATH
See `config.log' formoredetails
[root@justin apr-1.5.0]#

又出现了错误configure: error: no acceptable C compiler found in$PATH,从字面意思大致意思是没有发现可接受的C编辑器,因此需要安装下gcc套件

 

 
[root@justin apr-1.5.0]# yum install gcc -y

再次配置、编译、安装

 

 
[root@justin apr-1.5.0]# ./configure --prefix=/usr/local/apr/
[root@justin apr-1.5.0]# make
[root@justin apr-1.5.0]# make install

apr安装好了再来安装apache

 

 
[root@justin src]# cd httpd-2.4.7
[root@justin httpd-2.4.7]# ./configure --prefix=/usr/local/apache/
checking forchosen layout... Apache
checking forworking mkdir-p... yes
checking forgrepthat handles long lines and -e... /bin/grep
checking foregrep... /bin/grep-E
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking forAPR... yes
setting CC to "gcc"
setting CPP to "gcc -E"
setting CFLAGS to " -g -O2 -pthread"
setting CPPFLAGS to " -DLINUX -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE"
setting LDFLAGS to " "
configure:
configure: Configuring Apache Portable Runtime Utility library...
configure:
checking forAPR-util... no
configure: error: APR-util not found.  Please readthe documentation.
[root@justin httpd-2.4.7]#

再次出错configure: error: APR-util not found.  Please readthe documentation.没有找到APR-util,下载APR-util

 

 

 
[root@justin httpd-2.4.7]# wget http://archive.apache.org/dist/apr/apr-util-1.5.3.tar.gz -P /usr/local/src/
[root@justin httpd-2.4.7]# cd ..
[root@justin src]# tar zxvf apr-util-1.5.3.tar.gz
[root@justin src]# cd apr-util-1.5.3
[root@justin apr-util-1.5.3]# mkdir -p /usr/local/apr-util
[root@justin apr-util-1.5.3]# ./configure --prefix=/usr/local/apr
apr/      apr-util/
[root@justin apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util/ --with-apr=/usr/local/apr/bin/apr-1-config
[root@justin apr-util-1.5.3]# make && make install

再次回到安装apache

 
[root@justin apr-util-1.5.3]# cd ../httpd-2.4.7
[root@justin httpd-2.4.7]# ./configure --prefix=/usr/local/apache/
checking forchosen layout... Apache
checking forworking mkdir-p... yes
checking forgrepthat handles long lines and -e... /bin/grep
checking foregrep... /bin/grep-E
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking forAPR... yes
setting CC to "gcc"
setting CPP to "gcc -E"
setting CFLAGS to " -g -O2 -pthread"
setting CPPFLAGS to " -DLINUX -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE"
setting LDFLAGS to " "
configure:
configure: Configuring Apache Portable Runtime Utility library...
configure:
checking forAPR-util... no
configure: error: APR-util not found.  Please readthe documentation.
[root@justin httpd-2.4.7]#

已经安装好了APR-util还是提示没安装,我们需要在配置是指定他位置

 
[root@justin httpd-2.4.7]# ./configure --prefix=/usr/local/apache/ --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/
checking forchosen layout... Apache
checking forworking mkdir-p... yes
checking forgrepthat handles long lines and -e... /bin/grep
checking foregrep... /bin/grep-E
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking forAPR... yes
setting CC to "gcc"
setting CPP to "gcc -E"
setting CFLAGS to " -g -O2 -pthread"
setting CPPFLAGS to " -DLINUX -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE"
setting LDFLAGS to " "
configure:
configure: Configuring Apache Portable Runtime Utility library...
configure:
checking forAPR-util... yes
checking forgcc... gcc
checking whether the C compiler works... yes
checking forC compiler default output filename... a.out
checking forsuffix of executables...
checking whether we are cross compiling... no
checking forsuffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking forgcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking forgcc option to accept ISO C99... -std=gnu99
checking forpcre-config... false
configure: error: pcre-config forlibpcre not found. PCRE is required and available from http://pcre.org/
[root@justin httpd-2.4.7]#

编译安装就是这么坑,你需要解决一系列的依赖关系,再次提示错误configure: error: pcre-config forlibpcre not found. PCRE is required and available from http://pcre.org/,同样我们需要先安装这个pcre

 
[root@justin src]# wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.34/pcre-8.34.zip -P /usr/local/src/
[root@justin httpd-2.4.7]# cd ..
[root@justin src]# unzip -o pcre-8.34.zip
[root@justin src]# cd pcre-8.34
[root@justin pcre-8.34]# mkdir /usr/local/pcre
[root@justin pcre-8.34]# ./configure --prefix=/usr/local/pcre/
checking fordirent.h... yes
checking windows.h usability... no
checking windows.h presence... no
checking forwindows.h... no
configure: error: You need a C++ compiler forC++ support.
[root@justin pcre-8.34]#

configure: error: You need a C++ compiler forC++ support.需要安装C++,前面安装gcc的时候可以一起安装了,免得这步又的安装

 

 

 
[root@justin pcre-8.34]# yum install gcc-c++ -y

安装好再来安装pcre

 

 
[root@justin pcre-8.34]# ./configure --prefix=/usr/local/pcre/
[root@justin pcre-8.34]# make && make install

最后再回到安装Apache

 

 
[root@justin httpd-2.4.7]# ./configure --prefix=/usr/local/apache/ --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/
config.status: creating build/pkg/pkginfo
config.status: creating build/config_vars.sh
config.status: creating include/ap_config_auto.h
config.status: executing default commands
[root@justin httpd-2.4.7]# make && make install
mkdir/usr/local/apache/man/man8
mkdir/usr/local/apache/manual
make[1]: Leaving directory `/usr/local/src/httpd-2.4.7'
[root@justin httpd-2.4.7]#

总算没有再报错,源码编译安装虽然灵活但是安装很复杂,需要解决一系列依赖关系问题,这里只是纯粹的安装Apache服务。仅能提供最基本的静态网站数据而已,想要实现动态网站的话,最好还是要PHP与MySQL的支持

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容