編譯Apache服務部署靜態網站

Apache是世界使用排名第一的Web服務器軟件,它能夠運行在幾乎全部普遍使用的計算機平臺上,因爲其跨平臺和安全性被普遍使用,是最流行的Web服務器端軟件之一.它快速、可靠而且可經過簡單的API擴充,將Perl/Python等解釋器編譯到服務器中.同時Apache音譯爲阿帕奇,是北美印第安人的一個部落,叫阿帕奇族,在美國的西南部.也是一個基金會的名稱.html

編譯安裝Apache

1.配置Yum倉庫,安裝Apache所依賴的包文件,以及編譯器.web

[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum install -y gcc openssl openssl-devel zlib zlib-devel pcre pcre-devel expat-devel libxml2-devel

2.安裝Apr-1.6.3,主要爲上層的應用程序提供一個能夠跨越多操做系統平臺使用的底層支持接口庫.apache

[root@localhost ~]# wget http://www-eu.apache.org/dist//apr/apr-1.6.3.tar.gz
[root@localhost ~]# tar -xzvf apr-1.6.3.tar.gz
[root@localhost ~]# cd apr-1.6.3/
[root@localhost ~]# CC="gcc -m64" ./configure --prefix=/usr/local/apr
[root@localhost ~]# ./configure --prefix=/usr/local/apr
[root@localhost ~]# make && make install

3.安裝Apr-util-1.6.1,是包含了一些經常使用的開發組件,這些組件與apache的關係更加密切一些,好比存儲段和存儲段組,加密等.vim

[root@localhost ~]# wget http://www-eu.apache.org/dist//apr/apr-util-1.6.1.tar.gz
[root@localhost ~]# tar -xzvf apr-util-1.6.1.tar.gz
[root@localhost ~]# cd apr-util-1.6.1/
[root@localhost ~]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost ~]# make && make install

4.安裝Apache-2.4.33瀏覽器

[root@localhost ~]# wget http://www-eu.apache.org/dist//httpd/httpd-2.4.33.tar.gz
[root@localhost ~]# tar -xzvf httpd-2.4.33.tar.gz
[root@localhost ~]# cd httpd-2.4.33/
[root@localhost ~]# ./configure --prefix=/usr/local/apache2 \
--enable-rewrite \
--enable-so \
--enable-headers \
--enable-expires \
--with-mpm=worker \
--enable-modules=most \
--enable-deflate \
--enable-ssl \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--with-pcre=/usr/local/pcre
[root@localhost ~]# make && make install


配置Apache訪問控制

Apache能夠基於原主機名,原IP地址,或原主機上的瀏覽器特徵,對網站上的資源進行訪問控制,它經過Allow指令容許某個主機訪問服務器上的網站資源,經過Deny指令實現禁止訪問,還能夠給指定的頁面添加密碼認證.安全

◆基於用戶名密碼的認證◆

做用:當咱們打開指定網頁時,會提示須要輸入密碼才能訪問,這就是密碼認證技術.bash

1.編輯Apache主配置文件,在相應的區域中加入如下標★語句.服務器

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

235     # AllowOverride controls what directives may be placed in .htaccess files.
236     # It can be "All", "None", or any combination of the keywords:
237     #   AllowOverride FileInfo AuthConfig Limit
238     #
★      AllowOverride all        #修改成 AllowOverride all
240     #AllowOverride 表示是否去找.htacess文件做爲配置文件
241     #
242     # Controls who can get stuff from this server.

2.在要添加認證的網頁文件下建立 .htaccess 文件,並覆蓋寫入如下內容.dom

[root@localhost ~]# vim /usr/local/apache2/htdocs/.htaccess

authname  "welcome to admin"                                            #歡迎提示信息
authtype basic                                                          #認證類型
authuserfile /usr/local/apache2/htdocs/login.psd                        #認證文件存放位置
require valid-user                                                      #除認證用戶其餘用戶不容許登錄

3.藉助Apache的工具生成密碼文件,此處的用戶名密碼就是訪問網頁時的號碼.ide

[root@localhost ~]# /usr/local/apache2/bin/htpasswd  -c  /usr/local/apache2/htdocs/login.psd   LyShark  #建立認證用戶(覆蓋)
[root@localhost ~]# /usr/local/apache2/bin/htpasswd  -m  /usr/local/apache2/htdocs/login.psd   LyShark  #寫入認證用戶(追加)

4.重啓Apache服務,並訪問頁面測試便可.

[root@localhost ~]# /usr/local/apache2/bin/apachectl restart

◆基於IP地址的身份認證◆

做用:當咱們打開指定網頁時,會判斷您的IP地址是容許訪問仍是拒絕訪問,這就是基於IP的認證技術

◆容許個別IP地址訪問網頁,拒絕全部◆

1.編輯Apache主配置文件,在相應的區域中加入如下標★語句,在 嵌套裏寫入如下內容

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

234     #
235     # AllowOverride controls what directives may be placed in .htaccess files.
236     # It can be "All", "None", or any combination of the keywords:
237     #   AllowOverride FileInfo AuthConfig Limit
238     #
239     AllowOverride None
240 
★ <requireall>
★         require all granted
★         require ip 192.168.1.10                                           #容許訪問的IP地址
★ </requireall>
245 
246     #
247     # Controls who can get stuff from this server.
248     #
249     #Require all granted                            #註釋掉
250 </Directory>

2.重啓Apache服務

[root@localhost ~]# /usr/local/apache2/bin/apachectl restart

◆拒絕個別IP地址訪問網頁,容許全部◆

1.編輯Apache主配置文件,在相應的區域中加入如下標★語句,在 嵌套裏寫入如下內容

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

235     # AllowOverride controls what directives may be placed in .htaccess files.
236     # It can be "All", "None", or any combination of the keywords:
237     #   AllowOverride FileInfo AuthConfig Limit
238     #
239     AllowOverride None
240 <requireall>
241         require all granted
242         require not ip 192.168.1.10                                         #拒絕訪問的IP地址
243 </requireall>
244     #
245     # Controls who can get stuff from this server.
246     #
247     #Require all granted                            #註釋掉
248 </Directory>

2.重啓Apache服務

[root@localhost ~]# /usr/local/apache2/bin/apachectl restart


配置Apache目錄別名

有時候一個網頁目錄過深,每次輸入地址過長,此時咱們可使用別名的方式來減少目錄深度.

1.編輯Apache主配置文件,在配置文件行尾,追加寫入如下標★語句

[root@localhost ~]# vim  /usr/local/apache2/conf/httpd.conf

496 # Note: The following must must be present to support
497 #       starting without SSL on platforms with no /dev/random equivalent
498 #       but a statically compiled-in mod_ssl.
499 #
500 <IfModule ssl_module>
501 SSLRandomSeed startup builtin
502 SSLRandomSeed connect builtin
503 </IfModule>
504 
★ alias "/lyshark" "/usr/local/apache2/htdocs/a/b/c/d"              #地址簡化

#以上語句的含義:將訪問路徑由 http://IP地址/a/b/c/d 簡化爲 http://IP地址/lyshark

2.重啓Apache服務

[root@localhost ~]# /usr/local/apache2/bin/apachectl restart


配置Apache虛擬主機

利用虛擬主機功能,能夠把一臺處於運行狀態的物理服務器分割成多個,虛擬服務器,出於各類考慮目前各類企業都在使用虛擬主機功能,Apache虛擬主機功能,是服務器基於用戶的請求的不一樣Ip地址,主機域名或端口號,實現提供多個網站同時爲外部提供訪問服務的技術,用戶取得的資源不一樣最後取得的頁面也會不一樣.

◆基於IP的虛擬主機◆

若是一臺服務器有多個IP地址,並且每一個IP地址與服務器上部署的每一個網站對應,這樣當用戶請求訪問不一樣的IP時,會訪問到不一樣網站的頁面資源,並且每一個網站都有一個獨立的IP地址,如下實驗將實如今一臺服務器上配置多個IP,搭建多個網站,每一個網站使用一個IP地址.

1.在eno16777728上配置一個網卡子接口

[root@localhost]# ifconfig eno16777728:0 192.168.1.13  netmask 255.255.255.0

[root@localhost]# ifconfig

eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.12  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fe1e:14e2  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:1e:14:e2  txqueuelen 1000  (Ethernet)
        RX packets 40292  bytes 4129804 (3.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8962  bytes 1557264 (1.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777728:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.13  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:1e:14:e2  txqueuelen 1000  (Ethernet)

2.編輯主配置文件,開啓虛擬主機選項(取消註釋),在相應的區域中取消如下標★註釋

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

475 
476 # Virtual hosts
★  Include conf/extra/httpd-vhosts.conf             #取消本行註釋,開啓虛擬主機模塊
478 
479 # Local access to the Apache HTTP Server Manual

3.修改虛擬主機配置文件添加虛擬主機,在相應的區域中修改如下標★語句

[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

18 # VirtualHost example:
19 # Almost any Apache directive may go into a VirtualHost container.
20 # The first VirtualHost section is used for all requests that do not
21 # match a ServerName or ServerAlias in any <VirtualHost> block.
22 #
★ <VirtualHost 192.168.1.12:80>
24     ServerAdmin webmaster@dummy-host.example.com
★     DocumentRoot "/usr/local/apache2/htdocs/vhost1"
26     ServerName dummy-host.example.com
27     ServerAlias www.dummy-host.example.com
28     ErrorLog "logs/dummy-host.example.com-error_log"
29     CustomLog "logs/dummy-host.example.com-access_log" common
30 </VirtualHost>
31 
★ <VirtualHost 192.168.1.13:80>
33     ServerAdmin webmaster@dummy-host2.example.com
★     DocumentRoot "/usr/local/apache2/htdocs/vhost2"
35     ServerName dummy-host2.example.com
36     ErrorLog "logs/dummy-host2.example.com-error_log"
37     CustomLog "logs/dummy-host2.example.com-access_log" common
38 </VirtualHost>

4.分別建立 vhost1 和 vhost2 目錄,並放入兩個index.html文件

[root@localhost ~]# mkdir /usr/local/apache2/htdocs/vhost1
[root@localhost ~]# mkdir /usr/local/apache2/htdocs/vhost2

[root@localhost ~]# echo "vhost1 test page" > /usr/local/apache2/htdocs/vhost1/index.html
[root@localhost ~]# echo "vhost2 test page" > /usr/local/apache2/htdocs/vhost2/index.html

5.重啓Apache服務,測試效果,兩個IP分別對應兩個網站主頁

[root@localhost ~]# /usr/local/apache2/bin/apachectl restart

◆基於端口的虛擬主機◆

基於端口的虛擬主機,可讓用戶經過端口號,來訪問服務器上的資源,在使用Apache配置虛擬網站時,基於端口的配置方式最爲複雜,如下實驗將實如今一臺服務器上配置多個端口,搭建多個網站,每一個網站使用一個端口.

1.編輯主配置文件,開啓虛擬主機選項(取消註釋),在相應的區域中取消如下標★註釋

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

475 
476 # Virtual hosts
★  Include conf/extra/httpd-vhosts.conf             #取消本行註釋,開啓虛擬主機模塊
478 
479 # Local access to the Apache HTTP Server Manual

2.修改虛擬主機配置文件添加虛擬主機,在相應的區域中修改如下標★語句

[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

18 # VirtualHost example:
19 # Almost any Apache directive may go into a VirtualHost container.
20 # The first VirtualHost section is used for all requests that do not
21 # match a ServerName or ServerAlias in any <VirtualHost> block.
22 #
★ <VirtualHost 192.168.1.12:80>
24     ServerAdmin webmaster@dummy-host.example.com
★     DocumentRoot "/usr/local/apache2/htdocs/vhost1"
26     ServerName dummy-host.example.com
27     ServerAlias www.dummy-host.example.com
28     ErrorLog "logs/dummy-host.example.com-error_log"
29     CustomLog "logs/dummy-host.example.com-access_log" common
30 </VirtualHost>
31 
★ <VirtualHost 192.168.1.12:8080>
33     ServerAdmin webmaster@dummy-host2.example.com
★     DocumentRoot "/usr/local/apache2/htdocs/vhost2"
35     ServerName dummy-host2.example.com
36     ErrorLog "logs/dummy-host2.example.com-error_log"
37     CustomLog "logs/dummy-host2.example.com-access_log" common
38 </VirtualHost>

3.修改Apache主配置文件,添加兩個端口監聽

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

 48 # Change this to Listen on specific IP addresses as shown below to 
 49 # prevent Apache from glomming onto all bound IP addresses.
 50 #
 51 #Listen 12.34.56.78:80
★ Listen 80
★ Listen 8080

4.分別建立 vhost1 和 vhost2 目錄,並放入兩個index.html文件

[root@localhost ~]# mkdir /usr/local/apache2/htdocs/vhost1
[root@localhost ~]# mkdir /usr/local/apache2/htdocs/vhost2

[root@localhost ~]# echo "vhost1 test page" > /usr/local/apache2/htdocs/vhost1/index.html
[root@localhost ~]# echo "vhost2 test page" > /usr/local/apache2/htdocs/vhost2/index.html

5.重啓Apache服務,測試效果,兩個端口分別對應兩個網站主頁

[root@localhost ~]# usr/local/apache2/bin/apachectl restart

◆基於域名的虛擬主機◆

當服務器沒法爲每個網站分配一個獨立的IP的時候,能夠嘗試讓Apache自動識別用戶請求的域名,從而根據不一樣的域名請求來傳輸不一樣的內容,這裏咱們爲了驗證明驗要手動搭建一個DNS解析,如下實驗將實如今一臺服務器上多個域名,搭建多個網站,每一個網站使用一個域名.

1.首先搭建DNS域名解析,模擬vhost1.com與vhost2.com兩個網站域名.

[root@localhost ~]# yum install -y bind bind-chroot
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager.
Package 32:bind-9.9.4-61.el7.x86_64 already installed and latest version
Package 32:bind-chroot-9.9.4-61.el7.x86_64 already installed and latest version
Nothing to do

2.配置DNS解析,這裏咱們簡單配置便可,有關DNS詳細例子請查看其餘相關文章.

[root@localhost ~]# vim /etc/named.conf

 12 options {
 13         listen-on port 53 { any; };
 14         listen-on-v6 port 53 { ::1; };
 15         directory       "/var/named";
 16         dump-file       "/var/named/data/cache_dump.db";
 17         statistics-file "/var/named/data/named_stats.txt";
 18         memstatistics-file "/var/named/data/named_mem_stats.txt";
 19         allow-query     { any; };

[root@localhost ~]# vim /etc/named.rfc1912.zones

 43 zone "vhost1.com" IN {
 44         type master;
 45         file "vhost1.com.zone";
 46         allow-update { none; };
 47 };
 48 zone "vhost2.com" IN {
 49         type master;
 50         file "vhost2.com.zone";
 51         allow-update { none; };
 52 };

3.拷貝配置文件,並修改爲如下模樣,並重啓Bind

[root@localhost ~]# cp -a /var/named/named.localhost /var/named/vhost1.com.zone
[root@localhost ~]# cp -a /var/named/named.localhost /var/named/vhost2.com.zone

[root@localhost ~]# vim /var/named/vhost1.com.zone
$TTL 1D
@       IN SOA  dns.vhost1.com. rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      dns.vhost1.com.
dns     A       127.0.0.1
www     A       192.168.1.10

[root@localhost ~]# vim /var/named/vhost2.com.zone
$TTL 1D
@       IN SOA  dns.vhost2.com. rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      dns.vhost2.com.
dns     A       127.0.0.1
www     A       192.168.1.10

[root@localhost ~]# systemctl restart named

4.編輯Apache主配置文件,開啓虛擬主機選項(取消註釋),在相應的區域中取消如下標★註釋

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

475 
476 # Virtual hosts
★  Include conf/extra/httpd-vhosts.conf             #取消本行註釋,開啓虛擬主機模塊
478 
479 # Local access to the Apache HTTP Server Manual

5.修改虛擬主機配置文件添加虛擬主機,在相應的區域中修改如下標★語句

[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

18 # VirtualHost example:
19 # Almost any Apache directive may go into a VirtualHost container.
20 # The first VirtualHost section is used for all requests that do not
21 # match a ServerName or ServerAlias in any <VirtualHost> block.
22 #
23 <VirtualHost *:80>
24     ServerAdmin webmaster@dummy-host.example.com
★     DocumentRoot "/usr/local/apache2/htdocs/vhost1"
★     ServerName www.vhost1.com                 #vhost1.com解析到vhost1目錄下
27     ServerAlias www.dummy-host.example.com
28     ErrorLog "logs/dummy-host.example.com-error_log"
29     CustomLog "logs/dummy-host.example.com-access_log" common
30 </VirtualHost>
31 
32 <VirtualHost *:80>
33     ServerAdmin webmaster@dummy-host2.example.com
★     DocumentRoot "/usr/local/apache2/htdocs/vhost2"
★     ServerName www.vhost2.com                 #vhost2.com解析到vhost2目錄下
36     ErrorLog "logs/dummy-host2.example.com-error_log"
37     CustomLog "logs/dummy-host2.example.com-access_log" common
38 </VirtualHost>

6.分別建立 vhost1 和 vhost2 目錄,並放入兩個index.html文件

[root@localhost ~]# mkdir /usr/local/apache2/htdocs/vhost1
[root@localhost ~]# mkdir /usr/local/apache2/htdocs/vhost2

[root@localhost ~]# echo "vhost1 test page" > /usr/local/apache2/htdocs/vhost1/index.html
[root@localhost ~]# echo "vhost2 test page" > /usr/local/apache2/htdocs/vhost2/index.html

7.重啓Apache服務,測試效果,兩個域名分別對應兩個網站主頁

[root@localhost ~]# /usr/local/apache2/bin/apachectl restart


配置Apache我的主頁

若是想爲每一個系統獨立的用戶創建一個網站,一般狀況先是基於虛擬主機的功能來部署多個網站,可是這樣工做量實在太大,還好Apache爲咱們提供了我的主頁功能,如下實驗將實現給予不一樣的用戶一個單獨的網頁空間,實現每一個人能夠有本身的空間,相似QQ空間.

1.編輯主配置文件,開啓我的主頁選項(取消註釋),在相應的區域中取消如下標★註釋.

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

149 #LoadModule speling_module modules/mod_speling.so
★  LoadModule userdir_module modules/mod_userdir.so         #取消註釋(約在150行)
151 LoadModule alias_module modules/mod_alias.so
.....
470 # User home directories
★  Include conf/extra/httpd-userdir.conf                #取消註釋(約在471行)
472 
473 # Real-time info on requests and configuration

[root@localhost ~]# cat /usr/local/apache2/conf/extra/httpd-userdir.conf

2.建立用戶並設置密碼.

[root@localhost ~]# useradd lyshark
[root@localhost ~]# echo "123123" |passwd --stdin lyshark
Changing password for user lyshark.
passwd: all authentication tokens updated successfully.

3.進入lyshark用戶家目錄,建立相應文件,並賦予權限.

[root@localhost ~]# mkdir -p /home/lyshark/public_html                  #在用戶家目錄建立public_html
[root@localhost ~]# echo "this is lyshark web" > /home/lyshark/public_html/index.html   #建立測試頁

[root@localhost ~]# chmod 755 /home/lyshark/public_html/index.html          #賦予執行權限
[root@localhost ~]# chmod 755 /home/lyshark                     #賦予執行權限

4.重啓Apache服務,並進入瀏覽器輸入如下格式進行驗證.

[root@localhost ~]# /usr/local/apache2/bin/apachectl restart
[root@localhost ~]# elinks http://127.0.0.1/~lyshark


啓用Apache地址跳轉

在有些之後咱們須要跳轉一些頁面,好比說今天搞活動咱們要把之前的頁面替換掉讓其暫時跳轉到活動頁面上來,恰巧Apache提供這樣的域名跳轉的配置服務,下面實驗將實現當用戶訪問 vhost1.com 時會自動跳轉到 vhost2.com 網站上來.

1.首先搭建DNS域名解析,模擬vhost1.com與vhost2.com兩個網站域名.

[root@localhost ~]# yum install -y bind bind-chroot
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager.
Package 32:bind-9.9.4-61.el7.x86_64 already installed and latest version
Package 32:bind-chroot-9.9.4-61.el7.x86_64 already installed and latest version
Nothing to do

2.配置DNS解析,這裏咱們簡單配置便可,有關DNS詳細例子請查看其餘相關文章.

[root@localhost ~]# vim /etc/named.conf

 12 options {
 13         listen-on port 53 { any; };
 14         listen-on-v6 port 53 { ::1; };
 15         directory       "/var/named";
 16         dump-file       "/var/named/data/cache_dump.db";
 17         statistics-file "/var/named/data/named_stats.txt";
 18         memstatistics-file "/var/named/data/named_mem_stats.txt";
 19         allow-query     { any; };

[root@localhost ~]# vim /etc/named.rfc1912.zones

 43 zone "vhost1.com" IN {
 44         type master;
 45         file "vhost1.com.zone";
 46         allow-update { none; };
 47 };
 48 zone "vhost2.com" IN {
 49         type master;
 50         file "vhost2.com.zone";
 51         allow-update { none; };
 52 };

3.拷貝配置文件,並修改爲如下模樣,並重啓Bind

[root@localhost ~]# cp -a /var/named/named.localhost /var/named/vhost1.com.zone
[root@localhost ~]# cp -a /var/named/named.localhost /var/named/vhost2.com.zone

[root@localhost ~]# vim /var/named/vhost1.com.zone
$TTL 1D
@       IN SOA  dns.vhost1.com. rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      dns.vhost1.com.
dns     A       127.0.0.1
www     A       192.168.1.10

[root@localhost ~]# vim /var/named/vhost2.com.zone
$TTL 1D
@       IN SOA  dns.vhost2.com. rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      dns.vhost2.com.
dns     A       127.0.0.1
www     A       192.168.1.10

[root@localhost ~]# systemctl restart named

4.編輯主配置文件開啓相應功能(取消註釋)

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

145 LoadModule vhost_alias_module modules/mod_vhost_alias.so        #開啓虛擬主機(約在145行)
152 LoadModule rewrite_module modules/mod_rewrite.so            #開啓網頁重寫(約在152行)
.....
476 # Virtual hosts
477 Include conf/extra/httpd-vhosts.conf                #開啓虛擬主機(約在477行)

5.改寫虛擬主機配置文件,在相應的區域中取消如下標★註釋

[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

18 # VirtualHost example:
19 # Almost any Apache directive may go into a VirtualHost container.
20 # The first VirtualHost section is used for all requests that do not
21 # match a ServerName or ServerAlias in any <VirtualHost> block.
22 #
23 <VirtualHost *:80>
★     DocumentRoot "/usr/local/apache2/htdocs/vhost1"           #vhost1網頁存放位置
★     ServerName www.vhost1.com                     #域名vhost1.com
26 </VirtualHost>
27 
★ <Directory "/usr/local/apache2/htdocs/vhost1">            #vhost1頁面的路徑
★ Options indexes followsymlinks
★ Allowoverride all
★ Require all granted
★ </Directory>
33 
34 <VirtualHost *:80>
★     DocumentRoot "/usr/local/apache2/htdocs/vhost2"           #vhost2網頁存放位置
★     ServerName www.vhost2.com                     #域名vhost2.com
37 </VirtualHost>

6.分別建立網頁文件存儲位置,和測試頁

[root@localhost ~]# mkdir -p /usr/local/apache2/htdocs/vhost1
[root@localhost ~]# mkdir -p /usr/local/apache2/htdocs/vhost2

[root@localhost ~]# echo "vhost1 server" > /usr/local/apache2/htdocs/vhost1/index.html
[root@localhost ~]# echo "vhost2 server" > /usr/local/apache2/htdocs/vhost2/index.html

7.在要跳轉的網頁目錄下建立 .htaccess 文件 (本例中應在vhost1目錄下建立),覆蓋寫入如下內容便可

[root@localhost ~]# vim /usr/local/apache2/htdocs/vhost1/.htaccess

rewriteengine on                            #開啓跳轉功能
rewritecond %{HTTP_HOST} www.vhost1.com                 #將www.vhost1.com
rewriterule .* http://www.vhost2.com                    #跳轉到www.vhost2.com

8.重啓Apache服務,從新加載配置,如今訪問 vhost1.com 會跳轉到 vhost2.com 目錄下

[root@localhost ~]# /usr/local/apache2/bin/apachectl restart


啓用https SSL加密傳輸

在生產環境中,咱們須要讓咱們的網站更加的安全,多數狀況下咱們會開啓https的加密認證,來保證數據傳輸的安全性,下面實驗將實現啓動SSL證書,也就是https認證,端口爲443.

1.創建服務器私鑰,過程當中須要輸入密碼

[root@localhost ~]# openssl genrsa -des3 -out server.key 1024

----------------------------------------------------------------------------
註釋:
        Genrsa –des3                    #加密類型
        -out    server.key              #輸出文件
        -1024                       #加密長度
----------------------------------------------------------------------------

2.創建證書,生成的csr文件交給CA簽名後造成服務端本身的證書

[root@localhost ~]# openssl req -new -key server.key -out server.csr

----------------------------------------------------------------------------
註釋:
        req -new                #新建證書
        -key server.key             #私鑰文件
        -out server.csr             #輸出文件

注:依次輸入:國家 省 市 組織 機構 全稱 EMAIL 是否要改變密碼 是否更名稱
----------------------------------------------------------------------------

3.轉化成證書,這一步由證書CA機構來作的,這裏只是實驗.

[root@localhost ~]# openssl x509 -req -days 365 -sha256 -in server.csr -signkey server.key -out servernew.crt

4.配置服務器讓其支持https

[root@localhost ~]# cp -a servernew.crt /usr/local/apache2/conf/server.crt          #將證書複製到conf目錄下
[root@localhost ~]# cp -a server.key /usr/local/apache2/conf/server.key         #私鑰也要放入conf目錄下

5.編輯主配置文件開啓相應功能(取消註釋)

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

133 LoadModule ssl_module modules/mod_ssl.so        #開啓ssl功能(約在133行)

493 # Secure (SSL/TLS) connections
494 Include conf/extra/httpd-ssl.conf           #開啓ssl模板,搜索SSLRandomSeed(約在494行)

6.配置SSL模板文件

[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-ssl.conf

88 #   Inter-Process Session Cache:
89 #   Configure the SSL Session Cache: First the mechanism 
90 #   to use and second the expiring timeout (in seconds).
91 #SSLSessionCache         "dbm:/usr/local/apache2/logs/ssl_scache"        #註釋掉
92 #SSLSessionCache        "shmcb:/usr/local/apache2/logs/ssl_scache(512000)"   #註釋掉
93 #SSLSessionCacheTimeout  300                         #註釋掉

7.重啓Apache服務,輸入密碼

[root@localhost ~]# /usr/local/apache2/bin/apachectl restart

8.訪問網站,這裏注意要加上443端口,不然沒法訪問

[root@localhost ~]# https://127.0.0.1:443/index.html
相關文章
相關標籤/搜索