阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 6.6下安装Apache 2.2.31

162次阅读
没有评论

共计 9723 个字符,预计需要花费 25 分钟才能阅读完成。

安装环境:

[root@apache ~]# cat /etc/RedHat-release
CentOS release 6.6 (Final)
[root@apache ~]# uname -r
2.6.32-504.el6.x86_64

Apache 简介
Apache 是世界上最流行的 Web 服务器软件之一,当然,提供 WWW 服务的还有微软的 IIS,它是由微软开发的,只能用在微软的操作系统上,而 Apache 是一个自由软件。说到 Apache,还要联想到 LAMP,这个近年来也是应用得非常广泛,LAMP 就是:linux+Apache+mysql+php。Apache 的特点是简单、速度快、性能稳定。

Apache 的特点
功能强大,配置简单,速度快,应用广泛,性能稳定可靠,并可做代理服务器或负载均衡来使用。

Apache 的应用场合

  • 使用 Apache 运行静态 HTML 网页,图片(处理静态小文件能力不及 Nginx)
  • 使用 Apache 结合 PHP 引擎运行 PHP,Perl 等程序,LAMP 被称之为经典组合
  • 使用 Apache 结合 Tomcat/Resin 运行 JSP,Java 等程序,成为中小企业的首选
  • 使用 Apache 作代理,负载均衡,rewrite 规则过滤等等

安装 Apache
1:卸载系统默认安装的 Apache 软件包
一般来说,操作系统自带或者 apache rpm 包方法安装的 apache 版本都比较低,且更新不及时,因此我们需要卸载掉安装操作系统时默认被安装上的 apache 软件

通过 rpm -qa httpd* 命令,查询当前系统中已安装的 apache 软件包,具体查询操作过程如下:

rpm -qa httpd*
rpm -e –nodeps httpd-2.2.3-43.el5.centos.3

通过 rpm -e –nodeps 后面分别接查询到的 apache 软件包完整名称,即可卸载当前系统中通过 rpm 安装方式安装的 apache 软件包

–nodeps:Don’t do a dependency check,意思是不做软件间的依赖检查

对于具备一定经验的朋友,也可以直接执行下面的 shell 脚本命令来批量删除系统自带的 apache 软件包

 for name in `rpm -qa httpd*`;do rpm -e –nodeps $name;done

2:安装 zlib-devel 包
yum install zlib-devel -y

3:开始安装 Apache

Apache-2.2.31 版本可以到 Linux 公社资源站下载:

—————————————— 分割线 ——————————————

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2017 年资料 / 2 月 /19/CentOS 6.6 下安装 Apache 2.2.31/

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

—————————————— 分割线 ——————————————

tar zxvf httpd-2.2.32.tar.gz
cd httpd-2.2.31

4:创建安装目录
[root@apache httpd-2.2.31]# mkdir /application

5:编译
./configure \
–prefix=/application/apache2.2.31 \
–enable-deflate \
–enable-expires \
–enable-headers \
–enable-modules=most \
–enable-so \
–with-mpm=worker \
–enable-rewrite

# 编译参数说明:

#1. –prefix=/application/apache2.2.31 表示指定安装路径为:/application/apache2.2.31。如果不指定安装路径,则默认路径为:/usr/local/apache2

#2. –enable-deflate 提供对内容的压缩传输编码支持,一般 html,js,css 等内容的站点,使用此参数功能会大大提高传输速度,提升访问者访问体验。在生产环境中,这是 apache 调优的一个重要选项之一

#3. –enable-expires 激活允许通过配置文件控制 http 的“expires”和“cahe-control:”头内容,即对网站图片,JS,CSS 等内容,提供在客户端浏览器缓存的设置。这是 apache 调优的一个重要选项之一

#4. –enable-headers 提供允许对 HTTP 请求头的控制

#5. –with-mpm=worker 选择 apache mpm 的模式为 worker 模式,因为 worker 模式原理是更多的使用线程处理请求,所以可以处理更多的并发请求,而系统资源的开销小于基于进程的 mpm prefork。如果不指定此参数,默认的模式是 prefork 进程模式。有关 apache 模式的知识,后文会有详细阐述。这是 apache 调优的一个重要选项之一

#6. –enable-rewrite 提供基于 URL 规则的重写功能。即根据已知 URL 地址,转换其它想要的 URL 地址。如前文讲解的伪静态功能就是这个模块实现的。这是 APACHE 在生产环境中必用的一个重要功能。

#7. –enable-so 激活 APACHE 服务的 DSO(全称 dynamic shared object,动态共享对象)支持,即在以后可以以 DSO 的方式编译安装共享模块。这个模块本身不能以 DSO 方式编译。有关 DSO 的知识,后文讲解。

上面如果没有安装 zlib-devel 包,这步编译时会报如下错误:

checking for zlib location… not found
checking whether to enable mod_deflate… configure: error: mod_deflate has been requested but can not be built due to prerequisite failures

上面编译无误后执行下面的命令开始安装:

make
make install

6:创建软链接
[root@apache application]# ln -s apache2.2.31 apache
[root@apache application]# ll
total 4
lrwxrwxrwx  1 root root  12 Feb 15 10:30 apache -> apache2.2.31
drwxr-xr-x 15 root root 4096 Feb 15 10:30 apache2.2.31

查看 APACHE 编译的模块:

[root@apache application]# /application/apache/bin/apachectl -l
Compiled in modules:
  core.c
  mod_authn_file.c
  mod_authn_dbm.c
  mod_authn_anon.c
  mod_authn_dbd.c
  mod_authn_default.c
  mod_authz_host.c
  mod_authz_groupfile.c
  mod_authz_user.c
  mod_authz_dbm.c
  mod_authz_owner.c
…………………………………………….

7:启动 APACHE 并测试
[root@apache application]# /application/apache/bin/apachectl start
httpd: Could not reliably determine the server’s fully qualified domain name, using 192.168.17.16 for ServerName
# 第一次启动时,此提示可暂时忽略

查看 80 端口:

[root@apache application]# lsof -i :80
COMMAND  PID  USER  FD  TYPE DEVICE SIZE/OFF NODE NAME
httpd  58925  root    4u  IPv6  50990      0t0  TCP *:http (LISTEN)
httpd  58927 daemon    4u  IPv6  50990      0t0  TCP *:http (LISTEN)
httpd  58928 daemon    4u  IPv6  50990      0t0  TCP *:http (LISTEN)
httpd  58929 daemon    4u  IPv6  50990      0t0  TCP *:http (LISTEN)

查看 httpd 进程:

[root@apache application]# ps -ef|grep httpd
root    58925    1  0 10:32 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start
daemon  58926 58925  0 10:32 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start
daemon  58927 58925  0 10:32 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start
daemon  58928 58925  0 10:32 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start
daemon  58929 58925  0 10:32 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start

APACHE 启动成功后,在浏览器输入该服务器 IP 地址,可看到 It works! 字样:

[root@apache application]# ifconfig eth0|awk -F ‘[:]+’ ‘NR==2{print $4}’
192.168.17.16

CentOS 6.6 下安装 Apache 2.2.31

如果出不来 it works 的内容页面,请从下面原因中依次排查:

1:iptables 防火墙和 selinux 是否关闭

[root@apache application]# /etc/init.d/iptables stop

# 这是关闭防火墙的命令,如果是生产环境请允许 80 端口的访问,而不是关闭防火墙

# 允许命令如下:

iptables -I INPUT -p tcp –dport 80 -j ACCEPT

关闭 selinux:

临时关闭:setenforce 0

永久关闭(修改配置文件):cat /etc/selinux/config|grep SELINUX=disabled

2:通过下面命令确认 httpd 端口 80 是否存在

[root@apache application]# netstat -lnput|grep 80
tcp        0      0 :::80                      :::*                        LISTEN      58925/httpd

3:查看是否有 httpd 进程存在

[root@apache application]# ps -ef|grep http
root    58925    1  0 10:32 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start
daemon  58926 58925  0 10:32 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start
daemon  58927 58925  0 10:32 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start
daemon  58928 58925  0 10:32 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start
daemon  58929 58925  0 10:32 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start
daemon  59016 58925  0 10:34 ?        00:00:00 /application/apache2.2.31/bin/httpd -k start

4:在服务器本地 wget http://192.168.17.16 测试

注意:如果前面第 2 步和第 3 步都不符合要求,这步就不用查了,一定不行,直接下一步检查

这里把服务器本地当作客户端模拟用户检查 http 服务,此条的检查比第 2 步第 3 步的检查会更准确。同时可以排除防火墙的干扰来检查 http 服务,如果正常访问,说明 apache 服务没问题,那么就是网络或防火墙等问题了。

5:查看 apache 的错误日志看看是否有特殊异常

如果以上 5 条全都注意到了,基本问题就可以解决,如果是 2,3 步异常,则可以查看 apache 错误日志,获取信息

[root@apache application]# tail -10 /application/apache/logs/error_log

APACHE 的默认站点目录,是安装目录下的 /apache/htdocs,这可以从 APACHE 主配置文件 /application/apache/conf/httpd.conf 中可以查到:

[root@Web-Lamp application]# grep -i documentroot ./apache/conf/httpd.conf
# DocumentRoot: The directory out of which you will serve your
DocumentRoot “/application/apache2.2.31/htdocs”
# This should be changed to whatever you set DocumentRoot to.
# access content that does not live under the DocumentRoot.

此时,如果要部署网站服务,只需要把开发好的程序放置到 /application/apache2.2.32/htdocs 目录下即可!至此 Apache 的安装已完成!

APACHE 的目录结构

[root@Web-Lamp apache]# tree bin
bin
├── ab      #APACHE HTTP 服务器性能测试工具,简单,易用。同类软件还有 jmeter,loadrunner,webbench 等
├── apachectl      #这是 APACHE 的启动命令,需重点掌握,apachectl 是一个脚本
├── apxs        #apxs 是一个为 apache http 服务器编译和安装扩展模块的工具,在进行 DSO 方式模块编译时会用到
#后文在编译 php 软件时就用到此命令,如:–with-apxs2=/application/apache/apxs
├── htcacheclean #这是清理磁盘缓冲区的命令,需要在编译时指定相关参数才可使用,一般用的很少
├── htpasswd        #建立和更新基本认证文件,如:配置 nagios 等监控服务时会用到
├── httpd      #此为 apache 的控制命令程序,apachectl 执行时会调用 httpd
└── rotatelogs  #apache 自带的日志轮询命令,也还可以用,但在实际生产环境中习惯由 cronolog 替换它
提示:以上目录只列出了工作中比较常用的命令,其它末使用过的略过!
 
[root@Web-Lamp apache]# tree conf
conf
├── extra      #这是额外的 apache 配置文件目录,这个目录里的文件会经常访问修改:如 httpd-vhosts.conf
├── httpd.conf  #apache 的主配置文件,这个文件我们会经常访问修改,其中的每一行参数都应该弄清楚

APACHE 扩展的配置文件
apache 的扩展配置文件是通过在 httpd.conf 主配置文件中嵌入 include 命令实现的,不过默认情况下是注释状态。如下所示:

395 # Virtual hosts
396 #Include conf/extra/httpd-vhosts.conf

通过 tree 命令列出 apache 扩展的配置文件的在目录的配置文件

[root@Web-Lamp apache]# tree conf/extra
conf/extra
├── httpd-autoindex.conf
├── httpd-dav.conf
├── httpd-default.conf      #这个文件里配置的是 apache 的相关服务参数,如:超时时间,保持连接时间等
├── httpd-info.conf
├── httpd-languages.conf        #语言支持配置
├── httpd-manual.conf
├── httpd-mpm.conf      #服务器池管理,也就是优化 apache 的一个配置文件,如:选择 apache 的模式连接数等,常用的模式有 worker 模式和 profork 模式,默认情况下是 profork 模式!
├── httpd-multilang-errordoc.conf
├── httpd-ssl.conf              #提供 apache SSL 支持配置文件
├── httpd-userdir.conf
└── httpd-vhosts.conf          #虚拟主机配置文件

APACHE 虚拟主机的配置文件

[root@Web-Lamp apache]# grep -Ev “^#|^$” conf/extra/httpd-vhosts.conf
NameVirtualHost *:80
# 这里表示使用基于名称的虚拟主机配置,这是生产环境下最常用的配置。*:80 中的 * 表示监听本机所有 IP 地址,80 表示在 80 端口上提供 http 服务。如果 * 改为具体 IP,就表示监听本机指定 IP 地址的服务请求
<VirtualHost *:80>        #定义一个虚拟主机,监听本机所有 IP 地址 80 端口上提供的 http 服务请求
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot “/application/apache2.2.31/docs/dummy-host.example.com”
    ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
# 这是配置虚拟机的别名,也就是可以配置多个域名访问同一个站点,如:此处配置成 baidu.com,则输入 baidu.com 可以得到和 www.baidu.com 同样的结果,此功能需要 apache mode_alias 模块支持
    ErrorLog “logs/dummy-host.example.com-error_log”
    CustomLog “logs/dummy-host.example.com-access_log” common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot “/application/apache2.2.31/docs/dummy-host2.example.com”
    ServerName dummy-host2.example.com
    ErrorLog “logs/dummy-host2.example.com-error_log”
    CustomLog “logs/dummy-host2.example.com-access_log” common
</VirtualHost>

基于域名的虚拟主机实战配置
开启主配置文件 httpd.cond 中包含 httpd-vhost.conf 文件的配置:

[root@Web-Lamp apache]# vim /application/apache/conf/httpd.conf
395 # Virtual hosts
396 Include conf/extra/httpd-vhosts.conf            #将此行前面的 #号去掉

配置 httpd-vhosts.conf 文件:

[root@Web-Lamp extra]# cp httpd-vhosts.conf httpd-vhosts.conf.poe.20161010

备份后把 httpd-vhosts.conf 文件中的 <VirtualHost *.80></VirtualHost> 删除,只保留一个,然后在这一个的基础上修改

[root@Web-Lamp extra]# vi httpd-vhosts.conf
[root@Web-Lamp extra]# grep -Ev “^#” httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin root@linuxidc.net
    DocumentRoot “/var/html/blog”
    ServerName blog.linuxidc.com
    ServerAlias linuxidc.com
    ErrorLog “logs/blog-error_log”
    CustomLog “logs/blog-access_log” common
</VirtualHost>

检查修改过的 apache 配置文件的语法:

生产环境中这一步,特别重要,它让你在重起前可以查出配置文件中的错误,如果没有此步,很可能会因为配置错误,而导致网站短时宕机,这在正规互联网公司是无法接受的,即使公司可以接受,我们也要表现的更专业。

[root@Web-Lamp extra]# /application/apache/bin/apachectl -t
Warning: DocumentRoot [/var/html/blog] does not exist              #没有 /var/html/blog 这个目录
httpd: Could not reliably determine the server’s fully qualified domain name, using 192.168.17.12 for ServerName
Syntax OK

httpd:Cound…… 这一行的提示解决方案:

[root@Web-Lamp conf]# vim httpd.conf
96 # If your host doesn’t have a registered DNS name, enter its IP address here.
97 #如果你没有注册的 DNS 域名,这里可以输入 IP 地址
98 #ServerName www.example.com:80
99
100 ServerName 192.168.17.12

再次检查语法:

[root@Web-Lamp conf]# /application/apache/bin/apachectl -t
Syntax OK

语法检查通过后,执行 graceful 参数重起 apache 服务:

[root@Web-Lamp apache]# bin/apachectl graceful

注意:这里是 graceful 参数,而不是 restart 参数,graceful 表示优雅地重启,这个参数可以在重启时使正在浏览器的访问的用户无感知,即重启时不会强行中断用户的访问请求,而是处理完毕后再重新启动。这个 graceful 参数在生产环境中是非常重要,请在重启时务必使用 graceful 替代 restart 参数

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140803.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计9723字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中