lamp————apache

Apache是世界使用排名第一的Web服務器軟件。它可以運行在幾乎所有廣泛使用的計算機平臺上,由於其跨平臺和安全性被廣泛使用,是最流行的Web服務器端軟件之一。

它快速、可靠並且可通過簡單的API擴充,將Perl/Python解釋器編譯到服務器中。同時Apache音譯爲阿帕奇

用來提供http://(超文本傳輸服務)


一.安裝軟件和說明

1.[root@server ~]# yum install httpd -y    ##安裝httpd服務軟件


2.root@server ~]# yum install httpd-manual.noarch -y ##安裝httpd的說明


3.[root@server ~]# systemctl start httpd   ##開啓服務
   [root@server ~]# systemctl enable  httpd.service   ##設定服務開機自啓動


4.[root@server ~]# firewall-cmd  --permanent  --add-service=http   ##修改火牆策略 將http服務添加到裏面
   [root@server ~]# firewall-cmd --reload    ##更新火牆策略 讓新加入策略生效


5.排錯(安裝軟件時出現的錯誤)

*)報錯

[root@localhost yum.repos.d]# yum install httpd
Loaded plugins: langpacks

You have enabled checking of packages via GPG keys. This is a good thing.
However, you do not have any GPG public keys installed. You need to download
the keys for packages you wish to install and install them.
You can do that by running the command:
    rpm --import public.gpg.key


*)解決方法

[root@localhost yum.repos.d]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release





二.測試







三.apache的基本信息

主配置目錄 :  /etc/httpd/conf
主配置文件 :  /etc/httpd/conf/httpd.conf
子配置目錄 :  /etc/httpd/conf.d/
子配置文件 :  /etc/httpd/conf.d/*.conf
默認發佈目錄:  /var/www/html
默認發佈文件:  index.html
默認端口:        80
默認安全上下文:    httpd_sys_content_t
程序開啓默認用戶: apache
apache日誌 :       /etc/httpd/logs/*


1.修改默認端口

*)[root@server ~]# vim /etc/httpd/conf/httpd.conf  ##通過修改主配置文件修改端口 apache默認監聽端口80 修改後訪問時加:端口


*)[root@server ~]# systemctl restart httpd.service  ##重啓服務 使新配置生效

*)測試




2.修改默認發佈文件

默認發佈文件就是訪問apache時沒有指定文件名稱時默認訪問的文件
這個文件可以指定多個 有順序

*)[root@server ~]# vim /etc/httpd/conf/httpd.conf  ##通過修改主配置文件修改默認發佈文件
163 <IfModule dir_module>
164     DirectoryIndex index.html  test.html             ##當index.html不存在時訪問test.html


*)[root@server ~]# systemctl restart httpd.service   ##重啓服務 使新配置生效


*)創建默認發佈文件 並在文件中錄入內容 在訪問網站時可查看到文件中內容

     [root@server ~]# cd /var/www/html/

     [root@server html]# vim index.html

     [root@server html]# cat index.html
      LULUBAO index 默認

*)測試

*)按之前修改配置文件中當index.html不存在時訪問test.html

   [root@server html]# vim test.html
   [root@server html]# cat test.html
   LULUBAO test 默認測試
   [root@server html]# rm -fr index.html
   [root@server html]# ls 
   test.html
*)測試



3.修改默認發佈目錄

*)[root@server ~]# mkdir /www/html/ -p    ##創建一個新的目錄 準備將其設定爲默認發佈目錄
     [root@server ~]# cd /www/html/
   

*)[root@server html]# vim index.html          ##在目錄中創建一個文件 此文件內容就爲當前目錄爲默認發佈目錄時訪問當前服務器時出現的迴文
     [root@server html]# cat index.html
     當/www/html/ 爲默認發佈目錄時


*)[root@server ~]# vim /etc/httpd/conf/httpd.conf  ##通過修改主配置文件修改默認發佈目錄

121  DocumentRoot "/www/html"
122
123  <Directory "/www">  ##文件權限的修改
124      Require all granted  ##允許所有
125  </Directory>


*)[root@server ~]# systemctl restart httpd.service   ##重啓服務 使新配置生效

*)測試






四.爲apache配置多個虛擬機

虛擬主機允許從一個httpd服務器(一個ip)同時爲多個網站提供服務。

在本節中,我們將瞭解基於名稱的虛擬主機其中多個主機名都指向同一個IP地址,但是Web服務器根據用於到達站點的主機名提供具有不同內容的不同網站


1.爲準備設置的虛擬主機建立默認發佈目錄

[root@server html]# mkdir /var/www/virtual/c.lulubao.com/html/ -p
[root@server html]# mkdir /var/www/virtual/linux.lulubao.com/html/ -p


2.在默認發佈目錄中添加發布文件 方便測試時查看結果正確與否


3.在子配置文件中添加不同虛擬主機的相關配置

* )[root@server virtual]# cd /etc/httpd/conf.d/            ##位置移動到子配置文件目錄
    [root@server conf.d]# ls
    autoindex.conf  manual.conf  README  userdir.conf  welcome.conf

*)[root@server conf.d]# vim adefault.conf                   ##設置默認指向配置文件
   [root@server conf.d]# cat adefault.conf 
   <VirtualHost_default_:80>
        DocumentRoot "/var/www/html"                           ##站點默認發佈目錄
        CustomLog "logs/www.lulubao.com.log"  combined                    ##站點日誌 combined表示四種日>志的集合
   </VirtualHost> 


*)[root@server conf.d]# vim linux.conf                      ##設置linux的配置文件
     [root@server conf.d]# cat linux.conf
     <VirtualHost *:80>
         ServerName linux.lulubao.com                                  ##指定站點名稱
         DocumentRoot "/var/www/virtual/linux.lulubao.com/html"                 ####站點默認發佈目錄
         CustomLog  "logs/linux.lulubao.com.log"   combined
     </VirtualHost>

    <Directory "/var/www/virtual/linux.lulubao.com/html">    ##給目錄權限
          Require all granted                                              ##允許所有
    </Directory>


*)[root@server conf.d]# vim c.conf
     [root@server conf.d]# cat c.conf
      <VirtualHost *:80>
          ServerName c.lulubao.com
          DocumentRoot "/var/www/virtual/c.lulubao.com/html"
          CustomLog  "logs/c.lulubao.com.log"   combined
      </VirtualHost>

      <Directory "/var/www/virtual/c.lulubao.com/html">
          Require all granted
      </Directory>



4.重啓服務

*)[root@server conf.d]# systemctl restart httpd.service

*)排錯

[root@server conf.d]# > /var/log/messages           ##清空日誌

[root@server conf.d]# systemctl restart httpd.service             ##重其服務
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.

[root@server conf.d]# cat /var/log/messages       ##查看日誌 根據日誌內容一一排錯
Nov 21 01:56:29 server systemd: Starting The Apache HTTP Server...
Nov 21 01:56:29 server httpd: httpd: Syntax error on line 353 of /etc/httpd/conf/httpd.conf: Syntax error on line 4 of /etc/httpd/conf.d/adefault.conf: Expected </VirtualHost_default_:80> but saw </VirtualHost>
Nov 21 01:56:29 server systemd: httpd.service: main process exited, code=exited, status=1/FAILURE
Nov 21 01:56:29 server systemd: Failed to start The Apache HTTP Server.
Nov 21 01:56:29 server systemd: Unit httpd.service entered failed state.

*)排錯後 重啓服務成功




5.測試

*)在測試主機中做好本地解析
     [root@really ~]# vim /etc/hosts


*)測試默認指向

       

*)測試c虛擬機

*)測試linux虛擬機






五.apache內部的訪問控制


1.針對於主機的訪問控制

*)創建一個新發布目錄 並在發佈文件中寫入測試內容



*)在子配置文件的默認配置文件中 寫入test目錄的訪問權限

     [root@server ~]# cd /etc/httpd/conf.d
     [root@server conf.d]# ls
     adefault.conf   c.conf      manual.conf  userdir.conf
     autoindex.conf  linux.conf  README       welcome.conf
     [root@server conf.d]# vim adefault.conf
     [root@server conf.d]# cat adefault.conf  
     <VirtualHost _default_:80>
        DocumentRoot "/var/www/html"
        CustomLog "logs/www.lulubao.com.log"  combined
     </VirtualHost>

     <Directory "/var/www/html/test">
        Order allow,deny                                ##列表讀取順序 後讀取列表會覆蓋先讀取的重複部分                
        Allow from all                                     ##允許所有人訪問
        Deny from 172.25.99.250                   ##不允許指定主機訪問
     </Directory>


*)重啓服務

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


*)測試

172.25.99.250主機



其他主機








2.針對於用戶的訪問控制


*)新建用戶登陸認證文件 並添加信息

     [root@server conf.d]# htpasswd -cm /etc/httpd/userpass admin1         ##新建用戶認證文件/etc/httpd/userpass   並在其中添加用戶admin1的信息
     New password:                      ##輸入admin1的密碼
     Re-type new password:          ##再次輸入密碼確認
     Adding password for user admin1                                                          ##信息提示“成功爲admin1添加了密碼”
     [root@server conf.d]# cat /etc/httpd/userpass
     admin1:$apr1$iIUqqDun$aRDUwiAKC.ScT7yuXL9Xo.                         ##文件成功添加admin1密碼 (密碼加密)


*)在已經存在的認證文件中添加信息

     [root@server conf.d]# htpasswd -m /etc/httpd/userpass admin2            ##在已有文件中添加信息 (去掉參數c 否則會覆蓋原有信息)
     New password:
     Re-type new password:
     Adding password for user admin2
     [root@server conf.d]# cat /etc/httpd/userpass
     admin1:$apr1$iIUqqDun$aRDUwiAKC.ScT7yuXL9Xo.
     admin2:$apr1$JR/XpvZd$tuIBY5i6Hi8RYyn7b0sjZ0


*)新建發佈目錄 在目錄中新建發佈文件



*)在子配置文件的默認配置文件中 寫入admin目錄的訪問權限

     [root@server conf.d]# vim adefault.conf
     <Directory "/var/www/html/admin">
        AuthUserFile /etc/httpd/userpass                                      ##指定用戶認證文件
        AuthName "Please input your name and password"        ##輸入提示語
        AuthType basic                                                                  ##認證方式爲basic
        #Require user admin                                                        ##只允許addmin用戶登陸
        Require valid-user                                                             ##允許所有有效用戶登陸
     </Directory>

*)重啓服務

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


*)測試










六.apache支持的語言


1.html

超文本標記語言,標準通用標記語言下的一個應用。
超文本”就是指頁面內可以包含圖片、鏈接,甚至音樂、程序等非文字元素。
超文本標記語言的結構包括“頭”部分(英語:Head)、和“主體”部分(英語:Body),其中“頭”部提供關於網頁的信息,“主體”部分提供網頁的具體內容。



*)編輯html語言的腳步




*)測試






2.php


*)在默認發佈目錄下新建php語言的腳本



*)安裝php

[root@server home]# yum install php -y


*)測試





3.cgi


*)創建一個cgi目錄 並新建一個cgi語言編寫的腳本


 



*)給腳步文件加執行權限並執行測試一下腳本 確保腳本正常運行


*)



*)重啓服務

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


*)測試






七.https

是以安全爲目標的HTTP通道,簡單講是HTTP的安全版。即HTTP下加入SSL層,HTTPS的安全基礎是SSL,因此加密的詳細內容就需要SSL。 它是一個URI scheme(抽象標識符體系),句法類同http:體系。用於安全的HTTP數據傳輸。https:URL表明它使用了HTTP,但HTTPS存在不同於HTTP的默認端口及一個加密/身份驗證層(在HTTP與TCP之間)。


1.安裝開啓https需要端口的軟件(443端口)

[root@server www]# yum install mod_ssl.x86_64 -y


2.安裝生成安全證書的軟件
[root@server www]# yum install crypto-utils.x86_64 -y



3.新建證書 公鑰 私鑰

[root@server www]#genkey www.lulubao.com


4.編輯配置文件 使新生成的證書 密鑰投入使用

[root@server www]# vim /etc/httpd/conf.d/ssl.conf


*)重啓服務

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


*)測試



 


5.設定https虛擬主機並設定網頁重寫

*)[root@server www]# vim /etc/httpd/conf.d/adefault.conf

^(/.*)$                         ##客戶在瀏覽器地址欄中輸入的內容
https://                       ##強制客戶加密訪問
{HTTP_HOST}$1    ##客戶請求訪問的主機
$1                              ## “$1” 表示^(/.*)$ 的值
[redirect=301]         ## 訪問規則 (臨時轉換) 302爲永久轉換


*)創建發佈目錄和發表文件



*)重啓服務

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


*)測試




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章