Running my Java web app alongside other stuff (wordpress, static sites, other apps etc)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 .
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Apache Module mod_proxy Reverse Proxy Guide
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 .
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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">
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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/):
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
| keep an eye out for scorpions and black widows. But the tiny ads are safe. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |











