Apache是什么:
Apache HTTP Server(簡稱Apache)是Apache軟件基金會的一個高性能、功能強大、穩定可靠、又很靈活的開發源代碼的web服務軟件,對linux的支持相對好,有很多產品可以支持ssl技術,支持多個虛擬主機,到目前為止,市場占有率44.9%,世界上很多注明的網
站如百度、阿里、淘寶、亞馬遜、雅虎等都有在使用apache服務,它的成功之處主要在於他的開源、有一支開發的開發隊伍、支持跨平台的應用(幾乎可以在所有的windows、linux、unix系統運行)以及它的可移植性等是當前互聯網最流行的web服務端軟件之一。
Apache源於NCSA httpd服務,經過多次修改,成為世界上最流行的web服務器軟件之一,Apache取自“a patchy server”的讀音,意思是充滿補丁的服務器,因為它是自由軟件,所以不斷有人來為它開發新的功能、新的特性、修復原來的缺陷,才成為我們今天使用的安全穩定的web服務器。
注:以上web serve排行信息來源於https://w3techs.com/technologies/overview/web_server/all
Apache的特點及應用場合:
優點:
- 功能強大
- 配置簡單
- 速度快
- 應用廣泛
- 性能穩定可靠
- 可做代理服務器或負載均衡來使用
應用場合:
- 使用apache運行靜態html網頁、圖片(處理靜態小文件能力不及nginx)
- 使用apache結合php引擎運行php、perl、Python等程序,LAMP被稱之為經典組合。
- 使用apache結合tomcat/resin運行jsp,java等程序,成為中小型企業的首先。
- 使用apache做代理、負載均衡rewrite規則過濾等等。
提示:
進年來,網上比較流行的nginx server,對於提供純靜態小文件HTML頁、圖片等服務,Apache確實要不nginx遜色一些,但在結合PHP引擎提供php服務及其它很多方面並不比nginx差,而apache的穩定性更好,這也是為什么Apache服務排名仍然第一的原因之一。
下面我們來安裝Apache服務:
環境→系統:centos 6.5 軟件:httpd-2.2.27.tar.gz
[root@bqh-119 tools]# wget http://archive.apache.org/dist/httpd/httpd-2.2.27.tar.gz [root@bqh-119 tools]# ll 總用量 7344 -rw-r--r-- 1 root root 7519677 7月 13 22:39 httpd-2.2.27.tar.gz [root@bqh-119 tools]# tar xf httpd-2.2.27.tar.gz [root@bqh-119 tools]# ll 總用量 7348 drwxr-xr-x 11 nginx 500 4096 3月 14 2014 httpd-2.2.27 -rw-r--r-- 1 root root 7519677 7月 13 22:39 httpd-2.2.27.tar.gz [root@bqh-119 tools]# cd httpd-2.2.27 [root@bqh-119 httpd-2.2.27]# ./configure \
--prefix=/application/apache2.2.27
--enable-deflate \
--enable-expires \
--enable-headers \
--enable-modules=most \
--enable-so \
--with-mpm=worker \
--enable-rewrite
參數解釋:
--prefix=/application/apache2.2.27 安裝路徑,默認路徑為/usr/local/apache2
--enable-deflate 提供對內容的壓縮傳輸編碼支持,一般html,js,css等內容的站點,用此參數功能會大大提高傳輸速度,提升訪問者訪問體驗,調優選項之一
--enable-expires 激活允許通過配置文件控制http的"Expires:"和"Cache-Control:"頭內容,即對網站圖片、js、css等內容,提供在客戶端瀏覽器緩存的設置,調優選項之一
--enable-headers 提供允許對http請求頭的控制
--enable-modules=most 可以將一些不常用的,不在缺省常用模塊中的模塊編譯進來
--enable-so 激活apache服務的DSO(全稱Dynamic Shared object,動態共享對象)支持,即在以后可以以DSO的方式編譯安裝共享模塊,這個模塊本身不能以DSO方式編譯
--with-mpm=worker 選擇apache mpm的模式為worker模式,因worker模式原理是更多的使用線程來處理請求,所有可以處理更多的並發請求,而系統資源的開銷小於基於進程的MPM prefork。如果不指定此參數,默認的模式是prefork的進程模式,雖然消耗資源,但是更穩定,因此,選擇prefork進程模式而不選擇worker模式也是可以考慮的,調優選項之一
--enable-rewrite 提供基於URL規則的重寫功能,即根據已知URL地址,轉換其他想要的URL地址,偽靜態模塊。調優必選項
[root@bqh-119 httpd-2.2.27]# make
... ......此處省略 [root@bqh-119 httpd-2.2.27]# echo $? 0 [root@bqh-119 httpd-2.2.27]# make install Making install in srclib make[1]: Entering directory `/root/tools/httpd-2.2.27/srclib' Making install in apr make[2]: Entering directory `/root/tools/httpd-2.2.27/srclib/apr' make[3]: Entering directory `/root/tools/httpd-2.2.27/srclib/apr' make[3]: Nothing to be done for `local-all'. make[3]: Leaving directory `/root/tools/httpd-2.2.27/srclib/apr' /root/tools/httpd-2.2.27/srclib/apr/build/mkdir.sh /application/apache2.2.27/lib /application/apache2.2.27/bin /application/a pache2.2.27/build \ ... ......此處省略 mkdir /application/apache2.2.27/logs Installing CGIs mkdir /application/apache2.2.27/cgi-bin Installing header files Installing build system files Installing man pages and online manual mkdir /application/apache2.2.27/man mkdir /application/apache2.2.27/man/man1 mkdir /application/apache2.2.27/man/man8 mkdir /application/apache2.2.27/manual make[1]: Leaving directory `/root/tools/httpd-2.2.27' [root@bqh-119 httpd-2.2.27]# echo $? 0
做個軟連接(快捷方式)便於區分當前使用版本、安全考慮隱藏版本、方便以后升級等:
[root@bqh-119 httpd-2.2.27]# ln -s /application/apache2.2.27/ /application/apache [root@bqh-119 httpd-2.2.27]# ll /application/ 總用量 8 lrwxrwxrwx 1 root root 26 7月 13 23:36 apache -> /application/apache2.2.27/ drwxr-xr-x 15 root root 4096 7月 13 23:29 apache2.2.27
到此,Apache服務安裝已完成。
查看編譯的生效模塊:/application/apache/bin/apachectl -l
啟動前檢查配置文件語法:/application/apache/bin/apachectl -t
啟動Apache服務:/application/apache/bin/apachectl start
重啟Apache服務:/application/apache/bin/apachectl graceful
查看服務狀態:lsof -i:80 或 ps -ef|grep httpd
[root@bqh-119 httpd-2.2.27]# /application/apache/bin/apachectl -t httpd: apr_sockaddr_info_get() failed for bqh-119 httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName Syntax OK [root@bqh-119 httpd-2.2.27]# /application/apache/bin/apachectl start httpd: apr_sockaddr_info_get() failed for bqh-119 httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName [root@bqh-119 httpd-2.2.27]# lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 58405 root 4u IPv6 54509 0t0 TCP *:http (LISTEN) httpd 58407 daemon 4u IPv6 54509 0t0 TCP *:http (LISTEN) httpd 58408 daemon 4u IPv6 54509 0t0 TCP *:http (LISTEN) httpd 58409 daemon 4u IPv6 54509 0t0 TCP *:http (LISTEN) [root@bqh-119 httpd-2.2.27]# ps -ef|grep httpd root 58405 1 0 23:40 ? 00:00:00 /application/apache2.2.27/bin/httpd -k start daemon 58406 58405 0 23:40 ? 00:00:00 /application/apache2.2.27/bin/httpd -k start daemon 58407 58405 0 23:40 ? 00:00:00 /application/apache2.2.27/bin/httpd -k start daemon 58408 58405 0 23:40 ? 00:00:00 /application/apache2.2.27/bin/httpd -k start daemon 58409 58405 0 23:40 ? 00:00:00 /application/apache2.2.27/bin/httpd -k start root 58493 1473 0 23:41 pts/0 00:00:00 grep httpd
我們用curl 測試一下:(192.168.0.119為安裝apache的服務器的ip地址)
[root@bqh-119 httpd-2.2.27]# curl 192.168.0.119 <html><body><h1>It works!</h1></body></html>
瀏覽器測試一下:
ok,到此apache服務安裝成功!
注:如果未顯示It works的頁面,請查看是否關閉了iptables防火牆和selinux。
關閉防火牆:/etc/init.d/iptables stop
允許通過防火牆:iptables -I INPUT -p tcp --dprot 80 -j ACCEPT
臨時關閉selinux,可執行:
setenforce 0 // 設置selinux狀態 1啟用,0警告、不啟用。
getenforce //檢查selinux狀態是否生效
永久關閉selinux功能:
cat /etc/selinux/config
cp /etc/selinux/config /etc/selinux/config.root
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
如果要部署網站服務,需要把開發好的程序全部放置:/application/apache/htdocs
[root@bqh-119 ~]# cd /application/apache/htdocs/ [root@bqh-119 htdocs]# vim index.html [root@bqh-119 htdocs]# cat index.html <html> <head><title>bqh blog</title></head> <bldy> Hello, welcome to bqh's blog. This is my blog address. <a href="https://www.cnblogs.com/su-root/#top">https://www.cnblogs.com/su-root/#top</a> <br></br> Email:1147076062@qq.com </body> </html>
-------------------------------------------------
優化httpd.conf配置文件,避免暴露目錄結構:
[root@bqh-119 conf]# sed -n '132,160p' httpd.conf <Directory "/application/apache2.2.27/htdocs"> # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.2/mod/core.html#options # for more information. # Options Indexes FollowSymLinks #如果沒有首頁的情況下會展示目錄結構、 建議把Indexes刪除 或改為-Indexes 、如果把目錄展示禁用后、沒有首頁的情況下會報錯403 # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride None # # Controls who can get stuff from this server. # Order allow,deny Allow from all </Directory> [root@bqh-119 conf]#
Apache服務目錄結構簡要說明:
bin啟動文件目錄說明:
conf配置文件目錄說明:
站點存放目錄、日志存放目錄、模塊目錄說明: