Apache是什麼:php
Apache HTTP Server(簡稱Apache)是Apache軟件基金會的一個高性能、功能強大、穩定可靠、又很靈活的開發源代碼的web服務軟件,對linux的支持相對好,有不少產品能夠支持ssl技術,支持多個虛擬主機,到目前爲止,市場佔有率44.9%,世界上不少註明的網css
站如百度、阿里、淘寶、亞馬遜、雅虎等都有在使用apache服務,它的成功之處主要在於他的開源、有一支開發的開發隊伍、支持跨平臺的應用(幾乎能夠在全部的windows、linux、unix系統運行)以及它的可移植性等是當前互聯網最流行的web服務端軟件之一。html
Apache源於NCSA httpd服務,通過屢次修改,成爲世界上最流行的web服務器軟件之一,Apache取自「a patchy server」的讀音,意思是充滿補丁的服務器,由於它是自由軟件,因此不斷有人來爲它開發新的功能、新的特性、修復原來的缺陷,才成爲咱們今天使用的安全穩定的web服務器。java
注:以上web serve排行信息來源於https://w3techs.com/technologies/overview/web_server/alllinux
Apache的特色及應用場合:nginx
優勢:web
應用場合:apache
提示:vim
進年來,網上比較流行的nginx server,對於提供純靜態小文件HTML頁、圖片等服務,Apache確實要不nginx遜色一些,但在結合PHP引擎提供php服務及其它不少方面並不比nginx差,而apache的穩定性更好,這也是爲何Apache服務排名仍然第一的緣由之一。windows
下面咱們來安裝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配置文件目錄說明:
站點存放目錄、日誌存放目錄、模塊目錄說明: