I have to put nodejs in port 80, but apache is already using it. How can I put both (nodejs and apache) on the same port 80? I need it because in my university all the ports are blocked except for PORT 80. (This is a realtime application with nodejs and socket.io (websockets) and in the other side a php application).
Thanks a lot
I do this via node.js proxy..
Install http-proxy with npm or official page
Example:
var http = require('http'),
httpProxy = require('http-proxy'),
proxyServer = httpProxy.createServer ({
hostnameOnly: true,
router: {
'domain.com': '127.0.0.1:81',
'domain.co.uk': '127.0.0.1:82',
'127.0.0.1': '127.0.0.1:83'
}
});
proxyServer.listen(80);
This creates a node process listening to port 80, and forwarding requests for domains which go to :81,82,83 etc. I recommend running this with forever and adding an entry to init.d so your proxy is up in case system shuts down.
You can also use Apache 2's mod_proxy and mod_proxy_http, which might be more reliable or perform better depending on your system.
Here's an example:
Firstly run below command to proxy to allow
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
# Use Apache for requests to http://example.com/
# but use Node.js for requests to http://example.com/node/
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example/
<Location /node>
ProxyPass http://127.0.0.1:8124/
ProxyPassReverse http://127.0.0.1:8124/
</Location>
</VirtualHost>
And of course you can modify the directives to your needs, such as using a different port for your virtual host (e.g., 443), different port for Node.js, or set up the proxy under a different block, such as for a subdomain (e.g., node.example.com).
I've personally done this the other way round from #liammclennan. Some suggest that proxying through Apache defeats some of the performance and scalability advantages of Node (don't have experience myself as my server doesn't get that much traffic, but from #liammclennan's link: "Every request that comes in through Apache will cause an Apache thread to wait/block until the response is returned from your Node.js process.", which obviously doesn't mesh well with Node's architecture.)
I used node-http-proxy to set up a Node proxy server roughly as described in the first link (my Node proxy runs on port 80; Apache and my other Node services don't). Seems to be working well so far, though I have had occasional stability problems that I've 'solved' through checking the proxy's still running with a cron job (edit: it seems a lot more stable these days). The proxy's pretty lightweight, taking up about 30MB memory.
You can't. You have to run node.js on another port and then proxy requests through apache. You can do this using mod_proxy
http://davybrion.com/blog/2012/01/hosting-a-node-js-site-through-apache/
I usually use haproxy as the front-end in situations like that and have that proxy to the appropriate backend server. (Though making your node.js process a proxy server is a valid approach too depending on your needs).
for httpd.conf
activiate the module , proxy_module and proxy_http
if you are using virtual host
<virtualhost ...>
ServerName api.domain.com
........
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</virtualhost>
assume you are running nodejs server at 8080 , you don't need to take care ssl in nodejs , all should be done in apache
then try https://api.domain.com/
I found a cool gist Run apache and nodejs on port 80. did not try it yet but will do of course
Step 1
Get a VPS that offers 2 or more IP addresses.
Step 2
From the WHM cPanel, find the menu item Service Configuration,
select Apache Configuration and then click on Reserved IPs Editor.
Step 3
Tick the IP address you DON'T WANT Apache to listen to, and write
it down so you can use it in the next step. Click Save.
Step 4
Install Node.js, and create a server like this:
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('Hello, world!');
});
server.listen(80, '111.111.111.111');
Replacing 111.111.111.111 with the IP address you previously
reserved from the WHM cPanel.
Step 5
Stop wasting your time and never listen to those telling you to use
mod_rewrite to proxy Node.js again.
Update:
We can solve a problem in many different ways and IMHO, we should at least know each possible way 😉. We can do it without buying a new IP of course putting a proxy in front of both Apache and NodeJS server each running other ports except 80.
Related
I use a Bitnami MEAN installation (https://bitnami.com/stack/mean) v 3.2.11 on EC2. I didn't do any extra modifications of the apache2 setup except for the rerouting the port where my app runs to port 80, like this
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
Now my app is available fine on :80 port via http:// but I can't make it run via https://. But access via https is what I wanted to achieve. I could not figure the right step by step guide on how to create or enable SSL. All Bitnami docs sound like it's assumed to be working already.
As far as I understood from - https://docs.bitnami.com/aws/components/apache/#https-port the basic certificate is already included into the setup so you can make use of that one without creating your own. The only thing you need is to enable it - That may be wrong assumption, let me know.
Also here on bitnami support it says
Apache waits for HTTPS requests on port 443. Change that by editing
the /opt/bitnami/apache2/conf/bitnami/bitnami.conf file and modifying
the value specified in the Port directive. For example:
Listen 8443
Does that mean that I need to reroute my :5000 port to :8443 ??
I've also found this question - Installing SSL on AWS EC2 Bitnami Mean Stack that didn't recieve a proper answer for more than a year.
Does that mean that I need to reroute my :5000 port to :8443 ??
No, you don't need to redirect that. Your problem is that you're proxy-passing every request from port 80 to 5000 (where your app runs) but when using HTTPS your requests use the port 443. Those request are not being proxy-passed to port 5000 and, therefore, your app doesn't receive them.
What Apache configuration file did you modify including those lines? You need to be sure that both the VirtualHosts for port 80 and the one for port 443 include the ProxyPass redirections. For example, if you edited the file /opt/bitnami/apache2/conf/bitnami/bitnami.conf, ensure you add it in the Virtualhosts <VirtualHost _default_:80> and <VirtualHost _default_:443>
As far as I understood from - https://docs.bitnami.com/aws/components/apache/#https-port the basic certificate is already included into the setup so you can make use of that one without creating your own.
Yes, the Bitnami Stack includes a dummy certificate. However, it's recommended to create your own one since the browsers won't recognize it as a valid one since it's self-signed. You can find more information in the link below:
https://docs.bitnami.com/aws/components/apache/#how-to-create-an-ssl-certificate
I have a server running CentOS 7 that has an Apache web-server running on port 80. I am also using a common open-source Git project called GitLab, which uses the nginx web-server instead of Apache. I have configured GitLab's nginx to run on port 4444.
I have a subdomain "git.mydomain.com" that I would like to forward to "mydomain.com:4444" however I would like the URL to continue saying "git.mydomain.com".
I belived that I need to have an Apache VirtualHost file, however I'm not sure what to do.
Is this possible? If so, how can I do so?
Thanks
You would indeed need a git.mydomain.com VirtualHost with a proxy/reverse proxy directive. See https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
I've got a web app running on localhost:3000. I also have an apache server. I would like to reverse proxy the apache server so that requests to /mywebapp get forwarded to the server running on localhost:3000.
I currently have the following config at the bottom of my httpd.conf file, but I'm getting a server error when I try to access it:
ProxyPass /mywebapp http://localhost:3000
ProxyPassReverse /mywebapp http://localhost:3000
Edit - further details:
I'm running a jetty server with java -jar myapp.jar. I'd like to forward requests to an apache server listening on :80 to the jetty server.
I've got mod_proxy_http.so and mod_proxy.so enabled.
I can tell the server is running on localhost - it responds to curl with the appropriate http response. So I'm pretty sure the issue is with my apache setup, but I can't think what the problem would be.
Apache conf file in conf.d for reference: http://pastebin.com/vhXwjbQe
And I've got this in my httpd.conf:
Include conf.d/*.conf
It's hard to give a generic answer because every situation is different so here are some debugging questions to ask yourself:
if the protocol and port correct on the internal service, http and 3000.
Is the service actually listening for connections from localhost? is it running in a docker container etc that would require it to be listening on a different interface? You can check for this by looking at the output from mywebapp's logs and see if the request are making it through the proxy.
Do the paths on the internal service include the prefix that is being passed to Apache or does apache need to strip these off. if for instance mywebapp expects the path "/foo/bar" and apache's reverse proxy is sending it with the context path included "/mywebapp/foo/bar" then it will not match any path in mywebapp.
Couldn't find anywhere the answer to question that bothers me for a few days:
According to Apache documentation in virtual host files I should write this:
ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/path/to/socket.sock|fcgi://127.0.0.1:9000/path/to/your/documentroot/
But Do I have to use another fcgi port for other sites? e.g.
...socket2.sock|fcgi://127.0.0.1:9001...
...socket3.sock|fcgi://127.0.0.1:9002...
Or
...socket2.sock|fcgi://127.0.0.1:9000...
...socket3.sock|fcgi://127.0.0.1:9000...
It works both ways – no errors in logs or on screen. I know that if I would use TCP not UNIX sockets I should use different port for each site (and also it would throw an error if I use the same port) but I'm not sure I understand this Apache syntax.
It all depends how you configure your workers pools for php-fpm.
On centos 7 for example, you will find a www.conf in /etc/php-fpm.d/
This is generally the configuration for port 9000.
You can use the same port for all your virtualhost without any issue.
But if by design you want to use a different port for different virtualhosts, you can just duplicate the www.conf, rename it and edit the file.
Make sure you change the name of the pool at the top. You will find the default name to be [www]. You can edit it to [your_preferred_pool_name]
Then in the listen option you can change the port to what you want.
After restarting php-fpm you will have your [www] pool and your [your_preferred_pool_name] pool.
To configure a virtualhost to use the new pool, just update the ProxyPassMatch configuration by updating the port to your newly defined port in the conf file.
But again, using the same pool (port) for all your virtualhosts should work without any issue.
In a server hosting a website address.com (managed with drupal) with an apache2 server (running on port 8080) I would like to install a webservice (tomcat7 / axis2) which runs on the same port 8080. Is there a way do it? There're also svn, trac running on that port. Unfortunately, due to security restrictions, that's the only port accessible externally.
Thank you
You can absolutely expose multiple services on the same port, as long as they all live in distinct URL namespaces. For example, you're already running Trac and svn on port 8080, so obviously you are already doing exactly what you're asking about.
To add Tomcat to the mix, you would typically:
Run Tomcat locally on another port, and then
Use ProxyPass and ProxyPassReverse to expose the Tomcat service via your webserver on port 8080.
For example, if you wanted to make your Tomcat instance visible at http://myserver:8080/tomcat, you might add something like this to your Apache configuration:
ProxyPass /tomcat/ http://localhost:8888/
ProxyPassReverse /tomcat/ http://localhost:8888/
You can read more about these directives here. Note that you may need to perform additional configuration of your Tomcat application to reflect the fact that it is externally visible at /tomcat/.
You can also potentially take advantage of virtual hosting, assuming that you control DNS for this system; in that case, you can have:
http://myserver-trac:8080/
Lead to a different VirtualHost configuration than:
http://myserver-tomcat:8080/
You can read more about name-based virtual hosting here.
When you install the webservice listening on another port (at localhost), you can use Apache as a proxy (using mod_proxy) to access that service.
Maybe usefull: How to rewrite / proxy an Apache URI to an application listening on a specific port / server?