必要程序
准备工作
[root@httpd-server ~]# yum groupinstall "development tools"
[root@httpd-server ~]# yum -y install openssl-devel pcre-devel expat-devel
[root@httpd-server ~]# useradd -r -s /sbin/nologin apache
[root@httpd-server ~]# id apache
uid=498(apache) gid=498(apache) groups=498(apache)
[root@httpd-server ~]# cd /usr/src
[root@httpd-server src]# tar xf apr-1.6.2.tar.gz
[root@httpd-server src]# tar xf apr-util-1.6.0.tar.gz
[root@httpd-server src]# tar xf httpd-2.4.27.tar.gz
编译安装
[root@httpd-server src]# cd apr-1.6.2 #编译安装APR
[root@httpd-server apr-1.6.2]# ./configure --prefix=/usr/local/apr
[root@httpd-server apr-1.6.2]# make -j 4
[root@httpd-server apr-1.6.2]# make install
[root@httpd-server apr-1.6.2]# ll /usr/local/ |grep apr
drwxr-xr-x 6 root root 4096 Oct 1 19:40 apr
[root@httpd-server apr-1.6.2]# cd ../apr-util-1.6.0 #编译安装APR-util
[root@httpd-server apr-util-1.6.0]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@httpd-server apr-1.6.2]# make -j 4
[root@httpd-server apr-1.6.2]# make install
[root@httpd-server apr-util-1.6.0]# ll /usr/local/ |grep apr
drwxr-xr-x 6 root root 4096 Oct 1 19:40 apr
drwxr-xr-x 5 root root 4096 Oct 1 19:42 apr-util
[root@httpd-server apr-util-1.6.0]# cd ../httpd-2.4.27 #编译安装Httpd-2.4
[root@httpd-server httpd-2.4.27]# ./configure --prefix=/usr/local/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=prefork
[root@httpd-server httpd-2.4.27]# make -j 4
[root@httpd-server httpd-2.4.27]# make install
[root@httpd-server httpd-2.4.27]# cd /usr/local/
[root@httpd-server local]# ls
apr apr-util bin etc games httpd24 include lib lib64 libexec sbin share src
配置服务
[root@httpd-server local]# cd httpd24/
[root@httpd-server httpd24]# ls
bin build cgi-bin conf error htdocs icons include logs man manual modules
[root@httpd-server httpd24]# ss -tnl #查看当前端口情况
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
[root@httpd-server httpd24]# ./bin/apachectl start #启动httpd服务
AH00557: httpd: apr_sockaddr_info_get() failed for httpd-server #这是由于没有设定默认主机名导致的 不影响重新运行
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[root@httpd-server httpd24]# curl localhost #确认服务是否正确运行
<html><body><h1>It works!</h1></body></html>
httpd
服务是以daemon
身份运行的。[root@httpd-server httpd24]# ss -tnl #此时80端口已打开 说明Httpd服务正常运行
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
[root@httpd-server httpd24]# vim conf/httpd.conf
[root@httpd-server httpd24]# ./bin/httpd -t #语法检查
AH00557: httpd: apr_sockaddr_info_get() failed for httpd-server
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@httpd-server httpd24]# ./bin/apachectl restart #重启服务
PATH
[root@httpd-server httpd24]# cd bin/
[root@httpd-server bin]# ls
ab apxs dbmmanage envvars-std htcacheclean htdigest httpd logresolve
apachectl checkgid envvars fcgistarter htdbm htpasswd httxt2dbm rotatelogs
[root@httpd-server bin]# pwd
/usr/local/httpd24/bin
[root@httpd-server bin]# echo 'export PATH=/usr/local/httpd24/bin:$PATH' > /etc/profile.d/httpd24.sh
[root@httpd-server bin]# . /etc/profile.d/httpd24.sh
[root@httpd-server bin]# echo $PATH
/usr/local/httpd24/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@httpd-server bin]# scp user@Host:/sourceAutoStartScript-PATH /etc/rc.d/init.d/httpd24 #获取自启脚本 可参看RPM安装的服务脚本
[root@httpd-server bin]# vim /etc/rc.d/init.d/httpd24 #根据自身配置修改脚本
[root@httpd-server httpd24]# chkconfig --add httpd24 #添加到列表
[root@httpd-server httpd24]# chkconfig httpd24 on #设置为自启动
[root@httpd-server httpd24]# chkconfig httpd24 --list
httpd24 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@httpd-server httpd24]# mkdir /web
[root@httpd-server httpd24]# cd /web
[root@httpd-server web]# echo "Hi,XiaoMu." > index.html
[root@httpd-server web]# cd /usr/local/httpd24/conf/
[root@httpd-server conf]# ls
extra httpd.conf magic mime.types original
[root@httpd-server conf]# vim httpd.conf
#DocumentRoot "/usr/local/httpd24/htdocs" #禁用默认站点
[root@httpd-server conf]# vim extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/web"
ServerName www.zhimajihua.cn
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
<Directory "/web"> #httpd-2.4中,任意目录必须显式授权方可访问,否则默认拒绝
Require all granted
</Directory>
</VirtualHost>
[root@httpd-server conf]# vim httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf #取消该行注释 启用自定义的虚拟主机
[root@httpd-server conf]# apachectl restart
[root@httpd-server conf]# curl localhost #本机测试
Hi,XiaoMu.
客户端测试
[root@centos7 ~]# curl www.zhimajihua.cn
Hi,XiaoMu.
检查日志是否工作正常
[root@httpd-server conf]# cat /usr/local/httpd24/logs/access_log
::1 - - [01/Oct/2017:20:51:45 +0000] "GET / HTTP/1.1" 200 45
192.168.1.45 - - [01/Oct/2017:20:53:36 +0000] "GET / HTTP/1.1" 200 45