Apache 服务器配置 第14天

2012-09-11 22:13:12 0  category: linux

Apache 基本配置
配置文件:/etc/httpd/httpd.conf
改变页面目录 DocumentRoot
ServerName 指定 Apache 服务名称 ServerName 如果指定该项,在启动 Apache 服务时不会
出现 ServerName 出错提示
改变监听端口 Port
设置子进程启动数目
StartServers 默认启动子进程数目
MinSpareServers
MaxSpareServers
设置索引文件 DirectoryIndex
设置管理员邮箱 ServerAdmin
定义最大客户端访问量 MaxClients
/usr/local/apache2/logs 日志文件
日志文件 ErrorLog
定义服务类型 ServerType (新版本有的已经不用了)
定义进程用户及组 User Group
指定 apache 的根目录 ServerRoot
error_log “logs/error_log” 是以 ServerRoot 为根的
[root@php_notes ~]# cat /etc/httpd/httpd.conf
40 Listen 80
90 ServerAdmin php_notes@php.xlxz.org
99 #ServerName  php.xlxz.org:80 指定 Apache 服务器名称,可为 IP 地址
106 DocumentRoot “/usr/local/apache2/htdocs”
167 <IfModule dir_module>
168
DirectoryIndex index.html
169 </IfModule>
[root@php_notes ~]# cd /usr/local/apache2/
[root@php_notes apache2]# ls
bin build cgi-bin error htdocs icons include lib logs man manual
modules
[root@php_notes apache2]# cd logs/
[root@php_notes logs]# cat httpd.pid
2504
[root@php_notes logs]# ps -le | grep 2504
1 S
0 2504
1 0 78 0 – 966 -
?
00:00:00 httpd
[root@php_notes logs]# cat access_log
192.168.9.254 – - [28/Feb/2011:15:15:53 +0800] “GET / HTTP/1.1″ 200 44
192.168.9.254 – - [28/Feb/2011:15:15:53 +0800] “GET /favicon.ico HTTP/1.1″ 404 209
个人主页
配置选项 UserDir public_html,开启个人主页功能,取消子配置文件注释,加载 userdir 模

用户可以在自己的宿主目录下创建网页保存目录
http://php.xlxz.org/~sam /home/sam/public_html
~php_notes /home/php_notes/public_html
设置用户个人主页:
#useradd -d /export/home/php_notes -m php_notes
#passwd php_notes
#mkdir /export/home/php_notes/public_html
在此目录下创建一测试页面
访问:http://服务器 IP 或域名/~用户名


站点登录控制
第一步:在 Apache 的配置文件中添加:
Alias /doc“/share/doc” Alias 别名 原始记录(别名后加/就是另一个地址)
注:
“Alias”可以设置别名,使复杂的路径名称简化,(容器内的设置都是局部的)
<Directory“/share/doc”> <Directory File VirtualHost >
Options Indexes 开启文件列表功能
AllowOverride AuthConfig 设置认证访问 none 不认证 AuthConfig 认证
Order allow,deny 设置那些主机可以访问,允许优先
Allow from all
Deny from 202.8.94.20
Order deny,allow 禁止优先
Deny from all
Allow from 202.8.9.1 185.2.30.3
</Directory>
第二步:在/share/doc 目录下建立.htaccess 文件,其内容如下:
AuthName“The CUUG’s Docs”提示信息
AuthType Basic 设置密码验证加密
AuthUserFile /share/doc/.htpasswd 密码存放的文件
require valid-user 指定存放用户
require user zhangsan 只有 zhangsan 是有效用户
#vi .htaccess
AuthName “Welcome to doc”
AuthType Basic
AuthUserFile /share/doc/.htpasswd
require valid-user
编辑文件时不能留有空行
第三步:执行命令生成认证文件:
#/usr/local/apache/bin/htpasswd -c/share/doc/.htpasswd sam
更改已设定的密码,添加第一个用户时加-c,以后就不用加了
#/usr/local/apache/bin/htpasswd -m/share/doc/.htpasswd sam
第四步:测试
通过浏览器输入 http://服务器 IP 或域名/doc
[root@php_notes
[root@php_notes
413
414
415
416
417
apache2]# mkdir -p /share/doc
apache2]# vi /etc/httpd/httpd.conf
Alias /doc “/share/doc” 或者 Alias /doc/ “/share/doc/”
<Directory “/share/doc”>
Options Indexes
AllowOverride AuthConfig
Order allow,deny
418
Allow from all
419 </Directory>
[root@php_notes apache2]# /usr/local/apache2/bin/apachectl restart
此时在 Windows 浏览器中输入 http://192.168.0.105/doc 可以看到/share/doc 中的文件一
列标的形式列出来了
[root@php_notes apache2]# cd /share/doc
[root@php_notes doc]# vi .htaccess
1 AuthName “Welcome to doc”
2 AuthType Basic
3 AuthUserFile /share/doc/.htpasswd
4 require valid-user
[root@php_notes doc]# /usr/local/apache2/bin/htpasswd -c /share/doc/.htpasswd sam
New password:
Re-type new password:
Adding password for user sam(非系统用户)
[root@php_notes doc]# /usr/local/apache2/bin/htpasswd /share/doc/.htpasswd mary
New password:
Re-type new password:
Adding password for user mary(系统用户)
[root@php_notes doc]# /usr/local/apache2/bin/htpasswd -m /share/doc/.htpasswd mary
New password:
Re-type new password:
Updating password for user mary
配置文件不留空行不然出错
虚拟主机
1、公司一个服务器跑多个域名对应的 web server
2、空间商
基于 IP,服务器设置多个 IP 地址,每个 IP 对应一个域名
基于端口,用端口区分不同的域名访问
基于域名 NameVirtualHost
配置基于域名的虚拟主机:
1、启用虚拟主机配置文件
2、编辑虚拟主机配置文件
1)声明采用基于域名的虚拟主机
NameVirtualHost IP 地址
<VirtualHost 192.168.9.75>
ServerName 192.168.9.75 (域名或 IP)
DocumentRoot /usr/local/apache2/htdocs
</VirtualHost>
2)虚拟主机的配置
<VirtualHost 192.168.9.75>
# 改成本机 IP 地址
ServerAdmin php_notes@php.xlxz.org
# 管理员邮箱地址
DocumentRoot “/usr/local/apache2/docs/dummy-host.example.com”
# 指定虚拟主机网页存放目录
<Directory “/usr/local/apache2/docs/dummy-host.example.com”>
Options Indexes FollowSymLinks
# 目录列表、支持链接
AllowOverride None
# 不认证
Order allow,deny
Allow from all
# 设定哪些主机可以访问
</Directory>
ServerName php.xlxz.org
# 指定域名
ServerAlias php.xlxz.org
# 别名
ErrorLog “logs/dummy-host.example.com-error_log”
# 错误日志
CustomLog “logs/dummy-host.example.com-access_log” common
# 事件日志
</VirtualHost>
[root@php_notes ~]# /usr/local/apache2/bin/apachectl start
[root@php_notes ~]# iptables -F
[root@php_notes ~]# vi /etc/httpd/httpd.conf
编辑 apache 配置文件
393 Include /etc/httpd/extra/httpd-vhosts.conf 取消注释让虚拟主机配置文件生

“/etc/httpd/httpd.conf” 414L, 13673C written
[root@php_notes ~]# ls /etc/httpd/extra/
[root@php_notes ~]# vi /etc/httpd/extra/httpd-vhosts.conf 编辑虚拟主机配置文件,假设
本地地址为 192.168.9.7,同时设定两个虚拟主机 site1 和 sit2,修改配置如下:
NameVirtualHost 192.168.9.7
<VirtualHost 192.168.9.7>
DocumentRoot “/usr/local/apache2/htdocs/”
ServerName xlxz.org
</VirtualHost>
<VirtualHost 192.168.9.7>
ServerAdmin php_notes@xlxz.org
DocumentRoot “/usr/local/apache2/site1″
<Directory “/usr/local/apache2/site1″>
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ServerName php.xlxz.org
ErrorLog “logs/dummy-host2.example.com-error_log”
CustomLog “logs/dummy-host2.example.com-access_log” common
</VirtualHost>
<VirtualHost 192.168.9.7>
ServerAdmin hywmickey@sina.com
DocumentRoot “/usr/local/apache2/site2″
<Directory “/usr/local/apache2/site2″>
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ServerName site2.com
ErrorLog “logs/dummy-host2.example.com-error_log”
CustomLog “logs/dummy-host2.example.com-access_log” common
</VirtualHost>
“/etc/httpd/extra/httpd-vhosts.conf” 75L, 2462C written
[root@php_notes ~]# /usr/local/apache2/bin/apachectl restart
将/etc/httpd/extra/httpd-vhosts.conf 原来的配置信息注释之后重启 Apache 将不会出
现警告信息
在对应网页存放目录生成索引文件
[root@php_notes ~]# cd /usr/local/apache2/
[root@php_notes apache2]# mkdir site1
[root@php_notes apache2]# echo “This is site1″ > site1/index.html
[root@php_notes apache2]# mkdir site2
[root@php_notes apache2]# echo “This is site2″ > site2/index.html
在测试客户端通过/etc/hosts 文件做域名解析(互联网应用中不涉及需要通过 hosts 文件
或 DNS 服务器做域名解析的问题,客户端指定自己的 DNS 服务器即可),编辑 Windows 主机
的 C:\WINDOWS\system32\drivers\etc\hosts 文件添加:
192.168.9.7 php.xlxz.org
192.168.9.7 www.xlxz.org

在 Windows 浏览器中输入 php.xlxz.org、 www.xlxz.org、seme.me就可以访问不同的虚拟主
机了。