Apache與nginx對比優勢:
經筆者測試:對於提供純靜態文件HTML頁面、圖片等服務,Apache確實要比nginx遜色一些,但在結合PHP引擎服務及其它很多方面並不比nginx差,而Apache的穩定性更好,這也是爲什麼Apache仍然佔據WEB服務排名第一的一個原因。
一、安裝Apache
(1) 卸載系統默認安裝的Apache軟件包
操作系統自帶的Apache,版本都比較低。更新不及時
(2)下載線上常用的穩定版,這裏以2.2系列的版本爲例
右鍵選擇快捷方式,在Linux系統裏面使用wget下載即可。
[root@localhost ~]# wgethttp://mirrors.cnnic.cn/apache//httpd/httpd-2.2.27.tar.gz --2014-03-13 15:33:19-- http://mirrors.cnnic.cn/apache//httpd/httpd-2.2.27.tar.gz 正在解析主機 mirrors.cnnic.cn... 123.125.244.87 正在連接 mirrors.cnnic.cn|123.125.244.87|:80... 已連接。 已發出 HTTP 請求,正在等待迴應... 200 OK 長度:7519677 (7.2M) [application/x-gzip] 正在保存至: “httpd-2.2.27.tar.gz” 6%[===> ] 515,757 159K/s eta(英國中部時 7%[===> ] 564,717 157K/s eta(英國中部時 8%[====> ] 613,677 ……………………………………………………………………………………………………………………………………………………… 138K/s eta(英國中部時100%[=============================================================>]7,519,677 139K/s in 52s 2014-03-13 15:34:12 (141 KB/s) - 已保存“httpd-2.2.27.tar.gz”[7519677/7519677]) (3)解壓: (1)tar -zxvf httpd-xx.tar.gz (2)cd 到解壓目錄 (3) less README (4)下載keyts文件進行安全驗證: 1)進入http子目錄,下載keys文件 2)使用gpg導入keys文件 3)使用gpg --verify httpd-XXX.tar.gz.asc進行驗證 (4)./configure --help 查看編譯參數選項 [root@localhost httpd-2.2.27]#./configure --prefix=/application/apache2.2.22 --enable-deflate --enable-expires--enable-headers --enable-modules=most --enable-so --with-mpm=worker--enable-rewrite (5)編譯安裝 [root@localhost httpd-2.2.27]# echo $? 0 [root@localhost httpd-2.2.27]# make&& make install ./configure後的參數說明 ./configure \ --prefix=/application/apache2.2.22 安裝路徑 --enable-deflate \ 壓縮內容編碼支持 --enable-expires \ 提供客戶端緩存支持 --enable-headers \ HTTP請求頭控制 --enable-modules=most \ --enable-so \ 激活Apache服務的DSO支持 --with-mpm=worker \ Apache模式爲worker模式(線程處理請求,資源開銷比 (prefork小) --enable-rewrite 激活rewrite功能模塊,僞靜態,提供URL重寫 (6)創建軟連接 [root@localhost ~]# ln -s/application/apache2.2.22/ /application/apache [root@localhost ~]# ll /application/apache lrwxrwxrwx. 1 root root 26 4月 9 14:55 /application/apache ->/application/apache2.2.22/ 安裝完成之後創建軟連接的意義,根據線上經驗總結: ln -s /application/apache2.22 /application/apache 1)去掉Apache版本號 2)將Apache安裝路徑通過軟連接的方式更改爲/application/apache方便人類使用。 3)安裝時指定版本號路徑便於查看當前分區當前使用的Apache版本、也方便以後升級。 4)人類使用路徑/application/apache。 5)當Apache軟件升級帶新版本號後,刪除原來軟連接在重新建立新的到/application/apache的軟連接即可。 6)程序中如果有引用Apache路徑的地方,不需要做任何更改,因爲升級後訪問路徑還是/application/apache。 到此,Apache的安裝工作就完成了。 /application/apache/bin/apachectl 直接敲回車,查看有哪些參數可供使用 [root@localhost ~]#/application/apache/bin/apachectl Usage: /application/apache2.2.22/bin/httpd[-D name] [-d directory] [-f file] [-C"directive"] [-c "directive"] [-kstart|restart|graceful|graceful-stop|stop] [-v] [-V] [-h] [-l] [-L] [-t][-T] [-S] Options: -Dname : define a name for usein <IfDefine name> directives -ddirectory : specify an alternateinitial ServerRoot -ffile : specify an alternateServerConfigFile -C"directive" : processdirective before reading config files -c"directive" : processdirective after reading config files -elevel : show startup errors oflevel (see LogLevel) -Efile : log startup errors to file -v : show versionnumber -V : show compilesettings -h : listavailable command line options (this page) -l : list compiledin modules -L : listavailable configuration directives -t-D DUMP_VHOSTS : show parsed settings(currently only vhost settings) -S : a synonym for-t -D DUMP_VHOSTS -t-D DUMP_MODULES : show all loaded modules -M : a synonym for -t -D DUMP_MODULES -t : run syntaxcheck for config files -T : start withoutDocumentRoot(s) check 7)/application/apache/bin/apachectl -l 查看編譯的內容,默認Apache會編譯一些必須的選項 [root@localhost ~]# /application/apache/bin/apachectl -l Compiled in modules: core.c mod_authn_file.c mod_authn_dbm.c mod_authn_anon.c mod_authn_dbd.c mod_authn_default.c mod_authz_host.c mod_authz_groupfile.c mod_authz_user.c mod_authz_dbm.c mod_authz_owner.c mod_authz_default.c mod_auth_basic.c mod_auth_digest.c mod_dbd.c mod_dumpio.c mod_reqtimeout.c mod_ext_filter.c mod_include.c mod_filter.c mod_substitute.c mod_deflate.c mod_log_config.c mod_logio.c mod_env.c mod_expires.c mod_headers.c mod_ident.c mod_setenvif.c mod_version.c worker.c http_core.c mod_mime.c mod_dav.c mod_status.c mod_autoindex.c mod_asis.c mod_info.c mod_cgid.c mod_dav_fs.c mod_vhost_alias.c mod_negotiation.c mod_dir.c mod_p_w_picpathmap.c mod_actions.c mod_speling.c mod_userdir.c mod_alias.c mod_rewrite.c mod_so.c (7)檢驗安裝成果: Code: [root@localhost ~]#/application/apache/bin/apachectl start httpd: Could not reliably determine theserver's fully qualified domain name, using localhost.localdomain forServerName(此報錯在httpd.conf配置文件裏面有詳細的解決方法1、在ServerName後面直接填寫申請的合法域名 2、填寫服務器的ip地址或127.0.0.1本地迴環地址)
[root@localhost ~]# netstat -lnt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 :::80 :::* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN [root@localhost ~]# [root@localhost ~]# lsof -i tcp:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 26978 root 4u IPv6 61958 0t0 TCP *:http (LISTEN) httpd 26980 daemon 4u IPv6 61958 0t0 TCP *:http (LISTEN) httpd 26981 daemon 4u IPv6 61958 0t0 TCP *:http (LISTEN) httpd 26982 daemon 4u IPv6 61958 0t0 TCP *:http (LISTEN) [root@localhost ~]# ps -ef |grep http root 26978 1 0 14:58 ? 00:00:00 /application/apache2.2.22/bin/httpd-k start daemon 26979 26978 0 14:58 ? 00:00:00/application/apache2.2.22/bin/httpd -k start daemon 26980 26978 0 14:58 ? 00:00:00/application/apache2.2.22/bin/httpd -k start daemon 26981 26978 0 14:58 ? 00:00:00/application/apache2.2.22/bin/httpd -k start daemon 26982 26978 0 14:58 ? 00:00:00/application/apache2.2.22/bin/httpd -k start root 27067 26941 0 14:59 pts/2 00:00:00 grep http 說明: Apache安裝完成之後,不提供任何服務,需要啓動 /application/apache/bin/apachectl start 啓動Apache服務 netstat - lnt 查看啓動端口 lsof -i tcp:80 使用lsof反查tcp的80端口是不是HTTP服務 ps -ef |grep http 查看HTTP的進程 (8)訪問網站頁面
二、網站打不開排查方法:
1、iptables防火牆和selinux是否關閉 /etc/init.d/iptables stop 這是關閉防火牆的命令,如果是生產環境則是允許80端口的訪問,而不是關閉防火牆 iptables -I INPUT -p tcp --dport 80 -j ACCEPT 非正式環境可以禁止防火牆開機啓動,便於學習調試Apache服務。 chkconfig iptables stop /etc/init.d/iptables status 查看iptables的當前狀態 tail -100/application/apache/logs/error_log [root@localhost ~]# service iptables stop iptables:清除防火牆規則:[確定] iptables:將鏈設置爲政策 ACCEPT:filter [確定] iptables:正在卸載模塊:[確定] [root@localhost ~]# setenforce 0 [root@localhost ~]# getenforce 0 Permissive
三、部署一個簡單WEB站點
Apache的默認站點目錄,是安裝目錄下的/application/apache/htdocs這可以從Apache主配置文件/application/apache/conf/httpd.conf中查到,內容如下:
[root@localhost ~]# grep -i DocumentRoot/application/apache/conf/httpd.conf # DocumentRoot: The directory out of whichyou will serve your DocumentRoot"/application/apache2.2.22/htdocs" # This should be changed to whatever youset DocumentRoot to. # access content that does not live underthe DocumentRoot.
如果想要部署網站服務,只需要把開發好的程序全部放置到/application/apache2.2.22/htdocs下面即可
這裏我們進入到/application/apache2.2.22/htdocs下,刪除掉Apache默認的首頁index.html(原始內容爲itworks),然後建立一個index.html,加入一段網頁內容並保存
此時在瀏覽器端輸入http://IP地址就應該可以看到網頁內容了。
html相關代碼如下: <html><head> <title>白居易的詩</title> <bgsound src="秋日的私語.MP3"></bgsound> </head> <body background="pb.jpg"text="rgb(255,0,0)"> <p> <b><font face="華文彩雲" color="green"size="7">錢塘湖春行</font> </b> </p><p> <font face="華文新魏" color="red" size="6">孤山寺北賈亭西,水面初平雲腳低。</font> <b><font face="華文新魏"color="red" size="6">幾處早鶯爭暖樹,誰家新燕啄春泥。</font><b> <i><font face="華文新魏"color="red" size="6">亂花漸欲迷人眼,淺草才能沒馬蹄。</font><i> <u><font face="華文新魏"color="red" size="6">最愛西湖行不足,綠揚蔭裏白沙堤。</font><u> </u></u></i></i></b></b></p><p><b><b><i><i><u><u> <font face="宋體"color="blue" size="4">【說明】此詩爲作者任杭州刺史時作。寫西湖的山光水色、花草亭樹,>加上早鶯、新燕生機盎然,旖旎動人。是摹寫西湖秋色名篇。</font><b></b></u></u></i></i></b></b></p><b><b><i><i><u><u><b> </b></u></u></i></i></b></b></body></html>