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站點 1、基於域名的虛擬主機 2、基於端口的虛擬主機 3、基於IP的虛擬主機 一、配置一個虛擬站點: 1、配置文件路徑 /etc/httpd/conf/httpd.conf #主配置文件 /etc/httpd/conf.d/*.conf #從配置文件 2、爲每個虛擬站點添加配置 <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> 3、添加網站 [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 4、重啓服務 [root@server0 ~]# systemctl restart httpd [root@server0 ~]# systemctl enable httpd 5、測試 [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 客戶端口訪問服務器的限制 1、防火牆是否限制 2、服務本身的訪問控制 3、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而言: 1、/etc/httpd/conf/httpd.conf 2、/etc/http/conf.d/*.conf 3、/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,安全傳輸層協議 加密素材: 1、簽名證書 http://172.25.254.254/pub/tls/certs/server0.crt 2、證書密鑰 http://172.25.254.254/pub/tls/private/server0.key 3、證書籤名授權信息 http://172.25.254.254/pub/example-ca.crt 實現條件: 1、啓用SSL模塊支持 2、部署好加密素材(網站服務器的數字、網站服務器的私鑰、根證書(CA管理機構的證書)) 1、安裝模塊 [root@server0 ~]# yum -y install mod_ssl //安裝SSL模塊 [root@server0 ~]# ls /etc/httpd/conf.d/ssl.conf 2、部署網站證書 [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 //部署授權中心(公安局信息) 3、部署私鑰 [root@server0 ~]# cd /etc/pki/tls/private wget http://172.25.254.254/pub/tls/private/server0.key //部署私鑰(用於解密) 4、修改配置文件 [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 #根證書位置 5、重啓服務 [root@server0 ~]# systemctl restart httpd 6、測試 [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 //查看日誌 .. ..
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章