教你linux搭建web服务器

在我本机配置时间不长,不过花了好长的时间写了份配置文档,和大家分享一下,希望对大家有用
linux发行版本:fedora 14 , apache .2.2
安装就不说了!依然采用rpm包安装方式 ,下面进入正题:
httpd.conf配置文件介绍
1、与apache网络和系统相关的选项
#使用serverroot参数设置apache安装目录
# serverroot: the top of the directory tree under which the server's
# configuration, error, and log files are kept.
# you will save yourself a lot of trouble.
# do not add a slash at the end of the directory path.
#
serverroot /etc/httpd
#使用listen参数设置apache监听端口,apache默认是80
listen 80
#使用user参数设置apache进程的执行者
user apache
#使用group参数设置apache进程执行者所属的用户组
group apache
#使用serveradmin参数设置网站管理员的邮箱地址
2、与apache文件和目录权限相关选项
#使用documentroot参数设置网站根目录
# documentroot: the directory out of which you will serve your
# documents. by default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
documentroot /var/www/html
#使用directory段设置根目录权限
options followsymlinks
allowoverride none
#使用directory段设置/var/www/html目录权限
options indexes followsymlinks
allowoverride none
order allow,deny
allow from all
#设置首页为index.html
directoryindex index.html index.html.var
#.ht后缀文件的访问权限控制与上目录的allowoverride一起作用
# the following lines prevent .htaccess and .htpasswd files from being
# viewed by web clients.
#
order allow,deny
deny from all
satisfy all
3、与apache日志相关的选项如下
#使用errorlog参数设置错误日志的位置
# errorlog: the location of the error log file.
# if you do not specify an errorlog directive within a
# container, error messages relating to that virtual host will be
# logged here. if you *do* define an error logfile for a
# container, that host's errors will be logged there and not here.
#
errorlog logs/error_log
#使用loglevel参数设置错误日志的级别
# loglevel: control the number of messages logged to the error_log.
# possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
loglevel warn
#使用logformat参数设置访问日志的格式模板
# the following directives define some format nicknames for use with
# a customlog directive (see below).
#
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
#使用customlog参数设置访问日志的格式模板
# for a single logfile with access, agent, and referer information
# (combined logfile format), use the following directive:
#
customlog logs/access_log combined
4、相关参数的解释
1)serverroot参数 该参数用于指定apache软件安装的目录
2)listen参数 用于指定apache所监听的端口,默认情况下apache的监听端口为80.在服务器有多个ip地址的情况下,listen参数还可以用于设置监听的ip地址
3)user和group参数 用于指定apache进程的执行者和执行者所属的用户组,如果要用uig或者gid,必须在id前面加上#号
4)serveradmin参数 该参数用于指定web管理员的邮箱地址,这个地址会出现在系统连接出错的时候,以便访问者能够及时通知web管理员。
5)documentroot参数 该参数用于指定web服务器上的文档存放的位置,在未配置任何虚拟主机或虚拟目录的情况下,用户通过http访问web服务器,所有的输出资料文件均存放在这里。
6)errorlog 用于指定记录,apache运行过程中所产生的错误信息的日志文件位置,方便系统管理员发现和觖决故障
5、符号链接和虚拟目录
1)虚拟目录是一种将根目录以外的内容加入到站点中的办法。设置方法如下
#使用alias参数设置虚拟目录和实际目录的对应关系
alias /lopn /var/www/lopn
#使用directory段设置/var/www//lopn目录的访问权限
……………………………………
此时,重启服务器,使用 域名或ip/lopn即可访问
2)符号链接:其时符号链接所实现的功能和虚拟目录是完全一样的,只不过是实现机制不同而已!
还如上例:使用符号链接不需要修改配置文件,只需要在 根目录下创建一个连接 使用如下指令
#ln -s /var/www/lopn lopn
此时即可通过 域名或ip/lopn即可访问访问到/var/www/lopn 下的内容
6、个人空间
拥有帐号的本地帐号可以利用apache发布自己的东西,假设有一用户lopn,则其发布地址为http://主机/~lopn
下面我们开始配置
//表示加载这个模块,表示apache开放个人空间功能
#
# userdir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
userdir disabled root /禁止root用户发布
#
# to enable requests to /~user/ to serve the user's public_html
# directory, remove the userdir disabled line above, and uncomment
# the following line instead:
#
#userdir public_html /去掉注释符开启功能,表示个人用户目录下的public_html 内容发布
接下来要对目录的权限进行设置,如下,apache提供默认设置,去掉#号即可,如下所示
# control access to userdir directories. the following is an example
# for a site where these directories are restricted to read-only.
#
allowoverride fileinfo authconfig limit
options multiviews indexes symlinksifownermatch includesnoexec
order allow,deny
allow from all
order deny,allow
deny from all
#
这样就配置好了个人空间。不过设置的时候注意权限的设置
7、apache安全配置
apache 提供了多种的安全控制手段,包括web访问控制,用户登录密布认证以及.htaccess文件等。
1)访问控制
在配置文件中,direcotry段用于设置与目录相关的参数和指令,包括访问控制和认证
目录相关的配置参数和指令
相关指令:1.allow指令用于设置哪些客户端可以方问apache,格式如下
allow from [all/全域名/部分域名/ip地址/网络地址/cidr地址]
2.deny指令用于设置拒绝哪些客户端访问apache,格式跟allow一样
3.order指令用于指定执行访问规则的先后顺序
order allow,deny :先执行充许访问规则,再执行拒绝访问规则
order deny,allow
2)用户认证
apache的用户认证包括基本(basic)认证和摘要(digest)认证两种。这里我们只探讨一下基本认证
基本认证:当web浏览器请求经此认证模式保护的url时,将会出现一个对话框,要求用记输入用户名和口令。如果正确,则返回页面,否则返回401错误
要使用用户认证,首先要创建保存了用户名和口令的认证口令文件。可以用如下命令:
#htpasswd -c /etc/httpd/conf/users lopn *在/etc/httpd/conf目录下创建一个名为users 的认证口令文件,并加入一个名为lopn的用户
#htpasswd /etc/httpd/conf/users xrp *表示加入一个xrp用户
有了认证口令文件,接下来我们需要更改配置文件
#
# options indexes followsymlinks
# allowoverride none
#
# authtype basic *使用authtype参数设置认证类型
# authname 领域名称 *用天设置受保护领域的名称,此处没有限制,用户可根喜好设置
# authuserfile /etc/httpd/conf/users *使用authuserfile参数设置认证口令文件的位置
# require user lopn *设置lopn用户可以访问
# require valid-user *授权给认证口令文件中的所有用户 ,与上一个选一种即可
#
# order ……
# ……
#
3)分布式配置文件.htaccess
.htaccess,该文件可以覆盖httpd.conf文件中的配置,但是它只能设置对目录的访问控制和用户认证。.htaccess文件可以有多个,每个.htaccess文件的作用范围仅限于该文件所存放的目录以及该目录下的所有子目录。.htaccess所能实现的功能在配置文件中都能实现,但是因为在.htaccess修改配置后并不需要重启服务就能生效,所以有时候是非常有用的1,操作如下
打开配置文件在,设置
#
# *允许.htaccess文件覆盖httpd.conf文件中对该目录的配置
# allowoverride all
#
然后到指定的目录,创建一个.htaccess文件,写入配置,不用重启就可以生效
8、虚拟主机(基于主机名)下面为配置文件中的说详细说明,挺简单,这里就不重复了
#namevirtualhost *:80
#所有接口都接受该请求
# note: namevirtualhost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# ssl protocol.
#
#
# virtualhost example:
# almost any apache directive may go into a virtualhost container.
# the first virtualhost section is used for requests without a known
# server name.
#
#
# serveradmin webmaster@dummy-host.example.com
# documentroot /www/docs/dummy-host.example.com
# servername dummy-host.example.com
# errorlog logs/dummy-host.example.com-error_log
# customlog logs/dummy-host.example.com-access_log common
#

读懂PetaLinux:让嵌入式Linux在它上面“跑”起来
Arm宣布在华成立5G解决方案实验室
通过加速寿命试验来模拟产品在极端压力水平下的性能
微机电系统的三种材料和加工技术
军用连接器应用及市场的未来发展趋势
教你linux搭建web服务器
硅整流交流发电机的工作特性
吉利终于出招了!全新MPV不输GL8,10万让奥德赛元气大亏
RFID技术在服装行业有哪些应用?
费城半导体指数2023年跃升65%,创2009年以来最大涨幅
佰才邦Baicells完成由高通领投的亿元B+轮融资
Qlib的安装和运行内置算法策略
台积电7nm芯片已成5G产品的主要支撑力量
寄存器与锁存器与触发器
vivo计划在2020年推出至少5款5G机型
基于PC及和PIC16F877芯片实现情感表情机器人的研究与设计
SV-8003VP对讲广播SIP协议网络SIP话筒主机
OPPOMIX最新消息:OPPO全面屏来袭告别低配高价,骁龙835加持价格感人
瑞士保罗谢勒研究所近期成功研制出了一种微型机器人
重大突破!首款国产LPDDR5存储芯片来了,容量、速率均提升50%