天天看點

httpd服務的配置httpd服務的配置一、環境準備二、認識配置檔案三、虛拟Web主機

httpd服務的配置

一、環境準備

我們需要兩台虛拟機,而且都需要關閉防火牆和selinux

二、認識配置檔案

// 虛拟機A:建構基本Web服務   
// 我們需要先裝包
]# yum -y install httpd
]# rpm  -q  httpd
// 先進行簡單的測試
// /var/www/html/index.html ,其中index.html是我們預設的首頁
// /var/www/html 是我們預設的網頁根目錄
]# echo test >    /var/www/html/index.html
]# systemctl start httpd       #啟動服務
]# curl  http://10.0.0.200    #測試通路
test
// 主配置檔案:/etc/httpd/conf/httpd.conf
// 我們打開主配置檔案
[root@tk ~]# cat /etc/httpd/conf/httpd.conf
// .... 
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
// 該處的Listen表示httpd服務監聽的端口,我們可以配置多個Listen
Listen 80
// ... 
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
// 下面是httpd服務需要使用的使用者群組
User apache
Group apache
// ...
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
// 該處的<Directory /> 裡面寫的是我們的web通路規則
// Directory 後面寫的是網頁存放路徑,而且該規則是會繼承的
// Require all denied     #拒絕所有人通路
//  Require all granted      #允許所有人通路
# <Directory> blocks below.
#
<Directory />
    AllowOverride none
    Require all denied
</Directory>
// ... 
# 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  表示網頁的根目錄,我們通路web的時候,此時就會到該目錄下尋找index.html
DocumentRoot "/var/www/html"

// ...
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
// 該處是我們的錯誤日志的目錄
ErrorLog "logs/error_log"

//....           

三、虛拟Web主機

//  虛拟Web主機
//  由同一台伺服器提供多個不同的Web站點
//  區分方式
//  基于域名的虛拟主機
//  基于端口的虛拟主機
//  基于IP位址的虛拟主機
// 配置檔案路徑
//  /etc/httpd/conf/httpd.conf  #主配置檔案
//  /etc/httpd/conf.d/*.conf   #調用配置檔案

// 為每個虛拟站點添加配置
// <VirtualHost   IP位址:端口>
//       ServerName  此站點的DNS名稱
//       DocumentRoot  此站點的網頁根目錄
// </VirtualHost>

// 基于域名
[root@tk ~]# cat /etc/httpd/conf.d/test1.conf
<VirtualHost *:80>
ServerName www.test1.com
DocumentRoot /var/www/test1
</VirtualHost>
// 修改nds解析,解析到本機
[root@tk ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6q
10.0.0.200 www.test1.com
// 書寫測試的html
[root@tk ~]# echo "test1" > /var/www/test1/index.html
// 重新開機服務
[root@tk ~]# systemctl restart httpd
// 測試通路
[root@tk ~]# curl http://www.test1.com
test1
[root@tk ~]#

// 基于端口的虛拟Web主機
// 我們隻需要将端口修改即可
[root@tk ~]# cat /etc/httpd/conf.d/test1.conf
<VirtualHost *:80>
ServerName www.test1.com
DocumentRoot /var/www/test1
</VirtualHost>
<VirtualHost *:8080>
ServerName www.test1.com
DocumentRoot /var/www/test2
</VirtualHost>

[root@tk ~]#
// 注意:此時要在我們的主配置檔案中開啟8080端口
[root@tk ~]# mkdir /var/www/test2
[root@tk ~]# echo "test1:8080" > /var/www/test2/index.html
[root@tk ~]# systemctl restart httpd
test1:8080

// 基于IP進行配置

[root@tk ~]# cat /etc/httpd/conf.d/test1.conf
<VirtualHost *:80>
ServerName 10.0.0.200
DocumentRoot /var/www/test1
</VirtualHost>
<VirtualHost *:8080>
ServerName 10.0.0.201
DocumentRoot /var/www/test2
</VirtualHost>
[root@tk ~]#
[root@tk ~]# systemctl restart httpd
[root@tk ~]# curl http://10.0.0.200
test1
[root@tk ~]# curl http://10.0.0.201
test1
[root@tk ~]# curl http://10.0.0.201:8080
test1:8080

           

繼續閱讀