一、安装apache2
ubuntu下直接运行以下命令
sudo apt-get install apache2
默认安装目录为/etc/apache2
二、配置apache2
1. apache配置文件说明
mods-available
mods-enabled
sites-available
sites-enabled
conf-available
conf-enabled
这几个文件夹都是放配置文件的
mods-* ,存放模块的配置文件,比如连接tomcat的jk模块,http_proxy模块等的配置。
site-* ,存放虚拟主机的配置。
conf-*,存放apache全局配置文件。
apache2.conf:主配置文件,所有的配置文件都需要用IncludeOptional 包含进来才会生效
2. 修改网站根目录
vi /etc/apache2/apache2.conf
把文件里面的/var/www改成目标地址
<Directory /home/web/>
Options Indexes FollowSymLinks
AllowOverride all
#AllowOverride None
#Require all granted
</Directory>
3.配置站点
进到站点配置目录
cd /etc/apache2/sites-available/
新建一个test.conf文件
vim test.conf
在里面添加内容
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName test.com #域名按需求修改
DocumentRoot "/home/web/test" #网站所在路径,要在/home/web/下,也就是刚才配的根目录
ErrorLog "/home/logs/test/edunuke_errors.log"
CustomLog "/home/logs/test/edunuke_accesses.log" combined
<Directory /home/web/test/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
</VirtualHost>
创建软连接到 sites-enabled 文件夹
sudo ln -s /etc/apache2/sites-available/test /etc/apache2/sites-enabled/test
在apache2.conf 记得引入配置文件
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
IncludeOptional mods-enabled/*.conf
重启apache
service apache2 restart
浏览器访问http://test.com查看效果
三、整合tomcat
下载JKMount,编译,得到mod_jk.so文件,将mod_jk.so复制到/etc/apache2/modules下
并在apache2.conf中添加
LoadModule jk_module modules/mod_jk.so
在/etc/apache下新建workers.properties 文件用来分发请求
workers.tomcat_home=/home/tools/apache-tomcat-8.0.39/ #tomcat路径
workers.java_home=/usr/local/java/jdk1.8.0_171/jre #jdk路径
worker.list=worker1
# Set properties for worker1 下面的都可以不用修改,感兴趣可以搜索相关含
义
worker.worker1.type=ajp13
worker.worker1.port=8009
worker.worker1.lbfactor=50
#worker.worker1.cachesize=10
#worker.worker1.cache_timeout=600
#worker.worker1.socket_keepalive=1
#worker.worker1.socket_timeout=300
进到mods-available 文件夹新建一个mod_jk.conf文件用来配置jk
cd mods-available
vim mod_jk.conf
添加以下内容:
LoadModule jk_module /etc/apache2/modules/mod_jk.so
#JkWorkersFile 路径根据实际情况填写
JkWorkersFile /etc/apache2/workers.properties
# Where to put jk logs
JkLogFile /home/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompatUnparsed -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
#根据实际情况自己定义即可,就是将什么样的请求转发给worker1处理
JkMount /* worker1
JkMount /*.jsp worker1
JkMount /servlet/* worker1
JkMount /*.do worker1
JkMount /*.action worker1
#JkMount /*.class worker1
#JkMount /*.jar worker1
创建软连接到mods-enabled
sudo ln -s /etc/apache2/mods-available/mod_jk.conf /etc/apache2/mods-enabled/mod_jk.conf
重启apache
四.配置websocket支持
用域名访问的时候websocket没响应问题
要使用到mod_proxy模块,该模块已存在于/usr/lib/apache2/modules/
打开apache2.conf
在最下边添加以下内容:
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_wstunnel_module /usr/lib/apache2/modules/mod_proxy_wstunnel.so
LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
#ProxyPass /web/websocket/ ws://localhost:8080/web/websocket/
ProxyPass 8080:/ http://localhost:8080/
mod_proxy提供代理服务器功能,mod_proxy_balancer提供负载均衡功能, mod_proxy_http让代理服务器能支持HTTP协议
ProxyPass 添加映射
如果websocket部署在tomcat的话添加 ProxyPass 8080:/ [http://localhost:8080/]("http://localhost:8080/") 就OK了。
因为apache上也部署了网站且是80端口,打开test.com时应该要访问原来部署在apache上的站点,是http而不是websocket,所以要加上mod_proxy_http.so