Day10 - apache vhost wsgi

postfix電子郵件服務器 ——

郵件服務器的基本功能:
1)爲用戶提供郵箱帳號
2)爲用戶向外發送郵件
3)爲用戶接收並投遞郵件

標配郵件服務器:
本機的程序 ==>本機的postfix郵件服務器 

普通郵件服務器:
本機或其餘主機的程序 ==>postfix郵件服務器 

nullclient郵件服務器:
1)不爲用戶提供郵箱帳號
2)爲用戶向外發送郵件
3)不爲用戶接收並投遞郵件

本機程序 ==> postfix郵件服務器 ==> 後端郵件服務器

echo '郵件內容'  |  mail  -s  '郵件標題'  收件人1地址   收件人2地址 ... 

mail
mail  -u  用戶名

MariaDB數據庫 ——

搭建數據庫服務器:
# yum  -y  install  mariadb-server  mariadb
# systemctl  restart  mariadb

數據庫服務器的配置操做:
# vim  /etc/my.cnf
skip-networking
# systemctl  restart  mariadb

# mysqladmin  -u用戶名 -p舊密碼  password  '新密碼'

關於服務程序的監聽地址、端口
# netstat  -anptu

-a:全部的鏈接
-n:以數字的方式顯示
-p:顯示對應的進程號(PID)
-t:列出TCP鏈接
-u:列出UDP鏈接
-l:列出本機監聽的鏈接

如何鏈接數據庫:
# mysql  [-u用戶名]  [-p密碼]
MariaDB> SQL指令; 
MariaDB> exit

數據庫的增刪查:
MariaDB> create  database  庫名;
MariaDB> drop  database  庫名;
MariaDB> show  databases;
MariaDB> use  庫名;

數據表的查詢、刪除:
MariaDB> select  字段列表  from  庫名.表名;
MariaDB> select  字段列表  from  庫名.表名  where  字段名=值;
MariaDB> delete  from  庫名.表名  where  字段名=值 .. .. ;  

用戶受權控制:
MariaDB> grant  權限列表  on  庫名.表名   to  用戶名@客戶機地址  identfified  by  '密碼';

    <a href="http://www.baidu.com/">百度</a>

   vncviewer  172.40.50.113:7000

########################################################################

搭建Web服務的基本思路:
1)裝包
    [root@server0 ~]# yum -y install httpd      //在Server0上配置
2)配置(可選)
    (1).修改配置文件(/etc/httpd/conf/httpd.conf)
        -Listen:監聽地址:端口(80)
        -ServerName:本站點註冊的DNS名稱(www.baidu.com)
        -DocumentRoot:網頁根目錄(/var/www/html)
        -DirectoryIndex:起始頁/首頁文件名(index.html)
    檢查httpd配置語法:
    [root@server0 ~]# httpd  -t
    (2).添加網站
    http://server0.example.com/  ==>  /var/www/html/index.html
    http://server0.example.com/a.html  ==>  /var/www/html/a.html

3)啓服務
    [root@server0 ~]# systemctl restart httpd
    [root@server0 ~]# systemctl enable httpd

4)測試
    [root@server0 ~]# lynx 127.0.0.1        //本地測試
    [root@server0 ~]# elinks -dump 127.0.0.1
    [root@server0 ~]# firefox 127.0.0.1
    [root@desktop0 ~]# firefox 172.25.0.11      //在desktop0上測試
    [root@desktop0 ~]# elinks -dump 172.25.0.11

 網站訪問常見問題 ——
報錯:ELinks: 沒有到主機的路由
緣由:對方的防火牆阻止訪問

報錯:ELinks: 拒絕鏈接
緣由:對方的服務沒開啓

=============================================================
虛擬web主機(一旦啓動虛擬主機功能,全部站點都必須是虛擬主機)
    :由同一臺服務器提供多個不一樣的web站點
    一、基於域名的虛擬主機
    二、基於端口的虛擬主機
    三、基於IP的虛擬主機

1、配置一個虛擬站點:
一、配置文件路徑 
    /etc/httpd/conf/httpd.conf      #主配置文件
    /etc/httpd/conf.d/*.conf        #從配置文件

二、爲每一個虛擬站點添加配置
<VirtualHost IP地址:80>
    ServerName    站點域名(server0.example.com)
    DocumentRoot  站點根目錄(/var/www/server0)
    ServerAlias   站點別名(server1.example.com)
</VirtualHost>

[root@server0 ~]# ls /usr/share/doc/httpd-*/httpd-vhosts.conf       //幫助示例
例如:
[root@server0 ~]# vim /etc/httpd/conf.d/vhosts.conf     //虛擬主機從配置文件
    <VirtualHost  *:80>
        ServerName  www.baidu.com
        DocumentRoot  /var/www/www.baidu.com
        ServerAlias  bbs.baidu.com  
    </VirtualHost>

    <VirtualHost  *:80>
        ServerName  www.qq.com
        DocumentRoot  /var/www/www.qq.com
        ServerAlias  bbs.qq.com 
    </VirtualHost>

三、添加網站
    [root@server0 ~]# mkdir /var/www/www.baidu.com
    [root@server0 ~]# mkdir /var/www/www.qq.com
    [root@server0 ~]# echo This is www.baidu.com > /var/www/www.baidu.com/index.html
    [root@server0 ~]# echo This is www.qq.com > /var/www/www.qq.com/index.html

四、重啓服務
    [root@server0 ~]# systemctl restart httpd
    [root@server0 ~]# systemctl enable httpd

五、測試
    [root@server0 ~]# nslookup www.baidu.com    
    [root@server0 ~]# lynx www.baidu.com        //本地測試
    [root@server0 ~]# elinks -dump www.qq.com
    [root@server0 ~]# firefox bbs.baidu.com
    [root@desktop0 ~]# host bbs.baidu.com
    [root@desktop0 ~]# firefox bbs.qq.com       //在desktop0上測試
    [root@desktop0 ~]# elinks -dump www.baidu.com

================================================================================
httpd服務訪問控制
    /etc/httpd/conf/httpd.conf
    <Directory "/var/www">
        AllowOverride none
        #Allow open access:
        Require all granted     #容許全部
    </Directory>
[root@server0 ~]# mkdir /var/www/web01/private
[root@server0 ~]# echo welcome to My webSite!!>/var/www/web01/private/index.html
[root@server0 ~]# vim /etc/httpd/conf.d/vhosts.conf     //虛擬主機配置文件
    <VirtualHost  *:80>
        ServerName  www.web01.com
        DocumentRoot  /var/www/web01/private
        ServerAlias  bbs.web01.com  
    </VirtualHost>
[root@server0 ~]# elinks -dump www.web01.com        //本機測試
[root@desktop0 ~]# elinks -dump www.web01.com       //客戶端測試
[root@server0 ~]#
拷貝/etc/httpd/conf/httpd.conf中如下
    <Directory "/var/www">
        AllowOverride none
        #Allow open access:
        Require all granted
    </Directory>
[root@server0 ~]# vim /etc/httpd/conf.d/web_ACL.conf    //單獨創建訪問控制文件
    <Directory "/var/www/web01/private">
        #AllowOverride none
        #Allow open access:
        Require ip 172.25.0.11  127.0.0.1   #僅容許訪問的IP
    </Directory>
[root@server0 ~]# elinks -dump www.web01.com        //本機測試
[root@desktop0 ~]# elinks -dump www.web01.com       //客戶端測試

####################################################################################

SELinux對Web目錄的限制 —— 默認只能在 /srv/www、/var/www
客戶端口訪問服務器的限制
        一、防火牆是否限制 
        二、服務自己的訪問控制
        三、SELinux的限制

若是想用其餘的Web目錄 ——
1)調整httpd服務配置,添加Directory。。。容許
2)調整目錄SELinux屬性(chcon  -R  --reference=模板目錄  自定義目錄)

[root@server0 ~]# vim /etc/httpd/conf.d/vhosts.conf     //虛擬主機配置文件
    <VirtualHost  *:80>
        ServerName  www.web02.com
        DocumentRoot  /web02
        ServerAlias  bbs.web02.com  
    </VirtualHost>
[root@server0 ~]#  mkdir /web02
[root@server0 ~]#  echo web02 > /web02/index.html
[root@server0 ~]#  systemctl restart httpd
[root@server0 ~]#  elinks www.web02.com         //沒法訪問(SELinux啓用狀態)
[root@server0 ~]# vim /etc/httpd/conf.d/web_ACL.conf
    <Directory "/web02">
        #AllowOverride none
        #Allow open access:
        Require all granted     #容許全部IP訪問
    </Directory>
[root@server0 ~]#  elinks www.web02.com         //再次測試依然沒法訪問
SELinux策略保護:
    對httpd而言:
        一、/etc/httpd/conf/httpd.conf
        二、/etc/http/conf.d/*.conf
        三、/var/www

[root@server0 ~]# ls -Zd  /var/www/ //查看/var/www的安全標籤
[root@server0 ~]# ls -Zd  /web02        //對比與默認目錄的安全標籤
[root@server0 ~]# setenforce 0      //關閉SELinux,再測試
[root@server0 ~]# elinks www.web02.com  

[root@server0 ~]# chcon -R --reference=/var/www  /web02     //以/var/www爲參考遞歸修改/web02的SELinux標籤
[root@server0 ~]# setenforce 1
[root@server0 ~]# systemctl restart httpd
[root@desktop0 ~]# elinks www.web02.com         //在客戶端上進行測試

 安全web服務
 PKI(Public Key Infrastructure):公鑰基礎設施
    -公鑰:主要用來加密數據
    -私鑰:主要用來解密數據(與對應的公鑰匹配)
    -數字證書:證實擁有者的合法性/權威性(單位名稱、有效期、公鑰、頒發機構及簽名)
    -數字證書受權中心(CA:Certificate Authority):負責證書的申請、審覈、頒發、鑑定、撤銷等管理工做
    HTTPS(加密web通訊:TCP=443端口)
    -Secure Sockets Layer,安全套接字層
    -Transport Layer Security,安全傳輸層協議

加密素材:   
    一、簽名證書
        http://172.25.254.254/pub/tls/certs/server0.crt
    二、證書密鑰
        http://172.25.254.254/pub/tls/private/server0.key
    三、證書籤名受權信息
        http://172.25.254.254/pub/example-ca.crt

    實現條件:
        一、啓用SSL模塊支持 
        二、部署好加密素材(網站服務器的數字、網站服務器的私鑰、根證書(CA管理機構的證書))
一、安裝模塊
[root@server0 ~]# yum -y install mod_ssl        //安裝SSL模塊
[root@server0 ~]# ls /etc/httpd/conf.d/ssl.conf

二、部署網站證書
[root@server0 ~]# cd /etc/pki/tls/
[root@server0 tls]# ls
[root@server0 tls]# cd certs
    wget http://172.25.254.254/pub/tls/certs/server0.crt        //部署簽名證書(營業執照)
    wget http://172.25.254.254/pub/example-ca.crt       //部署受權中心(公安局信息)

三、部署私鑰 
[root@server0 ~]# cd /etc/pki/tls/private
    wget http://172.25.254.254/pub/tls/private/server0.key      //部署私鑰(用於解密)

四、修改配置文件
[root@server0 ~]# vim /etc/httpd/conf.d/ssl.conf
        DocumentRoot "/web02"       #站點根目錄
        ServerName www.web02.com:443    #域名

100行    SSLCertificateFile  /etc/pki/tls/certs/server0.crt      #網站證書位置
107行    SSLCertificateKeyFile   /etc/pki/tls/private/server0.key    #私鑰位置
122行    SSLCACertificateFile    /etc/pki/tls/certs/example-ca.crt   #根證書位置

五、重啓服務
[root@server0 ~]#  systemctl restart httpd

六、測試
[root@desktop0 ~]# firefox https://www.web02.com        //在客戶端上測試

動態網站:

  學過的知識點(經過練習過程鞏固)—— vim、mkdir、chmod
  正在重點講解的知識點(經過練習去熟悉)。。。。
  後面會學到的知識點(瞭解大概)安全上下文、正則表達式

[root@server0 ~]# vim webinfo.wsgi      //編寫Python動態網頁文件
#!/usr/bin/env python
import time

def application (environ,start_response):
    respone_body = 'UNIX EPOCH time is now: %s\n' %time.time()
    status = '200 OK'
    respone_headers = [('Content-Type','text/plain'),
                        ('Content-Length','1'),
                        ('Content-Length',str(len(respone_body)))]
    start_response(status,respone_headers)
    return [respone_body]

[root@server0 ~]# yum  -y  install  httpd  mod_wsgi     //安裝Python支持組件
[root@server0 ~]# mkdir  /var/www/pythonweb.com
[root@server0 ~]# cd  /var/www/pythonweb.com
[root@server0 webapp0]# cp ~/ webinfo.wsgi ./       //拷貝以前編寫的python網頁

————————————————————————————————————————————————
    WSGIScriptAlias  URL別名   實際訪問的文件路徑
    Alias       /   /var/www/webapp0/webinfo.wsgi       //定義別名
    WSGIScriptAlias  /   /var/www/webapp0/webinfo.wsgi

————————————————————————————————————————————————
[root@server0 ~]# vim  /etc/httpd/conf.d/vhosts.conf 
    <VirtualHost *:80>
        ServerName  www.pythonweb.com
        DocumentRoot  /var/www/pythonweb.com
        WSGIScriptAlias  /   /var/www/pythonweb.com/webinfo.wsgi    //使用wsgi腳本解析python網頁
    </VirtualHost>
[root@server0 ~]# setenforce  0
[root@server0 ~]# systemctl  restart  httpd
[root@server0 ~]# elinks -dump www.pythonweb.com            //測試
[root@desktop0 ~]# firefox  www.pythonweb.com           //測試
修改端口爲8089
[root@server0 ~]# vim  /etc/httpd/conf.d/vhosts.conf 
    Length 8089         #添加8089端口
    <VirtualHost *:8089>    #修改成8089端口
        ServerName  www.pythonweb.com
        DocumentRoot  /var/www/pythonweb.com
        WSGIScriptAlias  /   /var/www/pythonweb.com/webinfo.wsgi    //使用wsgi腳本解析python網頁
    </VirtualHost>
[root@server0 ~]# setenforce  1
[root@server0 ~]# systemctl  restart  httpd         //服務啓動失敗(由於SELinux)
[root@server0 ~]# semanage port -l | grep http      //查看SELinux開放的端口
[root@server0 ~]# semanage port -a -t http_port_t -p tcp 8089       //在SELinux中添加開放的端口(此操做:很耗內存及CPU資源)
[root@server0 ~]# semanage port -l | grep http      //查看SELinux開放的端口
[root@server0 ~]# systemctl  restart  httpd 
[root@server0 ~]# elinks -dump www.pythonweb.com:8089           //測試
[root@desktop0 ~]# firefox  www.pythonweb.com:8089          //測試

排除SELinux問題 ——
[root@server0 ~]# less  /var/log/messages       //查看日誌

.. ..
相關文章
相關標籤/搜索