我們平時訪問的網站服務就是Web網絡服務,一般是指允許用戶通過瀏覽器訪問到互聯網中各種資源的服務。Web網絡服務是一種被動訪問的服務程序,即只有接收到互聯網中其他主機發出的請求後纔會響應,最終用於提供服務程序的Web服務器會通過HTTP或HTTPS把請求的內容傳送給用戶。
1.安裝服務
web服務在rhel7中對應的服務是httpd,使用yum倉庫安裝httpd服務。
[root@shijie Desktop]# yum install -y httpd
打開火狐瀏覽器在地址欄中輸入http://127.0.0.1就可以看到網站的默認頁面了。
網站的數據默認保存在/var/www/html中,在該目錄中加入首頁文件index.html就可以在網站中看到文件內的內容了。
[root@shijie Desktop]# cd /var/www/html/
[root@shijie html]# ls
[root@shijie html]# echo hello world > index.html
[root@shijie html]# systemctl restart httpd
2.編輯配置文件
配置文件在/etc/httpd/conf/httpd.conf中,其中主要參數如下:
參數 作用
ServerRoot 服務目錄
ServerAdmin 管理員郵箱
User 運行服務的用戶
Group 運行服務的用戶組
ServerName 網站服務器的域名
DocumentRoot 網站數據目錄
Listen 監聽的IP地址與端口號
DirectoryIndex 默認的索引頁頁面
ErrorLog 錯誤日誌文件
CustomLog 訪問日誌文件
Timeout 網頁超時時間,默認爲300秒
3.開啓個人主頁
每個用戶都有自己的主頁,可以使用http://127.0.0.1/~用戶名來訪問,用戶主頁數據存放在用戶家目錄的public_html中。開啓個人主頁需要編輯/etc/httpd/conf.d/userdir.conf文件,將UserDir disabled註釋,UserDir public_html取消註釋。
[root@localhost html]# vim /etc/httpd/conf.d/userdir.conf
11 <IfModule mod_userdir.c>
12 #
13 # UserDir is disabled by default since it can confirm the presence
14 # of a username on the system (depending on home directory
15 # permissions).
16 #
17 # UserDir disabled
18
19 #
20 # To enable requests to /~user/ to serve the user's public_html
21 # directory, remove the "UserDir disabled" line above, and uncomment
22 # the following line instead:
23 #
24 UserDir public_html
25 </IfModule>
創建目錄和編輯主頁文件。
[root@localhost html]# vim /etc/httpd/conf.d/userdir.conf
[root@localhost html]# mkdir /home/shijie/public_html
[root@localhost html]# cd /home/shijie/public_html/
[root@localhost public_html]# echo hello shijie > index.html
重啓服務,關閉selinux。
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# setenforce 0
讓其他用戶對該家目錄擁有可讀可執行的權限,如果沒有權限是訪問不了的。
[root@localhost shijie]# chmod -Rf 755 /home/shijie
[root@localhost shijie]# ls -ld /home/shijie/
drwxr-xr-x. 15 shijie shijie 4096 May 23 04:37 /home/shijie/
使用http://127.0.0.1/~shijie就可以訪問個人主頁了。
4.個人主頁加密
使用htpasswd命令給個人主頁設置密碼,並將生成的密碼文件放到家目錄下。
[root@localhost shijie]# htpasswd -c /home/shijie/password shijie
New password:
Re-type new password:
Adding password for user shijie
編輯個人主頁配置文件,在最下方Directory中加入密碼驗證參數。
<Directory "/home/*/public_html">
AllowOverride all
authuserfile /home/shijie/password
authname "hello"
authtype basic
require user shijie
</Directory>
重啓服務後在訪問個人主頁時就需要輸入用戶和密碼了。輸入正確的密碼才能訪問個人主頁。
[root@localhost shijie]# systemctl restart httpd
5.虛擬網站服務器
虛擬網站主機是將一臺物理服務器邏輯上虛擬成多個網站,可以減少硬件資源的浪費。虛擬網站服務可以使用IP、域名和端口號來劃分虛擬服務器。
5.1基於IP
一臺服務器可以有多個IP,使用不同的IP綁定不同的網站,只要訪問綁定的IP就可以訪問相應的網站了。給服務器網卡增加IP,分別是192.168.10.10,192.168.10.11,192.168.10.12,重啓網卡後ping以下三個地址驗證是否配置成功。
[root@localhost shijie]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eno16777736
UUID=5305da6a-ee36-4f8e-bd14-985089fb8895
ONBOOT=yes
HWADDR=00:0C:29:04:A1:14
IPADDR0=192.168.10.10
PREFIX0=24
IPADDR1=192.168.10.11
PREFIX1=24
IPADDR2=192.168.10.12
PREFIX2=24
[root@localhost shijie]# systemctl restart network
在網站默認目錄中增加三個目錄分別對應三個IP。
[root@localhost shijie]# cd /var/www/html/
[root@localhost html]# mkdir 10 11 12
[root@localhost html]# ls
10 11 12
[root@localhost html]# echo 192.168.10.10 > 10/index.html
[root@localhost html]# echo 192.168.10.11 > 11/index.html
[root@localhost html]# echo 192.168.10.12 > 12/index.html
在網站配置文件中追加虛擬主機功能。
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
<VirtualHost 192.168.10.10>
DocumentRoot /var/www/html/10
<Directory /var/www/html/10>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.11>
DocumentRoot /var/www/html/11
<Directory /var/www/html/11>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.12>
DocumentRoot /var/www/html/12
<Directory /var/www/html/12>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost html]# systemctl restart httpd
輸入IP就可以訪問相對應的網站了。
5.2基於端口
基於端口號的虛擬主機功能可以讓用戶通過指定的端口號來訪問服務器上的網站資源。刪除上一節的目錄,創建三個端口分別對應的目錄。
[root@localhost html]# rm -rf 10 11 12
[root@localhost html]# mkdir 65510 65511 65512
[root@localhost html]# ls
65510 65511 65512
[root@localhost html]# echo 65510 > 65510/index.html
[root@localhost html]# echo 65511 > 65511/index.html
[root@localhost html]# echo 65512 > 65512/index.html
在網站配置文件中修改虛擬主機對應的IP、端口和對應的目錄,增加監聽端口,默認監聽80。
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
Listen 65510
Listen 65511
Listen 65512
<VirtualHost 192.168.10.10:65510>
DocumentRoot /var/www/html/65510
<Directory /var/www/html/65510>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.10:65511>
DocumentRoot /var/www/html/65511
<Directory /var/www/html/65511>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.10:65512>
DocumentRoot /var/www/html/65512
<Directory /var/www/html/65512>
allowOverride None
Require all granted
</Directory>
[root@localhost html]# systemctl restart httpd
使用http://192.168.10.10:65510就可以訪問對應端口的網站了。
5.3基於域名
創建三個網站數據的目錄分別對應三個域名shijie10.com、shijie11.com、shijie12.com。
[root@localhost html]# rm -rf 65510 65511 65512
[root@localhost html]# mkdir shijie11 shijie12 shijie10
[root@localhost html]# ll
total 0
drwxr-xr-x 2 root root 6 May 23 08:00 shijie10
drwxr-xr-x 2 root root 6 May 23 08:00 shijie11
drwxr-xr-x 2 root root 6 May 23 08:00 shijie12
[root@localhost html]# echo www.shijie11.com > shijie11/index.html
[root@localhost html]# echo www.shijie12.com > shijie12/index.html
[root@localhost html]# echo www.shijie10.com > shijie10/index.html
編輯配置文件,加入域名。
[root@localhost html]# vim /etc/httpd/conf/httpd.conf
<VirtualHost 192.168.10.10>
DocumentRoot /var/www/html/shijie10
ServerName www.shijie10.com
<Directory /var/www/html/shijie10>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.10>
DocumentRoot /var/www/html/shijie11
ServerName www.shijie11.com
<Directory /var/www/html/shijie11>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.10.10>
DocumentRoot /var/www/html/shijie12
ServerName www.shijie12.com
<Directory /var/www/html/shijie12>
allowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost html]# systemctl restart httpd
如果有DNS服務器,可以將這三個域名都綁定在192.168.10.10上,若沒有DNS服務器,就將域名和IP的對應關係寫在HOST文件中,可以在本機中使用域名訪問網站了。
[root@localhost html]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.10 www.shijie10.com www.shijie11.com www.shijie12.com
由於Linux服務器還未配置過DNS所以我配置了一臺windowsDNS服務器。可以讓局域網內全部可以使用域名訪問了。
使用域名訪問網站。