Web服務器的配置與應用

1.Apache服務器的安裝html

首先檢查是否安裝httpd服務,在終端輸入以下指令:web

#rpm -qa | grep httpd apache

輸出以下則表示已安裝httpd服務:瀏覽器

200022210.jpg

測試httpd服務是否安裝成功,可輸入以下指令啓動服務:服務器

#/etc/init.d/httpd start 或#/etc/ rc.d/init.d/httpd start 或#apachectl start 或#service httpd startide

查看httpd服務運行進程,可輸入以下指令:測試

#ps -eaf | grep httpdui

顯示以下:編碼

200024153.jpg

查看httpd服務是否在監聽端口80,可輸入以下指令:加密

#netstat -anp | grep :80

顯示以下:

200026460.jpg

打開防火牆TCP 80端口,即選中信任服務WWW(http)。(也可在終端輸入命令#setup)

200029306.jpg

200032382.jpg

在客戶端的瀏覽器中輸入Apache服務器的IP地址,出現以下圖提示信息,則表示Apache服務器安裝成功。

200036496.jpg


要想讓Apache服務開機自動運行,可在終端輸入以下指令:

#system-config-services

打開以下窗口,選中httpd項:

200040384.jpg


2.Apache服務的基本配置

Apache服務的主配置文件是/etc/httpd/conf/httpd.conf,它共有如下三部分。

### Section 1: Global Environment ,主要進行全局環境變量的設置。

### Section 2: 'Main' server configuration,進行主服務器配置。

### Section 3: Virtual Hosts,進行虛擬主機設置。

(1)Section 1: Global Environment(Apache全局設置)。

ServerRoot "/etc/httpd"

此爲Apache的根目錄,配置文件、記錄文件、模塊文件都在該目錄下。

PidFile run/httpd.pid

此文件保存着Apache父目錄ID。

KeepAlive Off

不容許客戶端同事提出多個請求,設爲on表示容許。

#Listen 12.34.56.78:80

Listen 80

設置Apache服務的監聽端口。通常在使用非80端口時設置。

#EntendStatus On

用於檢測Apache的狀態信息,預設爲Off。

User apache

Group apache

設置Apache工做時使用的用戶和組。

(2)Section 2: 'Main' server configuration(主服務器配置)。

ServerAdmin root@localhost

管理員的電子郵件地址。若是Apache有問題,則會寄信給管理員。

#ServerName www.example.com:80

此處爲主機名稱,若是沒有申請域名,使用IP地址也能夠。

DocumentRoot "/var/www/html"

設置Apache主服務器網頁存放地址。

<Directory />

Options FollowSynLinks

AllowOverride None

</Directory>

設置Apache根目錄的訪問權限和訪問方式。

<Directory "/var/www/html">

Options Indexes FollowSynLinks

AllowOverride None

Order allow,deny

Allow from all

</Directory>

設置Apache主服務器網頁文件存放目錄的訪問權限。

<IfModule mod_userdir.c>

UserDir disable

#UserDir public_html

</IfModule>

設置用戶是否能夠在本身的目錄下創建 public_html 目錄來放置文件。若是設置爲「UserDir public_html」,則用戶就能夠經過 「http://服務器IP地址:端口/~用戶名稱」來訪問其中的內容。

DirectoryIndex index.html index.html.var

設置預設首頁,默認是index.html。設置之後,用戶經過「http://服務器IP地址:端口/」訪問的就是「http://服務器IP地址:端口/index.html」。

AccessFileName .htaccess

設置Apache目錄訪問權限的控制文件,預設爲.htaccess,也能夠是其餘名字。

<Files ~ "^\.ht">

Order allow,deny

Deny from all

</Files>

防止用戶看到以「.ht」 開頭的文件,保護.htaccess、.htpasswd 的內容。主要是爲了防止其餘人看到預設能夠訪問相關內容的用戶名和密碼。

HostnameLookups Off

若是設置爲On,則每次都會向DNS服務器要求解析該IP,這樣就會花費額外的服務器資源,而且下降服務器端響應速度,因此通常設置爲Off。

Alias /icons/ "/var/www/icons/"

<Directory "/var/www/icons">

Options Indexes MultiViews

AllowOverride None

Order allow,deny

Allow from all

</Directory>

定義一個圖標虛擬目錄,並設置訪問權限。

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

<Directory "/var/www/cgi-bin">

AllowOverride None

Options None

Order allow,deny

Allow from all

</Directory>

同Alias同樣,只不過設置的是腳本文件目錄。

#DefaultLanguage nl

設置頁面的默認語言。

(3)Section 3: Virtual Hosts,(虛擬主機設置)。

#NameVirtualHost *:80

設置虛擬主機的名字和監聽端口。

#<VirtualHost *:80>

# 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

#</VirtualHost>

虛擬主機的全部相關信息。主服務器中採用的不少控制命令均可以在這裏使用。


下面講述一些對Apache訪問權限的設置:

1.查看服務器狀態

設置只容許IP地址爲192.168.91.240的主機查看服務器狀態。

在httpd.conf中修改以下:

去掉下行文字前面的#:

#EntendStatus On

還有下面文字前面的#:

#<Location /server-status>

# SetHandler server-status

# Order deny,allow

# Deny from all

# Allow from 192.168.91.240 //後面改成192.168.91.240

#</Location>

從新啓動httpd服務。 #apachectl restart

在192.168.91.240主機瀏覽器中輸入以下:

http://192.168.91.128/server-status

顯示結果以下圖:

200044858.jpg


.htaccess文件控制存取

.htaccess是一個訪問控制文件,用來配置相應目錄的訪問方法。不過,按照默認的配置時是不會讀取相應目錄下的.htaccess文件來進行訪問控制的。這是由於AllowOverride中配置爲AllowOverride None。所以只需修改AllowOverride的格式便可(有多項格式如:AuthConfig 、FileInfo、All等)。


3.用戶身份認證

建立用戶名和密碼

在/usr/local/httpd/bin目錄下,有一個htpasswd可執行文件,它就是用來建立.htaccess文件身份認證使用的密碼的,它的語法格式以下:

#htpasswd [-bcD] [-mdps] 密碼文件名 用戶名

參數說明以下:

-c :新建立一個密碼文件。

-D:刪除一個用戶。

-m:採用MD5編碼加密。

修改httpd.conf文件

在httpd.conf文件中輸入以下便可(可在文中</Directory>下):

<Directory "/var/www/html/mytest">

AllowOverride None

AuthType Basic

AuthName "myrealm"

AuthUserFile /var/www/passwd/.htpasswd

require user test //設置容許訪問的用戶,require Group則設置能夠訪問的羣組。

</Directory>

在客戶端輸入要反問的地址,則會出現以下:

200047464.jpg

輸入所設用戶和密碼便可。

4.虛擬主機

去除下行前面的#:

NameVirtualHost *:80

在文件結尾可輸入以下:

<VirtualHost *:80>

DocumentRoot /var/www/docs //設置虛擬主機文檔路徑

ServerName aas.test.com //設置主機名

</VirtualHost>

<VirtualHost *:80>

DocumentRoot /var/www/html/mytest

ServerName web.test.com

DirectoryIndex index.htm //設置預設首頁,默認是index.html

</VirtualHost>

在客戶端瀏覽器中輸入服務器虛擬主機名便可

如輸入:http://web.test.com。

注:因爲默認客戶端並不能解析域名aas.test.com,web.test.com。可在客戶端中修改文件C:\WINDOWS\system32\drivers\etc\hosts。

在hosts文件末尾添加以下兩行:

192.168.91.128 aas.test.com

192.168.91.128 web.test.com

相關文章
相關標籤/搜索