1、系統環境
ubuntu+apacheubuntu使用15.10版本,apache使用源碼安裝或者apt-get命令安裝。部署好ubuntu系統後,即開始apache的安裝配置,支持反向代理以及修改網頁內容的功能。
2、安裝apache
2.1命令方式安裝
sudo apt-get install apache2
測試apache安裝是否成功,命令行輸入如下: liang@ubuntu:~$ ls /var/www/html
打印輸出,產生了一個默認頁面:
index.html
命令行輸入如下,查看apache2的安裝目錄: liang@ubuntu:~$ ls /etc/apache2
打印如下內容: apache2.conf conf-enabled magic mods-enabled sites-available
conf-available envvars mods-available ports.conf sites-enabled
查看apache的網頁配置文件: liang@ubuntu:~$ ls /etc/apache2/sites-available
打印如下內容: 000-default.conf default-ssl.conf
重新啓動apache服務:
liang@ubuntu:~$ sudo /etc/init.d/apache2 restart
啓動成功:
[ ok ] Restarting apache2 (via systemctl): apache2.service.
瀏覽器訪問http://localhost/,出現 Apache2 Ubuntu Default Page 默認頁面即apache安裝到此結束。2.2源碼方式安裝
去官方網站下載源代碼,停止命令行安裝的apache2 liang@ubuntu:~/workspace/mod_line_edit$ sudo /etc/init.d/apache2 stop
安裝依賴軟件 sudo apt-get install libtool autoconf build-essential
源代碼安裝apr,進入apr源代碼目錄,運行 ./configure 報告出錯: rm: cannot remove 'libtoolT': No such file or directory
config.status: executing default commands
嘗試安裝 autoconf automake libtool 工具: liang@ubuntu:~/Desktop/depenforapche/apr-1.5.2$ sudo apt-get install autoconf automake libtool
打印如下,系統已經安裝如上工具: Reading package lists... Done
Building dependency tree
Reading state information... Done
autoconf is already the newest version.
automake is already the newest version.
automake set to manually installed.
libtool is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
在執行./configure 之前,先執行:
$autoreconf --force --install
$libtoolize --automake --force
$automake --force --add-missing
打印如下內容: liang@ubuntu:~/Desktop/depenforapche/apr-1.5.2$ autoreconf --force --install
aclocal: warning: autoconf input should be named 'configure.ac', not 'configure.in'
build/apr_network.m4:24: warning: underquoted definition of APR_TYPE_IN_ADDR
build/apr_network.m4:24: run info Automake 'Extending aclocal'
build/apr_network.m4:24: or see http://www.gnu.org/software/automake/manual/automake.html#Extending-aclocal
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `build'.
libtoolize: copying file `build/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `build'.
libtoolize: copying file `build/libtool.m4'
libtoolize: copying file `build/ltoptions.m4'
libtoolize: copying file `build/ltsugar.m4'
libtoolize: copying file `build/ltversion.m4'
libtoolize: copying file `build/lt~obsolete.m4'
libtoolize: Consider adding `-I build' to ACLOCAL_AMFLAGS in Makefile.am.
libtoolize: `AC_PROG_RANLIB' is rendered obsolete by `LT_INIT'
aclocal: warning: autoconf input should be named 'configure.ac', not 'configure.in'
build/apr_network.m4:24: warning: underquoted definition of APR_TYPE_IN_ADDR
build/apr_network.m4:24: run info Automake 'Extending aclocal'
build/apr_network.m4:24: or see http://www.gnu.org/software/automake/manual/automake.html#Extending-aclocal
liang@ubuntu:~/Desktop/depenforapche/apr-1.5.2$ libtoolize --automake --force
liang@ubuntu:~/Desktop/depenforapche/apr-1.5.2$ automake --force --add-missing
automake: warning: autoconf input should be named 'configure.ac', not 'configure.in'
configure.in: error: no proper invocation of AM_INIT_AUTOMAKE was found.
configure.in: You should verify that configure.in invokes AM_INIT_AUTOMAKE,
configure.in: that aclocal.m4 is present in the top-level directory,
configure.in: and that aclocal.m4 was recently regenerated (using aclocal)
configure.in:207: installing 'build/install-sh'
BUG.am: error: 'install.sh' is an anachronism; use 'install-sh' instead
automake: error: no 'Makefile.am' found for any configure output
重新輸入命令完成apr安裝即可:
./configure && sudo make && sudo make install
開始安裝apr-util,進入apr-util源碼目錄: liang@ubuntu:~/Desktop/depenforapche/apr-util-1.5.4$ ./configure --with-apr=/usr/local/apr
liang@ubuntu:~/Desktop/depenforapche/apr-util-1.5.4$ sudo make
liang@ubuntu:~/Desktop/depenforapche/apr-util-1.5.4$ sudo make install
開始安裝apache,進入apache源碼目錄: liang@ubuntu:~/Desktop/httpd-2.4.16$ ./configure
liang@ubuntu:~/Desktop/httpd-2.4.16$ sudo make
liang@ubuntu:~/Desktop/httpd-2.4.16$ sudo make install
上面執行正確後,可以啓動apache測試安裝是否成功,進入安裝目錄: liang@ubuntu:~/Desktop/httpd-2.4.16$ cd /usr/local/apache2/
進入命令行目錄: liang@ubuntu:/usr/local/apache2$ cd bin/
啓動apache服務: liang@ubuntu:/usr/local/apache2/bin$ sudo ./httpd
提示出錯: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
去修改apache配置文件, liang@ubuntu:/usr/local/apache2/bin$ sudo vi /usr/local/apache2/conf/httpd.conf
去掉下面的註釋,重啓apache2即可使用,瀏覽器訪問服務器地址,出現 It works! 字樣,即安裝成功。
#ServerName www.example.com:80
3、配置apache反向代理
這裏的配置使用的是命令行安裝的apache,源代碼安裝的配置原理一樣,但是配置過程不一樣,大概是操作http.conf文件。
查看apache安裝目錄:
liang@ubuntu:~$ whereis apache2
打印如下內容,切換至apache的安裝目錄:
apache2: /usr/sbin/apache2 /usr/lib/apache2 /etc/apache2 /usr/share/apache2 /usr/share/man/man8/apache2.8.gz
查看proxy模塊,並且啓用proxy代理模塊: liang@ubuntu:~$ cd /etc/apache2/mods-available
啓用proxy代理模塊:
添加代理模塊的配置,這裏以sina與163爲例進行配置:sudo ln -s /etc/apache2/mods-available/proxy.load /etc/apache2/mods-enabled/proxy.load sudo ln -s /etc/apache2/mods-available/proxy_http.load /etc/apache2/mods-enabled/proxy_http.load sudo ln -s /etc/apache2/mods-available/proxy_balancer.load /etc/apache2/mods-enabled/proxy_banancer.load
寫入下面的內容:vi /etc/apache2/sites_available/proxy
啓用代理配置:ProxyRequests Off ProxyVia On UseCanonicalName Off ProxyMaxForwards 10000 <VirtualHost *:80> ServerAlias www.sina.com.cn <Proxy> Order Deny,Allow Allow from all </Proxy> ProxyPass / http://www.sina.com.cn/ ProxyPassReverse / http://www.sina.com.cn/ </VirtualHost> <VirtualHost *:80> ServerAlias www.163.com <Proxy> Order Deny,Allow Allow from all </Proxy> ProxyPass / http://www.163.com/ ProxyPassReverse / http://www.163.com/ </VirtualHost>
到此配置反向代理已經成功,具體測試過程請參照第五步。sudo ln -s /etc/apache2/sites_available/proxy /etc/apache2/sites_enabled/proxy sudo /etc/init.d/apache2 restart
4、配置apache修改網頁
查看apxs工具是否安裝:打印如下即沒有安裝:liang@ubuntu:/usr/share/apache2/build$ apxs
使用命令行安裝:The program 'apxs' is currently not installed. You can install it by typing:
再次查看apxs工具是否安裝:sudo apt-get install apache2-dev
liang@ubuntu:/usr/share/apache2/build$ apxs打印如下即安裝成功:
部署修改網頁的功能,去下載 mod_line_edit.c 模塊源代碼,mod_line_edit 可以實現網頁內容修改功能,詳情說明內容到mod_line_edit網站查看。 添加mod_line_edit模塊:Usage: apxs -g [-S <var>=<val>] -n <modname> apxs -q [-v] [-S <var>=<val>] [<query> ...] apxs -c [-S <var>=<val>] [-o <dsofile>] [-D <name>[=<value>]] [-I <incdir>] [-L <libdir>] [-l <libname>] [-Wc,<flags>] [-Wl,<flags>] [-p] <files> ... apxs -i [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ... apxs -e [-S <var>=<val>] [-a] [-A] [-n <modname>] <dsofile> ...
打印如下內容,提示缺少pcre支持:liang@ubuntu:~/workspace/mod_line_edit$ apxs -c mod_line_edit.c
更新系統的軟件源,並且安裝pcre庫支持:/usr/share/apr-1.0/build/libtool --silent --mode=compile --tag=disable-static x86_64-linux-gnu-gcc -std=gnu99 -prefer-pic -pipe -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -DLINUX -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/apache2 -I/usr/include/apr-1.0 -I/usr/include/apr-1.0 -I/usr/include -c -o mod_line_edit.lo mod_line_edit.c && touch mod_line_edit.slo mod_line_edit.c:26:18: fatal error: pcre.h: No such file or directory compilation terminated. apxs:Error: Command failed with rc=65536
再次添加mod_line_edit模塊:sudo apt-get update sudo apt-get install libpcre3 libpcre3-dev
打印如下內容,即可進行下一步操作:liang@ubuntu:~/workspace/mod_line_edit$ sudo apxs -i -a -c mod_line_edit.c
配置修改網頁功能,執行命令vi /etc/apache2/sites_available/proxy,編輯proxy文件,覆蓋下面內容到文件中:/usr/share/apr-1.0/build/libtool --silent --mode=compile --tag=disable-static x86_64-linux-gnu-gcc -std=gnu99 -prefer-pic -pipe -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -DLINUX -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/apache2 -I/usr/include/apr-1.0 -I/usr/include/apr-1.0 -I/usr/include -c -o mod_line_edit.lo mod_line_edit.c && touch mod_line_edit.slo /usr/share/apr-1.0/build/libtool --silent --mode=link --tag=disable-static x86_64-linux-gnu-gcc -std=gnu99 -Wl,--as-needed -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -o mod_line_edit.la -rpath /usr/lib/apache2/modules -module -avoid-version mod_line_edit.lo /usr/share/apache2/build/instdso.sh SH_LIBTOOL='/usr/share/apr-1.0/build/libtool' mod_line_edit.la /usr/lib/apache2/modules /usr/share/apr-1.0/build/libtool --mode=install install mod_line_edit.la /usr/lib/apache2/modules/ libtool: install: install .libs/mod_line_edit.so /usr/lib/apache2/modules/mod_line_edit.so libtool: install: install .libs/mod_line_edit.lai /usr/lib/apache2/modules/mod_line_edit.la libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/sbin" ldconfig -n /usr/lib/apache2/modules ---------------------------------------------------------------------- Libraries have been installed in: /usr/lib/apache2/modules If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the `LD_RUN_PATH' environment variable during linking - use the `-Wl,-rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to `/etc/ld.so.conf' See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- chmod 644 /usr/lib/apache2/modules/mod_line_edit.so [preparing module `line_edit' in /etc/apache2/mods-available/line_edit.load] Enabling module line_edit. To activate the new configuration, you need to run: service apache2 restart
查看是否已經啓用mod_line_edit模塊:ProxyRequests Off ProxyVia On UseCanonicalName Off ProxyMaxForwards 10000 #RequestHeader unset Accept-Encoding <VirtualHost *:80> ServerAlias www.sina.com.cn <Proxy> Order Deny,Allow Allow from all </Proxy> ProxyPass / http://www.sina.com.cn/ ProxyPassReverse / http://www.sina.com.cn/ <IfModule line_edit_module> RequestHeader unset Accept-Encoding SetOutputFilter line-editor SetEnv LineEdit 'text/plain;text/css;text/html;test/javascript' LERewriteRule photo.sina.com.cn hello.photo.sina.com.cn </IfModule> </VirtualHost> <VirtualHost *:80> ServerAlias www.163.com <Proxy> Order Deny,Allow Allow from all </Proxy> ProxyPass / http://www.163.com/ ProxyPassReverse / http://www.163.com/ </VirtualHost>
liang@ubuntu:~/workspace/mod_line_edit$ ll /etc/apache2/mods-enabled/line_edit.load
打印如下內容,即已經啓用:
lrwxrwxrwx 1 root root 32 Feb 25 13:35 /etc/apache2/mods-enabled/line_edit.load -> ../mods-available/line_edit.load
此時重啓apache服務:
liang@ubuntu:~/workspace/mod_line_edit$ sudo /etc/init.d/apache2 restart
如果提示出錯:
配置了headers解析模塊的功能,而沒有啓用該模塊:[....] Restarting apache2 (via systemctl): apache2.serviceJob for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details. failed!
打印如下內容:liang@ubuntu:/etc/apache2/mods-enabled$ sudo ln -s /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load liang@ubuntu:/etc/apache2/mods-enabled$ ll /etc/apache2/mods-enabled/headers.load
再次重啓apache:lrwxrwxrwx 1 root root 40 Feb 25 13:59 /etc/apache2/mods-enabled/headers.load -> /etc/apache2/mods-available/headers.load
啓動成功:liang@ubuntu:~/workspace/mod_line_edit$ sudo /etc/init.d/apache2 restart
到此,修改網頁內容的功能配置已經完成,根據第五步進行測試。其實,還有一種修改網頁內容的方法,但是是針對URL的,使用模塊mod_proxy_html。[ ok ] Restarting apache2 (via systemctl): apache2.service.
5、測試過程
測試環境:虛擬機ubuntu15.10 代理服務器、虛擬機windows7 客戶機。兩個虛擬機都使用NAT的網絡連接方式,修改Windows7的hosts文件,末尾添加如下內容:但是之前配置的hosts文件是如下格式的:192.168.213.132 www.sina.com.cn 192.168.213.132 www.163.com
不知道爲何現在要配置成上面的格式纔可以正常運行,留下了一個問題需要去思考。192.168.213.132 sina.com.cn 192.168.213.132 163.com
瀏覽器訪問sina主頁,發現http://photo.sina.com.cn/的url修改成了http://hello.photo.sina.com.cn/,證明了上面的反向代理以及修改網頁內用兩個模塊功能配置成功。這時候,去查看apache2日誌即可看到代理了163與sina網站。