1. 程式人生 > >http-----虛擬目錄與動態網頁搭建

http-----虛擬目錄與動態網頁搭建

虛擬目錄:

每個 Internet服務可以從多個目錄中釋出。通過以通用命名約定 (UNC) 名、使用者名稱及用於訪問許可權的密碼指定目錄,可將每個目錄定位在本地驅動器或網路上。虛擬伺服器可擁有一個宿主目錄和任意數量的其它釋出目錄。其它釋出目錄稱為虛擬目錄。
虛擬目錄不出現在目錄列表中(也稱為http://www.服務的“目錄瀏覽”)。要訪問虛擬目錄,使用者必須知道虛擬目錄的別名,並在瀏覽器中鍵入 URL,對於http://www.服務,還可在 HTML 頁面中建立連結。對於 gopher 服務,可在標誌檔案中建立明確的連結,以便使用者可訪問虛擬目錄。對於FTP服務,可使用目錄註釋列出虛擬目錄。
關於apache虛擬目錄alias
關於apache虛擬目錄的問題,apache的config檔案中documentRoot 後面的是apache在解析頁面時候的根目錄,如果在本機上同時存在兩個工作目錄那麼如果不虛擬(alias)目錄的話,需要不斷修改documentroot的路徑,然後重啟apache,相當麻煩,解決這個問題的辦法之一就是設定虛擬目錄
優點:
(1)便於訪問
(2)便於移動站點中的目
(3)能靈活加大磁碟空間
(4)安全性好
使用alias 建立虛擬目錄
Htpasswd命令是Apache的Web伺服器內建工具,用於建立和更新儲存使用者、域和使用者基本認證的密碼檔案
Htpasswd -c建立一個加密目錄
1、建立口令檔案
[

[email protected] ~]# htpasswd -c /etc/httpd/mysecretpwd abc
New password:
Re-type new password:
Adding password for user abc
[[email protected] conf.d]# htpasswd /etc/httpd/mysecretpwd tom
New password:
Re-type new password:
Adding password for user tom
[[email protected] ~]# mkdir /usr/local/mysecret
[
[email protected]
~]# echo ” This is mysecret” > /usr/local/mysecret/index.html
2、配置使用者認證和虛擬目錄
[[email protected] conf.d]# vim vhost.conf
<Directory “/usr/local/mysecret”>
AuthType Basic
AuthName “This is a private directory,Please Login:”
AuthUserFile /etc/httpd/mysecretpwd
Require user abc tom
</Directory>
<Directory “/www”>
AllowOverride None
Require all granted
</Directory>
<VirtualHost 172.16.50.37:80>
DocumentRoot “/www/haha”
Alias /mysecret “/usr/local/mysecret” 虛擬目錄
ServerName www.haha.com
ServerAlias www1.haha.com
Errorlog “/var/log/httpd/www.haha.com-error_log”
CustomLog “/var/log/httpd/www.haha.com-acces_log” common
</VirtualHost>
~[
[email protected]
conf.d]# systemctl restart httpd
這裡寫圖片描述
CGI通用閘道器介面
CGI 是Web 伺服器執行時外部程式的規範,按CGI 編寫的程式可以擴充套件伺服器功能。CGI 應用程式能與瀏覽器進行互動,還可通過資料庫API 與資料庫伺服器等外部資料來源進行通訊,從資料庫伺服器中獲取資料。格式化為HTML文件後,傳送給瀏覽器,也可以將從瀏覽器獲得的資料放到資料庫中。幾乎所有伺服器都支援CGI,可用任何語言編寫CGI,包括流行的C、C ++、VB 和Delphi 等。CGI 分為標準CGI 和間接CGI兩種。標準CGI 使用命令列引數或環境變量表示伺服器的詳細請求,伺服器與瀏覽器通訊採用標準輸入輸出方式。間接CGI 又稱緩衝CGI,在CGI 程式和CGI 介面之間插入一個緩衝程式,緩衝程式與CGI 介面間用標準輸入輸出進行通訊
動態網頁 : 在伺服器端儲存的文件非HTML格式,是由程式語言開發的指令碼,指令碼在接受客戶端的引數之後進行執行,執行後生成HTML格式的文件,然後把生成的網頁發給客戶端。

安裝動態網頁模組
[[email protected] conf.d]# yum install mod_wsgi -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
base | 4.1 kB 00:00:00
Resolving Dependencies
–> Running transaction check
—> Package mod_wsgi.x86_64 0:3.4-12.el7_0 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

=========================================================================================
**Package Arch Version Repository Size
=========================================================================================**
Installing:
mod_wsgi x86_64 3.4-12.el7_0 base 76 k
**Transaction Summary
=========================================================================================**
Install 1 Package
Total download size: 76 k
Installed size: 197 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mod_wsgi-3.4-12.el7_0.x86_64 1/1
Verifying : mod_wsgi-3.4-12.el7_0.x86_64 1/1
Installed:
mod_wsgi.x86_64 0:3.4-12.el7_0
Complete!
主配置檔案
[[email protected] conf.d]# vi vhost.conf
<Directory “/var/www/alt”>
AllowOverride None
#Allow open access:
Require all granted
</Directory>
listen 8909
<VirtualHost 172.16.50.37:8909>
WSGIScriptAlias / /var/www/alt/webinfo.wsgi
servername www.haha.com
</VirtualHost>
[[email protected] conf.d]# mkdir /var/www/alt
[[email protected] alt]# vim webinfo.wsgi
def application(environ, start_response):
status = ‘200 OK’
output = ‘Hello World’
response_headers = [(‘Content-type’, ‘text/plain’),
(‘Content-Length’, str(len(output)))]
start_response(status, response_headers)
return [output]
[[email protected] alt]# systemctl restart httpd
這裡寫圖片描述