【高可用HA】Apache (1) —— Mac下安裝Apache Httpd到自定義路徑(非/etc/apache2)


Mac下安裝Apache Httpd


httpd版本: httpd-2.4.17

參考來源:

Tomcat Clustering - A Step By Step Guide

Apache HTTP Server Version 2.4 - Compiling and Installing

Stackoverflow - configure: error: C compiler cannot create executables

Problems with compiling apache2 on Mac OS X Mountain Lion

安裝

Download	$ lynx http://httpd.apache.org/download.cgi
Extract		$ gzip -d httpd-NN.tar.gz
$ tar xvf httpd-NN.tar
$ cd httpd-NN
Configure	$ ./configure --prefix=PREFIX
Compile		$ make
Install		$ make install
Customize	$ vi PREFIX/conf/httpd.conf
Test		$ PREFIX/bin/apachectl -k start

運行錯誤

C compiler cannot create executables
$ sudo ./configure --prefix=/httpd-2.4.17-a
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking build system type... x86_64-apple-darwin14.4.0
checking host system type... x86_64-apple-darwin14.4.0
checking target system type... x86_64-apple-darwin14.4.0
configure: 
configure: Configuring Apache Portable Runtime library...
configure: 
	checking for APR... yes
	setting CC to "/Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin/cc"
	setting CPP to "/Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin/cc -E"
	setting CFLAGS to " "
	setting CPPFLAGS to " -DDARWIN -DSIGPROCMASK_SETS_THREAD_MASK -DDARWIN_10"
	setting LDFLAGS to " "
configure: 
configure: Configuring Apache Portable Runtime Utility library...
configure: 
	checking for APR-util... yes
	checking for gcc... /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin/cc
	checking whether the C compiler works... no
configure: error: in `/Users/Richard/Documents/Dev/servers/cluster/httpd/httpd-2.4.17-a':
configure: error: C compiler cannot create executables
See `config.log' for more details

一個可行的解決辦法

依次在Terminal中執行下面三行命令:

$ sudo xcode-select -switch /

.

sudo mkdir -p /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin

.

sudo ln -s /usr/bin/cc /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin/cc

注意上面OSX10.10的版本號與本機操作系統的保持一致,可以在上面錯誤日志中找到

編譯與安裝

$ make

.

$ make install

當運行make時,Mac會提示錯誤

make: command not found

這是因為Mac下默認沒有安裝make,可以通過打開xcode,preferences -> Downloads -> Components,Command Line Tools安裝

(Xcode5.1后不再支持通過此方式安裝,需要訪問Downloads for Apple Developers選擇正確版本安裝)

再運行make是,Mac會提示錯誤

make: *** No targets specified and no makefile found.  Stop.

查看之前的./configure 執行的log發現最后執行出錯"libpcre not found"

checking whether /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin/cc accepts -g... yes
checking for /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin/cc option to accept ISO C89... none needed
checking how to run the C preprocessor... /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin/cc -E
checking for /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin/cc option to accept ISO C99... none needed
checking for pcre-config... false
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

pcre的編譯安裝

$ tar -zxvf pcre-8.10.tar.gz
$ cd pcre-8.10
$ ./configure
$ make 
$ make install
注意:此處要安裝pcre,pcre2不適用

安裝成功之后,在httpd目錄下重新運行./configure(配置httpd到自定義目錄)

$ ./configure --prefix=/Users/Richard/Documents/Dev/servers/cluster/httpd/node-a

執行結果末尾幾行的輸出:

config.status: creating support/split-logfile
config.status: creating build/rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating build/config_vars.sh
config.status: creating include/ap_config_auto.h
config.status: executing default commands	

運行

$ make

執行結果末尾幾行輸出:

/usr/share/apr-1/build-1/libtool --silent --mode=link /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain/usr/bin/cc             -o mod_rewrite.la -rpath /Documents/Dev/servers/cluster/httpd/httpd-2.4.17-b/modules -module -avoid-version  mod_rewrite.lo 

關於make可能也會輸出以下信息(可以暫時忽略,稍后再關注此問題)

Building shared: mod_status.la mod_autoindex.la mod_info.la mod_cgid.la
make[4]: Nothing to be done for `local-shared-build'.
Building shared: mod_dav_fs.la
make[4]: Nothing to be done for `local-shared-build'.
Building shared: mod_vhost_alias.la mod_negotiation.la mod_dir.la mod_actions.la mod_speling.la mod_userdir.la mod_alias.la mod_rewrite.la
make[4]: Nothing to be done for `local-shared-build'.	

運行(sudo)

$ make install

執行結果末尾幾行輸出:

Installing man pages and online manual
mkdir /Documents/Dev/servers/cluster/httpd/httpd-2.4.17-b/man
mkdir /Documents/Dev/servers/cluster/httpd/httpd-2.4.17-b/man/man1
mkdir /Documents/Dev/servers/cluster/httpd/httpd-2.4.17-b/man/man8
mkdir /Documents/Dev/servers/cluster/httpd/httpd-2.4.17-b/manual
*查看端口占用
lsof -n -i TCP:80

測試

編譯成功后,我們可以在編譯的目標目錄(此處為./cluster/httpd/node-a)下找到:

$ ls
httpd-2.4.17	node-a
$ cd node-a
$ ls
bin	build	cgi-bin	conf	error	htdocs	icons	include	logs	man	manual	modules

先使用默認的httpd.conf配置,在/bin下執行

apachectl -k start

運行結果中會提示兩個錯誤AH00558和AH00072

$ apachectl -k start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using weizhedeMacBook-Pro.local. Set the 'ServerName' directive globally to suppress this message
(13)Permission denied: AH00072: make_sock: could not bind to address [::]:80
(13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
AH00015: Unable to open logs

修改./cluster/httpd/node-a/conf/httpd.conf,加入配置ServerName

#ServerName www.example.com:80
ServerName localhost

然后再運行相同命令,仍然報相同錯誤。

如果對etc/apache2/conf/httpd.conf進行修改,再運行相同命令,發現AH00558錯誤解決,但AH00072錯誤仍然存在。

此處懷疑命令的權限不夠

Apache error – (13)Permission denied: make_sock: could not bind to address,可以通過sudo運行,或者修改Apache的權限。此處暫時使用sudo

$ sudo apachectl -k start

然后AH00072錯誤沒有了,用瀏覽器嘗試訪問localhost

此處懷疑當前運行的apachectl命令仍然使用Mac默認路徑etc/apache2下的apache

為了驗證這個想法,我們查看etc/apache2/conf/httpd.conf文件發現其DocumentRoot為:

DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">

我們在該路徑下(Command+Shift+G),找到文件index.html.en並修改

<html><body><h1>It works! Apache2 Default</h1></body></html>

然后通過瀏覽器重新訪問localhost得到運行結果

問題來了

如何才能運行我們自定義目錄下的httpd呢?

因為默認情況下,運行apachectl或者httpd命令會指向系統自帶的etc/apache2目錄。

如果要運行我們自行安裝的./node-a/httpd實例

$ ./bin/httpd -k start

默認情況下命令回去嘗試尋找"/Documents/Dev/servers/cluster/httpd/node-a/conf/httpd.conf",而出錯(為什么待解)

httpd: Could not open configuration file /Documents/Dev/servers/cluster/httpd/node-a/conf/httpd.conf: No such file or directory

我們用"-f"指定conf文件

$ ./bin/httpd -f /Users/Richard/Documents/Dev/servers/cluster/httpd/node-a/conf/httpd.conf -k start

這時可能會遇到錯誤

[Tue Dec 08 14:20:58.432961 2015] [core:error] [pid 18447:tid 4381061120] (13)Permission denied: [client ::1:50428] AH00035: access to / denied (filesystem path '/Users/Richard/Documents/Dev') because search permissions are missing on a component of the path
[Tue Dec 08 14:20:58.601201 2015] [core:error] [pid 18447:tid 4381061120] (13)Permission denied: [client ::1:50428] AH00035: access to /favicon.ico denied (filesystem path '/Users/Richard/Documents/Dev') because search permissions are missing on a component of the path, referer: http://localhost:81/

可以通過Httpd Wiki上的說明,

  • AH00132: file permissions deny server access
  • AH00035: access denied because search permissions are missing on a component of the path

查看"./node-a/htdocs/index.html"的授權

ls -l index.html

如果需要可以通過以下命令修正

chmod 644 index.html	

最后我們修改"./node-a/htdocs/index.html"的內容

<html><body><h1>It works! Node-a</h1></body></html>

然后通過瀏覽器訪問localhost:81

這樣一個自定義路徑的httpd實例就安裝測試成功了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM