apahce与nginx经常被用于作为Web服务器,其实apache与nginx有非常多的拓展功能,可以通过访问官方网站了解相关内容。之前使用过apache实现反向代理的功能,于是把它记录下来,方便以后做其他的配置。反向代理的时候,也使用了两种方式实现了修改网页内容的功能,也碰到了一些问题,折腾了不少时间。

1、系统环境

ubuntu+apache
   ubuntu使用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-availableconf-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 directoryconfig.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... DoneBuilding dependency tree       Reading state information... Doneautoconf 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 --installaclocal: warning: autoconf input should be named 'configure.ac', not 'configure.in'build/apr_network.m4:24: warning: underquoted definition of APR_TYPE_IN_ADDRbuild/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-aclocallibtoolize: 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_ADDRbuild/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-aclocalliang@ubuntu:~/Desktop/depenforapche/apr-1.5.2$ libtoolize --automake --forceliang@ubuntu:~/Desktop/depenforapche/apr-1.5.2$ automake --force --add-missingautomake: 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' insteadautomake: 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/aprliang@ubuntu:~/Desktop/depenforapche/apr-util-1.5.4$ sudo makeliang@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 makeliang@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代理模块:

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

添加代理模块的配置,这里以sina与163为例进行配置:

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:

使用命令行安装:

sudo apt-get install apache2-dev

再次查看apxs工具是否安装:

liang@ubuntu:/usr/share/apache2/build$ apxs

打印如下即安装成功:

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> ...

部署修改网页的功能,去下载 mod_line_edit.c 模块源代码,mod_line_edit 可以实现网页内容修改功能,详情说明内容到mod_line_edit网站查看。        添加mod_line_edit模块:

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

更新系统的软件源,并且安装pcre库支持:

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

再次添加mod_line_edit模块:

liang@ubuntu:~/workspace/mod_line_edit$ sudo apxs -i -a -c mod_line_edit.c 

打印如下内容,即可进行下一步操作:

/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/modulesIf 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 variableduring execution- add LIBDIR to the `LD_RUN_PATH' environment variableduring 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

配置修改网页功能,执行命令vi /etc/apache2/sites_available/proxy,编辑proxy文件,覆盖下面内容到文件中:

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>

查看是否已经启用mod_line_edit模块:

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

如果提示出错:

[....] 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!

配置了headers解析模块的功能,而没有启用该模块:

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

打印如下内容:

lrwxrwxrwx 1 root root 40 Feb 25 13:59 /etc/apache2/mods-enabled/headers.load -> /etc/apache2/mods-available/headers.load

再次重启apache:

liang@ubuntu:~/workspace/mod_line_edit$ sudo /etc/init.d/apache2 restart

启动成功:

[ ok ] Restarting apache2 (via systemctl): apache2.service.

到此,修改网页内容的功能配置已经完成,根据第五步进行测试。其实,还有一种修改网页内容的方法,但是是针对URL的,使用模块mod_proxy_html。

5、测试过程

测试环境:虚拟机ubuntu15.10 代理服务器、虚拟机windows7 客户机。两个虚拟机都使用NAT的网络连接方式,修改Windows7的hosts文件,末尾添加如下内容:

192.168.213.132    www.sina.com.cn
192.168.213.132    www.163.com

但是之前配置的hosts文件是如下格式的:

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网站。

Apache 反向代理与修改网页内容相关推荐

  1. Ubuntu下Apache反向代理设置

    Apache可通过mod_proxy*.so系列模块很好的实现正向.反向代理功能,亦可通过反向代理实现负载均衡,本文将讲述基于Apache反向代理实现负载均衡的配置方法. 1.环境配置说明: 代理服务 ...

  2. 用apache反向代理解决单外网ip对应内网多个web主机的问题

    用apache反向代理解决单外网ip对应内网多个web主机的问题 转载一个有独立外网IP,需内网服务器对外发布的例子,是应用apache虚拟主机的. 来源地址:http://www.itshantou ...

  3. [转]用apache反向代理解决单外网ip对应内网多个web主机的问题

    用apache反向代理解决单外网ip对应内网多个web主机的问题 转载一个有独立外网IP,需内网服务器对外发布的例子,是应用apache虚拟主机的. 来源地址:http://www.itshantou ...

  4. Apache反向代理访问code-server登陆账户后页面空白该怎么办?

    问题复现 添加反向代理:直接在宝塔面板的网站设置中添加反向代理,如图所示 这样操做会产生 Websocket 报错1006,导致登陆进去显示一片空白页面. 主要原因apache的反向代理需要手动添加W ...

  5. Apache反向代理设置【转载】

    编译Apache时要进行的设置 ./configure --enable-mods-shared="all proxy" 1>修改 httpd.conf 文件中: LoadM ...

  6. 使用Apache反向代理实现全能主机的搭建

    闲谈: 很多站长在购买虚拟主机的时候,会看虚拟主机的一些参数,其中最重要的就是支持的程序语言.现在很多IDC商家都在宣称全能主机. 最好笑的一个事情就是,笔者刚建站的时候,购买了国内一家小主机商(名叫 ...

  7. OpenBSD配置Apache反向代理

    反向代理简言之就是通过一台代理服务器,让Internet用户可以访问到内部网络上的服务器. 实验环境:一台带有两块网卡的OB服务器,一块连接外网1.1.1.1,一块连接内网192.168.100.1 ...

  8. Apache 反向代理,Laravel获取用户真实IP

    文章目录 Apache (测试能够使用) 1. 激活加载remoteip模块 2. 定义代理与服务 3. 配置Apache日志格式以使用X-Forwarded-For 4. 结论 5. 参考链接 Ng ...

  9. apache 反向代理_反向代理?听起来有点东西 ——Nginx学习笔记

    反向代理是个什么东西? 外部网络用户向内部服务器发出请求,并将这些请求转发给内部网络上的服务器,然后将从内部服务器上得到的响应返回给Internet上请求连接的客户:执行反向代理服务的服务器称为反向代 ...

最新文章

  1. 关系数据库的设计理论
  2. Xcode8.0 删除插件路径
  3. docker容器的基本操作
  4. 荣耀v40搭载鸿蒙吗,荣耀V40照常发布,将更换操作系统,同nova8搭载鸿蒙2.0发布...
  5. Chrome浏览器多开,亲测有效
  6. win10推送_Win10推送更新升级Edge浏览器:用户发现电脑速度因此变慢
  7. 记一次ElasticSearch 更改 mapping 字段类型的过程
  8. 细胞亚器互作之细胞代谢调节液泡与线粒体的接触位Cellular Metabolism Regulates Contact Sites between Vacuoles and Mitochondria
  9. 怎样解决An internal error has occurred. Index out of bounds
  10. 三角形度数计算机公式,角度数换算公式(三角函数计算换算角度)
  11. bim 水利枢纽 运维_BIM——运维专篇
  12. php 简转繁体,PHP简体转繁体——MediaWiki-zhconvert
  13. Qt 中文和英文分别使用不同的字体
  14. java实现中国象棋3:走棋规则的实现
  15. 谷歌要抛弃Android拥抱Andromeda
  16. 【信奥赛一本通】 1413:确定进制(详细代码)
  17. 服务器的主要防护手段有哪些
  18. java中黑点是什么意思_[Java教程]input输入密码变黑点密文
  19. (附源码)计算机毕业设计ssm高校线上教学系统
  20. python给excel排序_用python处理excel数据(六)实现excel表中排序功能

热门文章

  1. 品优购项目——index.html
  2. 【金猿信创展】火山引擎——云上增长新动力
  3. 火山引擎荣获中国信通院“2021边缘计算融合创新奖”
  4. 如何用Arduino IDE开发9.9元的合宙LuatOS ESP32C3开发板?
  5. k8s 灰度发布(未完)
  6. 超详细 Springboot 入门教程 ---- IDEA + maven 实现英雄联盟角色创建
  7. 基于共词分析的中国近代史实体关系图构建(毕业设计:数据处理)
  8. mysql读写分离codis_Codis 安装 - Rayn——做今天最好的自己 - OSCHINA - 中文开源技术交流社区...
  9. 机器学习_决策树(信息熵,决策树,决策树优化,剪枝)
  10. php 监听条码枪输入,使用jQuery监听扫码枪输入并禁止手动输入的实现方法(推荐)...