web服務

第1、apache
        
    一、web服務器的概念以及http的概念
        架構: C/S  : Client (瀏覽器) <-------> Server (apache)
        網站的技術:靜態和動態
            靜態頁面:通常都是.html .htm .shtml
            動態頁面:通常是 .php .asp .jsp  
        特色:須要語言的解析模塊去完成頁面代碼編譯,最後把結果返回給web服務器,還有一個常見的特色就是涉及到數據庫查詢的php

    二、apache軟件包
        # yum install httpd httpd-devel httpd-manual -yhtml

        服務名字:httpd
        進程名字:httpd
        默認端口:80  (https:443)
            :8080linux

        服務的工做目錄: /etc/httpd
        主配置:/etc/httpd/conf/httpd.confweb

    三、分析配置文件
    分三部分:全局配置、局部配置、虛擬空間配置
    全局:
    ServerRoot "/etc/httpd"  <---後面的路徑不能添加 /,rpm安裝不要去修改
    Listen 80  <---監聽的端口
    Listen 8080
    ServerName www.example.com:80 <---設定服務器默認網站綁定的域名(ip)和端口數據庫

    實例1:安裝後直接重啓,使用默認網站apache

        service httpd restartvim

    實例2:取消默認報錯而且對主配置文件進行簡單的修改
    功能:取消test page的顯示,顯示咱們本身的首頁瀏覽器

                Listen 80
                Listen 192.168.0.7:80
                Listen 192.168.2.7:8080安全

        一、vim /etc/httpd/conf/httpd.conf
        ServerName 10.1.1.20:80
        或者 vim /etc/hosts <---把你的主機名字和你的IP對應上服務器

        二、vim /var/www/html/index.html  《---默認狀況下,默認網站的根目錄 /var/www/html
            概念:網站的根目錄--->網站頁面程序存放的起始路徑
            默認狀況下,apache讀取一個網站的的默認首頁就是index.html

            DirectoryIndex index.html index.html.var  《---這裏就是定義網站的,默認首頁的名字

        三、修改參數
            DirectoryIndex index.htm index.html index.html.var

        新建一個index.htm  <---內容隨便你,用來測試區別

        四、service httpd restart
        
        五、打開firefox,在瀏覽器中輸入域名或者IP就能夠。


    實例3:修改默認網站的根目錄

        一、修改配置文件的根目錄定義參數
            DocumentRoot "/www"

        二、建立定義的網站根目錄
            mkdir /www
            vim /www/index.html


        三、重啓服務,讓配置生效
            service httpd restart

    實例4:學習目錄控制參數
        <Directory "/var/www/html">
            Options Indexes FollowSymLinks
            AllowOverride None   # 是否啓動.htaccess
            Order allow,deny
            Allow from all
        </Directory>

                xml

        分析參數:
        
            Options
                Indexes  <---是否對於沒有默認首先目錄下面的文件以及子目錄進行索引,去掉以後就取消這個功能
                FollowSymLinks  <---跟蹤軟鏈接,存在的意義:隱藏目錄的真實路徑,能夠方便網站擴容

<Directory "/www">  <---取消indexes的配置
                Options  FollowSymLinks
                AllowOverride None
                Order allow,deny
                Allow from all
</Directory>

    實例5:啓用訪問控制

<Directory "/www">
                Options  FollowSymLinks
                AllowOverride None
                Order allow,deny   <-----定義acl控制的順序,這裏表明先容許,再也不容許範圍內的都拒絕
                Allow from all      <----定義容許全部
</Directory>    


<Directory "/www">  
                Options  FollowSymLinks
                AllowOverride None
                Order allow,deny   <-----定義acl控制的順序,這裏表明先容許,再也不容許範圍內的都拒絕
                Allow from 10.1.1.0/24      <----定義只容許10.1.1.0/24
</Directory>

----------------------------

<Directory "/www">
                Options  FollowSymLinks
                AllowOverride None
                Order deny,allow
                Deny from 10.1.2.0/24  《---只拒絕10.1.2.0/24這個網段訪問
</Directory>

    實例6:口令驗證控制

    一、修改參數 ,把AllowOverride none 改爲 AllowOverride ALL        
<Directory "/www">
                Options  FollowSymLinks
                AllowOverride ALL  《---ALL啓用口令驗證,none關閉口令驗證
                Order deny,allow
                Deny from 10.1.2.0/24  《---只拒絕10.1.2.0/24這個網段訪問
</Directory>
    
    二、創建.htaccess文件,定義驗證配置文件

我要對網站的根目錄進行驗證,那麼在根目錄下建立 .htaccess
vim /www/.htaccess

authname "Just for test"
authtype basic
authuserfile /etc/httpd/userpw
require valid-user

    三、創建密碼賬號文件

htpasswd -c /etc/httpd/userpw test

htpasswd  /etc/httpd/userpw bbs
    

    四、service httpd restart


    實例7:創建用戶我的空間
    
    一、修改配置文件
    搜索 UserDir
<IfModule mod_userdir.c>
    #
    # UserDir is disabled by default since it can confirm the presence
    # of a username on the system (depending on home directory
    # permissions).
    #
    #UserDir disable

    #
    # To enable requests to /~user/ to serve the user's public_html
    # directory, remove the "UserDir disable" line above, and uncomment
    # the following line instead:
    #
    UserDir public_html   《---啓用用戶我的空間的功能,訪問http://ip/~mary  --》讀取/home/mary/public_html

</IfModule>

<Directory /home/*/public_html>
    AllowOverride FileInfo AuthConfig Limit
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    <Limit GET POST OPTIONS>
        Order allow,deny
        Allow from all
    </Limit>
    <LimitExcept GET POST OPTIONS>
        Order deny,allow
        Deny from all
    </LimitExcept>
</Directory>

    二、創建用戶以及我的空間的目錄

    useradd web
    mkdir /home/web/public_html    《---創建我的空間根目錄
    vim /home/web/public_html/index.html
        This is Web's Home!

    chmod o+x /home/web  <---默認用戶家目錄權限是700,httpd進程沒法訪問,因此給予它訪問的權限

    三、重啓服務,使配置生效

    service httpd restart

    4. 測試: http://localhost/~web/

===========================================================
    實例8:創建目錄別名

    Alias /tobbs    "/bbs"  <---訪問 IP/tobbs 的時候實際讀取的內容是/bss(系統的絕對路徑)

    加入咱們的apache定義哪一個別名指向那個文件夾裏面,存放的是一些cgi,perl...alias定義的別名就沒法達到執行程序的目的。

    ScriptAlias /tobbs "/bbs" <---這樣就解決的執行腳本的問題
        
    
    做用:方便擴容,隱藏目錄增長安全性

 

 

    實例9:虛擬空間  <---一個服務器跑多個網站
        
        @@基於域名的虛擬空間: 《---在同一個服務器,在只有一個IP的狀況下,經過不一樣域名來區分不一樣的網站
        一、啓用參數:
    NameVirtualHost *:80  《--讓apache在一個服務器上,單個IP 運行多個網站,每一個網站獨立內容

        二、添加虛擬主機配置
<VirtualHost *:80>
    DocumentRoot /www/uplooking
    ServerName www.uplooking.com
    ErrorLog logs/www.uplooking.com-error_log
    CustomLog logs/www.uplooking.com-access_log common
</VirtualHost>  

        三、創建網站目錄和文件
mkdir /www/uplooking
vim /www/uplooking/index.html

        四、配置DNS

        作A記錄或CNAME記錄都行, 若是都作A記錄不行, 就把其中一個域名作A記錄,
        其餘的作CNAME記錄

真正在互聯網該如何操做?

        背景:公司搭建一臺web服務器,綁定的網址 www.uplooking.com,IP是公網:61.191.53.56
        公司想創建一個論壇,綁定的網址bbs.uplooking.com,網站是創建同一個服務器上

        a、添加虛擬主機配置
<VirtualHost *:80>
    DocumentRoot /bbs
    ServerName bbs.uplooking.com
    ErrorLog logs/bbs.uplooking.com-error_log
    CustomLog logs/bbs.uplooking.com-access_log common
</VirtualHost>  

        b、創建網站目錄和文件
mkdir /bbs
vim /bbs/index.html


    測試: http://www.uplooking.com --->訪問的是/www/uplooking
        http://bbs.uplooking.com  --->訪問的是/bbs下的內容


        @@@基於IP的虛擬空間:
            一、修改配置文件

#NameVirtualHost *:80  <---註釋這個配置

<VirtualHost 10.1.1.21:80>
    DocumentRoot /bbs
    ServerName bbs.uplooking.com
    ErrorLog logs/bbs.uplooking.com-error_log
    CustomLog logs/bbs.uplooking.com-access_log common
</VirtualHost>

            二、修改DNS,添加A記錄
bbs     IN      A       10.1.1.21


    測試: http://bbs.uplooking.com   過程: Client ---> 數據包[dst:10.1.1.21  url:bbs.uplooking.com]-->Server

        @@@基於同一個IP不一樣端口:
            一、修改參數文件

[root@kadefor ule-sa3]# grep ^Listen  /etc/httpd/conf/httpd.conf
Listen 80
Listen 8080  <---增長apache監聽的端口
[root@kadefor ule-sa3]# tail -12 !$
tail -12 /etc/httpd/conf/httpd.conf
<VirtualHost 192.168.56.1:80>
    DocumentRoot /www2
    ServerName web.kadefor.com
    ErrorLog logs/www2.kadefor.com-error_log
    CustomLog logs/www2.kadefor.com-access_log common
</VirtualHost>
<VirtualHost 192.168.56.1:8080>
    DocumentRoot /www
    ServerName web.kadefor.com
    ErrorLog logs/www.kadefor.com-error_log
    CustomLog logs/www.kadefor.com-access_log common
</VirtualHost>
[root@kadefor ule-sa3]#
            

            二、增長DNS記錄

        測試    過程: Client ---> 數據包[dst:192.168.56.1  url:web.kadefor.com  port:8080]-->Server

http://web.kadefor.com:8080/  
http://web.kadefor.com

 

做業:
    創建兩個虛擬主機(vhost),分別幫定兩個網址是: www.uplooking.com 和 bbs.uplooking.com
    www.uplooking.com對應的網站根目錄是/www/uplooking,隨便編寫一個默認首頁測試,方便區別不一樣網站
    bbs.uplooking.com對應的網站根目錄是/bbs
    要求利用其中一種別名的方法,讓訪問www.uplooking.com/bbs的時候,實際訪問的是/bbs下的內容
    網站所用的網址要求使用本身的DNS服務器去解析,而不是修改hosts文件
    網站要求只容許10.1.1.0網段去訪問,但不能讓10.1.1.20訪問

 

 

答案:
    1 vim /etc/httpd/conf/httpd.conf
    NameVirtualHost *:80
    <VirtualHost *:80>
        DocumentRoot /www/uplooking
        ServerName www.uplooking.com
        ErrorLog logs/www.uplooking.com-error_log
        CustomLog logs/www.uplooking.com-access_log common
        Alias /bbs      "/bbs"
        <Directory "/www/uplooking">
            Order allow,deny
            deny from 10.1.1.20
            allow from 10.1.1.0/24
        </Directory>
    </VirtualHost>

    <VirtualHost *:80>
        DocumentRoot /bbs
        ServerName bbs.uplooking.com
        ErrorLog logs/bbs.uplooking.com-error_log
        CustomLog logs/bbs.uplooking.com-access_log common
        <Directory "/www/uplooking">
            Order allow,deny
            deny from 10.1.1.20
            allow from 10.1.1.0/24
        </Directory>
    </VirtualHost>

    二、mkdir /www/uplooking;mkdir /bbs
        vim /www/uplooking/index.html
            This www.uplooking.com
        vim /bbs/index.html
            This bbs.uplooking.com

    三、配置DNS添加別名記錄
web     IN      A       10.1.1.20
www     IN      CNAME   web
bbs     IN      CNAME   web

    service named restart
    service httpd restart


怎樣設置只能經過域名訪問, 而不能經過IP訪問?

方法1.

添加一個虛擬主機能夠實現:
<VirtualHost *:80>
    ServerName localhost
    DocumentRoot /nosite     # 這個目錄不存在!
</VirtualHost>


方法2
<VirtualHost *:80>
    ServerName localhost
    #DocumentRoot /nosite
    <Location />        # 禁止全部人訪問
        order deny,allow
        deny from all
    </Location>
</VirtualHost>


1. yum install httpd -y


2. yum install httpd-manual -y
   /etc/init.d/httpd restart
   firefox   http://<ip>/manual

3. change DocumentRoot
    vim /etc/httpd/conf/httpd.conf

    DocumentRoot "/var/www/html"
    -->
    DocumentRoot "/opt/www"
    
    <Directory "/var/www/html">
    -->
    <Directory "/opt/www">

4. control directory
    <Directory "/opt/www/abc">
        order deny,allow
        deny from 192.168.248.1
    </Directory>


5. UserDir
    UserDir  disabled
    -->
    UserDir  public_html

    <Directory "/home/*/public_html">
        Options Indexes FollowSymlinks
        AllowOverride None
    </Directory>

    test:
        su - oracle
        mkdir public_html
        chown o+x ~oracle

6. .htaccess

    相應目錄控制語句裏, 修改 AllowOverride all

    eg:
    /opt/www/abc

    vim /etc/httpd/conf/httpd.conf

    <Directory />
        allowoverride all
    </Diretory>

    or: 若是有:
    <Directory "/opt/www/abc">
        allowoverride all
    </Diretory>

    vim /opt/www/abc/.htaccess

    authname "Must auth"
    authtype basic
    authuserfile  /etc/httpd/conf/user.db
    require  valid-user

7.  cgi

    a) 確認 cgi 模塊是否加載
    b) 確認存放 cgi 程序的目錄:
       ScriptAlias "/cgi-bin/"  "/var/www/cgi"

    c) 控制目錄容許執行cgi程序:
        <Directory /var/www/cgi>
            Options +ExecCGI
        </Directory>

    b) 取消註釋
       AddHandler  cgi-script   .cgi

8. selinux --> cgi

   chcon -t httpd_sys_script_exec_t /var/www/cgi-bin -R

9. v-host

  a)  port
    
    vim /etc/httpd/conf/httpd.conf

    Listen 8080

    <VirtualHost *:80>
        ServerAdmin  root@localhost
        ServerName   www.up.com
        DocumentRoot /opt/www80
        ErrorLog     logs/www80.error.log
        CustomLog    logs/www80.access.log common
    </VirtualHost>

    <VirtualHost *:8080>
        ServerAdmin  root@localhost
        ServerName   www.up.com
        DocumentRoot /opt/www8080
        ErrorLog     logs/www8080.error.log
        CustomLog    logs/www8080.access.log common
    </VirtualHost>

  b) name-based

    vim /etc/httpd/conf/httpd.conf

    NameVitrualHost  *:80

    <VirtualHost *:80>
        ServerAdmin  root@localhost
        ServerName   www.up.com
        DocumentRoot /opt/www
        ErrorLog     logs/www80.error.log
        CustomLog    logs/www80.access.log common
    </VirtualHost>

    <VirtualHost *:80>
        ServerAdmin  root@localhost
        ServerName   blog.up.com
        DocumentRoot /opt/blog
        ErrorLog     logs/blog.error.log
        CustomLog    logs/blog.access.log common
    </VirtualHost>

  c)  ip

    vim /etc/httpd/conf/httpd.conf

    Listen *:80

    <VirtualHost 192.168.248.11:80>
        ServerAdmin  root@localhost
        ServerName   www.up.com
        DocumentRoot /opt/www80
        ErrorLog     logs/www80.error.log
        CustomLog    logs/www80.access.log common
    </VirtualHost>

    <VirtualHost 172.16.248.11:80>
        ServerAdmin  root@localhost
        ServerName   blog.up.com
        DocumentRoot /opt/www8080
        ErrorLog     logs/www8080.error.log
        CustomLog    logs/www8080.access.log common
    </VirtualHost>


10.   禁止使用ip,或非法解析的域名來訪問:

    創建第一個虛擬主機, 使之不可訪問

    <VirtualHost 192.168.248.11:80>
        DocumentRoot /www/docs
        <Directory /www/docs>
           order deny,allow
           deny from all
        </Directory>
    </VirtualHost>


https    
[root@dns ~]# yum install openssl mod_ssl -y
[root@dns ~]# ls /etc/httpd/conf.d/ssl.conf
/etc/httpd/conf.d/ssl.conf
[root@dns ~]#
[root@dns ~]# cd /etc/pki/tls/certs
[root@dns certs]# ls Makefile
Makefile
[root@dns certs]# make testcert
[root@dns certs]#
[root@dns certs]# ls ../private/localhost.key
../private/localhost.key
[root@dns certs]# ls localhost.crt
localhost.crt
[root@dns certs]# mkdir /etc/httpd/ssl
[root@dns certs]# cp ../private/localhost.key  /etc/httpd/ssl/server.key
[root@dns certs]# cp localhost.crt /etc/httpd/ssl/server.crt
[root@dns certs]#
[root@dns certs]# vim /etc/httpd/conf.d/ssl.conf
[root@dns certs]# grep server.key  /etc/httpd/conf.d/ssl.conf
SSLCertificateKeyFile /etc/httpd/ssl/server.key
[root@dns certs]# grep server.crt  /etc/httpd/conf.d/ssl.conf
SSLCertificateFile /etc/httpd/ssl/server.crt
#SSLCACertificateFile /etc/httpd/ssl/server.crt
[root@dns certs]# /etc/init.d/httpd restart
[root@dns certs]# --> password
[root@dns certs]#
[root@dns certs]# openssl rsa -in /etc/httpd/ssl/server.key -out /etc/httpd/ssl/nopw.key
Enter pass phrase for /etc/httpd/ssl/server.key:
writing RSA key
[root@dns certs]# vim /etc/httpd/conf/httpd.conf
[root@dns certs]# vim /etc/httpd/conf.d/ssl.conf
[root@dns certs]# grep nopw.key /etc/httpd/conf.d/ssl.conf
SSLCertificateKeyFile /etc/httpd/ssl/nopw.key
[root@dns certs]#
[root@dns certs]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[root@dns certs]#


注意     1,DirectoryIndex   index.html index.html index.html.var  設置默認訪問頁面
    2,/etc/httpd/conf/httpd.conf 修改httpd 的配置文件目錄
    3,index.html文件建在設置的DocumentRoot /www/up18 這個家目錄裏面

4.    elinks 字符界面瀏覽器 curl 查看網頁信息

5,/etc/httpd/conf.d 在這裏添加                                          

相關文章
相關標籤/搜索