天天看點

RHS333-10 httpd進階配置

httpd進階配置

一、虛拟主機配置

1、基于ip

要求:通過192.168.32.31可以通路/var/www/html目錄内容,通過192.168.32.32可以訪  問/var/www/virt目錄内容

[root@station1 ~]#vi /etc/httpd/conf/httpd.conf

<VirtualHost 192.168.32.31:80>

    ServerAdmin [email protected]

    DocumentRoot /var/www/html

    ServerName 192.168.32.31:80

    ErrorLog logs/dummy-host.example.com-error_log

    CustomLog logs/dummy-host.example.com-access_log common

</VirtualHost>

<VirtualHost 192.168.32.32:80>

    DocumentRoot /var/www/virt

    ServerName 192.168.32.32:80

2、基于端口

要求:通過192.168.32.31的80端口可以通路/var/www/html目錄内容,通過192.168.32.31的8080端口可以通路/var/www/virt目錄内容

Listen 80            #此端口配置檔案預設就有

Listen 8080          #手動添加此端口

<VirtualHost 192.168.32.31:8080>

    ServerName 192.168.32.31:8080

3、基于主機頭

要求:通過station1.example.com可以通路/var/www/html目錄内容,通過www.example.com可以通路/var/www/virt目錄内容  (注意要求DNS伺服器上有這兩個網站解析)

NameVirtualHost 192.168.32.31:80   #要求必須由此行,此行表示打開主機頭虛拟主機

    ServerName station1.example.com

    ServerName www.example.com

二、多種使用者認證方式配置

1、使用htpsswd工作生成的密碼檔案認證使用者來源

[root@station1 conf.d]# htpasswd -cm /etc/httpd/.webusers netsword

[root@station1 conf.d]# htpasswd -m /etc/httpd/.webusers netswordster

[root@station1 conf.d]# htpasswd -m /etc/httpd/.webusers zhxy

[root@station1 conf.d]# htpasswd -m /etc/httpd/.webusers zxy

[root@station1 conf.d]# vi /etc/httpd/.webgroup  #給使用者分組

net:netsword netswordster

zh:zhxy zxy

  # -c:表示建立密碼檔案

  # -m:用md5方式加密認證資訊

  # -D:從密碼檔案中删除使用者

[root@station1 conf.d]#

[root@station1 conf.d]# vi /etc/htttpd/conf/httpd.conf

  <Directory /var/www/html>

     AuthName TestAdmin    #提示資訊

     AuthType basic        #基本身份認證,即基于密碼檔案的身份認證

     AuthUserFile /etc/httpd/.webusers

     Require valid-user                      #所有授權使用者均可通路;

     AuthGroupFile /etc/httpd/.webgroup      #可通路使用者為net組中使用者

     Require Group net           

#valid-user:表所有密碼檔案中的使用者均可通路此目錄,也可為Require netsword則表示隻有密碼檔案中netsword賬戶可以通路此目錄

 </Directory>

2、使用MySQL資料庫認證使用者來源

安裝mysql及httpd中mysql認證子產品

[root@station1 ~]# yum install mysql-server.i386

[root@station1 ~]# yum install mysql-devel.i386

[root@station1 ~]# yum install mod_auth_mysql.i386

[root@station1 ~]# service mysqld start

[root@station1 ~]# chkconfig mysql on

建立認證使用者和認證組

   [root@station1 ~]# mysqladmin -u root password redhat

   [root@station1 ~]# mysql -uroot -predhat

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 131

Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database apacheusers;

mysql> use apacheusers;

mysql> create table user (name char(25),pwd char(25), primary key (name));

mysql> create table grp (uname char(25),gname char(25),primary key (uname,gname));

mysql> grant select on apacheusers.user to apacheuser@localhost identified by 'redhat';

mysql> grant select on apacheusers.grp to apacheuser@localhost identified by 'redhat';

mysql> insert into user (name,pwd) values ('netsword','111');

mysql> insert into user (name,pwd) values ('netswordster','111');

mysql> insert into user (name,pwd) values ('zhxy','222');

    mysql> insert into user (name,pwd) values ('zxy','222');

    mysql> insert into grp (uname,gname) values ('netsword','net');

    mysql> insert into grp (uname,gname) values ('netswordster','net');

    mysql> insert into grp (uname,gname) values ('zhxy','zh');

    mysql> insert into grp (uname,gname) values ('zxy','zh');

修改配置檔案,開啟mysql認證

   [root@station1 ~]# vi /etc/httpd/conf/httpd.conf

       NameVirtualHost 192.168.32.31:80

     AuthName TestAdmin

     AuthType basic

     AuthMySQLEnable on

     AuthMySQLUser apacheuser

     AuthMySQLPassword redhat

     AuthMySQLDB apacheusers

     AuthMySQLUserTable user

     AuthMySQLNameField name

     AuthMySQLPasswordField pwd

     Require valid-user

     AuthMySQLGroupTable grp

     AuthMySQLGroupField gname

     Require Group net

三、HTTPS配置

1、自頒發證書

[root@station1 ~]#yum install mod_ssl.i386

[root@station1 ~]#mkdir /etc/httpd/.sslkey

[root@station1 ~]#openssl genrsa -out /etc/httpd/.sslkey/server.key 1024

[root@station1 ~]#openssl req -new -x509 -key /etc/httpd/.sslkey/server.key -out /etc/httpd/.sslkey/server.cert #生成密鑰對

[root@station1 ~]#chmod -R 400 /etc/httpd/.sslkey    #保證證書安全

 <VirtualHost 192.168.32.31:443>

    ServerAdmin [email protected]

    SSLEngine on                                         #開啟ssl認證

    SSLCertificateFile /etc/httpd/.sslkey/server.crt     #證書檔案

    SSLCertificateKeyFile /etc/httpd/.sslkey/server.key  #密鑰檔案

四、各種安全參數

1、目錄通路控制

 [root@station2 ~]# vi /etc/httpd/conf/httpd.conf 

   <Directory /var/www/virt1>  

    Order allow,deny

    Allow from all

    Deny from 192.168.32.33

   </Directory>

   #定義通路/var/www/virt1目錄權限(含其下子目錄)

  Order allow,deny:除了明确定義允許的,預設拒絕所有,同時滿足允許和拒絕定義的用戶端則拒絕優先。即如無allow from all,則所有用戶端均不可通路/var/www/virt1目錄。

  Orde deny,allow:除了明确定義拒絕的,預設允許所有,同時滿足允許和拒絕定義的用戶端則允許優先。

2、基于通路控制檔案.htaccess(無需重新開機httpd)

[root@station2 ~]# vi /etc/httpd/conf/httpd.conf 

AccessFileName .htaccess   

<Files ~ "^\.ht">

    Deny from all

</Files>

#預設配置檔案中含有以上行

<Directory /var/www/virt1/test>  

    Allowoverride all  #該行定義http是否檢查該目錄下.htacess檔案及如何檢查

#Allowoverride後可接如下參數:

all:全部指令組

none:禁止使用所有指令?,禁止處理.htaccess檔案

Authconfig:允許使用與認證授權相關給的指令(AuthDBMGroupFile, AuthDBMUserFile, AuthGroupFile, AuthName, AuthType, AuthUserFile, Require, 等)

FileInfo:允許使用控制文檔類型的指令(DefaultType, ErrorDocument, ForceType, LanguagePriority, SetHandler, SetInputFilter, SetOutputFilter, mod_mime中的 Add* 和 Remove* 指令等等) 、控制文檔中繼資料的指令(Header, RequestHeader, SetEnvIf, SetEnvIfNoCase, BrowserMatch, CookieExpires, CookieDomain, CookieStyle, CookieTracking, CookieName)、mod_rewrite中的指令(RewriteEngine, RewriteOptions, RewriteBase, RewriteCond, RewriteRule)和mod_actions中的Action指令。

Indexs:允許使用控制目錄索引的指令(AddDescription, AddIcon, AddIconByEncoding, AddIconByType, DefaultIcon, DirectoryIndex, FancyIndexing, HeaderName, IndexIgnore, IndexOptions, ReadmeName, 等)。

Limit:允許使用控制主機通路的指令(Allow, Deny, Order)。

Options[=Option,...]:允許使用控制指定目錄功能的指令(Options和XBitHack)。可以在等号後面附加一個逗号分隔的(無空格的)Options選項清單,用來控制允許Options指令使用哪些選項。

[root@station2 ~]# vi /var/www/virt1/test/.htaccess

Order allow,deny

Allow from all

Deny from 192.168.32.33

#禁止192.168.32.33通路test目錄,.htaccess詳解另述

3、options參數

options 參數如下:

  Indexes :Creates a directory listing if no index file is present

  ExecCGI: Allows the execution of CGI scripts

  Includes: Enables Server Side Includes (SSI)

  IncludesNoExec: Enables SSI without executing any commands

  FollowSymLinks: Symbolic links are followed

  SymLinksIfOwnerMatch: Only if the owner of the symlink is the same as the target file

  MultiViews: If a document is available in multiple languages it is displayed according to the

  Language: settings for the browser.

  All :All options are turned on

  None: All options are disabled

執行個體:

 [root@station2 test]# vi /etc/httpd/conf/httpd.conf

<Directory /var/www/virt1/test>

Options  Indexes –FollowSymLinks   

#indexes顯示檔案清單,前加-則不顯示,客戶通路顯示拒絕通路目錄,建議關閉indexes

#FollowSymLinks:顯示連結檔案說連結的檔案或目錄,前加-則不顯示,客戶通路顯示拒絕通路目錄,建議關閉

本文轉自netsword 51CTO部落格,原文連結:http://blog.51cto.com/netsword/504044