1. 打包后端项目(jar包)
IDEA里执行package将后台工程打成jar包,上传至服务器
我将jar包上传至了target目录,cd至target目录,执行以下命令启动后台(&表示后台运行,退出不会结束进程):
[root@zy-host target]# java -jar exam-admin.jar &
2. 打包前端项目(dist包)
在前端工程下执行以下命令对前台项目打包。
# 构建生产环境
npm run build:prod
构建完之后,前端目录会出现一个dist文件夹,将该文件夹的内容上传至服务器指定目录,这里的目录可以自定义,和apache中指定的目录一致即可。我上传至了/usr/local/apache2.4.54/htdocs/html目录中。
3. Apache 配置
(1)在apache的配置目录中/usr/local/apache2.4.54/conf/httpd.conf,指定站点目录
DocumentRoot "/usr/local/apache2.4.54/htdocs"
<Directory "/usr/local/apache2.4.54/htdocs">
Options Indexes FollowSymLinks
AllowOverride none
Require all granted
</Directory>
#进一步放宽站点目录权限
<Directory "/usr/local/apache2.4.54/htdocs/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
(2)打开代理服务模块
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
mod_proxy提供代理服务器功能, mod_proxy_http让代理服务器能支持HTTP协议。
(3)配置ProxyPass
在httpd.conf文件末尾追加:
请求到 /exam-api/v4/ 上的请求,会转发到后端项目的发布地址 http://39.107.232.89:8080/
ProxyPass /exam-api/v4/ http://39.107.232.89:8080/
ProxyPassReverse /exam-api/v4/ http://39.107.232.89:8080/
退出保存,然后重启httpd服务,访问 http://39.107.232.89 ,可以成功访问:
(4)进去刷新会出现404错误,如下:
只需在httpd.conf文件中加以下配置即可,/ 后面根据项目具体情况来定。
FallbackResource /exam
2022-12-26补充:
或者将需要配置的内容写入一个配置文件中:
ProxyPass /geomis-api/v1 http://127.0.0.1:8090
ProxyPassReverse /geomis-api/v1 http://127.0.0.1:8090
Alias /geomis "/home/kylin/geomis/dist"
<Directory "/home/kylin/geomis/dist">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order Deny,Allow
Allow from all
Satisfy Any
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /geomis/
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /geomis/index.html [L]
</IfModule>
FallbackResource /geomis
</Directory>
对应的主配置文件内容:
httpd.conf
ServerRoot "/etc/httpd"
#监听端口
Listen 8088
#采用配置文件加载模块
Include conf.modules.d/*.conf
#用户组设置
User apache
Group apache
ServerAdmin root@localhost
<Directory />
AllowOverride none
Require all denied
</Directory>
#指定网站文件存放的目录路径,通常配置在主配置文件中,或虚拟主机文件中
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#IfModule模块用于 如果存在该模块,标签中的内容就生效
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<Files ".ht*">
Require all denied
</Files>
#错误日志存放目录
ErrorLog "logs/error_log"
#设置错误日志等级
LogLevel warn
#Apache日志格式配置
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
#EnableMMAP off
EnableSendfile on
# 在 "/etc/httpd/conf.d" 目录中加载配置文件
IncludeOptional conf.d/*.conf
#ServerTokens 参数设置 http 头部返回的 apache 版本信息, prod设置为仅返回软件名称,如apache
ServerTokens Prod
#在服务器生成的文档上配置页脚
ServerSignature off
TraceEnable off
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/87997.html