apache的應用(發佈目錄,黑白名單,虛擬主機,PHP-cgi支持,正向代理,https加密,)

[root@apache1 ~]# yum install httpd -yphp

[root@apache1 ~]# cd /var/www/html/   進入默認發佈目錄html

[root@apache1 html]# lsapache

[root@apache1 html]# vim index.html      編輯默認讀取的頁面vim

[root@apache1 html]# systemctl start httpd服務器

 

 

 ###################################################測試

修改默認發佈目錄ui

 

[root@apache1 html]# vim /etc/httpd/conf/httpd.conf 編輯配置文件加密

 

#DocumentRoot "/var/www/html"           註釋此行(119左右)spa

DocumentRoot "/var/www/test"               本身寫入要更改的發佈目錄代理

<Directory "/var/www/test">

    Require all granted

</Directory>

 

 

<IfModule dir_module>                           約在167行左右

    DirectoryIndex  test.html   index.html   修改默認讀取的文件

</IfModule>

 

[root@apache1 html]# mkdir /var/www/test   建立本身設置的發佈目錄(有時需注意權限)

[root@apache1 html]# vim /var/www/test/test.html      編輯本身默認讀取的文件(ex:test)

[root@apache1 html]# systemctl restart httpd.service    重啓服務,讀取文件

頁面測試:

 

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

黑白目錄(名單的設置):

 

[root@apache1 html]# vim /etc/httpd/conf/httpd.conf

DocumentRoot "/var/www/test"

<Directory "/var/www/test">            修改此模塊中的內容

    Order Allow,Deny                      ##黑白名單的讀取順序

    Allow from ALL                          ##白名單Allow,容許全部人訪問

    Deny from 192.168.52.0/24     ##黑名單Deny,拒絕192.168.52.0這個網段的人訪問

</Directory>

[root@apache1 html]# systemctl restart httpd.service

測試:

 

 

訪問失敗

此時咱們再來修改一下配置文件:

 

DocumentRoot "/var/www/test"

<Directory "/var/www/test">

    Order Deny,Allow               ##將黑白名單的讀取順序改變一下先Deny,後Allow

    Allow from ALL

    Deny from 192.168.52.0/24

</Directory>

[root@apache1 html]# systemctl restart httpd.service           ##從新服務,讀取文件     

再次訪問時會發現又能夠訪問通了。由此可得出一個結論:

在黑白名單中,後讀取的規則會覆蓋先讀取的規則。

 

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

指定用戶訪問發佈目錄:

 

[root@apache1 html]# cd /etc/httpd/

[root@apache1 httpd]# ls

conf  conf.d  conf.modules.d  logs  modules  run

[root@apache1 httpd]# htpasswd -cm apacheuser admin        創建用戶並生成認證文件

New password:

Re-type new password:

Adding password for user admin

[root@apache1 httpd]# htpasswd -m apacheuser bss

New password:

Re-type new password:

Adding password for user bss

[root@apache1 httpd]# cat apacheuser                       查看用戶信息

admin:$apr1$U.IY9J0s$SWnZa5LQlH59vFF3gVfsz/

bss:$apr1$cKBxKH39$8maYvvh5aDGV.8/iQRrSA1

 

編輯配置文件

[root@apache1 httpd]# vim /etc/httpd/conf/httpd.conf

DocumentRoot "/var/www/test"

<Directory "/var/www/test">

    AuthUserFile  /etc/httpd/apacheuser            讀取的認證文件

    AuthName    "Please input user and password!!"             訪問頁面

    AuthType    basic                   基本的認證方式

    Require user admin                  只容許admin用戶訪問

</Directory>

[root@apache1 httpd]# systemctl restart httpd.service

 

訪問

 

 

 

[root@apache1 httpd]# vim /etc/httpd/conf/httpd.conf

 

DocumentRoot "/var/www/test"

<Directory "/var/www/test">

    AuthUserFile  /etc/httpd/apacheuser

    AuthName    "Please input user and password!!"

    AuthType    basic

    Require valid-user                    ##此時容許全部用戶登錄

</Directory>

[root@apache1 httpd]# systemctl restart httpd.service

這樣全部的用戶均可以登錄

 

 

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

虛擬主機的建立:

 

[root@apache1 httpd]# cd /etc/httpd/conf.d/

[root@apache1 conf.d]# vim default.conf

 

<VirtualHost _default_:80>

        DocumentRoot    /var/www/html

        CustomLog       "logs/default.log" combined

</VirtualHost>

 

[root@apache1 conf.d]# cp default.conf  new.conf

[root@apache1 conf.d]# vim new.conf

 

<VirtualHost *:80>

        ServerName      new.bss.com

        DocumentRoot    "/var/www/html/new"

        CustomLog       "logs/new.log" combined

</VirtualHost>

<Directory "/var/www/html/new">

        Require all granted

</Directory>

 

[root@apache1 conf.d]# cp new.conf  lol.conf

[root@apache1 conf.d]# vim lol.conf

 

<VirtualHost *:80>

        ServerName      lol.bss.com

        DocumentRoot    "/var/www/html/lol"

        CustomLog       "logs/lol.log" combined

</VirtualHost>

<Directory "/var/www/html/lol">

        Require all granted

</Directory>

 

建立虛擬目錄,須要與文件中寫的一致

[root@apache1 conf.d]# mkdir /var/www/html/new  

[root@apache1 conf.d]# mkdir /var/www/html/lol

 

編寫發佈頁面

[root@apache1 conf.d]# echo "new.txt" > /var/www/html/new/index.html

[root@apache1 conf.d]# echo "lol.txt" > /var/www/html/lol/index.html

 

添加本地解析(須要用哪臺測,就寫在哪臺機子上)

[root@apache1 conf.d]# vim /etc/hosts

192.168.52.147  lol.bss.com   new.bss.com  www.bss.com

 

測試:

 

 

 

 

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

Apache: php與cgi

 

Php的支持:

[root@apache1 conf.d]# yum install php -y

[root@apache1 conf.d]# vim  /var/www/html/index.php  ##寫入php測試頁面

<?php

phpinfo();

?>

 

編輯apache配置文件,在默認訪問頁面的模塊加入php頁面

<IfModule dir_module>

    DirectoryIndex  index.php   index.html

</IfModule>

 

[root@apache1 conf.d]# systemctl restart httpd.service

測試:

 

 

Cgi的支持:

[root@apache1 conf.d]# mkdir /var/www/html/cgi

[root@apache1 conf.d]# vim /var/www/html/cgi/index.cgi

 

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print `date`

 

[root@apache1 conf.d]# chmod +x /var/www/html/cgi/index.cgi ##給予cgi可執行權限

[root@apache1 conf.d]# /var/www/html/cgi/index.cgi       ##執行cgi腳本

Content-type: text/html

 

Wed Mar 13 21:30:25 CST 2019

 

可是此時頁面訪問只會顯示腳本文件內容,不會執行。

此時咱們編輯apache配置文件,

[root@apache1 conf.d]# vim /etc/httpd/conf/httpd.conf

 

<Directory /var/www/html/cgi>        ##添加此模塊

    Options +ExecCGI

    ADDHandler  cgi-script .cgi

</Directory>

 

[root@apache1 conf.d]# systemctl restart httpd.service

 

測試:

 

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

https加密認證

[root@apache1 conf.d]# yum install mod_ssl.x86_64  crypto-utils.x86_64 -y

[root@apache1 ~]# genkey www.bss.com

 

 

[root@apache1 conf.d]# vim /etc/httpd/conf.d/ssl.conf  ##根據圖中所示路徑修改下面兩行

 

SSLCertificateFile /etc/pki/tls/certs/www.bss.com.crt                                ##100行

 

SSLCertificateKeyFile /etc/pki/tls/private/www.bss.com.key                      ##107行

 

[root@apache1 conf.d]# systemctl restart httpd   ##重啓服務

 

 

 

地址轉換:

Vim /etc/hosts

192.168.52.147    lls.bss.com

[root@apache1 ~]# vim  /etc/httpd/conf.d/bss.conf

 

<Virtualhost *:443>

         ServerName lls.bss.com

         DocumentRoot "/var/www/lls"

         CustomLog "logs/lls.log" combined

         SSLEngine on

         SSLCertificateFile /etc/pki/tls/certs/www.bss.com.crt

         SSLCertificateKeyFile /etc/pki/tls/private/www.bss.com.key

</Virtualhost>

 

<Virtualhost *:80>

         ServerName lls.bss.com

         RewriteEngine on

         RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]

</Virtualhost>

 

[root@apache1 ~]# mkdir /var/www/lls

[root@apache1 ~]# echo "GOD" > /var/www/lls/index.html

[root@apache1 ~]# systemctl restart httpd

 

 

 

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

代理服務器:

可上網的機器

[root@apache1 ~]# yum install squid.x86_64 -y

[root@apache1 ~]# vim /etc/squid/squid.conf

 

http_access allow all                 ##第56行,容許全部人經過

 

http_port 3128                                ##59行

 

cache_dir ufs /var/spool/squid 100 16 256           ##62行,

 

[root@apache1 ~]# systemctl restart squid

 

在不能上網的機器上:

Edit > Preferences > Advanced > Network >  Settings > Manual proxy configuration > HTTP Proxy

 

相關文章
相關標籤/搜索