How configure multiples process on 443 port? Apache - ubuntu - node.js

here its my problem:
I have 2 applications configured on a server. React (client) and nodejs backend with their respective domains.
1 - example1.com
2 - example2.com
I have configured both SSL certificates for each of it as well. The problem occurs when you want to start the backend on the same port that the client is running.
Is it possible to run 2 processes on it? How should I do it?
these are my virtual host files:
example1-le.ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName example1.com
ServerAlias www.example1.com
ServerAdmin info#xample.com
DocumentRoot /var/www/example1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/example1.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example2.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
example1.conf
ServerName example1.com
ServerAlias example1.com
ServerAdmin info#example.com.ar
DocumentRoot /var/www/example/build
<Directory "/var/www/example/build">
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteCond %{SERVER_NAME} =example1.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
And the same for the another one.
When i tried to start the second project i receive : EADDRINUSE: address already in use :::443
Thanks a lot for you help

You can NOT have to 2 different processes bind on the same port. What Olaf Kock suggested works fine and is easy to implement: you install apache on a dedicated server (or on the same machine: there's no difference) and then you configure a reverse proxy (the module is called mod_proxy). There are also other solutions (like haproxy) which require a bit more complex configuration but provide many more configuration options.

Related

Getting 404 on a specific path /pricing with Apache server [migrated]

This question was migrated from Stack Overflow because it can be answered on Server Fault.
Migrated 2 days ago.
I've set up an Apache server on Ubuntu 20.04
The site loads fine when I load the home page first (https://leadzilla.ai) and after that when I click on the pricing button and it takes me to https://leadzilla.ai/pricing and the that page loads fine as well.
But when I go directly to https://leadzilla.ai/pricing in the browser, I get a 404
Here is what I have in /etc/apache2/sites-available/leadzilla.ai.conf
<VirtualHost *:80>
DocumentRoot /var/www/leadzilla.ai
ServerName leadzilla.ai
ServerAlias www.leadzilla.ai
<Directory /var/www/leadzilla.ai>
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =leadzilla.ai [OR]
RewriteCond %{SERVER_NAME} =www.leadzilla.ai
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I have this config in /etc/apache2/sites-available/leadzilla.ai-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /var/www/leadzilla.ai
ServerName leadzilla.ai
ServerAlias www.leadzilla.ai
<Directory /var/www/leadzilla.ai>
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
#Deny from all
#Allow from 127.0.0.1
#Allow from ::1
</Directory>
<Directory /var/www/leadzilla.ai/blog>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/leadzilla.ai/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/leadzilla.ai/privkey.pem
</VirtualHost>
</IfModule>
I have Wordpress on https://leadzilla.ai/blog so that has to be taken care of as well.
Here is what I have tried:
RewriteRule ^pricing$ pricing.html [NC]
I added it before the other rewrite rule but that doesn't seem to be working. Any ideas?
[EDIT]
This is solved now. The issue was a Next.js config, not an Apache config.
I put in exportTrailingSlash: true in my module.exports and it worked
I find this to be curious behavior. But if you have a RewriteRule in the <VirtualHost *:443>, then you should also have RewriteEngine On.
Are there any symbolic links in your directory at all. Anything like foo -> foo.html?
Are there any directories like /var/www/html/pricing/ in your directory structure?
Also, remember, that all of your traffic ends up on HTTPS, which means that only the <VirtualHost *:443> is in play. The other virtual host entry only is used long enough to redirect from HTTP to HTTPS. Any rewrite rules for the :80 VirtualHost do not apply on HTTPS.

Proxyreverse in Apache2 with wordpress

I have installed and configured Wordpress on my server using also apach2 virtualhosts.
I made a virtualhost with this config
<VirtualHost *:80 *:443>
ServerAdmin yourluxuryroad#gmail.com
ServerName yourluxuryroad.com
ServerAlias www.yourluxuryroad.com
DocumentRoot /var/www/yourluxuryroad
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.yourluxuryroad.com [OR]
RewriteCond %{SERVER_NAME} =yourluxuryroad.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
ProxyPreserveHost On
ProxyRequests Off
ProxyPass /node-yrl-book http://localhost:5000
ProxyPassReverse /node-yrl-book http://localhost:5000
</VirtualHost>
<Directory /var/www/yourluxuryroad/>
AllowOverride All
</Directory>
As you can see from the config i'm trying to set the ProxyPass directive for redirect the requests recived on the path /node-yrl-book to a nodejs service ( made using expressjs ) at port 5000 but this is not working, instead of getting a redirect to that service i get the 404 Page not found wordpress page.
If I make a request at my_ip/node-yrl-book instead it works correctly and i am redirected to the service at port :5000
I suppose that i'm missing something in my configuration but i'm not understanding what..
Maybe is something in wordpress that has to be changed?
You have way too much going on.
ProxyPass -or- DocumentRoot, not both.
You can either serve the page from apache (by using DocumentRoot), or you can serve the page from nodejs (by using ProxyPass).
Finally i solved this, I made an SSL certificate for my website using let's encrypt certbot, This script created a new virtualhost in another file for the https requests ( called /etc/apache2/sites-available/myDomain-le-ssl.conf ) That virtualhost was overriding my proxypass directive, editing also this virtualhost made all work

Running two Flask apps on two different domains using apache2 and WSGI

I have two python (3.6) Flask apps running on Ubuntu 18.04 and I am trying to use Apache2 (v2.4.29) to serve these two apps to two different domains – app1domain.com and app2domain.com. I have two .conf files that I have been trying to modify to get this to work. They currently look like this (replace app1 with app2 for the second one):
WSGIDaemonProcess app1 python-home=/var/www/app1/venv user=brett group=sudo home=/ threads=5
WSGIScriptAlias / /var/www/app1/app.wsgi
WSGIProcessGroup app1
WSGIApplicationGroup %{GLOBAL}
<VirtualHost *:80>
ServerAdmin myemail#outlook.com
ServerName app1domain.com
ServerAlias www.app1domain.com
<Directory /var/www/app1>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =app1domain.com [OR]
RewriteCond %{SERVER_NAME} =www.app1domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
ServerName app1domain.com
ServerAlias www.app1domain.com
ServerAdmin myemail#outlook.com
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/app1domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/app1domain.com/privkey.pem
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Here is where I get stuck, and here is what I have tried:
If both conf files are in the format shown above, app1 will be served to both https://app1domain.com and https://app2domain.com, and app2 will not be shown.
If I disable the conf for app1, app2 is served to both https://app1domain.com and https://app2domain.com which suggests that, at a minimum, both conf files 'work' and the apps are working correctly.
From my investigations, I see a lot of conf files have the WSGI instructions inside the <VirtualHost> tags. If I do this for both confs, the default Apache2 page is displayed on both domains.
I have tried just about every combination of the WSGI instructions inside and outside the <VirtualHost> tag and also the nested <Directory> tag. Most of the just resulting in the default apache2 page.
Am I missing some other option that I need to change? What am I doing wrong here?
I have also been looking for some good documentation on how to interpret these conf files, what the options actually do, so would love if someone could point me to something, particularly if it covers WSGI.
Seems so obvious in hind sight. Turns out the issue was a result of me blindly trusting certbot to autogenerate the new VirtualHost and redirect, and me not really thinking about how this was working.
If you look at the example .conf file in the question, the reason it works is because the WSGI instructions are created outside the scope of the <VirtualHost> tags, which allows them to get picked up by both VirtualHosts. But at the same time, because they are created globally, the WSGI instructions in the .conf file that comes first alphabetically override the others, hence app1 shows up on app1domain.com and app2domain.com.
When I moved the WSGI instructions inside the <VirtualHost> tags, I was moving it inside the <VirtualHost *:80> tags, because all the examples I found were doing that (because they weren't using SSL). When I did that, instead of running the app, the RewriteEngine was redirecting the request to the https version of the website. That gets picked up by <VirtualHost *:443> where you will notice I had no instructions on how to run the app, so we get a default page.
In the end I rewrote my .conf files as follows:
<VirtualHost *:80>
ServerAdmin myemail#outlook.com
ServerName app1domain.com
ServerAlias www.app1domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =app1domain.com [OR]
RewriteCond %{SERVER_NAME} =www.app1domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
ServerName app1domain.com
ServerAlias www.app1domain.com
ServerAdmin myemail#outlook.com
WSGIDaemonProcess app1 python-home=/var/www/app1/venv user=brett group=sudo home=/ threads=5
WSGIScriptAlias / /var/www/app1/app.wsgi
<Directory /var/www/app1>
WSGIProcessGroup app1
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
LogLevel debug
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/app1domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/app1domain.com/privkey.pem
</VirtualHost>

You configured HTTPS(443) on the standard HTTP(80) port

In my apache error logs, I have bunch of ssl warnings saving You configured HTTPS(443) on the standard HTTP(80) port!
Here is my site.ca.conf file
<VirtualHost *:80>
ServerName site.ca:80
DocumentRoot "/var/www/site/public"
<Directory "/var/www/site/public">
AllowOverride all
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =site.ca
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
and here is my site.ca-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName site.ca:80
DocumentRoot "/var/www/site/public"
<Directory "/var/www/site/public">
AllowOverride all
</Directory>
ServerAlias site.ca
SSLCertificateFile /etc/letsencrypt/live/site.ca/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/site.ca/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Every thing works fine. I am not sure why this warning shows up every day in my apache log files, and how can I resolve it?
Generally, this is because you do not have any SSL configuration on the virtual host on port 443. You may need to enable "SSLEngine on" and provide certificate information. The warning indicates are serving regular HTTP traffic on what is usually an HTTPS port.
In these config files listed by the requester, there are some tweaks/corrections to be done:
we don't need to have a DocumentRoot in the site.ca.conf because we will redirect HTTP to the HTTPS (site.ca-le-ssl.conf)
the ServerName directive shouldn't have a port number, instead it should be in the VirtualHost level
the ServerName and ServerAlias should be near to each other and there should be www.site.ca as an Alias too in both files to handle the requests containing the www
in site.ca-le-ssl.conf file there is a ServerName site.ca:80 and that's not correct (there should be no port number)
there must be a SSLEngine on in the site.ca-le-ssl.conf
I hope that help someone even this is an old question

High Load on server due to websocket and Apache

We have 2 servers (Ubuntu 14.04 - 4vCPUs and 12 GB RAM), running a codeigniter application with Apache 2.4.7. These servers are load balanced as well. We have an average of 300 users accessing the site at a time and also the website has refresh functionality in many areas. So we introduced websockets to reduce the load. But even after introducing that, we are facing high load. Node is running on one of these server. After enabling mod_status I can see new connections are not getting opened for apache.
Vhost
<VirtualHost *:443>
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/html/domain
SSLEngine on
SSLProxyEngine On
SSLCertificateFile /etc/apache2/ssl/domain-ssl/a.crt
SSLCertificateKeyFile /etc/apache2/ssl/domain-ssl/domain.key
SSLCertificateChainFile /etc/apache2/ssl/domain-ssl/gd_bundle.crt
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/node/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule "^/node/socket.io" "ws://IPADDRESS:8080/socket.io/" [P,L]
ProxyPreserveHost On
ProxyRequests off
</VirtualHost>
<Location /node/>
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
ProxyPass http://IPADDRESS:8080/ connectiontimeout=3 timeout=3 retry=0
ProxyPassReverse http://IPADDRESS:8080/
</Location>
Please let me know the details you need to investigate on this. Thanks
I installed a seperate server for node and redis which has reduced the load.

Resources