can apache handle non-http messages - apache

I have a apache webserver, and we need to develop a module in it (using mod_python or mod_wsgi). It will ofcourse handle the requests sent as http GET/POST. However, we would like this module to ALSO handle messages sent by another remote applications which are NOT http based. That is, the body of the those tcp packets may just be a json or xml formatted message. That remote application just keeps spitting out notifications every once in a while and is not expecting any responses from the apache module.
I was wondering if I can spawn two threads in my module (one receiving messages from apache when http messages are received on port 80, the other thread listening on some other port .. say 2000 .. and my remote machine is configured to send to port 2000). Another possibility is that I run a separate application on my apache machine listening on port 2000. When it receives notification, I wrap it into http message and send it back to myself on port 80.
I guess the question is: Is my module limited to getting messages only via the apache (from port 80) or can my apache module also listen on other ports ? If there's a simpler solution, please let me know. Thanks.

You can setup apache to listen on different ports, using the VirtualHost directive:
Listen 80
Listen 2000
<VirtualHost *:80>
ServerName www.example.com
WSGIScriptAlias / /path/to/script.wsgi
<Location /my/location>
...
</Location>
</VirtualHost>
<VirtualHost *:2000>
ServerName www.example.com
WSGIScriptAlias / /path/to/script.wsgi
<Location /my/location>
...
</Location>
</VirtualHost>
More info here.
However, it might be easier to listen on port 80 and differentiate by using different paths, like so:
<VirtualHost *:80>
WSGIScriptAlias /normal /path/to/script.wsgi
WSGIScriptAlias /notifications /path/to/script.wsgi
WSGIApplicationGroup %{GLOBAL}
...
</VirtualHost>

Because Apache in most configurations is a multi process web server, you can't just listen on a separate socket from your Python code as multiple processes would attempt to do the same and so they would clash.
Technically you could use mod_wsgi daemon mode for your WSGI application and run it with a single process to avoid the multiprocess issue, but would still question whether this is a good idea and the completely separate process distinct from Apache is probably better.

Related

How can I change all my links on my Apache site to look for port 8080 instead of 80 after changing my site from 80?

My local Apache environment, on XAMPP, was set up to listen on port 80. I now changed it to listen to port 8080. my site now works on port 8080. I have to go to mysite.local:8080 to make it work, which is fine. The only problem is when I click a link it goes to mysite.local/page instead of mysite.local:8080/page and the page does not work. Also, some of my pages that use a log in require me to NOT use 8080 in the url so I have to use mysite.local/admin instead of mysite.local:8080/admin so I am constantaly swithing between adding the port number in the url and taking it away for some other pages. Is there a way where I can make it where I don't have to specify port number on any of the pages. This happened after installing IIS by the way.
My vhost page now looks like the below:
<VirtualHost mysite.local:8080>
DocumentRoot "C:\repos\www.mysite.org\web"
ServerName mysite.local
ErrorLog "logs/mysite-error.log"
CustomLog "logs/mysite-access.log" common
<Directory "C:\repos\www.mysite.org\web">
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
The problem is, that after you changed Apache to listen on 8080 Apache does not even see traffic on :80 (and that's where default HTTP traffic goes).
As you said that you have IIS listening on :80, the only way to solve your problem is to make IIS act as a reverse proxy that would forward all the traffic that should be meant for Apache to Apache.

Using Apache with another web server

On my system I have more than one web daemon running, one of them is Apache which is listening on ports 80 and 443. Another only accepts local connections on a different port.
Is it possible for Apache to forward connections to another daemon, wait for the reply before sending it back to the original client?
Possible config file could look like:
<VirtualHost *:80>
ServerName another-hostname.com
ForwardConnectionTo localhost:4949
</VirtualHost>
Use a ProxyPass. Make sure you have mod_proxy and mod_proxy_http enabled:
<VirtualHost *:80>
ServerName another-hostname.com
ProxyPerserveHost On
ProxyPass / http://localhost:4949
</VirtualHost>

Best approach to set up PHP and Java application on same host

I have two web applications one in PHP and one in Java (Play framework).
I want to make both these applications available to my clients and I have only one server for test environment.
What would be the best and easy to maintain approach for my problem?
I am already looking at options of virtual hosts on Apache server. But is the best? Are there any third party tools which can help me to divert traffic to PHP and Java apps based on the port in the http request?
Port nos for PHP app is 80 and Java app is 9000.
Regards,
Suraj
assuming both ports are forwarded correctly and Apache is only listening for traffic on port 80 and java is only listening on 9000 then going to YourIp:80 should take you to apache and YourIP:9000 should take you to the java app
For PHP app create common vhost as usally, and for Play app create reverse-proxy vhost (with other domain and/or subdomain for this), take a look at samples in docs
LoadModule proxy_module modules/mod_proxy.so
...
<VirtualHost *:80>
DocumentRoot "/path/to/your/php/app/root/folder/"
ServerName your-php-app.com
ErrorLog "/path/to/apache/logs/folder/your-php-app.com-error_log"
CustomLog "/path/to/apache/logs/folder/your-php-app.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ServerName your-play-app.com
ProxyPass /excluded !
ProxyPass / http://127.0.0.1:9000/
ProxyPassReverse / http://127.0.0.1:9000/
</VirtualHost>
Thanks for your comment.
Yeah, I did the same thing. I am simply pointing my domain name to Apache Http server. So its using default port 80 and does not show up in urls.
Also, I am using Apache as front server for all my requests to port 9000. So, I am rerouting my requests for Play application at port 9000 through Apache port 80. It needed a change in url patterns so that play and apache specific urls can be distinguished.
Changed urls and used proxy_http_module + ProxyPassMatch directive to get the rerouting working.
Suraj

Remove Port Number from URL Thin Server 1.5 with Rails 3

I have a small Rails app currently being served (dev and production) by Thin server on port 3000, on a Debian box that also has apache2 running, serving PHP applications.
I am not certain if this is even possible given the current setup but is there a way to remove the port number from the rails app url but still have Thin listening from the same port so not to bump into apache?
Current
https://my-rails-site:3000/
Proposed
https://my-rails-site/
Or should I just use something like Passenger?
OR
mod_proxy?
You want to remove the port number from the URL and have your app listening on a non-standard port at the same time? Then you must have Apache/Nginx must listen on the standard port and proxy requests to your app's non-standard port. You need to either setup Apache with mod_proxy or Nginx with proxy_module.
If you just want your app to be accessible over a standard port then you can just use Phusion Passenger, that makes things very easy.
If you are using passenger here is what I had to use to get it working on www.mysite.com without using www.mysite.com:80 on a centos server:
In etc/httpd/conf the key was to uncomment the NameVirtualHost *:80 and change the * to my server's IP address. Make sure Listen 80 is uncommented. Also add your ip to the VirtualHost tag. It must be running on port 80, not 8080 or something of your choosing.
NameVirtualHost xx.xx.xx.xx:80
Listen 80
<VirtualHost xx.xx.xx.xx:80>
ServerName www.mysite.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/vhosts/mysite.com/httpdocs/public/
<Directory /var/www/vhosts/mysite.com/httpdocs/public/>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>

Send subdomain to node.js

My work runs a couple different internal web apps on an ubuntu server (10.10) running apache. I'm currently developing another web app, and am seriously considering developing on top of a custom-built node.js web server. My reasoning for wanting to do this is:
Speed/Scalability
Security - Pages will be served with a switch...case, instead of just serving the (potentially malicious) user whatever they ask for.
Ease of setup - my intentions are for this to be an open-source project, and node.js is much easier for users to set up, rather than dealing with apache/IIS/etc.
My question is, on a server where I've got apache listening to port 80, how can I pass off a certain subdomains to node.js. I've seen a couple articles about using apache virtual hosts to pass it off, but that seems to defeat the purpose of using node.js. If I have to go through apache, then all three of my reasons for avoiding apache/IIS have voided themselves.
I know I could use a different port (:8080?), but from an end-user standpoint, it's pretty confusing having to put in custom ports. Any alternative ideas?
Thanks
<VirtualHost *:80>
ServerName subdomain.yourdomain.com
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
</VirtualHost>
Thanks to http://www.chrisshiplet.com/2013/how-to-use-node-js-with-apache-on-port-80/
if socket.io node is running, be sure to enable also few apache mods:
a2enmod proxy
a2enmod proxy_balancer
a2enmod proxy_express
a2enmod proxy_http
in file /etc/apache2/sites-available/chat.example.com.conf
<VirtualHost *:80>
ServerName chat.example.com
<Location "/">
ProxyPreserveHost On
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
</VirtualHost>
then of course service apache2 reload
How about doing things the other way round : bind node to port 80, handle the traffic targeted at the subdomain and use it as a reverse proxy to apache for everything else ?
Let me start from the ground up:
You have a DNS. And a dns server maps one DNS to one IP!
You then have apache running on your computer that listens for connections on port 80 for http:// and on port 443 for https://. http://example/ is actually a request on http://example:80/.
You can't use node.js to listen on the same machine on the same port as apache. That's why using port 8080 is viable.
You can also map the subdomain to a different IP. The only caveat here is that you need to have a public IP Address.
You can't serve port 80 from both Apache and node.js. Having Apache as a reverse proxy wouldn't be much efficient and that's why nginx is popular in this scenario. Other alternative than nginx based reverse proxy can be as Khez suggested mapping your subdomain to different IP address which will node.js program listen to or maybe use node.js itself as a reverse proxy for Apache.
You could configure a virtual host in apache for your new site and add a permanent redirect within it to the localhost and port used by node.js.
This is how I do it on a server with several other virtual hosts and my node.js application running on port 3000:
NameVirtualHost *:80
[Other virtual hosts omitted for brevity]
...
ServerName mynewsite.com
RedirectMatch (.*) http://localhost:3000$1