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.

eric@eric:cd /etc/apache2

eric@eric:/etc/apache2$ apache2 -version
Server version: Apache/2.4. (Ubuntu)
Server built: Apr :: eric@eric:/etc/apache2# ls -l
total
-rw-r--r-- root root Jan : apache2.conf
drwxr-xr-x root root Jun : conf-available
drwxr-xr-x root root Jun : conf-enabled
-rw-r--r-- root root Jan : envvars
-rw-r--r-- root root Jan : magic
drwxr-xr-x root root Jun : mods-available
drwxr-xr-x root root Jun : mods-enabled
-rw-r--r-- root root Jan : ports.conf
drwxr-xr-x root root Jun : sites-available
drwxr-xr-x root root Jun : 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
()Address already in use: AH00072: make_sock: could not bind to address [::]:
()Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:
no listening sockets available, shutting down

If so, we need to:

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

eric@eric:cd /etc/apache2
eric@eric:cd vi apache2.conf ...
ServerName localhost
...

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

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

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

Then, we can restart apache2.

eric@eric: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/".

eric@eric:sudo vi /etc/apache2/ports.conf

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

Listen 

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

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

eric@eric:sudo vi /etc/apache2/sites-enabled/-default.conf

Modify

<VirtualHost *:>

as

<VirtualHost *:>

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

eric@eric:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/proxy.load
eric@eric:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/proxy_http.load
eric@eric:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/proxy.conf

Then, execute the a2enmod command.

eric@eric:/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
ProxyTimeout <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.
</Proxy>

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

Add SSL Support

1. Install openssl and ssl_cert

eric@eric: sudo apt-get install openssl ssl_cert

2. Generate private key and certification

eric@eric: sudo mkdir /etc/apache2/ssl
eric@eric: cd /etc/apache2/ssl
eric@eric:/etc/apache2/ssl$ sudo openssl genrsa -des3 -out my-server.key
eric@eric:/etc/apache2/ssl$ sudo openssl req -key my-server.key -x509 -out my-server.crt -config /etc/ssl/openssl.cnf -days

3. Activate SSL module

eric@eric:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/ssl.load
eric@eric:/etc/apache2/mods-enabled$ sudo ln -s ../mods-available/ssl.conf
eric@eric:/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 *:>
... RewriteEngine on
RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$ [L,R]
</VirtualHost>
<VirtualHost *:>
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的更多相关文章

  1. Drupal与大型网站架构(译)- Large-Scale Web Site Infrastructure and Drupal

    Drupal与大型网站架构(译)- Large-Scale Web Site Infrastructure and Drupal Linuxjournal 网站经典文章翻译,原文地址: Large-S ...

  2. Web site collections

    Integration 伯乐在线 极客头条 Hacker News Stack Overflow RFC Search Security Python Hacker - Freebuf PrimalS ...

  3. Microsoft Azure Web Sites应用与实践【2】—— 通过本地IIS 远程管理Microsoft Azure Web Site

    Microsoft Azure Web Sites应用与实践 系列: [1]—— 打造你的第一个Microsoft Azure Website [2]—— 通过本地IIS 远程管理Microsoft ...

  4. Azure Web Site 之 利用Azure Web site 发布网站

    由于经常混迹于MSDN Azure论坛,少不了和一些外国朋友打交道.有的时候觉得还是有一些东西可以写出来与外国友人们分享下的, 所以就用一个开源项目建了一个英文blog项目. 在发布的时候,首选的就是 ...

  5. Azure China (7) 使用WebMetrix将Web Site发布至Azure China

    <Windows Azure Platform 系列文章目录> 本章介绍的是,使用世纪互联运维的Azure云服务. 1.首先我们登陆Azure管理界面.http://manage.wind ...

  6. Windows Azure Web Site (6) 使用FTP发布Azure Web Site

    <Windows Azure Platform 系列文章目录> 笔者在之前的文章中介绍的都是使用IDE,也就是Visual Studio,将本地的aspx发布到Azure Web Site ...

  7. Windows Azure Web Site (7) Web Site配置

    <Windows Azure Platform 系列文章目录> 在上一章内容中,我们已经部署了Azure WebSite.我们可以在Web Site配置页面进行配置.如下图: 另外,我们还 ...

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

    <Windows Azure Platform 系列文章目录> 许多已经使用Azure Web Site的用户已经发现了,Azure Web Site默认的系统时间是UTC时区. 比如我们 ...

  9. Windows Azure Web Site (9) Web Site公网IP地址

    <Windows Azure Platform 系列文章目录> 本文会同时介绍国内由世纪互联运维的Azure China和海外Azure Global. 熟悉Windows Azure平台 ...

随机推荐

  1. JS基础篇--JS的event.srcElement与event.target(触发事件对象)

    IE下,event对象有srcElement属性,但是没有target属性; Firefox下,event对象有target属性,但是没有srcElement属性.但他们的作用是相当的,即: fire ...

  2. maven安装的详细步骤

    1.下载maven的bin,在apache官方网站下载.window系统下的下载红色方框的 2.解压后, 把bin的位置设在环境变量里,新建环境变量 MAVEN_HOME.这个配置是方便以后更换mav ...

  3. python数据分组运算

    摘要: pandas 的 GroupBy 功能可以方便地对数据进行分组.应用函数.转换和聚合等操作.   # 原作者:lionets GroupBy 分组运算有时也被称为 “split-apply-c ...

  4. codeforces 435 B. Pasha Maximizes 解题报告

    题目链接:http://codeforces.com/problemset/problem/435/B 题目意思:给出一个最多为18位的数,可以通过对相邻两个数字进行交换,最多交换 k 次,问交换 k ...

  5. hdu 4544 湫湫系列故事——消灭兔子(优先队列)

    题意:n只兔子(有血量),m只箭(有伤害.花费),每只兔子只能被射一次,求射死所有兔子的最少花费. 思路:贪心,2重循环,兔子从血量高到低,箭从伤害高到低,用能射死兔子的箭中花费最小的箭射. #inc ...

  6. Apriori算法实例

    Apriori算法与实例 R. Agrawal 和 R. Srikant于1994年在文献[2]中提出了Apriori算法,该算法的描述如下: 下面是一个具体的例子,最开始数据库里有4条交易,{A.C ...

  7. 出现Insufficient space for shared memory file错误解决

    今天在linux下敲命令,出现上面的错误,原来是临时文件目录(/tmp)下的空间不够了,df一看/下100%了.

  8. LA-4726 (斜率优化+单调队列)

    题意: 给定一个01序列,选一个长度至少为L 的连续子序列使其平均值最大;输出这个子序列的起点和终点;如果有多个答案,输出长度最小的,还有多个就输出第一个编号最小的; 思路: 用sum[i]表示[1, ...

  9. codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

    题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...

  10. 「LuoguP1238」 走迷宫

    Description 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这m*n个数据和起始点.结束点(起始点和结束点都是用两个数据来描 ...