你好!Apache

也许放弃 才能靠近你 不再见你 你才会把我记起 时间累积 这盛夏的果实 回忆里爱情的香气 我以为不露痕迹 思念却满溢 或许这代表了我的心 不要刻意说 你还爱我

<盛夏的果实> --莫文蔚

Apache官网

为什么更换为Apache的服务端软件?

主要原因有以下几点:

  • 随着学习的不断的深入,不断能感受到Apache基金会的牛逼过人之处,以及微软,所以数据库软件我也由原本的mysql换成了sql server,不得不说大企业的软件就是好用👌
  • caddy只适合一些轻量级的网站,性能捉急
  • 稳定是排在第一位的

主要想要做到以下几点:

  • 全站ssl加密,即https
  • 代理 设置比较方便,配置文件比较好设置
  • 目录浏览这个我打算自己写一个好的

由于刚接触很久没有接触Apache,我打算在虚拟机上面熟悉以后再搬到生产环境上面去。接下来的笔记将会从以下几个方面来记录:

  • Apache安装包括编译安装
  • Apache配置文件位置,熟悉配置文件
  • 对服务器的优化设置

macOS下面,httpd.conf里面设置User为自己系统里面的用户名,可以解决没有搜索权限

1
2
3
4
5
6
7
8
9
10
11
12
13
Listen 80
<VirtualHost *:80>
DocumentRoot /Users/sagit/Documents/wwwroot/Blog/public
ServerName blog.ourfor.com
<Directory "/Users/sagit/Documents/wwwroot/Blog/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Require all granted
Allow from all
</Directory>
ErrorLog "/usr/local/var/log/httpd/blog.ourfor.com.com-error_log"
</VirtualHost>

如果页面提示禁止没有权限,那么不要忘了<Directory>里面的内容:
1
2
3
4
5
6
7
<Directory "/Users/sagit/Documents/wwwroot/Blog/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Require all granted
Allow from all
</Directory>

macOS编辑mod_jk,安装好httpd -apache
编译命令:

1
2
./configure CFLAGS='-arch x86_64' APXSLDFLAGS='-arch x86_64' --with-apxs=/usr/local/bin/apxs
make

有些脚本是在window下面写的,在unix上面有些不可见字符,可以用vi打开后设置fileformat,即:set fileformat=unix或者:set ff=unix

apache httpd服务器连接tomcat

演示系统环境macOS Majave 10.14.4

  • 加载mod_jk模块,我已经编译好了最新的,适用于Apache版本Server version: Apache/2.4.39 (Unix)

httpd的配置文件中添加一个名为httpd-jk.conf(名字可以随便取),同时在httpd.conf里面导入这个配置文件:

1
2
# Virtual hosts
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf

顺便把User改成你的登录用户名,原本为_www

1
2
User sagit
Group _www

这样的话网站更目录就不需要放在默认的位置了

  • 配置httpd-jk.conf文件,该文件负责导入mod_jk,将下载好的mod_jk.so文件放到httpd的模块文件夹里面,然后编辑httpd-jk.conf文件:

https://github.com/ourfor/Blog/blob/d8b3eb027971e15ce854055dc1348de2367009be/tomcat/mod_jk/httpd-jk.conf#L22-L123

主要注意下面几个位置

1
2
3
4
5
6
7
8
9
10
11
LoadModule jk_module lib/httpd/modules/mod_jk.so
<IfModule jk_module>
# and in the global server
JkWorkersFile /usr/local/etc/httpd/extra/workers.properties

# Our JK error log
# You can (and should) use rotatelogs here
JkLogFile /usr/local/var/log/httpd/mod_jk.log

# Our JK shared memory file
JkShmFile /usr/local/var/log/httpd//mod_jk.shm

加载mod_jk的路径,这里的路径跟随httpd.conf里面写,因为这个文件会被原封不动的include到httpd.conf里面,只需要修改一下最后的模块文件名,即mod_jk.so
接下来这个JkWorkersFile指明workers.properties的位置,这个worker.properties文件最好放在和httpd-jk.conf同一目录下面,并且按照httpd.conf里面导入额外的配置文件的父路径一致,JkLogFileJkShmFile这两个文件的父路径和httpd.conf里面存放日志文件的父路径一致

  • workers.properties里面写什么

    1
    2
    3
    4
    5
    6
    7
    worker.list=jk-manager,tomcatA
    worker.jk-manager.type=status

    worker.tomcatA.type=ajp13
    worker.tomcatA.host=localhost
    worker.tomcatA.port=8009

    主要注意worker.list,这里面写了几个东西,下面就要配置几个

  • 接下来是tomcat的server.xml文件

https://github.com/ourfor/Blog/blob/d8b3eb027971e15ce854055dc1348de2367009be/tomcat/mod_jk/server.xml#L128-L133

  • Apache里面配置虚拟主机就行了
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <VirtualHost *:80>
    DocumentRoot /usr/local/Cellar/tomcat/9.0.17/libexec/webapps/lab_war_exploded
    ServerName file.ourfor.com
    JkMount /* tomcatA
    DirectoryIndex index.html index.jsp
    <Directory "/usr/local/Cellar/tomcat/9.0.17/libexec/webapps/lab_war_exploded">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    </Directory>
    </VirtualHost>
    其中jkMount是匹配规则,匹配到什么路径才交给tomcat处理,以及交给谁处理

如果需要在一个服务器上面部署配置多个tomcat的webapp的话,只需要在httpd-vhosts.confserver.xml里面添加就可以了.

server.xml

https://github.com/ourfor/Blog/blob/e1a974fe623816814c20f87acdd51aefa0234494/tomcat/mod_jk/server.xml#L128-L140

httpd-vhosts.conf

https://github.com/ourfor/Blog/blob/e1a974fe623816814c20f87acdd51aefa0234494/tomcat/mod_jk/httpd-vhosts.conf#L37-L61

如果使用idea开发的话,这里server.xml里面的docBase也写成绝对路径,或者将appbase写成绝对路径,二选一
1
2
3
4
5
6
7
8
9
10
11
12
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcatA">
<Host name="file.ourfor.com" appBase="/Users/sagit/Desktop/www"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="lab_war_exploded" />
</Host>

<Host name="doc.ourfor.com" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/Users/sagit/Desktop/www/demo" />
</Host>

通过jk连接器,web应用程序的数据源找不到

可以考虑将数据源配置到tomcat的server.xml里面,但是我更推荐下面这种:

在web程序的web.xml里面添加:

1
2
3
4
5
<resource-ref>
<res-ref-name>jdbc/shop</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

然后将context.xml复制为$CATALINA_BASE/conf/[enginename]/[hostname]/context.xml.default

具体原因查看官方文档

When autoDeploy or deployOnStartup operations are performed by a Host, the name and context path of the web application are derived from the name(s) of the file(s) that define(s) the web application. Consequently, the context path may not be defined in a META-INF/context.xml embedded in the application and there is a close relationship between the context name, context path, context version and the base file name (the name minus any .war or .xml extension) of the file.

If no version is specified then the context name is always the same as the context path. If the context path is the empty string then the base name will be ROOT (always in upper case) otherwise the base name will be the context path with the leading ‘/‘ removed and any remaining ‘/‘ characters replaced with ‘#’.

If a version is specified then the context path remains unchanged and both the context name and the base name have the string ‘##’ appended to them followed by the version identifier.
Some examples of these naming conventions are given below.

一些网友的解决方案