centos6下httpd2.2的配置

Apache httpd 2.2

主配置文件:/etc/httpd/conf/httpd.conf
铺助配置文件:/etc/httpd/conf.d/*.conf

主配置文件主要分为3部分:

[root@dns1 ~]# grep "Section" /etc/httpd/conf/httpd.conf 
### Section 1: Global Environment
    # 全局配置;
### Section 2: 'Main' server configuration
    # 主要配置;
### Section 3: Virtual Hosts
    # 虚拟主机配置;

常用配置选项说明:

修改监听端口:

Listen [IP:]PORT
eg: Listen 192.168.1.108:80

修改持久连接选项:
# KeepAlive: Whether or not to allow persistent connections (more than
KeepAlive Off
    # 开关
# MaxKeepAliveRequests: The maximum number of requests to allow
MaxKeepAliveRequests 100
    # 按资源请求数量决定持久连接时长;
# KeepAliveTimeout: Number of seconds to wait for the next request from the
KeepAliveTimeout 15
    # 直接按时间决定持久连接时长,单位秒;
httpd服务使用三种工作模式:

prefork:多进程模型,一个进程响应一个请求;
worker:多线程模型(多个进程生成,一个进程生成多个线程),每个线程响应一个请求;
event:事件驱动模型,一个进程响应多个请求;

配置httpd服务使用的工作模式:
[root@dns2 html]# grep "HTTPD=/" /etc/sysconfig/httpd 
#HTTPD=/usr/sbin/httpd.worker
    # 此条默认被注释,即系统默认使用的是prefork的工作模式,若想修改工作模式,修改此条语句即可;修改完成重启服务;
修改httpd服务的文档根路径:
[root@dns2 html]# grep "DocumentRoot" /etc/httpd/conf/httpd.conf 
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"
    # 比如DocumentRoot修改成/var/test/htdocs;
    # 则http://IP:PORT/index.html,访问的是/var/test/htdocs下的index.html;(我按默认,没有修改。)
使用Directory实现站点访问控制:

格式:

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
#   Deny from IP、NET or ALL
</Directory>

# 此例是从主配置文件中摘录的一段自带配置;
# 属于Section 2: 'Main' server configuration部分;
# 此例控制的正是httpd root(/var/www/html)访问控制;
# <Directory>...</Directory>是一对;
# Options定义的是此<Directory>下的选项配置,常用的有:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI
# MultiViews None All
# 此项设置成index比较危险,最好设置成None;
# 因为当请求找不到index的时候,系统会把整个目录下的文件列表发送给用户,供
# 其选择;
# 从安全性考虑,根目录的AllowOverride属性一般都配置成“None”不允许任何
# Override(优先级);
# order定义的是allow,deny控制顺序;第二的是默认项,第一是第二的附加;
# Allow from 定义的是运行访问的主机,Deny from定义的是不允许访问的;

下面的例子是禁止我的windows系统访问httpd服务器:

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    Deny from 192.168.1.103
        # 192.168.1.103是我的windows系统;
</Directory>
[root@dns1 html]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using dns1.mysite.com for ServerName
                                                           [  OK  ]

打开如下网站会提示:192.168.1.108/index.html
You don’t have permission to access /index.html on this server.

设置打开的默认网页:
DirectoryIndex index.html index.html.var
日志文件设置:
ErrorLog logs/error_log
    # 错误文件路径/名定义;
LogLevel warn
    # 错误文件记录级别;
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
    # 日志格式定义;关于格式说明可以参考官方文档;
    # 在此做简要说明:
    # %h: 客户端IP地址
    # %l: Remote logname
    # %u: Remote user
    # %t: 服务器端收到请求时的时间
    # %r: 请求报文的首行信息
    # %>s: 响应状态码
    # %b: 响应报文的大小, 单位是字节, 不包括响应报文首部
    # %{Referer}i: %{Foobar}i, 请求报文中Referer首部的值, 当前资源的
    # 访问入口,即从哪个页面中的超链接跳转而来;
    # %{User-Agent}i: 请求报文中user-agent首部的值,发出请求用到的应用
    # 程序
#CustomLog logs/access_log common
#CustomLog logs/referer_log referer
#CustomLog logs/agent_log agent
    # 上面是被关闭的三个日志文件,可以取代“#”将其开启;
CustomLog logs/access_log combined
    # 访问日志的定义,且使用的是combined的LogFormat;
路径别名的设置:
Alias /icons/ "/var/www/icons/"
如果访问http://192.168.1.103/icons/README.html,其实访问的是
/var/www/icons/README.html;
如果不知道别名,则http://192.168.1.103/icons/README.html
访问的是/var/www/html/icons/README.html(其实不存在)文件;
以basic明文方式认证用户登录(即用户必须输入用户名和密码):

下面以设置/var/www/html/test目录为例:

<Directory "/var/www/html/test">
    Options None
    AllowOverride None
    AuthType Basic
        # 认证方式,basic明文认证类型;
    AuthName "STRING"
    AuthUserFile "/etc/httpd/conf/.htpasswd"
        # 用户认证文件保存位置;
    Require user tom
        # 允许认证的用户;
    Order allow,deny
    Allow from all
</Directory>

# 需要使用htpasswd命令创建认证文件:
[root@dns1 html]# htpasswd -c -m /etc/httpd/conf/.htpasswd tom
    # 此处添加tom用户;
New password: 
Re-type new password: 
Adding password for user tom
[root@dns1 html]# htpasswd -m /etc/httpd/conf/.htpasswd jerry
    # 此处添加Jerry用户,他被设置为不能登录;
New password: 
Re-type new password: 
Adding password for user jerry
[root@dns1 html]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using dns1.mysite.com for ServerName
                                                           [  OK  ]
[root@dns1 html]# 

也可以把用户放在组中,还是以/var/www/html/test目录为例:

<Directory "/var/www/html/test">
    Options None
    AllowOverride None
    AuthType Basic
    AuthName "STRING"
    AuthUserFile "/etc/httpd/conf/.htpasswd"
    AuthGroupFile "/etc/httpd/conf/.htgroup"
        # 添加group文件;
    Require group gp1
        # 认证的组名为gp1;
    Order allow,deny
    Allow from all
</Directory>

# 添加新用户jacen,并把tom和jacen添加到gp1组中:
[root@dns1 ~]# htpasswd -m /etc/httpd/conf/.htpasswd jacen
New password: 
Re-type new password: 
Adding password for user jacen
[root@dns1 html]# vim /etc/httpd/conf/.htgroup
gp1: tom jacen
    # 注意此处group文件的编写格式;
[root@dns1 html]# service httpd restart
    # 然后可以测试发现Jerry还是不能登录,但是新加的用户jacen却可以;
虚拟主机的配置:

注意:要使用虚拟主机,须先在“Main”配置部分禁用中心主机:

#DocumentRoot "/var/www/html"
    # 在语句前面加"#"注释掉即可;
虚拟主机属于配置文件的第三部分

其一般通过三种方式实现:IP、端口或主机名;也可以混用上述三种方式;

基于多IP实现虚拟主机:
<VirtualHost 192.168.1.108:80>
    DocumentRoot /var/www/html/web1
        # 注意IP和根文件目录;
    ServerName dns1.mysite.com
</VirtualHost>
<VirtualHost 192.168.1.121:80>
    DocumentRoot /var/www/html/web2
        # 注意IP和根文件目录,与上面的是不同的;
    ServerName dns1.mysite.com
</VirtualHost>
    # 以上是对配置文件做的修改;
    # 以下是创建了虚拟主机的根目录并在其根目录放置了不同的文件;
[root@dns1 html]# mkdir -pv web{1..4}
mkdir: created directory `web1'
mkdir: created directory `web2'
mkdir: created directory `web3'
mkdir: created directory `web4'
[root@dns1 html]# ll
total 24
-rw-r--r--. 1 root root   16 Jan 11 23:56 index.html
drwxr-xr-x. 2 root root 4096 Jan 12 00:22 test
drwxr-xr-x. 2 root root 4096 Jan 12 10:41 web1
drwxr-xr-x. 2 root root 4096 Jan 12 10:41 web2
drwxr-xr-x. 2 root root 4096 Jan 12 10:41 web3
drwxr-xr-x. 2 root root 4096 Jan 12 10:41 web4
[root@dns1 html]# pwd
/var/www/html
[root@dns1 html]# vim web1/index.html
[root@dns1 html]# vim web2/index.html
[root@dns1 html]# vim web3/index.html
[root@dns1 html]# vim web4/index.html
[root@dns1 html]# service httpd restart
    # 在浏览器中输入192.168.1.121和192.168.1.108会发现浏览器分别打开
    # 了web1和web2下的页面;
基于端口实现虚拟主机:

首先需要添加监听端口:

#Listen 12.34.56.78:80
Listen 80
Listen 8080 # 此行为新加;
然后再添加如下虚拟主机:
<VirtualHost 192.168.1.121:8080>
    DocumentRoot /var/www/html/web3
    ServerName dns1.mysite.com
</VirtualHost>
[root@dns1 html]# service httpd restart
    # 重启服务测试;
基于主机名实现虚拟主机:
NameVirtualHost 192.168.1.121:80
    # 要实现主机名区分虚拟主机须修改此项,IP与后面的IP保持一致;
<VirtualHost 192.168.1.121:80>
    ServerName dns2.mysite.com
    DocumentRoot /var/www/html/web4
</VirtualHost>
    # 上面为新加的虚拟主机;
<VirtualHost 192.168.1.121:80>
    ServerName dns1.mysite.com
    DocumentRoot /var/www/html/web2
</VirtualHost>
    # 注意两台主机ServerName的不同;
    # 下面提到的https用的是此虚拟主机, 此主机保持此样即可, 无须多做修改;
配置https

配置https需要私建CA,且为web服务器生成证书,关于此些内容请参考我的另一篇博客。
(我假设你现在已经有了CA证书以及web服务器的证书。)
可以使用xshell的sftp命令把CA证书拷贝至windows系统上,然后双击证书把CA证书安装至”受信任的证书颁发机构“;
要使用https,你首先应安装ssl模块:

[root@dns1 conf]# yum install mod_ssl

修改/etc/httpd/conf.d/ssl_conf文件:

DocumentRoot "/var/www/html"
    # 设置ssl文档根目录;
ServerName dns1.mysite.com:443
    # 设置对应的主机名和端口;
    # 若使用了虚拟主机, 虚拟主机部分不要修改端口号(保持80即可);
SSLCertificateFile /etc/httpd/ssl/dns1.mysite.com.crt
    # 定位证书文件;
SSLCertificateKeyFile /etc/httpd/ssl/dns1.mysite.com.key
    # 定位私钥文件;
[root@dns1 html]# service httpd restart
    # 然后重启httpd服务;

现在使用windows下的浏览器验证即可;
注意:你在浏览器中输入的名字应是你的web服务器证书中注册的名字,比如,我的服务器dns1.mysite.com注册证书时使用的是”dns1.mysite.com“,那么你在浏览器中键入的名字应是:https://dns1.mysite.com/否则打不开链接(直接输入IP也不行。)。

–完–

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值