apachectl2.4编译安装及虚拟目录的实现

前提: 安装开发环境 yum groupinstall "Deveopment Tools" "Server Platform Developent" httpd依赖于:apr, apr-util, apr-icon 其他依赖包:pcre-devel zlib-devel 在安装前请确保以上基础的依赖包已经安装! 开始安装: 源码包安装顺序为:arp,apr-util,httpd 环境: os:cetos6.6 httpd-2.4.10.tar.bz2 apr-1.5.0.tar.bz2 apr-iconv-1.2.1.tar.bz2 apr-util-1.5.3.tar.bz2 (1)apr [root@localhost yum.repos.d]# tar xf apr-1.5.0.tar.bz2 [root@localhost yum.repos.d]# cd apr-1.5.0 [root@localhost apr-1.5.0]# ./configure --prefix=/usr/local/apr [root@localhost apr-1.5.0]# make && make install (2)apr-util [root@localhost apr-1.5.0]# cd .. [root@localhost yum.repos.d]# tar xf apr-util-1.5.3.tar.bz2 [root@localhost yum.repos.d]# cd apr-util-1.5.3 [root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util -with- apr=/usr/local/apr [root@localhost apr-1.5.0]# make && make install (3)创建系统用户 [root@localhost apr-util-1.5.3]# groupadd -r apache [root@localhost apr-util-1.5.3]# useradd -r -g apache apache (4)httpd [root@localhost apr-1.5.0]# cd .. [root@localhost yum.repos.d]# tar xf httpd-2.4.10.tar.bz2 [root@localhost yum.repos.d]# cd httpd-2.4.10 [root@localhost httpd-2.4.10]# ./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=prefork [root@localhost httpd-2.4.10]# # make && make install (5)启动脚本的创建 1,编辑脚本文件 [root@localhost httpd-2.4.10]# vim /etc/profile.d/httpd.sh export PATH=/usr/local/apache/bin:$PATH [root@localhost httpd-2.4.10]# cd /etc/profile.d/ 2,执行脚本程序 [root@localhost profile.d]# . /httpd.sh 3,查看是否已经加载 [root@localhost profile.d]# which apachectl /usr/local/apache/bin/apachectl 4,清楚hash [root@localhost httpd-2.4.10]hash -r 5,启动apachectl [root@localhost httpd-2.4.10]# 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 6,查看80端口是否监听 [root@localhost httpd-2.4.10]# ss -untl | grep :80 7,浏览器测试是否出现It works! 其他: 查看编辑安装的命令:其中包括安装路径,配置文件,安装的模块 [root@localhost httpd-2.4.10]# cd /usr/local/apache/ [root@localhost apache]# less build/config.nice 网页图标:icons/ 头文件:include/ 头文件如果要被系统调用的话,需要连接至/usr/include/ [root@localhost apache]# ln -sv /usr/local/apache/include/ /usr/include/httpd/ (6)日志和man手册 [root@localhost apache]# ls logs/ access_log error_log httpd.pid man手册的创建,当你编译完成后,是由单独的httpd man配置文件,添加至系统man配置中 [root@localhost apache]# ls man man1 man8 1,/etc/man.config 44行作业添加 [root@localhost apache]# vim /etc/man.config MANPATH /usr/local/apache/man 2,/etc/httpd24/httpd.conf 搜索/manual打开这行功能(我打开后服务起不来了,后续再 来研究) [root@localhost apache]# vim /etc/httpd24/httpd.conf Include /etc/httpd24/extra/httpd-manual.conf 到此,apache已经编译安装完成。 一:接下来修改模块!(可选) 1,使用httpd -M可以查看目前使用的是什么模块! 这个版本编译后默认是mpm_prefork_module (shared) 2,我们尝试着将prefork修改为event 1-35行 [root@localhost apache]# vim /etc/httpd24/httpd.conf 、 修改前复制一个并且注释 #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so prefork修改为event LoadModule unixd_module modules/mod_unixd.so LoadModule mpm_event_module modules/mod_mpm_event.so 3,而后,在使用httpd -M查看是否已经修改! 二:修改Main server的DocumentRoot 1,创建一个目录 [root@localhost htdocs]# mkdir /data/web/htdocs -pv mkdir: created directory `/data' mkdir: created directory `/data/web' mkdir: created directory `/data/web/htdocs' 2,编辑一个测试页 [root@localhost htdocs]# vim /data/web/htdocs/index.html

hello apache!

3,修改配置文件/etc/httpd24/httpd.conf DocumentRoot Directory 218行 4,重启apachectl,打开浏览器测试网站根目录是否已经跳转! 三,基于IP地址的访问控制 Require all granted是:允许所有主机访问 [root@localhost htdocs]# vim /etc/httpd24/httpd.conf Require all granted 245行 拒绝所有主机访问:Require all deny控制特定IP访问: Require ip IPADDR:授权指定来源地址的主机访问 Require not ip IPADDR:拒绝指定来源地址的主机访问 IPADDR可以是以下类型: IP: 172.16.100.2 Network/mask: 172.16.0.0/255.255.0.0 Network/Length: 172.16.0.0/16 Net: 172.16 控制特定主机(HOSTNAME)访问 Require host HOSTNAME Require not host HOSTNAME HOSTNAME: FQDN: 特定主机 DOMAIN:指定域内的所有主机 四,虚拟主机 使用虚拟主机需要把DocumentRoot关掉才能使用 另外,虚拟主机定义在一个配置文件:/etc/httpd24/extra/httpd-vhosts.conf 1,打开/etc/httpd24/httpd.conf,删除注释,开启虚拟主机 [root@localhost htdocs]#vim /etc/httpd24/httpd.conf Include /etc/httpd24/extra/httpd-vhosts.conf 查看LogFormat是否开启 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common 基于IP、Port和FQDN都支持; 基于FQDN的不再需要NameVirtualHost指令 2,创建/vhost/a.com和b.com [root@localhost ~]# mkdir /vhost/a.com/ -p [root@localhost ~]# mkdir /vhost/b.com/ -p [root@localhost ~]# tree /vhost/ /vhost/ ├── a.com └── b.com 2 directories, 0 files [root@localhost ~]# vim /vhost/a.com/index.html

www.a.com

[root@localhost ~]# vim /vhost/b.com/index.html

www.b.com

[root@localhost ~]# vim /vhost/b.com/index.html

www.b.com

~ 2,编辑vhosts.conf文件 vim /etc/httpd24/extra/httpd-vhsots.conf ServerAdmin webmaster@a.com DocumentRoot "/vhost/a.com" ServerName www.a.com ErrorLog "logs/www.a.com-error_log" CustomLog "logs/www.a.com-access_log" combined Options none AllowOverride none Require all granted ServerAdmin webmaster@b.com DocumentRoot "/vhost/b.com" ServerName www.b.com ErrorLog "logs/www.b.com-error_log" CustomLog "logs/www.b.com-access_log" combined Options none AllowOverride none Require all granted 3,检查配置文件 [root@localhost ~]# httpd -t 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 Syntax OK 4,查看区域设置 [root@localhost ~]# httpd -S 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 VirtualHost configuration: *:80 is a NameVirtualHost default server www.a.com (/etc/httpd24/extra/httpd-vhosts.conf:23) port 80 namevhost www.a.com (/etc/httpd24/extra/httpd-vhosts.conf:23) port 80 namevhost www.b.com (/etc/httpd24/extra/httpd-vhosts.conf:31) ServerRoot: "/usr/local/apache" Main DocumentRoot: "/usr/local/apache/htdocs" Main ErrorLog: "/usr/local/apache/logs/error_log" Mutex default: dir="/usr/local/apache/logs/" mechanism=default PidFile: "/usr/local/apache/logs/httpd.pid" Define: DUMP_VHOSTS Define: DUMP_RUN_CFG User: name="daemon" id=2 Group: name="daemon" id=2 [root@localhost ~]# 5,测试 [root@www httpd-2.4.10]# curl www.a.com

www.a.com

[root@www httpd-2.4.10]# curl www.b.com

www.b.com

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的解决方法就是打开httpd.conf中ServerName就可以   https可以参考openssl应用简述PDF