一、练习
编译安装apache(httpd-2.4.35.tar.gz)并启动此服务?
搭建编译环境
[root@wybaron_host1015 ~]# yum groupinstall -y "开发工具"
官网下载需要的软件包(工作环境建议不要使用最新版,而是使用稳定版本)
# apache软件包
http://httpd.apache.org/download.cgi#apache24
# pcre软件包
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
# APR、APR-util、APR-iconv软件包
http://apr.apache.org/download.cgi
[root@wybaron_host1015 ~]# wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.35.tar.gz
...
[root@wybaron_host1015 ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre2-10.21.tar.gz
...
[root@wybaron_host1015 ~]# wget http://mirrors.shu.edu.cn/apache//apr/apr-1.6.5.tar.gz
...
[root@wybaron_host1015 ~]# wget http://mirrors.shu.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
...
[root@wybaron_host1015 ~]# wget http://mirrors.shu.edu.cn/apache//apr/apr-iconv-1.2.2.tar.gz
...
[root@wybaron_host1015 ~]# ll httpd* apr* pcre*
-rw-r--r-- 1 root root 1073556 9月 14 12:07 apr-1.6.5.tar.gz
-rw-r--r-- 1 root root 1248250 10月 23 2017 apr-iconv-1.2.2.tar.gz
-rw-r--r-- 1 root root 554301 10月 23 2017 apr-util-1.6.1.tar.gz
-rw-r--r-- 1 root root 9167147 9月 21 20:37 httpd-2.4.35.tar.gz
-rw-r--r-- 1 root root 1943713 10月 14 16:29 pcre2-10.21.tar.gz
[root@wybaron_host1015 ~]# tar zxf apr-1.6.5.tar.gz -C /usr/local/src/
[root@wybaron_host1015 ~]# tar zxf apr-iconv-1.2.2.tar.gz -C /usr/local/src/
[root@wybaron_host1015 ~]# tar zxf apr-util-1.6.1.tar.gz -C /usr/local/src/
[root@wybaron_host1015 ~]# tar zxf httpd-2.4.35.tar.gz -C /usr/local/src/
编译安装apr库
[root@wybaron_host1015 ~]# cd /usr/local/src/apr-1.6.5/
[root@wybaron_host1015 apr-1.6.5]# ./configure --prefix=/usr/local/apr
[root@wybaron_host1015 apr-1.6.5]# make && make install
[root@wybaron_host1015 ~]# cd /usr/local/src/apr-iconv-1.2.2/
[root@wybaron_host1015 apr-iconv-1.2.2]# ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
[root@wybaron_host1015 apr-iconv-1.2.2]# make && make install
[root@wybaron_host1015 ~]# cd /usr/local/src/apr-util-1.6.1/
[root@wybaron_host1015 apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-apr-iconv=/usr/local/apr-iconv/bin/apriconv
[root@wybaron_host1015 apr-util-1.6.1]# make && make install
## 笔者这里make的时候出现了一个错误,通过以下方式处理了问题之后再次进行make && make install 操作
[root@wybaron_host1015 ~]# yum install expat-devel
编译安装pcre库
[root@wybaron_host1015 ~]# tar zxf pcre2-10.21.tar.gz -C /usr/local/src/
[root@wybaron_host1015 ~]# cd /usr/local/src/pcre2-10.21/
[root@wybaron_host1015 pcre2-10.21]# ./configure --prefix=/usr/local/pcre
pcre2-10.21 configuration summary:
Install prefix .................. : /usr/local/pcre
C preprocessor .................. : gcc -E
C compiler ...................... : gcc
Linker .......................... : /usr/bin/ld -m elf_x86_64
C preprocessor flags ............ :
C compiler flags ................ : -g -O2 -fvisibility=hidden
Linker flags .................... :
Extra libraries ................. :
Build 8-bit pcre2 library ....... : yes
Build 16-bit pcre2 library ...... : no
Build 32-bit pcre2 library ...... : no
Include debugging code .......... : no
Enable JIT compiling support .... : no
Enable Unicode support .......... : yes
Newline char/sequence ........... : lf
\R matches only ANYCRLF ......... : no
\C is disabled .................. : no
EBCDIC coding ................... : no
EBCDIC code for NL .............. : n/a
Rebuild char tables ............. : no
Use stack recursion ............. : yes
Internal link size .............. : 2
Nested parentheses limit ........ : 250
Match limit ..................... : 10000000
Match limit recursion ........... : MATCH_LIMIT
Build shared libs ............... : yes
Build static libs ............... : yes
Use JIT in pcre2grep ............ : no
Buffer size for pcre2grep ....... : 20480
Link pcre2grep with libz ........ : no
Link pcre2grep with libbz2 ...... : no
Link pcre2test with libedit ..... : no
Link pcre2test with libreadline . : no
Valgrind support ................ : no
Code coverage ................... : no
[root@wybaron_host1015 pcre2-10.21]# make && make install
编译安装apache(期间遇到了一些错误,比如出现的pcre报错,出现openssl版本太老,编译选项问题等,这些问题完全可以根据编译未通过给出的提示来解决)
[root@wybaron_host1015 ~]# yum -y install pcre-devel
[root@wybaron_host1015 ~]# yum install openssl openssl-devel -y
[root@wybaron_host1015 ~]# cd /usr/local/src/httpd-2.4.35/
[root@wybaron_host1015 httpd-2.4.35]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event
[root@wybaron_host1015 httpd-2.4.35]# make && make install
配置apache
[root@wybaron_host1015 ~]# echo "PidFile \"/var/run/httpd.pid\"" >> /etc/httpd24/httpd.conf
提供服务脚本
## 具体可以参考文件“/usr/local/apache/bin/apachectl”
[root@wybaron_host1015 ~]# cat /etc/rc.d/init.d/httpd
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
# 给予脚本执行权限
[root@wybaron_host1015 ~]# chmod +x /etc/rc.d/init.d/httpd
# 加入服务列表
[root@wybaron_host1015 ~]# chkconfig --add httpd
##################################################################################
###
### 也可以使用添加环境变量的方式进行
### ~]# cp /usr/local/apache/bin/apachectl /etc/init.d/httpd
### ~]# echo 'export PATH=$PATH:/usr/local/apache/bin' > \/etc/profile.d/httpd.sh
### ~]# chmod +x /etc/profile.d/httpd.sh
### ~]# source /etc/profile.d/httpd.sh
###
##################################################################################
[root@wybaron_host1015 ~]# ss -nlp
...
tcp LISTEN 0 128 :::80 :::* users:(("httpd",pid=80672,fd=4),("httpd",pid=80671,fd=4),("httpd",pid=80670,fd=4),("httpd",pid=80668,fd=4))
...
浏览器测试(http://wybaron.com/,这里域名做了重定向,所以直接使用的是域名),出现以下界面,则说明编译安装并启动成功了!
注:
/etc/httpd24/httpd.conf # 配置文件位置
ServerRoot "/usr/local/apache" # Apache的安装目录
DocumentRoot "/usr/local/apache/htdocs" # 网站根目录
二、补充
编译项的意义
~]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event
--prefix=PREFIX 指定安装的位置为“PREFIX”,如果不指定,默认为“/usr/local/apache2”
--sysconfdir=DIR 配置文件位置为“DIR”,如果不指定,默认为“PREFIX/etc”
--enable-so 允许运行时加载DSO模块
--enable-ssl 编译ssl模块,用于使用https
--enable-cgi 支持cgi机制(能够让静态web服务器能够解析动态请求的一个协议)
--enable-rewrite 支持url重写
--with-zlib 支持zlib压缩(网络通用压缩库)
--with-pcre 支持正则表达式
--with-apr=/usr/local/apr 指明依赖的apr所在目录
--with-apr-util=/usr/local/apr-util 指明依赖的apr-util所在的目录
--enable-modules=most 启用大多数常用的模块
--enable-mpms-shared=all 启用MPM所有支持的模式
--with-mpm=event 指明httpd的默认工作方式为event
# 使用一下命令可以其它查看详细的选项帮助信息
[root@wybaron_host1015 httpd-2.4.35]# ./configure --help
其它
APR 库为apache提供跨平台的支持,完整的 APR 实际上包含了三个依赖包:apr、apr-util 以及 apr-iconv
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl兼容的正则表达式库。这些在执行正规表达式模式匹配时用与Perl 5同样的语法和语义是很有用的。Boost太庞大了,使用boost regex后,程序的编译速度明显变慢。测试了一下,同样一个程序,使用boost::regex编译时需要3秒,而使用pcre不到1秒。因此改用pcre来解决C语言中使用正则表达式的问题
参考链接:http://www.jinbuguo.com/apache/menu22/install.html