教你linux搭建web服务器

嵌入式设计应用

131人已加入

描述

  在我本机配置时间不长,不过花了好长的时间写了份配置文档,和大家分享一下,希望对大家有用

  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

  #

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分