Apache服务基础调优参数详解
一、准备工作:准备一台服务器(Centos6.5系统)IP:192.168.2.1011、安装服务器端:httpd
# yum -y install httpd
2、安装浏览器:
# yum -y install elinks
使用方法:
# elinks www.baidu.com
3、启动服务
# service httpd restart
报错
# vim /etc/hosts
4、查看端口监听状态
# netstat -anptu | grep 80
5、修改配置文件,实战举例
二、相关参数调优案例举例
案例一:搭建一台测试web服务器
部门内部搭建一台WEB服务器,采用的IP地址和端口为192.168.2.101:80,首页采用index.html
文件。管理员E-mail地址为hk@hk.cn,网页的编码类型采用UTF-8,所有网站资源都存放在/var/www/html目录下,并将Apache的配置文件根目录设置为/etc/httpd目录。
需要修改配置文件相关参数:
#vim /etc/httpd/conf/httpd.conf
行号 参数
57 ServerRoot "/etc/httpd"#apache配置文件的根目录
70 Timeout 60#超时时间,接收和发送前超时秒数
136 Listen 80 #监听的端口
262 ServerAdmin root@localhost#设置管理员,e-mail 地址
276 ServerName 192.168.2.101:80 #服务器主机名.按模板添加一行
293DocumentRoot "/var/www/html" #网站页面根目录
Options Indexes FollowSymLinks #当一个目录没有默认首页时,允许显示此目录列表
禁止 Apache 显示目录结构列表,只需将 Option 中的 Indexes 去掉即可,或者改成-Indexes
403 DirectoryIndex index.html index.html.var#指定默认首页 添加网页类型
760AddDefaultCharset UTF-8# 设置服务器的默认编码为: UTF-8
案例二:取消apache默认欢迎页:
# vim /etc/httpd/conf.d/welcome.conf
原默认欢迎界面如下
修改后如下但是目录结构还能显示:
332行取消目录结构后重启服务如下图:
# This configuration file enables the default "Welcome"
# page if there is no default index page present for
# the root URL.To disable the Welcome page, comment
# out all the lines below.
#
# #把红色内容进行注释
# Options -Indexes
# ErrorDocument 403 /error/noindex.html
#
重启:
# service httpd restart
创建首页文件
# echo "welcome to this country" > /var/www/html/index.html注:selinux 关闭getenforce查看
案例三:修改网站根目录及对应参数,设置访问权限
设置根文档目录为/var/www/html/test 并显示结构目录选项,/var/www/html设置不允许查看目录选项
重启服务后测试如下图:
并没有访问/var/www/html/test/td文件的权限
说明:目录与访问控制:
#子目录会继承这个目录的属性,所以读取不了td的目录结构,但test目录下没有index.html文件时也被限制访问目录权限(测试得到的结果)
Options -Indexes FollowSymLinks #Options:Indexes:目录浏览 #Followsymlinks:可以用连接
AllowOverride None #不允许任何Override
Order allow,deny
Allow from 192.168.2.0/24 #从哪里来的允许
Deny from 192.168.2.0/24 #从哪里来的拒绝
Allow from.baidu.com
#Allow,Deny都会读取,如果有冲突和未说明的时候按照Order选项逗号后面的那个为准。
谁写到后面,谁的优先级高。
配置完重启apache服务
测试:
案例四: 使用别名功能,引用网站根目录以外的路径。
将/usr/local/http 目录通过虚拟目录功能添加到网站根目录。当访问http://192.168.2.101/http/ 时,就可以访问目录/usr/local/htp中的内容。
注:apache的别名也叫虚拟目录
语法:
AliasURL路径 PATH物理路径
创建测试目录和数据
# mkdir /usr/local/http
# cp -r /boot/grub/ /usr/local/http/
# echo "test1"> /usr/local/http/test.html
修改配置文件
#vim /etc/httpd/conf/httpd.conf
注:Alias /http/ "/usr/local/http/" #/http/ 可以随意起名比如改/http/ 为/HK/则访问链接: http://192.168.1.63/HK/
重启服务
案例五:当一个目录下没有默认首页时,访问http://192.168.2.101/http/禁止显示目录列表
修改配置文件:
#vim /etc/httpd/conf/httpd.conf# 修改红色标记内容
结构目录禁止访问
页面文件还是可以访问
案例六: 打开软链接功能,通过软件链接直接引用网站根目录以外的内容
# mkdir /wg
# echo test000 > /wg/ss.html
# ln -s /wg/ /var/www/html/wg
# cd /var/www/html/
# ls
wg
# cat /wg/ss.html
test000
#vim /etc/httpd/conf/httpd.conf将根目录修改为/var/www/html后重启服务
案例七:通过用户认证的方式,对网站下/usr/local/http/目录进行保护。 设置/usr/local/http/目录,只能通过用户名密码方式访问。
方法一:
Alias /http/ "/usr/local/http/"
Options Indexes FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
authtype basic #基本认证
authname "MySQL admin software" 可以随意命名
authuserfile /etc/httpd/conf/passwd.secret #密文配置文件路径
require valid-user #有效用户
#require user tom#拒绝访问的用户
#require user tom bob #拒绝访问的多个用户
重启apache 访问http://192.168.2.101/http验证
生成认证账号和密码
# htpasswd -cm /etc/httpd/conf/passwd.secret hk
New password:
Re-type new password:
Adding password for user hk
再次验证:
方法二:
# vim /etc/httpd/conf/httpd.conf
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
AllowOverride AuthConfig
# AllowOverride AuthConfig一行表示允许对/usr/local/http/目录下的文件进行用户认证。
# vim /usr/local/phpmyadmin/.htaccess
authtype basic
authname "haa"
authuserfile /etc/httpd/conf/passwd.secret
#require user tom
require valid-user
配置Apache虚拟主机,实现在一台服务器上运行多个网站:http://blog.51cto.com/13719714/2109040
相关Mysql-proxy实现读写分离参考链接:http://blog.51cto.com/13719714/2107691
使用LAMP环境搭建wordpress论坛参考链接:http://blog.51cto.com/13719714/2108415
源码编译LNMP请参考链接:http://blog.51cto.com/13719714/2110940
页:
[1]