【茶余饭后】安装Apache配置正向代理和反向代理

文章目录

    • Apache安装(离线)
      • 1、准备必要软件包
      • 2、解压编译
      • 3、验证Apache

Apache安装(离线)

1、准备必要软件包

  • apr-1.4.5.tar.gz
    注:APR(Apache portable Run-time libraries,Apache可移植运行库)的目的如其名称一样,主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库。相当于apache的虚拟环境
  • apr-util-1.3.12.tar.gz
    apr-util安装依赖于apr,因此需要首先安装apr。常用的一些存储段和存储段组,加密组件等等
  • httpd-2.2.31.tar.gz
    apache的核心软件,也是http的主程序
  • pcre-8.10.zip

2、解压编译

默认解压路径为“/opt/”
1. 依赖包安装apr-1.4.5.tar.gz

	1. tar -xzvf apr-1.4.5.tar.gz
	2. cd apr-1.4.5
	3. yum -y install gcc-c++
	4. ./configure --prefix=/usr/local/apr
    5. make && make install

2. 依赖包安装apr-util-1.3.12.tar.gz

	1. tar -xzvf apr-util-1.3.12.tar.gz
	2. cd apr-util-1.3.12
	3. ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
    4. make && make install

3. 依赖包安装pcre.8.10.zip

	1. tar -xzvf pcre-8.10.zip 或者 unzip pcre-8.10.zip
	2. cd pcre-8.10
	2. ./configure --prefix=/usr/local/pcre
    3. make && make install

4. Apache安装

	1. tar zxvf httpd-2.2.31.tar.gz
	2. yum install zlib-devel -y
	3. cd httpd-2.2.31
	4. ./configure --prefix=/usr/local/apache2  --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre  --enable-modules=so --enable-mods-shared=all --enable-proxy --enable-proxy-connect --enable-proxy-ftp --enable-proxy-http --enable-proxy-ajp --enable-proxy-balancer --enable-rewrite --enable-ssl --with-ssl=/usr/local/httpd/ssl --with-expat=builtin ap_cv_void_ptr_lt_long=no --with-z=/usr/lib/
	5. make && make install

以上执行会报错,configure: error: …No recognized SSL/TLS toolkit detected,这是需要先执行第五步中的“yum install openssl*”,若不需要代理https的,可以把以上第四步中的*–enable-ssl --with-ssl=/usr/local/httpd/ssl*给去掉
5. 使用https代理需要安装ssl

1. yum install openssl*
2. openssl genrsa -des3 1024 > /usr/local/apache2/conf/server.key
3. openssl genrsa -des3 1024 > /usr/local/apache2/conf/server.key > /usr/local/apache2/conf/server2.key 
4. mv /usr/local/apache2/conf/server2.key /usr/local/apache2/conf/server.key
5. openssl req -new -key /usr/local/apache2/conf/server.key -out /usr/local/apache2/conf/server.csr
6. openssl x509 -in /usr/local/apache2/conf/server.csr -out /usr/local/apache2/conf/server.crt -req -signkey /usr/local/apache2/conf/server.key  -days 3650

该生成的证书路径需要和apache创建后的host_ssl.conf中的SSL证书路径一致,查看证书信息“openssl x509 -noout -text -in server.crt”

3、验证Apache

1. 启动Apache

###由于在安装apache的时候指定的安装前缀是/usr/local/apache2
1. cd /usr/local/apache2/bin
2. ./apachectl start
###报错:httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName

报错httpd: Could not reliably determine the server’s fully qualified domain name, using localhost.localdomain for ServerName,需要在原始的httpd.conf文件中配置ServerName,如下:

3. cd /usr/local/apache2/conf
4. vim httpd.conf
###使用:set number 打开行号找到157行,或者使用?ServerName找到位置,在原来的下面新增一行
6. ServerName localhost:80

【茶余饭后】安装Apache配置正向代理和反向代理_第1张图片
然后重新启动apache,./apachectl restart
以上其实并没有结束,可以查看端口启用情况

netstat -na | grep 80

发现并没有启动成功
下面需要进一步配置apache
2. 配置Apache
首先修改主配置httpd.conf

1. cd /usr/local/apache2/conf/
2. vim httpd.conf
####保证一下模块处于开启的状态,前面有#号的去掉
#LoadModule proxy_module modules/mod_proxy.so 
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#Include conf/extra/httpd-vhosts.conf
#####如果使用到了https的反向代理,需要打开下面这个配置
#Include conf/extra/httpd-vhosts.conf

其次修改虚拟主机的httpd配置,即httpd-vhosts.conf
以下同时使用80端口做了正向代理和反向代理,仅供参考

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache2/shared"
    ServerName xxx.xxx.xxx.xxx:80
    ErrorLog "/usr/local/apache2/logs/error.log"
    CustomLog "/usr/local/apache2/logs/access.log" common

    SetEnvIfNoCase Referer ^http://xxx.xxx.xxx.xxx:80 local_ref
    SetEnvIf Referer ^$ local_ref
    ###作为https的反向代理必须加上以下的配置
    SSLProxyEngine on
    ###以下作为https的反向代理配置
    ProxyPass /wechat  https://weixin.qq.com
    ProxyPassReverse /wechat https://weixin.qq.com
    <Directory "/usr/local/apache2/shared">
        Require all denied
        Options FollowSymLinks
        AllowOverride None
        Order deny,allow
        Allow from all
    </Directory>
    ###以下是作为正向代理的配置
    ProxyRequests On
    ProxyVia On

    <Proxy *>
        Order deny,allow
        Deny from all
        Allow from all
    </Proxy>
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI "\.(?:rar|json|g?zip)$" no-gzip

</VirtualHost>

重启apache,然后再查看端口启用情况即可

你可能感兴趣的:(linux学习)