Deploy springboot application and react application to ubuntu server - spring

I created a simple application, which consists of a reactjs frontend and a spring boot app as backend. It works fine on my local environment; with the react app running on port 3000, and the spring app running on port 9000.
I am trying to host this application in the staging environment. I want to host both the reactjs and spring boot app on a ubuntu server that I have on digitalocean. I have managed to deploy the reactjs frontend using nginx and now I want to deploy the backend using apache2.
I am having difficulty deploying the Spring boot app. I have generated a WAR file.
In my react application I call the backend spring app to serve as the api to the frontend to the browser. I have set up apache app to listen on port 9000 see below:
<VirtualHost *:9000>
ServerName 46.101.34.160
ServerAdmin webmaster#localhost
DocumentRoot
/var/www/html/BackEndBookingAndCollections
</VirtualHost>
My configuration details for the frontend goes as follows in the file;
/etc/nginx/sites-available/FEBookingAndCollections
The contents is the following:
server {
listen 80;
root /var/www/FEBookingAndCollections/build;
listen [::]:80 default_server;
server_name 46.101.34.160;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
location /api/bookings/ {
proxy_pass http://46.101.34.160:9000/api/bookings;
}
}
In google chrome's browser I get the following error:
Failed to load resource: net::ERR_CONNECTION_REFUSED :9000/api/bookings:1

With spring boot, you don't have to use external Tomcat. You can package your app to jar with embedded tomcat. You can find more info here - https://www.baeldung.com/deployable-fat-jar-spring-boot.

You can't deploy war file without a server. Just follow bellow steps
1) install tomcat
2) find tomcat webapps path ( /usr/local/tomcat/webapps )
3) copy your war file to webapps. ( use scp )
4) make sure your war file and tomcat listen on same port. 8080 or 9000
5) proxy your api though apache

Related

Spring Boot with embedded Tomcat behind Apache SSL proxy

I'm searching for a solution to be able to run a Spring application behind an Apache SSL proxy. I tried a lot of configurations without success. All Spring responses go to http causing a Not Found error.
The following is apache configuration:
SetEnv proxy-initial-not-pooled 1
ProxyPreserveHost On
KeepAlive On
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
ServerName server.mydomain.dom
ProxyTimeout 600
ProxyPass /excluded !
RequestHeader set X-Forwarded-Proto https
RequestHeader set X-Forwarded-Port 443
ProxyPass / http://127.0.0.1:8081/
ProxyPassReverse / http://127.0.0.1:8081/
These are the Spring options:
server.port=8081
server.forward-headers-strategy=NATIVE
#server.tomcat.redirect-context-root=false
server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto
server.tomcat.internal-proxies=.*
I'm using Spring Boot 2.5.6 on Apache Tomcat/9.0.54. The OS Apache is a 2.4.25 version running on a Debian 9.13.
The problem seems to happen after login into the application and logout. If I substitute http to https after the login action, I'm able to navigate into the application. All links works fine until I logout. When I logout the application goes again to http.
I solve the problem. The first step was to add
server.tomcat.use-relative-redirects=true
in the application.properties. With this directive, the proxy works fine.
In the end, I configure the apache/application to use AJP.

Link Domain with Nginx and Tomcat with a specific Port and Folder

I have a JSP application running on a Tomcat server with Port 8070
the root directory of the application is called myapp and it's accessed via localhost:8070/myapp/
Now I am trying to host it under a domain myapp.com on a Windows VM with Nginx and tomcat
Could someone help me how to do the Nginx configuration for the above scenario?

Getting subdomains to work locally with Laravel

I am having a hard time getting subdomains to work locally. I have Docker serving the application to port 8080, and I am able to see the Laravel welcome screen. I then have a simple route setup like this:
Route::domain('{name}.localhost:8087')->group(function () {
return 'Hello World';acrylic dns
});
I am using Laravel's basic server, i.e. php artisan serve --host=0.0.0.0 --port=8087
When I try and view the page, nothing happens. It just goes to the welcome screen. I have even tried adding 'test.localhost' to the /etc/hosts file. Couple questions:
1) Can you have the port in the host like I have it there (in the Laravel route)?
2) I have seem somewhat similar posts where the solution was to use acrylic dns (on windows). I am using a Mac. Is this something where I need an actual DNS server?
3) I am planning on using nginx, do I need a 'beefier' web server to accomplish this?
With the basic Laravel server I have tried hard coding test.localhost in the route, with and without the port. I'm sure I am goofing something up, just not sure where. I am on a Mac, and I am running Laravel 5.6. Thanks in advance!
1) No, the web server configuration will listen on the port.
// nginx
server {
listen 8080;
...
}
2) You add the subdomains in your /etc/hosts file and create separate nginx configurations:
// /etc/hosts
subdomain1.foo.localhost 127.0.0.1
subdomain2.foo.localhost 127.0.0.1
subdomain3.foo.localhost 127.0.0.1
// nginx subdomain1.foo.localhost.conf
server {
listen 8080;
server_name subdomain1.foo.localhost;
...
}
// nginx subdomain2.foo.localhost.conf
server {
# set different port if needed
# listen 8082
listen 8080;
server_name subdomain2.foo.localhost;
...
}
// nginx subdomain3.foo.localhost.conf
server {
# set different port if needed
# listen 8083
listen 8080;
server_name subdomain3.foo.localhost;
...
}
3) Nginx is a production ready web server, you may need load balancers and multiple instances of the web servers to scale out, but nginx will be more than sufficient.
If you're using Artisan serve, go to
/etc/hosts (or similar)
127.0.0.1 subdomain.localhost
And open in the browser
subdomain.localhost:8087

Deploying gradle spring application on a 1and1 cloud server

I have an apache/2.4.18 ubuntu server and I want to host my spring application on it. I generated a JAR file and can run it on the server. It starts an embedded tomcat server on port 8090.
However when i navigate to 'my-site-ip:8090' the connection times out.
I have zero experience deploying web applications so any help would be appreciated.
I've created a TCP rule for port 8090 and still no joy.
The solution was adding a proxy to the Myapp.conf file as below:
ProxyRequests off
ProxyPreserveHost On
ProxyPass / http://localhost:8090/
ProxyPassReverse / http://localhost:8090/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
It´s very hard to explain all the steps in one answer but you can follow these steps to get into the full configuration by your own. I did the same on my 1&1 cloud server.
First of all you need root access to your server.
Normally, on your server the port 80 and 443 should already be open. Else you can define that in the 1&1 Admin Portal. If your Server already has the apache configuration you should be able to see the apache site if you go to your server address. You can find details and the full setup if you dont have an apache installed for this step here:
How To Install the Apache Web Server on Ubuntu
The second step would be to configure a virtual host on your apache webserver.
This is cool because you can define multiple domains and there applications on your server. So http://yourServer.com(port 80 or 443 from extern) goes to yourApp1. (port 8090 from intern).
In this step you will tell apache if your enter your url to go to your app with port 8090
How To Set Up Apache Virtual Hosts on Ubuntu
The last step would be to install your spring-boot app as a service on your machine. The docs of Spring describes it very well.
Installation as an init.d Service
If you install the app as a service you are able to start and stop the app with the service command.
service myapp start
And dont forget to add the plugin for maven or gradle to your pom.xml. This is necessary to run the app as a service.
If you follow these Steps you should be able to reach you app without specify a port and be ready to go with your app in production if necessary.
The best approach for this would be to use the apache proxy. This should get it done.
https://blog.marcnuri.com/running-apache-tomcat-and-apache-httpd-on-port-80-simultaneously/

Tomcat 7 - Ubuntu : create a domain name instead of IP:8080/appName

I have deployed my webapp on an Ubuntu server in my company.
the war is in the folder :
/var/lib/tomcat7/webapps
Tomcat7 has deployed the archive in the folder
/var/lib/tomcat7/webapps/bioApp/
My problem is that to access the web app I need to type the url :
http://IP_OF_SERVER:8080/bioApp
I would like the users to type :
www.bioApp.com
I have tried many solutions (modifying server.xml, hosts file, etc).
None of them work.
If anyone has a working solution...
Thanks
The first thing you need is a local dns server in your company. In your dns server add a rule to route your ip (IP_OF_SERVER) to the local domain name
IP_OF_SERVER bioApp.com
If you had an apache server, you must add a virtualhost in your conf file (See this page for more information)
<VirtualHost *:80>
ServerName bioApp.com
ProxyPass / http://IP_OF_SERVER:8080/bioApp
ProxyPassReverse / http://IP_OF_SERVER:8080/bioAppr
</VirtualHost>
If you don't have a apache server, you can change your tomcat listen port.(See this post).

Resources