查看 Apache Http Server 版本号:apachectl -v

本文打包后的程序为例,包含前后端(后端只是代理一下),使用 Apache Http Server 2.4 版本。如果是刚安装完的 Apache,默认没有开启 proxy 模块,需要手动开启:

1
2
# 开启所有Proxy模块功能
a2enmod proxy*

然后可以直接 copy 模板 /etc/apache2/sites-available/000-default.conf

1
2
3
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

vim /etc/apache2/sites-available/example.com.conf

添加以下内容,注意 port 要修改为自己前端程序使用的端口。

/etc/apache2/sites-available/example.com.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<VirtualHost *:port>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
# 前端打包的程序路径
DocumentRoot /var/lib/projects/workspace/deploy/dist

# 后端地址代理
# 正向代理
ProxyPass /prod-api http://localhost:8081
# 反向代理
ProxyPassReverse /prod-api http://localhost:8081

FallbackResource /index.html

# 前端打包的程序路径
<Directory /var/lib/projects/workspace/deploy/dist>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

然后启用新的配置:

1
2
3
4
5
# 启用新的配置
a2ensite example.com.conf

# 查看已启用的配置
ll /etc/apache2/sites-enabled/

Apache 的默认监听端口只有 80,如果其它的端口,我们还需要添加一下:

/etc/apache2/ports.conf
1
2
3
4
5
6
7
8
9
10
11
12
# 添加自己需要的端口
Listen port

Listen 80

<IfModule ssl_module>
Listen 443
</IfModule>

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

最后重启服务:

1
systemctl restart apache2

本站由 江湖浪子 使用 Stellar 1.29.1 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。