Apache 編譯安裝2.2

來源:互聯網
上載者:User

標籤:linux基礎營運學習

一、Apache介紹

1、Apache是什嗎?

  Apache是世界使用排名第一的Web伺服器軟體。它可以運行在幾乎所有廣泛使用的電腦平台上,由於其跨平台和安全性被廣泛使用,是最流行的Web伺服器端軟體之一。它快速、可靠並且可通過簡單的API擴充,將Perl/Python等解譯器編譯到伺服器中。同時Apache音譯為阿帕奇,是北美印第安人的一個部落,叫阿帕奇族,在美國的西南部。Apache源於NCSA server服務。

1.1 Apache的應用場合

  1》使用Apache運行靜態HTML頁,圖片,(處理靜態檔案不如Nginx)

  2》使用Apache結合PHP、Perl等程式,lamp被稱之為經典組合

  3》使用Apache結合Tomcat/resin運行jsp,java等程式,成為中小企業的首選

  4》使用Apache做代理.負載平衡,rewrite規則過濾等

二、Apache的編譯安裝

2、開始安裝Apache軟體包

  Apache的安裝非常簡單,具體操作過程如下

2.1 下載安裝包

[[email protected] /]# mkdir -p /home/loveyu/tools[[email protected] /]# cd  /home/loveyu/tools[[email protected] tools]# wget http://mirrors.hust.edu.cn/apache[[email protected] tools]# ls -l-rw-r--r-- 1 root root 7684420 Jan 13 02:38 httpd-2.2.32.tar.gz

2.2 編譯安裝Apache

  編譯安裝參數說明(通過命令./configure -help 查看Apache的編譯參數)

  1》./configure檢查編譯環境是否符合編譯需求,並定義當前程式編譯時間啟用哪個特性或功能,以及安裝路徑的定義

  2》--prefix=/usr/local/apache指定Apache的安裝路徑為/usr/local/apache,如果不指定安裝路徑,則為預設路徑為/usr/local/apache

  3》--enable-deflate提供對內容的壓縮傳輸編碼支援,一般html,js,css的內容的網站,使用此參數會極大提高傳輸速度,提升訪問者的體驗

  4》--enable-expires啟用允許通過設定檔控制http的"expires:"金額"cache-control"頭內容,及即對網站圖片,js,css等內容,提供用戶端瀏覽器緩衝設定。這是Apache調優的一個重要選項之一

  5》--enable-heads 提供允許對http要求標頭的控制

  6》--with-mpm-worker選擇Apache mpm的模式為worker模式,因worker模式原理是更多的使用線程來處理請求,所以可以處理更多的並發請求,而系統資源的開銷小於基於進程的mpm prefork(預設為他)

  7》--enable-rewrite 提供基於URL規則的重寫功能。即根據已知URL地址,轉換其他想要的URL地址,這是Apache調優的一個重要選項之一,(偽靜態功能)

  8》 --enable-so啟用Apache服務的DSO(全稱Dynamic Shared Object,動態共用對象)支援,即在以後可以以DSO的、方式編譯安裝共用模組,這個模組本身不能以DSO方式編譯

[[email protected] tools]#./configure --prefix=/usr/local/apache --enable-deflate --enable-expires --enable-heads --with-mpm-worker --enable-rewrite  --enable-so[[email protected] tools]#make    #執行編譯命令[[email protected] tools]#make install [[email protected]  tools]# ln -s /var/local/apache/   /application/apache #將其做個做連結,方便使用[[email protected]  tools]# ls -ld /application/apachelrwxrwxrwx 1 root root 17 May  7 11:45 apache -> /usr/local/apache[[email protected] local]# /application/apache/bin/apachectl -l檢查安裝

   /application/apache/bin/目錄下apachectl是一個shell指令碼,用來啟動、檢查、Apache服務。

2.2 檢驗安裝成果

   apache 完成安裝後並不直接提供服務,需要先啟動Apache服務,具體操作如下

/application/apache/bin/apachectl  start[[email protected] local]# netstat -lnt   #檢查80連接埠lsof -i tcp:80   #反查80連接埠ps -ef |grep http查看進程在瀏覽器中輸入192.168.0.102出現IT works則表時Apache安裝成功

注意:在此過程中要學會看/application/apache/logs/error_log Apache的錯誤記錄檔。

2.3 部署一個簡單的web網站

   Apache的預設網站目錄,是安裝目錄下的/application/apache/htdocs,這可以從Apache主設定檔/application/apache/conf/httpd.conf中查到

[[email protected] local]# cat /application/apache/conf/httpd.conf|grep -i Documentroot# DocumentRoot: The directory out of which you will serve yourDocumentRoot "/usr/local/apache/htdocs"執行預設的Apache網站目錄# This should be changed to whatever you set DocumentRoot to.# access content that does not live under the DocumentRoot.[[email protected] local]#cat  /application/apache/htdocs/index.html<html><body><h1>my name is fenghui</h1></body></html>

    650) this.width=650;" src="https://s2.51cto.com/wyfs02/M01/9D/8F/wKiom1mB08HCSyt2AAF-qDjYJPg497.png" title="Q3I3310C)K~1%2ZJ493R~3Y.png" alt="wKiom1mB08HCSyt2AAF-qDjYJPg497.png" />

2.4Apache的bin和log子目錄說明

1》bin 下的子目錄ab            #Apache http伺服器效能測試工具,簡單易用,同類軟體有jmeter,loadrunner,webbench等apachectl     #Apache的啟動命令,apachectla是一個指令碼apxs          #它是一個為Apache http伺服器編譯和安裝擴充模組的工具,在進行DSO方式模組編譯時間會用到.如後文在編譯PHP軟體時就用到了此命令--with-apxs2=/application/apache/bin/apxshtcacheclean  #這是清理磁碟緩衝區的命令,需要在編譯時間指定相關參數才可以,一般很少用htpasswd      #建立和更新基本認證檔案,如,配置nagios等監控服務時會用到httpd         #httpd為apache的控制命令,也還可以用apachect執行httpdrotatelogs   #apache 內建的日誌輪詢命令,也可以用,但在老男孩的工作情境習慣有cronolog等替代2》logs子目錄(這是Apache預設的日誌路徑包括錯誤記錄檔即訪問日誌)access_log   #這是Apache的預設訪問記錄檔,使用tail -f access.log 可以查看網站使用者訪問情況資訊error_log    #這是Apache的錯誤記錄檔檔案,如果Apache出現啟動故障等問題時,一定要看這個檔案httpd.pid    #這是httpd的pid檔案,http進程啟動後,會把所有進程的ID號寫到此檔案[[email protected] apache]# ps -ef |grep httpd|grep -v greproot     28666     1  0 11:56 ?        00:00:00 /usr/local/apache/bin/httpd -k startdaemon   28667 28666  0 11:56 ?        00:00:00 /usr/local/apache/bin/httpd -k startdaemon   28668 28666  0 11:56 ?        00:00:00 /usr/local/apache/bin/httpd -k startdaemon   28669 28666  0 11:56 ?        00:00:00 /usr/local/apache/bin/httpd -k startdaemon   28670 28666  0 11:56 ?        00:00:00 /usr/local/apache/bin/httpd -k startdaemon   28671 28666  0 11:56 ?        00:00:00 /usr/local/apache/bin/httpd -k startdaemon   28727 28666  0 12:02 ?        00:00:00 /usr/local/apache/bin/httpd -k start[[email protected] apache]# cat logs/httpd.pid 286663》modules Apache的模組目錄,比如PHP,memcaceh等模組編譯後都在這裡

2.5Apache的擴充檔案

Apache的擴充設定檔是通過在httpd.conf主設定檔中嵌入include命令實現的,不過預設情況是注釋狀態摘錄http.conf主設定檔 中部分include行,如下所示;# Virtual hosts#Include conf/extra/httpd-vhosts.conf如果取消此行前面的#號,表示開啟虛擬機器主機的配置,即載入conf/extra/httpd-vhosts.conf# Local access to the Apache HTTP Server Manual#Include conf/extra/httpd-manual.conf# Distributed authoring and versioning (WebDAV)#Include conf/extra/httpd-dav.conf
[[email protected] conf]# tree -L 2 /application/apache/conf/extra//application/apache/conf/extra/├── httpd-autoindex.conf├── httpd-dav.conf├── httpd-default.conf  #這個檔案配置的是Apache的相關服務參數,如逾時時間,保持連線時間等├── httpd-info.conf├── httpd-languages.conf  #語言支援配置├── httpd-manual.conf├── httpd-mpm.conf   #伺服器集區管理,也就是最佳化Apache的一個設定檔,如選擇Apache的模式及配置串連數等,常用的模式有worker模式和profork模式├── httpd-multilang-errordoc.conf├── httpd-ssl.conf   #提供Apache SSL支援設定檔├── httpd-userdir.conf└── httpd-vhosts.conf   #這是虛擬機器主機的設定檔

2.6Apache虛擬機器主機的配置

2.6.1基於網域名稱的虛擬機器主機實戰配置

   1》開啟設定檔httpd.conf中包含httpd-vhosts.conf檔案配置

使用vi命令編輯Apache主設定檔httpd.conf。具體步驟:[[email protected] loveyu]# cd /application/apache/conf/[[email protected] conf]# cp httpd.conf httpd.conf.loveyu.20170511[[email protected] conf]# vi httpd.conf404 # Virtual hosts405 Include conf/extra/httpd-vhosts.conf#取消"#"號等於開啟虛擬機器主機的配置

   2》配置虛擬機器主機(如果預設的虛擬機器主機配置不刪除,重啟Apache會報語法錯誤)進入虛擬機器主機設定檔,編輯虛擬機器主機設定檔 vi httpd-vhosts.conf具體操作如下

[[email protected] /]# cd /application/apache/conf/extra/[[email protected] extra]# cp httpd-vhosts.conf httpd-vhosts.conf.loveyu.20170511[[email protected] extra]# grep -v \# httpd-vhosts.confNameVirtualHost *:80<VirtualHost *:80>ServerAdmin [email protected]DocumentRoot "/var/blog"     #(這個網站目錄必須存在不然會報錯)ServerName blog.feitianxihuanni.orgServerAliasfeitain.orgErrorLog "logs/blog-error_log"CustomLog "logs/blog-access_log" common</VirtualHost>[[email protected] extra]# mkdir /var/blog -p#檢查語法錯誤:[[email protected] extra]# ../../bin/apachectl -tSyntax OK

   3》建立虛擬機器主機的網站目錄和首頁index.html(測試用)

     [[email protected] extra]# mkdir /var/blog -p

     [[email protected] extra]# chown -R apache.apache /var/blog賦予許可權,這個目錄就是將來放程式用的。此處的授權相當重要,本命令不是最安全的配置,更最佳化的網站目錄許可權控制方法,見後文最佳化Apache部分,對於本節可以不執行

[[email protected] extra]# ../../bin/apachectl  graceful重啟Apache服務

#提示:這裡是graceful參數,graceful表示優雅的重啟,這個參數可以在重啟是使正在瀏覽的使用者沒有感覺,不會強行中斷使用者的訪問參數,而是處理完畢請求後在重新啟動

   4》在我們的膝上型電腦上編輯hosts檔案添加如下內容

     hosts檔案的通用路徑為:C:\Windows\System32\drivers\etc,hosts檔案是本地的DNS解析檔案,其功能是把指定的網域名稱解析成對應的IP,多個網域名稱對應一個IP,預設情況下hosts檔案中的配置解析優先於DNS伺服器。

    192.168.0.102www.feitian.com添加到hosts檔案中,下面是虛擬機器主機的設定檔,加入了擴充模組,在後面的部落格中我們會介紹。

[[email protected] extra]# egrep  -v "^#|^$"  httpd-vhosts.confNameVirtualHost *:80<VirtualHost *:80>    ServerAdmin [email protected]    DocumentRoot "/var/blog"    ServerName www.feitian.com    ServerAliasfeitian.com    ErrorLog "logs/blog-error_log"     # CustomLog "logs/blog-access_log" common    CustomLog "|/usr/local/sbin/cronolog /var/logs/access_www_%w.log" combinedExpiresActive OnExpiresDefault "access plus 12 month"ExpiresByType text/css "now plus 12 month"ExpiresByType image/jpeg "access plus 12 months"ExpiresByType image/x-icon "access plus 2 months"ExpiresByType text/javascript "access plus 12 month"ExpiresByType image/gif "access plus 12 month"ExpiresByType image/jpg "access plus 12 mont"ExpiresByType image/png "access plus 12 mont"ExpiresByType application/x-shockwave-flash "access plus 12 month"ExpiresByType video/x-flv "access plus 12 month"<ifmodule mod_deflate.c>  AddOutputFilterByType DEFLATEtext/html text/plain text/css  text/javascript  text/cssAddOutputFilterByType DEFLATEapplication/x-httpd-phpAddOutputFilterByType DEFLATEapplication/x-javascriptAddOutputFilter DEFLATE js cssDeflateCompressionLevel 6  SetOutputFilter DEFLATE  </ifmodule> </VirtualHost>

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M01/9D/A3/wKiom1mDAwqBdDbnAAIeMArH8jA228.png" title=")JKK42VURQGEACDZC3[AIUA.png" alt="wKiom1mDAwqBdDbnAAIeMArH8jA228.png" />

下面是網站目錄中的index.html檔案

[[email protected] extra]# ll /var/blog/total 4-rw-r--r-- 1 root root 161 Aug  3 18:50 index.html[[email protected] extra]# cat /var/blog/index.html <html><head><title>feitian blog</title></head> <body><br><br><br><br>I am fenghui<br><br>https://user.qzone.qq.com/2219701263/infocenter</body></html>


本文出自 “13122323” 部落格,請務必保留此出處http://13132323.blog.51cto.com/13122323/1953402

Apache 編譯安裝2.2

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.