1、安裝Apache前準備:
1、檢查該環境中是否已經存在httpd服務的配置文件,默認存儲路徑:/etc/httpd/httpd.conf(這是centos預裝的Apache的一個ent版本,一般我們安裝源代碼版的Apache)。如果已經存在/etc/httpd/httpd.conf,請先卸載或者關閉centos系統自帶的web服務,執行命令:chkconfig httpd off,再或者把centos自帶的httpd服務的80端口改爲其他端口,只要不與我們安裝的Apache服務的端口衝突就可以啦。
停止並卸載Linux系統自帶的httpd服務:
1、service httpd stop
2、ps -ef | grep httpd
3、kill -9 pid號(逐個刪除)
4、rpm -qa |grep httpd
5、rpm -e httpd軟件包
2、下載Apache安裝包
3、解壓到/usr/local下
#tar xzvf httpd-2.2.16.tar.gz
4、配置安裝參數
#cd /usr/local/httpd-2.2.16
在安裝Apache時,我分別針對不同版本進行了安裝,在編譯時是不同的,configure後跟的參數不同。
httpd-2.2.23版本編譯命令:
./configure --prefix=/usr/local/apache2 (安裝目錄參數後面可以不加任何參數,直接安裝即可) make make install
httpd-2.4.3版本編譯命令:
./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre (除了指定Apache的安裝目錄外,還要安裝apr、apr-util、pcre,並指定參數) make make install
編譯時或換成如下參數:(僅供參考)
#./configure --prefix=/usr/local/apache2 --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-rewrite --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
5、安裝報錯情況處理
在編譯Apache(在安裝httpd-2.4.3時遇到的問題)時分別出現了apr not found、APR-util not found、pcre-config for libpcre not found的問題,下面就httpd-2.4.3的這些問題解決來實際操作一把。
http://apr.apache.org/download.cgi 下載apr-1.4.5.tar.gz、apr-util-1.3.12.tar.gz
http://sourceforge.net/projects/pcre/files/latest/download 下載pcre-8.31.zip
1.解決apr not found問題
[root@localhost bin]# tar -zxf apr-1.4.5.tar.gz [root@localhost apr-1.4.5]# ./configure --prefix=/usr/local/apr [root@localhost apr-1.4.5]# make [root@localhost apr-1.4.5]# make install
2.解決APR-util not found問題
[root@localhost bin]# tar -zxf apr-util-1.3.12.tar.gz [root@localhost apr-util-1.3.12]# ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr/bin/apr-1-config [root@localhost apr-util-1.3.12]# make [root@localhost apr-util-1.3.12]# make install
3、解決pcre-config for libpcre not found問題
[root@localhost ~]# unzip pcre-8.31.zip [root@localhost ~]# cd pcre-8.31 [root@localhost pcre-8.31]# ./configure --prefix=/usr/local/pcre [root@localhost pcre-8.31]# make[root@localhost pcre-8.31]# make install
6、apache的啓動、停止
啓動Apache:
#cd /usr/local/apache2/bin
#./apachectl start
停止Apache:
#cd /usr/local/apache2/bin
#./apachectl stop
重啓Apache:
#cd /usr/local/apache2/bin
#./apachectl restart
7、安裝成功後,配置參數修改:
#vi /usr/local/apache/conf/httpd.conf
Listen 80 //apache默認端口,可以修改
DocumentRoot
"/home/mahaibo/apache/htdocs"
//用戶默認訪問的apache目錄
//日誌輸出的2種格式
LogFormat
"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
combined
LogFormat "%h %l %u %t \"%r\"
%>s %b" common
CustomLog logs/access_log common
//訪問日誌:access_log的輸出採用common格式
8、網站的測試:
網站放在/usr/local/apache2/htdocs目錄下
在IE中通過http://localhost:80
如果看到頁面中顯示“It works!”字樣,則代表Apache驗證通過。
如果網站的index後綴是PHP格式的,則要修改httpd.conf配置文件(/usr/local/apache2/conf),在DirectoryIndex增加 index.php。
#
# DirectoryIndex: sets the file that Apache will serve if a directory# is requested.#<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
9、網站的安全加固:
修改文件的所有者和所有組
cd /usr/local/apache
chown -R nobody:root htdocs修改apache下所有文件權限
chmod -R 755 *
10、apache的默認重要配置信息如下:
配置文件:/usr/local/apache2/httpd/httpd.conf
服務器的根目錄:/usr/local/apache2/htdocs
訪問日誌文件:/var/log/httpd/access_log
錯誤日誌文件:/var/log/httpd/error_log
運行apache的用戶:apache
運行apache的組:apache
端口:80
模塊存放路徑:/usr/lib/httpd/modules
11、安裝apache補充說明
安裝apache2 , ./configure ;make;make install。安裝完畢後卻不能找到mod_proxy和mod_rewrite模塊,主要原因是APACHE2.2默認的安裝選項是最小化的安裝,一些擴展模塊在默認的狀態下都沒有被安裝,如果需要要在./configure後用參數指定,比方要用到mod_proxy 和 mod_rewrite兩個模塊進行實驗,所以命令行是:./configure --enable-mods-shared='proxy proxy_ajp proxy_balancer proxy_connect proxy_ftp proxy_http proxy_rewrite'。這個配置不僅指定了這些模塊,同時也默認安裝其他最小項。這樣配置完後make和make install後,mod_proxy和mod_rewrite兩個模塊都能找到了