搭建http服務器及配置


web服務器主要是提供上網信息瀏覽服務。

Apache HTTPD Server 簡稱 Apache,是 Apache 軟件基金會的一個開源的網頁服務器, 可以在大多數計算機操作系統中運行,由於其多平台和安全性被廣泛使用,是最流行的 Web 服務器端軟件之一。它快速、可靠並且可通過簡單的 API 擴展,將 Perl/Python 等解釋器編 譯到服務器中!Apache HTTP 服務器是一個模塊化的服務器,各個功能使用模塊化進行插拔! 目前支持 WindowsLinuxUnix 等平台!

 

Web 服務器的工作模式和端口

 工作模式是:B/S 模式

工作端口是:80/http 正常端口443/https SSL 端口

 

安裝apache服務
[root@x101 ~]# yum -y install httpd

安裝elink字符瀏覽器
[root@x101 ~]# yum -y install elinks


編輯httpd主配置文件
[root@x101 ~]# vim /etc/httpd/conf/httpd.conf 

啟動httpd服務
[root@x101 ~]# systemctl start httpd

字符瀏覽器訪問測試
[root@x101 ~]# elinks 192.168.0.101
也可以使用windows客戶端瀏覽器訪問測試,
firefox地址欄輸入  http://192.168.0.101

 

httpd服務主配置文件解析
[root@x101 ~]# vim /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"               

httpd服務安裝目錄
Listen 80                                     
監聽端口

Include conf.modules.d/*.conf      
包含conf.moudules.d目錄下所有的.conf文件

User apache
運行的用戶身份

Group apache
運行的組身份

ServerAdmin root@localhost
管理員郵箱

#ServerName www.example.com:80    
服務器主機名,域名


<Directory />
    AllowOverride none
    Require all denied
</Directory>
httpd的一個功能模塊

DocumentRoot "/var/www/html"
默認網站服務的主目錄

<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
對/var/www目錄做的權限設置


<Directory "/var/www/html">
    Options Indexes FollowSymLinks MultiViews
    indexes表示如何網站主目錄中沒用默認首頁時,會顯示網站目錄索引列表;FollowSymLinks表示允許在此目錄中使用符號鏈接;MultiViews模糊查詢;

    Order allow,deny			
    這里默認后者生效,也就是 deny 生效
   
    Allow from all				
    這里說允許所有
    
    AllowOverride None
    Require all granted
</Directory>
<Directory "/var/www/html">表示對/var/www/html目錄做的權限設置


<IfModule dir_module>
    DirectoryIndex index.html
    指定默認首頁
</IfModule>

ErrorLog "logs/error_log"
定義錯誤日志存放位置


</IfModule>
    CustomLog "logs/access_log" combined   
定義訪問日志存儲路徑
</IfModule>

AddDefaultCharset UTF-8
設置服務器的默認編碼為: UTF-8

IncludeOptional conf.d/*.conf
加載conf.d目錄下的所有conf文件,通常用於一台服務器配置多個虛擬主機,每個虛擬主機使用單獨的.conf配置文件,存儲在conf.d目錄下

 

取消網站默認首頁,配置文件welcome.conf,注釋掉以下4行即可
[root@x101 html]# vim /etc/httpd/conf.d/welcome.conf 
#<LocationMatch "^/+$">
#    Options -Indexes
#    ErrorDocument 403 /.noindex.html
#</LocationMatch>

[root@x101 html]# systemctl restart httpd

 訪問測試:

 

取消網站目錄列表顯示,編輯httpd.conf,去掉以下選項中的Indexes即可。 

[root@x101 ~]# vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/html">

    Options Indexes FollowSymLinks

    indexes表示如何網站主目錄中沒用默認首頁時,會顯示網站目錄索引列表;FollowSymLinks表示允許在此目錄中使用符號鏈接

</Directory>

修改配置文件,重啟httpd服務生效。

[root@x101 html]# systemctl restart httpd

測試:

 

apache軟件版本查看
[root@x101 html]# yum list httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Installed Packages
httpd.x86_64                2.4.6-67.el7.centos                @rhel
Available Packages
httpd.x86_64                2.4.6-93.el7.centos                base 

或者使用以下命令
[root@x101 html]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: Aug 4 2017 03:19:10

 客戶端與服務端查看結果一致:

 

 

創建默認首頁

[root@x101 html]# echo "welcome to yuqianwan" > index.html

測試訪問:

 

更改網站默認主目錄為自定義目錄wcg,並更改權限
[root@x101 html]# vim /etc/httpd/conf
DocumentRoot "/var/www/html/wcg"
<Directory "/var/www/html/wcg">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    allow from 192.168.0.0/24
    deny from 192.168.0.0/24
    Require all granted
</Directory>

注意權限,Order allow,deny,在Order最后面的權限較高
[root@x101 html]# mkdir /var/www/html/wcg
[root@x101 html]# cat /var/www/html/wcg/index.html
welcome to WCG 

 更改Order deny,allow之后,測試訪問:

 

 

使用別名,引用網站更目錄以外路徑
將/usr/local/share目錄通過虛擬目錄功能添加到網站根目錄。當訪問http://192.168.0.101/share時,就可以訪問目錄/usr/local/share中的內容
注意;apache的別名也叫做虛擬目錄
修改httpd.conf主配置文件
[root@x101 html]# vim /etc/httpd/conf/httpd.conf
Alias /share /usr/local/share
<Directory "/usr/local/share">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from 192.168.0.0/24
Require all granted
</Directory>
[root@x101 html]# systemctl restart httpd
[root@x101 ~]# mkdir /usr/local/share
[root@x101 share]# cp /boot/*    .

 訪問測試: 

 

 

打開軟鏈接功能,直接引用網站根目錄以外內容

[root@x101 html]# mkdir /web1
[root@x101 html]# echo "hello web1" > /web1/a.html
[root@x101 html]# ln -s /web1  /var/www/html/wcg/web1
[root@x101 html]# ll  wcg/ 
total 4
-rw-r--r-- 1 root root 16 Aug 19 12:38 index.html
lrwxrwxrwx 1 root root  5 Aug 19 14:55 web1 -> /web1


[root@x101 html]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html/wcg"
<Directory "/var/www/html/wcg">
    Options  Indexes FollowSymLinks
    AllowOverride None
    Order deny,allow
    allow from all
    deny from 192.168.0.0/24
    Require all granted
</Directory>

 注意:httpd.conf文件中必需要有Options  Indexes FollowSymLinks配置項,才可以使用軟鏈接功能。 

訪問測試:

 

 

通過用戶認證的方式,對網站下/usr/local/share目錄進行保護。設置/usr/local/share目錄,只能通過用戶名密碼方式訪問。
方法一:[root@x101 html]# vim /etc/httpd/conf/httpd.conf 
Alias   /share   /usr/local/share
<Directory "/usr/local/share">
    Options Indexes FollowSymLinks
    AllowOverride AuthConfig	#開啟認證方式
    Order deny,allow
    Allow from all
    authtype basic				#基本認證
    authname “weicome to web”   #提示信息
    Authuserfile /etc/httpd/conf/passwd.secret #密碼文件路徑
    Require valid-user
</Directory>

方法二: [root@x101 html]# vim /etc/httpd/conf/httpd.conf Alias /share /usr/local/share <Directory "/usr/local/share"> Options Indexes FollowSymLinks AllowOverride AuthConfig #開啟認證方式 Order deny,allow Allow from all </Directory>

在別名目錄中新建頭文件.htaccess,將auth相關的內容寫入其中,重啟服務生效。 [root@x101 html]# vim /usr/local/share/.htaccess authtype basic #基本認證 authname “weicome to web” #提示信息 Authuserfile /etc/httpd/conf/passwd.secret #密碼文件路徑 Require valid-user [root@x101 html]# systemctl restart httpd 生成密碼文件 [root@x101 html]# htpasswd -cm /etc/httpd/conf/passwd.secret feng New password: Re-type new password: Adding password for user feng [root@x101 html]#

參數詳解:

authtype basic#authtype命令:指定認證類型為:basic。

authname "my web site "    #AuthName命令:指定認證區域名稱。區域名稱是在提示要求認證的對話框中顯示給用戶的

authuserfile /etc/httpd/conf/passwd.secret   #AuthUserFile命令:指定一個包含用戶名和密碼的文本文件,每行一對。

require命令        #指定哪些用戶或組才能被授權訪問。如:

require user user1 user2(只有用戶user1和user2可以訪問)

require valid-user (在AuthUserFile指定的文件中任何用戶都可以訪問)

訪問測試:

 

 

 

 

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM