Apache虛機主機的三種配置方式
阿新 • • 發佈:2021-08-08
一、虛機主機的三種方式
1、基於IP
2、基於IP+埠
3、基於域名
官網文件:http://httpd.apache.org/docs/2.4/
二、安裝Apache
1、系統環境
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@localhost ~]# ip ad 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:5c:ff:91 brd ff:ff:ff:ff:ff:ff inet 192.168.253.128/24 brd 192.168.253.255 scope global dynamic eno16777736 valid_lft 1388sec preferred_lft 1388sec inet6 fe80::20c:29ff:fe5c:ff91/64 scope link valid_lft forever preferred_lft forever
2、yum安裝
[root@localhost ~]# yum install -y httpd ***** ====================================================================================================================================================== Package 架構 版本 源 大小 ====================================================================================================================================================== 正在安裝: httpd x86_64 2.4.6-80.el7.centos base 2.7 M 為依賴而安裝: apr x86_64 1.4.8-3.el7_4.1 base 103 k apr-util x86_64 1.5.2-6.el7 base 92 k httpd-tools x86_64 2.4.6-80.el7.centos base 89 k mailcap noarch 2.1.41-2.el7 base 31 k *** 已安裝: httpd.x86_64 0:2.4.6-80.el7.centos 作為依賴被安裝: apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-80.el7.centos mailcap.noarch 0:2.1.41-2.el7
完畢!
可以檢視安裝了內容
[root@localhost ~]# rpm -ql httpd | less
3、配置Selinux檔案,SELINUX=disabled。
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
SELINUXTYPE=targeted
或者臨時關閉
[root@localhost ~]# setenforce 0
4、關閉防火牆
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
5、啟動httpd服務,訪問測試
[root@localhost ~]# systemctl start httpd
三、修改主配置檔案
1、檢視apache主配置檔案,確保存在以下配置,因為等下需要在conf.d/建立虛機主機配置。
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
IncludeOptional conf.d/*.conf
2、另外,把 Require all denied預設拒絕訪問設定為允許訪問: Require all granted,方便測試。
<Directory />
AllowOverride none
# Require all denied
Require all granted
</Directory>
四、新增虛擬主機配置檔案
1、新增基於多個IP的虛擬主機
1)建立配置檔案
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
<VirtualHost 192.168.253.128:80>
ServerName a.com
DocumentRoot "/www/a.com/" #網頁路徑
</VirtualHost>
<VirtualHost 192.168.253.129:80>
ServerName b.com
DocumentRoot "/www/b.com/" #網頁路徑
</VirtualHost>
2)網絡卡繫結多個IP(我的網絡卡名是eno16777736,不是eth0)
[root@localhost conf.d]# ip addr add 192.168.253.129 dev eno16777736
[root@localhost conf.d]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:5c:ff:91 brd ff:ff:ff:ff:ff:ff
inet 192.168.253.128/24 brd 192.168.253.255 scope global dynamic eno16777736
valid_lft 1542sec preferred_lft 1542sec
inet 192.168.253.129/32 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe5c:ff91/64 scope link
valid_lft forever preferred_lft forever
3)建立虛機主機a.com和b.com的主頁面
[root@localhost conf.d]# mkdir -pv /www/{a.com,b.com}
mkdir: 已建立目錄 "/www"
mkdir: 已建立目錄 "/www/a.com"
mkdir: 已建立目錄 "/www/b.com"
[root@localhost conf.d]# vim /www/a.com/index.html
<h1>Hello,a.com</h1>
[root@localhost conf.d]# vim /www/b.com/index.html
<h1>Hello,b.com</h1>
4)檢查配置檔案是否正常
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
5)重啟httpd服務
[root@localhost conf.d]# systemctl restart httpd
2、配置基於IP+埠的虛擬主機
1)建立配置檔案
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
<VirtualHost 192.168.253.128:80>
ServerName a.com
DocumentRoot "/www/a.com/"
</VirtualHost>
<VirtualHost 192.168.253.128:8080>
ServerName b.com
DocumentRoot "/www/b.com/"
</VirtualHost>
2)修改httpd主配置檔案,在Listen 80下面新增一行監控8080埠
[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf
Listen 8080
3)建立虛機主機a.com和b.com的主頁面(如果前面已經建立就不用重複)
[root@localhost conf.d]# mkdir -pv /www/{a.com,b.com}
mkdir: 已建立目錄 "/www"
mkdir: 已建立目錄 "/www/a.com"
mkdir: 已建立目錄 "/www/b.com"
[root@localhost conf.d]# vim /www/a.com/index.html
<h1>Hello,a.com</h1>
[root@localhost conf.d]# vim /www/b.com/index.html
<h1>Hello,b.com</h1>
4)檢查配置檔案
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
5)重啟httpd服務
[root@localhost conf.d]# systemctl restart httpd
3、基於域名的虛擬主機
1)建立配置檔案
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
<VirtualHost *:80>
ServerName a.com
DocumentRoot "/www/a.com/"
</VirtualHost>
<VirtualHost *:80>
ServerName b.com
DocumentRoot "/www/b.com/"
</VirtualHost>
2)修改物理主機hosts檔案(C:\Windows\System32\drivers\etc),因為這裡是因為物理機去訪問Apache伺服器
新增:
192.168.253.128 a.com
192.168.253.128 b.com
3)物理主機ping域名測試
4)建立虛機主機a.com和b.com的主頁面(如果前面已經建立就不用重複)
[root@localhost conf.d]# mkdir -pv /www/{a.com,b.com}
mkdir: 已建立目錄 "/www"
mkdir: 已建立目錄 "/www/a.com"
mkdir: 已建立目錄 "/www/b.com"
[root@localhost conf.d]# vim /www/a.com/index.html
<h1>Hello,a.com</h1>
[root@localhost conf.d]# vim /www/b.com/index.html
<h1>Hello,b.com</h1>
5)檢查配置檔案
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
6)重啟httpd服務
[root@localhost conf.d]# systemctl restart httpd
五、擴充套件虛機主機配置檔案
1、先修改回apache主配置檔案,設定 Require all denied預設拒絕訪問
<Directory />
AllowOverride none
Require all denied
</Directory>
這時候再去訪問以上的三種配置虛機主機,會全部訪問不了。因此需要針對虛機目錄設定訪問許可權。
2、修改虛擬主機配置檔案
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
<VirtualHost *:80>
#繫結的主域
ServerName a.com
#繫結的子域名
ServerAlias www.test.com
#網站主目錄
DocumentRoot "/www/a.com/"
#錯誤日誌目錄
ErrorLog "/var/log/httpd/a.com/error.log"
#訪問日誌目錄
CustomLog "/va/log/httpd/a.com.access.log"
<Directory "www/a.com/">
Options FollowSymLinks
AllowOverride All
#允許任意訪問
Require all granted
</Directory>
</VirtualHost>
3、建立日誌目錄
[root@localhost b.com]# cd /var/log/httpd/
[root@localhost httpd]# mkdir a.com
[root@localhost httpd]# ll
總用量 60
-rw-r--r--. 1 root root 37976 1月 23 22:26 access_log
drwxr-xr-x. 2 root root 6 1月 23 22:41 a.com
-rw-r--r--. 1 root root 17795 1月 23 22:38 error_log
[root@localhost httpd]# cd a.com/
[root@localhost a.com]# touch error.log
[root@localhost a.com]# touch access.log
日誌目錄記得更改屬主和屬組為Apache,否則httpd啟動失敗
[root@localhost httpd]# chown -R apache:apache a.com/
4、配置指定IP可以訪問虛擬主機(可以單個IP,也可以是一個網段)
[root@localhost conf.d]# vim /etc/httpd/conf.d/virtual.conf
<VirtualHost *:80>
#繫結的主域
ServerName a.com
#繫結的子域名
ServerAlias www.test.com
#網站主目錄
DocumentRoot "/www/a.com/"
#錯誤日誌目錄
ErrorLog "/var/log/httpd/a.com/error.log"
#訪問日誌目錄
CustomLog "/va/log/httpd/a.com.access.log"
<Directory "www/a.com/">
Options FollowSymLinks
AllowOverride All
#允許任意訪問
Require ip 192.168.253.0/24
</Directory>
5、配置指定使用者可以訪問虛擬主機
[root@localhost conf.d]# vim /etc/httpd/conf.d/virtual.conf
<VirtualHost *:80>
#繫結的主域
ServerName a.com
#繫結的子域名
ServerAlias www.test.com
#網站主目錄
DocumentRoot "/www/a.com/"
#錯誤日誌目錄
ErrorLog "/var/log/httpd/a.com/error.log"
#訪問日誌目錄
CustomLog "/va/log/httpd/a.com.access.log"
<Directory "www/a.com/">
Options FollowSymLinks
AllowOverride authconfig
AuthType basic
AuthName "Restrict area"
AuthUserFile "etc/httpd/.htpasswd"
Require valid-user
</Directory>
建立使用者檔案,第一次建立的時候要加-c,以後建立都不用加-c,否則會覆蓋原資料
[root@localhost conf.d]# pwd
/etc/httpd/conf.d
[root@localhost conf.d]# htpasswd -h
htpasswd: illegal option -- h
Usage:
htpasswd [-cimBdpsDv] [-C cost] passwordfile username
htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password
htpasswd -n[imBdps] [-C cost] username
htpasswd -nb[mBdps] [-C cost] username password
-c Create a new file.
-n Don't update file; display results on stdout.
-b Use the password from the command line rather than prompting for it.
-i Read password from stdin without verification (for script usage).
-m Force MD5 encryption of the password (default).
-B Force bcrypt encryption of the password (very secure).
-C Set the computing time used for the bcrypt algorithm
(higher is more secure but slower, default: 5, valid: 4 to 31).
-d Force CRYPT encryption of the password (8 chars max, insecure).
-s Force SHA encryption of the password (insecure).
-p Do not encrypt the password (plaintext, insecure).
-D Delete the specified user.
-v Verify password for the specified user.
On other systems than Windows and NetWare the '-p' flag will probably not work.
The SHA algorithm does not use a salt and is less secure than the MD5 algorithm.
[root@localhost conf.d]# htpasswd -c -m /etc/httpd/.htpasswd tom
New password:
Re-type new password:
Adding password for user tom
重啟httpd服務
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@localhost conf.d]# systemctl restart httpd
6、Require參考
Require all granted
無條件允許訪問。
Require all denied
訪問被無條件拒絕。
Require env env-var [env-var] ...
只有在給定的環境變數之一被設定的情況下才允許訪問。
Require method http-method [http-method] ...
只有給定的HTTP方法才允許訪問。
Require expr expression
如果表示式計算結果為true,則允許訪問。
Require user userid [userid] ...
只有指定的使用者才能訪問資源。
Require group group-name [group-name] ...
只有指定組中的使用者才能訪問資源。
Require valid-user
所有有效的使用者都可以訪問資源。
Require ip 10 172.20 192.168.2
指定IP地址範圍內的客戶端可以訪問資源。
``
##7、Options
None:不支援任何選項
Indexes:允許索引目錄
FollowSymLinks:允許訪問符號連結指向的原檔案
Includes:允許執行服務端包含(SSI)
ExecCGI:允許允許CGI指令碼
ALL:支援所有選項