Express app + WordPress as a subdirectory (Apache) - node.js

I have a:
Domain: example.com
Express application running on port 123
WordPress installation in /var/www/wordpress
I would like to run the express application under the domain example.com and the WordPress site as a subdirectory of the domain (example.com/wordpress).
My Apache 2.4 VirtualHost looks like:
<VirtualHost *:80>
ServerName example.com
ProxyPreserveHost on
ProxyPass "/" "http://localhost:123/"
ProxyPassReverse "/" "http://localhost:123/"
RewriteRule "^/wordpress$" "/var/www/wordpress"
<Directory /var/www/wordpress>
Options +FollowSymLinks +Multiviews +Indexes
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
The express application (example.com) and all of its routes can be accessed fine, but the WordPress application (www.example.com/wordpress) returns an express 404 (not an Apache 404).
Is this an issue in my VirtualHost, an additional express route I have to define, neither, both, something else?
Thank you for your help!

Related

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

Handle acme-challenge Letsencrypt requests

I have a NodeJs app running behind an Apache configuration using ProxyPass. The HTTPS is setup using Letsencrypt.
As you probably know, to validate a Letsencrypt certificat, we have to handle a request like the one bellow, sent by Letsencrypt server.
http://sub.afakedomain.com/.well-known/acme-challenge/some-random-stringhere
At the moment, the request results into a 404 Not Found because the ProxyPass redirect the request directly to my NodeJs app which didn't handle this request.
A solution would be to define a route in my NodeJs app to handle the request
Another solution would be to detect the request in Apache and instead of routing the request to the NodeJs app, route it directly to the folder containing the .well-known directory.
I would like to use the Apache solution, but I'm not able to find the right way to do it.
Path to well-known directory
/var/www/html/.well-known/
My vhost setting
<VirtualHost *:80>
DocumentRoot /var/www/html/fail
ServerName sub.afakedomain.com
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]
</VirtualHost>
<VirtualHost *:443>
ProxyPreserveHost On
ProxyRequests Off
ServerName sub.afakedomain.com
Proxypass / http://localhost:5555/
ProxyPassReverse / http://localhost:5555/
SSLEngine On
SSLCertificateFile /etc/letsencrypt/live/afakedomain.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/afakedomain.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/afakedomain.com/chain.pem
SSLCACertificateFile /etc/letsencrypt/live/afakedomain.com/fullchain.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
If you have some suggestions, feel free! Thanks!
If you want to exclude the .well-known directory from being proxied, you just need to add an exclusion. Add the following before the existing ProxyPass lines:
ProxyPass /.well-known/ !
And you should be all set. See the ProxyPass documentation for more info.

Apache and Node on same IP

I have a single VPS with one IP. I'm using Apache to serve cloud.mysite.com and I have a NodeJS application listening on port 3000.
I'm trying to configure my VPS in a way so that when I visit mysite.com, I get my NodeJS application.
Instead, when I visit mysite.com, I'm forwarded to cloud.mysite.com.
My .conf files are below.
mysite.com.conf
<VirtualHost mysite.com:80>
ServerName mysite.com
ProxyPreserveHost On
ProxyRequests off
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
cloud.mysite.com
<VirtualHost cloud.mysite.com:80>
ServerName cloud.mysite.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =cloud.mysite.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
<VirtualHost cloud.mysite.com:443>
# Basics
ServerName www.cloud.mysite.com
ServerAlias www.cloud.mysite.com
# Next line puts ownCloud at the domain root instead of a /owncloud/ subdirectory (e.g. example.com vs. example.com/owncloud/)
Alias /owncloud "/var/www/owncloud/"
DocumentRoot /var/www/owncloud
# SSTL STUFF GOES HERE
# ownCloud
<Directory /var/www/owncloud/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dav.c>
Dav off
</IfModule>
SetEnv HOME /var/www/owncloud
SetEnv HTTP_HOME /var/www/owncloud
</Directory>
</VirtualHost>
SSLStaplingCache shmcb:/var/run/ocsp(128000)
Edit: I resolved this by clearing my cache.
It seems that it should proxy requests to your Node app if it's running on and listening on port 3000 on the same host, or fail if it isn't - but not to proxy requests to the other virtual host.
Make sure that you restarted Apache or made it reload the configuration after you made those changes and that your new config file is where it should be and is enabled.

Configuring symfony and apache

I'm learning symfony right now. I want to use apache webserver instead of the shipped php. How can I add a virtualhost if I want to use also the default localhost? (localhost:80 for my notebook and I have a "personalhomepage")
Here is one of the vHosts I am currently using (cronolog is used for log rotation):
<VirtualHost *:80>
ServerName projectname.dev
DocumentRoot /var/www/projectname/html/web
<Directory /var/www/projectname/html/web>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog "|/usr/bin/cronolog /var/www/projectname/log/%Y-%m/error_%d_%m_%Y.log"
CustomLog "|/usr/bin/cronolog /var/www/projectname/log/%Y-%m/access_%d_%m_%Y.log" combined
</VirtualHost>
For testing and development purposes I usually create a local domain with the .dev or .local ending in the hosts file, e.g.:
127.0.0.1 projectname.dev
Make sure the domain name corresponds with the one in the vHost config. Oh, and don't dont forget to restart apache. Hope this helps.
Using these steps, you can use either projectname.dev, projectname.dev/app_dev.php or you set the vHost's DirectoryIndex directly, if you want to always use app_dev.php:
<Directory /var/www/projectname/html/web>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex app_dev.php
</Directory>
You would setup Apache in the same way as any other webservers that have more than one site on them - ideally, a named vhost. Symfony has an example Apache vhost config,
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
ServerAlias www.sitename.127.1.0.1.xip.name
DocumentRoot /var/www/project/web
<Directory /var/www/project/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
# In local development, I default this to app_dev.php
</IfModule>
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
In this example, domain.tld (or www.) is the name you would use to reach the site. You would have to be able to reach those names via DNS though - on your local machine, 127.0.0.1. There are also some 'wildcard DNS' services that may help, such as http://xip.name/ With services such as these, you could put ServerName sitename.127.1.0.1.xip.name into the above configuration and then use that to reach your site.
I do something similar myself with a wildcard DNS sub-domain I own for a local machine. I have a number of such Apache Vhost configurations setup.

Setting IP in Apache vhost as testing domain

I was wondering if I can set the server external IP as a domain for testing.
On my Amazon server setup it works because all the domains point to the same folder.
domain1.com - /var/www/domain1
domain2.com - /var/www/domain2
ex.ter.nal.ip - /var/www/
So I can use ex.ter.nal.ip/test for a testing website.
On my Linode setup I moved the folders inside the user directories
domain1.com - /home/user1/public/domain1
domain2.com - /home/user2/public/domain2
ex.ter.nal.ip goes to domain2.com (I think it points to the last enabled site in Apache -a2ensite)
Is there a way to make it work?
I tried adding a vhost with with the /home/test DocumentRoot, but then all the websites point to the test directory.
My domain vhost file looks like this:
<VirtualHost *:80>
ServerName www.domain1.com
ServerAlias domain1.com
DirectoryIndex index.html index.php
DocumentRoot /home/user1/public/domain1.com/public
<Directory /home/user1/public/domain1.com/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
My testing one looks lithe this, but when enabled all the domains point to the test folder
<VirtualHost ex.ter.nal.ip>
ServerName ex.ter.nal.ip
DirectoryIndex index.html index.php
DocumentRoot /home/test/public
<Directory /home/test/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>

Resources