• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Running my Java web app alongside other stuff (wordpress, static sites, other apps etc)  RSS feed

 
Ranch Hand
Posts: 59
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a noob when it comes to understanding the Web so forgive me if my question seems a bit basic

Here is what I have so far:

I have created a very simple app that periodically scrapes my uni's website and provides some information in the form of a basic REST api, at the moment it's just two types of GET request. I used sparkjava (with embedded Jetty server) for that and simply run the jar file using nohup. I have it running on port 4567. Now, I also want to have apache2 running to host other stuff, including Wordpress.

The question is, I want my API (and other projects) to be accessible from standard port (80). I guess I cannot run it on 80 as that's occupied by apache2. So how do I do it?

Basically what I want is to be able to have it like this:

mydomain.com/myApi   --> mydomain.com/myApi:4567
mydomain.com/mySecondApp --> mydomain.com/mySecondApp:8080
mydomain.com/blog



I tried googling the problem but didn't know what I was looking for, I guess I need to configure virtual hosts on apache, just wanted to ask if this is the right way or am I doing it completely wrong?

many thanks



 
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Grabowski wrote:Basically what I want is to be able to have it like this:

mydomain.com/myApi   --> mydomain.com/myApi:4567
mydomain.com/mySecondApp --> mydomain.com/mySecondApp:8080
mydomain.com/blog



If the services are running on the same platform as Apache HTTP, the virtual host configuration should look something like this:This will map requests with path /myApi to 127.0.0.1:4567/myApi, and /mySecondApp to 127.0.0.1:8080/mySecondApp .
 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These pages provide information on how to configure Apache HTTP proxying:

Apache Module mod_proxy    Reverse Proxy Guide
 
Adrian Grabowski
Ranch Hand
Posts: 59
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your post, that's very helpful.

So, I have edited the 00-default.conf file so it looks like this:



But I when I try to restart apache it fails:

adrian@vps528219:~$ sudo service apache2 restart
Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.


adrian@vps528219:~$ systemctl status apache2.service
● apache2.service - LSB: Apache2 web server
  Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
 Drop-In: /lib/systemd/system/apache2.service.d
          └─apache2-systemd.conf
  Active: failed (Result: exit-code) since Sun 2018-12-30 21:47:23 CET; 1min 3s ago
    Docs: man:systemd-sysv-generator(8)
 Process: 17333 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS)
 Process: 7371 ExecReload=/etc/init.d/apache2 reload (code=exited, status=0/SUCCESS)
 Process: 17596 ExecStart=/etc/init.d/apache2 start (code=exited, status=1/FAILURE)

I tried both <VirtualHost _default_:80>   and <VirtualHost *:80>

Am I doing something wrong? After commenting out ProxyPass and ProxyPassReverse apache restarts fine.

 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try checking the system message log to see if it provides any clues (on my server it is at /var/log/messages).

Are you loading mod_proxy and related modules?  For my server, under conf.modules.d I have a file named 00-proxy.conf (name may be different) with the following contents:
I'm not sure specially which ones are required, but I would guess at least mod_proxy.so and mod_proxy_http.so .
 
Adrian Grabowski
Ranch Hand
Posts: 59
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were right, those modules weren't loaded, now it works fine Thank!

I have encountered another problem, HTML loads fine but CSS is gone

So when I go to mydomain.com/myapp there is no styling, just pure HTML

But mydomain.com:4567 loads the app with CSS

All the CSS is inside jar file and works fine without proxying. Any hints?
 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never used Spark, so I'm not sure what type of debugging information it can provide.

When I troubleshoot these kinds of problems, I tend to start at the wire and work my way up using something like Wireshark or tcpdump.  I would look at the network traffic going in and out of Spark and see what the differences are when connecting directly and when proxied.  If there is nothing obvious there, look from the browser side of things to see if maybe ProxyPassReverse processing somehow messed-up the response.  This could be done with the same packet sniffers, using the browser's Developer Mode, or with Postman.
 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem may be with the CSS paths.  ProxyPassReverse only rewrites the headers (if required), but not the body.  Use relative CSS paths rather than absolute.  For example:

Relative:
<link rel="stylesheet" type="text/css" href="../css/site.css">

Absolute:
<link rel="stylesheet" type="text/css" href="/mySecondApp/css/site.css">
<link rel="stylesheet" type="text/css" href="http://mydomain.com/mySecondApp/css/site.css">
 
Adrian Grabowski
Ranch Hand
Posts: 59
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I'm stilll trying to figure it out, in the meantime let me explain how I have it at the moment:

So, in my project structure, I have my CSS file in src/main/resources/public/bootstrap/css/bootstrap.css

In my sparkjava app I have



And in the HTML (well, actually Handlebars template but I guess it's not important) I have



Now, when I use developer tools I can see that the request path looks like this:

http://www.mydomain.com/bootstrap/css/bootstrap.css

and it returns 404

if I manually add 4567 port number it finds the css file

According to spark documentation:


Static Files
You can assign a folder in the classpath serving static files with the staticFiles.location() method. Note that the public directory name is not included in the URL.
A file /public/css/style.css is made available as http://{host}:{port}/css/style.css

// root is 'src/main/resources', so put files in 'src/main/resources/public'
staticFiles.location("/public"); // Static files


 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Grabowski wrote:I have my CSS file in src/main/resources/public/bootstrap/css/bootstrap.css


Where is the HTML file located - directly under src/main/resources/public/?
What is the URL that you are entering in to the browser?

Adrian Grabowski wrote:And in the HTML (well, actually Handlebars template but I guess it's not important) I have Now, when I use developer tools I can see that the request path looks like this: http://www.mydomain.com/bootstrap/css/bootstrap.css


That is going to be a problem you don't have a rule like ProxyPass /bootstrap http://127.0.0.1:4567/bootstrap to cause that URL to be proxied to your Spark application.

Try making the link relative to the HTML file (assuming HTML files located under src/main/resources/public/):

 
Adrian Grabowski
Ranch Hand
Posts: 59
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Try making the link relative to the HTML file (assuming HTML files located under src/main/resources/public/):



That worked! Thank you so much, looks like I still need to learn a lot but at least I got it up and running and can go back to coding

 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!