查看 Apache Http Server 版本号:
apachectl -v。
本文打包后的程序为例,包含前后端(后端只是代理一下),使用 Apache Http Server 2.4 版本。如果是刚安装完的 Apache,默认没有开启 proxy 模块,需要手动开启:
# 开启所有Proxy模块功能
a2enmod proxy*
然后可以直接 copy 模板 /etc/apache2/sites-available/000-default.conf:
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
vim /etc/apache2/sites-available/example.com.conf
添加以下内容,注意 port 要修改为自己前端程序使用的端口。
<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>
然后启用新的配置:
# 启用新的配置
a2ensite example.com.conf
# 查看已启用的配置
ll /etc/apache2/sites-enabled/
Apache 的默认监听端口只有 80,如果其它的端口,我们还需要添加一下:
# 添加自己需要的端口
Listen port
Listen 80
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
最后重启服务:
systemctl restart apache2

说些什么吧!