How to proxy a web site by apache2 in Ubuntu

Install apache2

To execute the install command in terminal:

sudo apt-get install apache2

Then, we can find that the apache2 has been installed in "/etc/" directory.

[email protected]:cd /etc/apache2

[email protected]:/etc/apache2$ apache2 -version
Server version: Apache/2.4.7 (Ubuntu)
Server built:   Apr  3 2014 12:20:28

[email protected]:/etc/apache2# ls -l
total 80
-rw-r--r-- 1 root root  7115 Jan  7 21:23 apache2.conf
drwxr-xr-x 2 root root  4096 Jun 17 15:09 conf-available
drwxr-xr-x 2 root root  4096 Jun 17 15:09 conf-enabled
-rw-r--r-- 1 root root  1782 Jan  3 22:48 envvars
-rw-r--r-- 1 root root 31063 Jan  3 22:48 magic
drwxr-xr-x 2 root root 12288 Jun 17 15:09 mods-available
drwxr-xr-x 2 root root  4096 Jun 17 15:09 mods-enabled
-rw-r--r-- 1 root root   320 Jan  7 21:23 ports.conf
drwxr-xr-x 2 root root  4096 Jun 17 15:08 sites-available
drwxr-xr-x 2 root root  4096 Jun 17 15:09 sites-enabled

Attention:

After executing the install command, some echo exception messages may shown like that.

AH00558: apache2: 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
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
(98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down

If so, we need to:

1) Config the "ServerName" in apache2.conf.

[email protected]:cd /etc/apache2
[email protected]:cd vi apache2.conf

...
ServerName localhost
...

2) End the existed process which is using the 80 socket.

netstat -ap | grep 80
lsof -i:80
kill {PID}

Or modify the listen socket. (See Config listening ports)

Then, we can restart apache2.

[email protected]:sudo /etc/init.d/apache2 restart

Config listening ports

We can change and add the listening ports by modifying port.conf file in "/etc/apache2/".

[email protected]:sudo vi /etc/apache2/ports.conf

For example, we change the default port from 80 to 81 to avoid the in used portd.

Listen 81

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

After changing the default port, the default site configuration (/etc/apache2/sites-enabled/000-default.conf) also need be updated.

[email protected]:sudo vi /etc/apache2/sites-enabled/000-default.conf

Modify

<VirtualHost *:80>

as

<VirtualHost *:81>

Config proxy or reverse proxy

Here, there is a Tomcat worked in 8080 port as our J2EE server and an application named "jreport" running in it. We will config the apache to proxy it.

1. Activate proxy module

There are "mods-available" and "mods-enabled" two directories in apache. The "mods-available" directory includes all available module configuration files. If we want to make them take effect, they must be copied or linked into the "mods-enabled" directory.

For activating the proxy module, we create some soft link for "proxy.load", "proxy_http.load" and "proxy.conf".

[email protected]:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/proxy.load
[email protected]:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/proxy_http.load
[email protected]:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/proxy.conf

Then, execute the a2enmod command.

[email protected]:/etc/apache2$ a2enmod proxy

2. Config proxy

After activating the proxy module, we can config the "Forward Proxy" or "Reverse Proxy" for the "jreport" application in Tomcat.

  • Reverse Proxy

Reverse proxy is the most used way.

ProxyRequests Off
ProxyPass /jreport ${JREPORT_SERVER}/jreport
ProxyPassReverse /jreport ${JREPORT_SERVER}/jreport

or

ProxyRequests Off

Timeout 36000
ProxyTimeout 36000

<Location /jreport/>
    ProxyPass ${JREPORT_SERVER}/jreport
    ProxyPassReverse ${JREPORT_SERVER}/jreport
    ProxyPassReverseCookiePath /jreport /
</Location>

For easy to config, we define a variable named "JREPORT_SERVER" in "/etc/apache2/envvars".

export JREPORT_SERVER=http://192.168.0.88:8080

After restarting the apache with the latest configuration, we can access the "jreport" application with:

http://localhost:81/jreport
  • Forward Proxy

For example, to control who can access your proxy:

ProxyRequests On
ProxyVia On
<Proxy *>
  Require ip 192.168.0
</Proxy>

For more details, please see the official doc about mod_proxy.

Add SSL Support

1. Install openssl and ssl_cert

[email protected]: sudo apt-get install openssl ssl_cert

2. Generate private key and certification

[email protected]: sudo mkdir /etc/apache2/ssl
[email protected]: cd /etc/apache2/ssl
[email protected]:/etc/apache2/ssl$ sudo openssl genrsa -des3 -out my-server.key 1024
[email protected]:/etc/apache2/ssl$ sudo openssl req -key my-server.key -x509 -out my-server.crt -config /etc/ssl/openssl.cnf -days 3650

3. Activate SSL module

[email protected]:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/ssl.load
[email protected]:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/ssl.conf
[email protected]:/etc/apache2/mods-enabled$ sudo a2enmod ssl

4. Add SSL support for site

Now, we modify the default site configuration (/etc/apache2/sites-enabled/000-default.conf) to add SSL support and make non-https access use the https automatically.

Usually, we config the 443 port for SSL support.

<VirtualHost *:81>
    ...

    RewriteEngine on
    RewriteCond %{HTTPS} !=on

    RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R]
</VirtualHost>
<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/my-server.crt
    SSLCertificateKeyFiel /etc/apache2/ssl/my-server.key

    ...

</VirtualHost>

Postscript

I have just recorded my first attempt to proxy a web site by apache for memo. There are some other useful and complex modules in apache, such as rewrite, load balance and so on.

Reference

  1. Apache official doc: http://httpd.apache.org/docs/2.4/

How to proxy a web site by apache2 in Ubuntu,布布扣,bubuko.com

时间: 2024-10-15 15:25:28

How to proxy a web site by apache2 in Ubuntu的相关文章

Apache2: How To Redirect Users To Mobile Or Normal Web Site Based On Device Using mod_rewrite

http://www.howtoforge.com/apache2-how-to-redirect-users-to-mobile-or-normal-web-site-based-on-device-using-mod_rewrite Apache2: How To Redirect Users To Mobile Or Normal Web Site Based On Device Using mod_rewrite Since the massive rise of smartphones

Windows Azure Web Site (8) 设置Web Site时区

<Windows Azure Platform 系列文章目录> 许多已经使用Azure Web Site的用户已经发现了,Azure Web Site默认的系统时间是UTC时区. 比如我们在本地使用Visual Studio 2013创建Web Site项目,在Default.aspx.cs输入以下代码: Label1.Text = DateTime.Now.ToString(); 编译通过后,在本地调试显示的是本地系统时间. 而我们部署到Azure Web Site之后,显示的是UTC时区.

phpMyAdmin 应用程序“DEFAULT WEB SITE”中的服务器错误

分析原因:没有"C:\inetpub\wwwroot\phpmyadmin\"此目录 解决办法:新建路径 分析原因:IIS设置少了一项,需添加"服务端包含"选项 解决办法:控制面板 -> 程序 ->  程序和功能 -> 打开或关闭windows功能 -> internet 信息服务 -> 万维网服务 -> 应用程序开发功能 -> 服务器端包含(勾上) 控制面板 -> 系统和安全 -> 管理工具 -> int

Web Application和Web Site两个模板的比较

Scenario Web Application Project Web Site Project 项目定义 跟 Visual Studio .NET 2003 类似,由于项目文件的存在,只有被项目文件所引用的文件才会在Solution Explorer中出现.而且只有这些文件才会被编译.可以很容易的把一个ASP.NET应用拆分成多个Visual Studio项目.可以很容易的从项目中和源代码管理中排除一个文件. 一个目录结构就是一个WEB项目.没有项目文件存在.这个目录下的所有文件,都被作为项

IIS恢复Default Web Site的解决方法

1.文件路径C:\Windows\System32\inetsrv\config\applicationHost.config,找到该文件. 2.编辑该文件,找到文件<sites>标签,在其之下添加如下xml段落: <site name="Default Web Site" id="1"> <application path="/"> <virtualDirectory path="/"

为Azure Web Site 添加ADFS验证支持之二 在代码里使用ADFS

下面我们来创建一个MVC 5.0的ASP.Net程序,并且将它部署到Azure Web Site上 通过Visual Studio 2015创建Web Project 在选择ASP.net模板的地方,更改验证方式   在选择验证方式时选择"Work And School Accounts",在文本框中填入 1.你公司的ADFS的Metadata的地址,这个地址可以找ADFS的管理员要到,通常如以下形式: https://{youradfs.yourcompany.com}/federa

走进云背后:微软Azure web 项目通过web service部署web site

探索云那不为人知的故事(一):Web Services部署web site 前奏:Windows Azure是微软基于云计算的操作系统,现在更名为“Microsoft Azure”,和Azure Services Platform一样,是微软“软件和服务”技术的名称.Windows Azure的主要目标是为开发者提供一个平台,帮助开发可运行在云服务器.数据中心.Web和PC上的应用程序.云计算的开发者能使用微软全球数据中心的储存.计算能力和网络基础服务.Azure服务平台包括了以下主要组件:Wi

Best Practices for Speeding Up Your Web Site

The Exceptional Performance team has identified a number of best practices for making web pages fast. The list includes 35 best practices divided into 7 categories. Looking to optimize your mobile app experience? Check out Flurry Analytics. Filter by

Azure Web Site FTP用户名和密码设置

1.  打开Azure门户,点击网站的仪表板,点击重置部署凭据设定FTP用户名和密码: 2.  设置用户名和密码: 3.  FTP 主机名下的URL以及用户名.用户名格式为网站名称\用户名. 4.  输入之前设置的用户名和密码.(注:MVCAzure为我的网站名称): Azure Web Site FTP用户名和密码设置,布布扣,bubuko.com