/****源文件安装过程演示****/
(1)运行httpd-2.4.12中的configure文件
[root@localhost httpd-2.4.12]# ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite
checking for chosen layout... Apache checking for working mkdir -p... yes checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu configure: configure: Configuring Apache Portable Runtime library... configure: checking for APR... no configure: error: APR not found. Please read the documentation. # 报错了,提示需要APR
(2)安装依赖包APR
(A)可以从Apache网站上下载一个APR包,通过filezilla上传到Linux机器上,并安装这个程序包:
[root@localhost bin_src]# ll
total 5624 -rw-r--r--. 1 root root 694427 Aug 17 18:58 apr-util-1.5.4.tar.bz2 drwxr-xr-x. 11 user2 games 4096 Aug 17 18:54 httpd-2.4.12 -rw-r--r--. 1 root root 5054838 Aug 17 17:51 httpd-2.4.12.tar.bz2
[root@localhost bin_src]# tar xf apr-1.5.1.tar.bz2
[root@localhost bin_src]# cd apr-1.5.1
[root@localhost apr-1.5.1]# ls
apr-config.in build configure.in libapr.dep memory random threadproc apr.dep buildconf docs libapr.dsp misc README time apr.dsp build.conf dso libapr.mak mmap READMENaNake tools apr.dsw build-outputs.mk emacs-mode libapr.rc network_io shmem user apr.mak CHANGES encoding LICENSE NOTICE strings apr.pc.in CMakeLists.txt file_io locks NWGNUmakefile support apr.spec config.layout helpers Makefile.in passwd tables atomic configure include Makefile.win poll test
(B)首先来查看一下APR包的安装文档:
[root@localhost apr-1.5.1]# cat README
Apache Portable Runtime Library (APR) ------------------------------------- The Apache Portable Runtime Library provides a predictable and consistent interface to underlying platform-specific ... Configuring and Building APR on Unix ==================================== Simply; ./configure --prefix=/desired/path/of/apr make make test make install Configure has additional options, ./configure --help will offer you ... Generating Test Coverage information with gcc ============================================= If you want to generate test coverage data, use the following steps: ./buildconf CFLAGS="-fprofile-arcs -ftest-coverage" ./configure make cd test make ./testall cd .. make gcov
(C)现在来运行configure脚本
[root@localhost apr-1.5.1]# ./configure --prefix=/usr/local/apr
checking langinfo.h usability... yes ... config.status: executing default commands
[root@localhost apr-1.5.1]# ls
apr-1-config atomic config.nice helpers Makefile passwd test apr-config.in build config.status include Makefile.in poll threadproc ... apr.pc CMakeLists.txt emacs-mode libtool network_io strings apr.pc.in config.layout encoding LICENSE NOTICE support apr.spec config.log file_io locks NWGNUmakefile tables # 运行 ./configure脚本后发现该目录中多了一些文件
(D)执行make命令
这一步不需要任何参数,因为默认参数是gcc,这也是比较常用的编译器。
make文件会使用到Makefile,这是个文本文件,可以查看一下该文件:
[root@localhost apr-1.5.1]# file Makefile
Makefile: ASCII English text
[root@localhost apr-1.5.1]# less Makefile
srcdir=. top_srcdir=/yum/bin_src/apr-1.5.1 top_blddir=/yum/bin_src/apr-1.5.1 ... prefix=/usr/local/apr exec_prefix=${prefix} bindir=${exec_prefix}/bin libdir=${exec_prefix}/lib includedir=${prefix}/include/apr-${APR_MAJOR_VERSION} installbuilddir=${prefix}/build-1 ... # Macros for target determination
[root@localhost apr-1.5.1]# make
... sed 's,^\(location=\).*$,\1installed,' < apr-1-config > apr-config.out sed -e 's,^\(apr_build.*=\).*$,\1/usr/local/apr/build-1,' -e 's,^\(top_build.*=\).*$,\1/usr/local/apr/build-1,' < build/apr_rules.mk > build/apr_rules.out make[1]: Leaving directory `/yum/bin_src/apr-1.5.1' # 这个过程是自动完成的, leaving directory表示编译完成
(E)执行make install命令
[root@localhost apr-1.5.1]# make install
/yum/bin_src/apr-1.5.1/build/mkdir.sh tools /bin/sh /yum/bin_src/apr-1.5.1/libtool --silent --mode=compile gcc -g -O2 -pthread ... mkdir /usr/local/apr # 程序包的根目录 mkdir /usr/local/apr/lib # 程序包的库目录 mkdir /usr/local/apr/bin # 程序包生成的二进制码 mkdir /usr/local/apr/build-1 mkdir /usr/local/apr/lib/pkgconfig mkdir /usr/local/apr/include mkdir /usr/local/apr/include/apr-1 ... for f in make_exports.awk make_var_export.awk; do \ /usr/bin/install -c -m 644 /yum/bin_src/apr-1.5.1/build/${f} /usr/local/apr/build-1; \ done /usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr/build-1/apr_rules.mk /usr/bin/install -c -m 755 apr-config.out /usr/local/apr/bin/apr-1-config
(3)安装apache服务器
(A)解决依赖关系
解决了APR包的依赖关系,现在可以编译安装apache服务器了,还是遵循上述步骤:
[root@localhost bin_src]# cd httpd-2.4.12
[root@localhost httpd-2.4.12]# ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite
checking for APR-util... no configure: error: APR-util not found. Please read the documentation. ... checking for pcre-config... false configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
这次提示没有APR-util,可以重复上述安装APR包的方法安装APR-util以及另一个依赖包PCRE。
当APR, APR-util和PCRE依赖包都解决后,安装apache时可能需要加上 --with-apr=/usr/local/apr,--with-apr-util=/usr/local/apr-util/ 和 --with-pcre=/usr/local/pcre来指定依赖包的路径
(B)执行./configure脚本
[root@localhost httpd-2.4.12]# ./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --with-pcre=/usr/local/bin/pcre-config
# pcre使用的默认安装,故pcre-congfig路径如上 config.status: creating build/rules.mk ... config.status: executing default commands
(C)执行make和make install命令
[root@localhost httpd-2.4.12]# make
dules/database -I/yum/bin_src/httpd-2.4.12/modules/filters -I/yum/bin_src/httpd-2.4.12/modules/ldap ... make[1]: Leaving directory `/yum/bin_src/httpd-2.4.12' [root@localhost httpd-2.4.12]# make install ... mkdir /usr/local/apache/manual make[1]: Leaving directory `/yum/bin_src/httpd-2.4.12'
[root@localhost httpd-2.4.12]# cd /usr/local/apache
[root@localhost apache]# ls
bin build cgi-bin conf error htdocs icons include logs man manual modules # 可以看到成功创建了很多目录
(D)启动apache服务器
[root@localhost apache]# cd bin
[root@localhost bin]# ls
ab apxs dbmmanage envvars-std htcacheclean htdigest httpd logresolve apachectl checkgid envvars fcgistarter htdbm htpasswd httxt2dbm rotatelogs
[root@localhost apache]# ls include
# .h结尾的多为头文件 apache_noprobes.h ap_release.h http_vhost.h mod_so.h util_ebcdic.h ap_compat.h ap_slotmem.h mod_auth.h mod_ssl.h util_fcgi.h ap_config_auto.h ap_socache.h mod_cache.h mod_status.h util_filter.h ap_config.h cache_common.h mod_cgi.h mod_unixd.h util_ldap.h
[root@localhost apache]# ls conf
extra httpd.conf magic mime.types original # httpd.conf是apache的主配置文件,httpd是apache服务器的主程序,而执行脚本apachectl,加上start选项,可以启动该服务器
[root@localhost apache]# apachectl start
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName # 这里提示服务器确实start了,但不是启动的刚才安装的服务器,因为系统默认安装了rpm包的apache服务器。
[root@localhost apache]# hash
hits command 1 /usr/sbin/apachectl # 这是系统默认的apachectl命令 7 /bin/ls
[root@localhost apache]# bin/apachectl start
# 通过全路径启动刚才安装的服务器,仍然提示命令名称模糊 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80 (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down # 80端口被系统原来的apache服务器占用了 AH00015: Unable to open logs
(E)修改服务器的监听端口
[root@localhost apache]# vim conf/httpd.conf
#Listen 12.34.56.78:80 #Listen 80 Listen 8889 # # Dynamic Shared Object (DSO) Support
(F)再次启动服务器
[root@localhost apache]# bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message # 这次没有报服务器端口占用的错误了
[root@localhost apache]# netstat -tnlp
# netstat命令可以查看系统上目前正在被监听的端口
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1338/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1609/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1425/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1689/master tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 2379/sshd tcp 0 0 0.0.0.0:50409 0.0.0.0:* LISTEN 1385/rpc.statd tcp 0 0 :::50764 :::* LISTEN 1385/rpc.statd tcp 0 0 :::111 :::* LISTEN 1338/rpcbind tcp 0 0 :::80 :::* LISTEN 2438/httpd # 系统自带的服务器端口 tcp 0 0 :::22 :::* LISTEN 1609/sshd tcp 0 0 ::1:631 :::* LISTEN 1425/cupsd tcp 0 0 :::8889 :::* LISTEN 2512/httpd # 新安装的服务器监听端口 tcp 0 0 ::1:25 :::* LISTEN 1689/master tcp 0 0 ::1:6010 :::* LISTEN 2379/sshd
可以看到服务器正常工作了。
(4)额外步骤
(A) 配置服务器启动命令的环境变量
在实际生产环境中,在程序包的编译安装完成后,可能需要配置环境变量(PATH /usr/local/apache/bin, PATH /usr/local/apache/sbin),否则只能全路径来使用该程序,而使用全路径来执行服务器启动命令很不方便,因此可以创建一个配置文件,为服务器的启动命令配置环境变量:
[root@localhost apache]# vim /etc/profile.d/apache.sh
export PATH=/usr/local/apache/bin:$PATH
该文件不会马上生效,需要重新登录,或者使用source命令来使其生效:
[root@localhost apache]# . /etc/profile.d/apache.sh
[root@localhost apache]# echo $PATH
/usr/local/apache/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
再使用hash命令查看一下当前启动的服务器
[root@localhost apache]# help hash
hash: hash [-lr] [-p pathname] [-dt] [name ...] Remember or display program locations. Determine and remember the full pathname of each command NAME. If no arguments are given, information about remembered commands is displayed. Options: -d forget the remembered location of each NAME -l display in a format that may be reused as input -p pathname use PATHNAME is the full pathname of NAME -r forget all remembered locations -t print the remembered location of each NAME, preceding each location with the corresponding NAME if multiple NAMEs are given Arguments: NAME Each NAME is searched for in $PATH and added to the list of remembered commands. Exit Status: Returns success unless NAME is not found or an invalid option is given.
[root@localhost apache]# hash -d
hash: hash table empty
[root@localhost apache]# hash
hash: hash table empty
[root@localhost apache]# apachectl stop
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
[root@localhost apache]# apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message # 再次启动服务器
[root@localhost apache]# hash
hits command 2 /usr/local/apache/bin/apachectl # 由于手动安装的apache服务器路径被放在了系统默认的服务器前面,因此默认启动该服务器
(B)添加库目录的搜索路径
除了修改默认启动的命令,有可能需要导出软件包的库目录搜索路径,通过编辑/etc/ld.so.conf,或者新建/etc/ld.so.conf.d/*.conf, 加入库目录的路径。可以使用ldd命令来查看某一个程序依赖的库:
[root@localhost apr-1.5.1]# ldd /bin/ls
# 查看ls命令依赖的库 linux-vdso.so.1 => (0x00007fff149ff000) libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003c63000000) librt.so.1 => /lib64/librt.so.1 (0x0000003c62800000) libcap.so.2 => /lib64/libcap.so.2 (0x0000003c65000000) libacl.so.1 => /lib64/libacl.so.1 (0x0000003c6e800000) libc.so.6 => /lib64/libc.so.6 (0x0000003c61800000) libdl.so.2 => /lib64/libdl.so.2 (0x0000003c61400000) /lib64/ld-linux-x86-64.so.2 (0x0000003c61000000) libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c61c00000) libattr.so.1 => /lib64/libattr.so.1 (0x0000003c6ce00000)
使用ldconfig -v 命令可以重新搜索当前系统上所有库文件搜索路径下的库文件,并生产缓存/etc/
[root@localhost apache]# ldconfig -v | grep /usr/local/apache
# 不包含手动的库目录
[root@localhost apache]# vim /etc/ld.so.conf.d/apache.conf
/usr/local/apache/lib # 编辑配置文件
[root@localhost apache]# ldconfig -v | grep /usr/local/apache
/usr/local/apache/lib:
(C)添加man命令的搜索路径
[root@localhost apache]# man -M /usr/local/apache/man httpd
# man命令指定httpd的全路径,比较麻烦,每次开新的终端都需要手动键入路径 ... -w Keep the console window open on error so that the error message can be read. Apache HTTP Server 2012-02-10 HTTPD(8)
如果想使用man命令直接加上httpd,则需要编辑/etc/man/config文件,加入 MANPATH=/usr/local/apache/man;
[root@localhost apache]# ls man
man1 man8
[root@localhost apache]# vim /etc/man.config
# Every automatically generated MANPATH includes these fields # MANPATH /usr/local/apache/man MANPATH /usr/man MANPATH /usr/share/man
[root@localhost apache]# man httpd
# 检验是否成功加入 ... -w Keep the console window open on error so that the error message can be read. Apache HTTP Server 2012-02-10 HTTPD(8)
(D)为头文件创建链接
/usr/include下有很多头文件,为了对刚才安装的服务器的头文件加以区分,建议为其创建一个链接
[root@localhost apache]# ls /usr/include
aio.h eti.h jmorecfg.h ncurses QtCore string.h aliases.h etip.h jpeglib.h ncurses_dll.h QtDBus strings.h ... error.h jconfig.h mtd Qt3Support stdio.h zconf.h et jerror.h nc_tparm.h QtAssistant stdlib.h zlib.h
[root@localhost apache]# ln -sv /usr/local/apache/include /usr/include/httpd
`/usr/include/httpd' -> `/usr/local/apache/include'
[root@localhost apache]# ls /usr/include
aio.h etip.h jpeglib.h ncurses.h QtGui sysexits.h ... elf.h httpd mntent.h pwd.h stdint.h Xm # 新创建的头文件目录链接 ... eti.h jmorecfg.h ncurses_dll.h QtDesigner syscall.h
[root@localhost apache]# ls include
apache_noprobes.h ap_release.h http_vhost.h mod_so.h util_ebcdic.h ... ap_regkey.h http_request.h mod_session.h util_cookies.h
[root@localhost apache]# cd /usr/include/httpd
[root@localhost httpd]# ls
apache_noprobes.h ap_release.h http_vhost.h mod_so.h util_ebcdic.h ... ap_regkey.h http_request.h mod_session.h util_cookies.h