Laravel routes behind reverse proxy generating wrong url - laravel

Let me explain my situation. I have a server that host five sites in www/html folder using the Apache 2 server but all of them run on PHP 5.4. I was asked to upload a system on this server, but it uses PHP 7+. The system is dockerized, so it contains their own container running php 7. So, I was thinking in use a reverse proxy to redirect the requests to docker container. Ok, I managed to do it. My container is running and I can access the system in production. But the system is broken, the static files and the routes are not being well formed.
This image shows the error in console. I just hid the IPs.
https://i.stack.imgur.com/5dvVe.png
This my Apache configuration in sites-available folder. I just added the Location tag to redirect for my container.
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
LimitRequestLine 16382
LimitRequestFieldSize 16382
Timeout 12000
AddDefaultCharset ISO-8859-1
SetEnv no-gzip 1
<Directory /var/www/html>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<Location "/educar">
ProxyPreserveHost On
ProxyPass "http://localhost:8001/educar"
ProxyPassReverse "http://localhost:8001/educar"
</Location>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
This is my first question here, so I just apologized for any mistake.

Related

Not propperly routing in reversed proxy Laravel App

I have configurated an Apache Server which uses reverse proxy to show a web app developed in laravel and mounted in another apache web server. Let's say that the domain is myapp.app
Which works properly, but when I click to a subdomain, let's say myapp.app/register the browser returns myapp.appregister/.
At the moment I've configurated my webserver as it follows:
<IfModule mod_ssl.c>
<VirtualHost *:443>
SSLEngine On
ServerName myapp.app
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location "/">
RequestHeader set X-Forwarded-Proto https
RequestHeader set X-Forwarded-Ssl on
RequestHeader set X-Url-Scheme https
</Location>
ProxyPreserveHost on
ProxyPass / http://172.31.0.234/
ProxyPassReverse / http://172.31.0.234/
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.redes-erdica.tech
SSLCertificateFile /***.pem
SSLCertificateKeyFile /***.pem
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName myapp.app
Redirect permanent / https://myapp.app/
</VirtualHost>
</IfModule>
And the config file for the inner apache server has no other configurations besides the location of the laravel Project. Some curious event that happens is that if I type manually myapp.app/register it sends me where it should be, so why the browser sends me in a wrong direct when I click in the register button?
In addition, the app in localserver works properly.
You need to modify the main configuration file located on the /etc/apache2 folder, you need to change the value of the section AllowOveride that points to the folder on which your aplication lives and change it like this:
<Directory [your_app_folder]
Options ...
AllowOverride All
Require ...
</Directory>

Laravel api on apache2 ubuntu server 404 not found

I have a locally created laravel RESTFUL API and I deployed it on my ec2 ubuntu server using git.
I've set it like my main website.. /var/www/api.myapp.com/public_html/myapp-be and for its config I have this /etc/apache2/sites-available/api.myapp.com.conf.
Almost all blogs and tutorials that I found about deploying laravel on apache2 ubuntu server were almost the same and I've followed them all. However, when I test my API on postman with https://api.myapp.com/api/somefunctionhere it says 404 not found, The requested URL was not found on this server. Apache/2.4.29 (Ubuntu) Server at api.myapp.com port 443.
But when I test my local api on postman, it works fine, thus, I confirm that nothing is wrong with my laravel.
My config looks like this
<VirtualHost *:80>
<Directory /var/www/api.myapp.com/public_html/myapp-be/public/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
ServerAdmin developer#myapp.com
ServerName api.myapp.com
Redirect permanent / https://api.myapp.com/
ServerAlias api.myapp.com
DocumentRoot /var/www/api.myapp.com/public_html/myapp-be/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/error.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =api.myapp.com [OR]
RewriteCond %{SERVER_NAME} =api.myapp.com
RewriteRule ^ https://${SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:443>
<Directory /var/www/api.myapp.com/public_html/myapp-be/public/>
AllowOverride All
</Directory>
ServerName api.myapp.com
DocumentRoot /var/www/api.myapp.com/public_html/myapp-be/public
</VirtualHost>
Is there a spot that I missed here? Obviously I missed a spot but I've been looking for it for almost 3hours already and still wont work.
Any idea guys? Thanks!
UPDATE: I tried adding /public to the prefix and it worked, I don't even know why it worked - https://api.myapp.com/public/api/somefunctionhere

Run Laravel on another URL root path http://example.xom/laravel_proj1 and http://example.xom/laravel_proj2

I want to use Laravel projects at the same time. How you can do it? For example one project at
http://example.xom/laravel_proj1
and another on
http://example.xom/laravel_proj2
Configuration: Laravel 7 / 8 and Apache
Update: This is 2 different Projects. But are there some problems if I use this 2 equals Projects. Upsides of MySQL Datables of course.
File: /opt/bitnami/apache2/conf/vhosts/APPNAME-vhost.conf
<VirtualHost 127.0.0.1:80 _default_:80>
ServerAlias *
DocumentRoot /opt/bitnami/projects/APPNAME/public
<Directory "/opt/bitnami/projects/APPNAME/public">
Options -Indexes +FollowSymLinks -MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Usually you work with subdomains to have multiple laravel projects running at the same time. This means that one instance runs at first.example.com and the other at second.example.com. In my opinion what you are trying is more a workaround if you only have one vhost running.
So I would have two vhost files like you have now. One like:
<VirtualHost 127.0.0.1:80 _default_:80>
ServerAlias first.example.com
DocumentRoot /opt/bitnami/projects/first/public
<Directory "/opt/bitnami/projects/first/public">
...
</Directory>
</VirtualHost>
and
<VirtualHost 127.0.0.1:80 _default_:80>
ServerAlias second.example.com
DocumentRoot /opt/bitnami/projects/second/public
<Directory "/opt/bitnami/projects/second/public">
...
</Directory>
</VirtualHost>
Then you would add both subdomains to your hosts file. I don't know where that one is located but in there you would have to add:
127.0.0.1 first.example.com
127.0.0.1 second.example.com
This is not exactly how I do it now, but I think it could be a good starting point. You also need to activate your vhosts and restart apache.

Getting mod_wsgi(reviewboard) and mod_php(wordpress) working on same servername but different path

I'm trying to get my wordpress site as well as my reviewboard site working under the same domain name.
Ex:
www.mysite.com (this is where i host my wordpress site)
www.mysite.com/reviewboard (this is where I want to host my reviewboard site)
I can get one or the other to work depending on my httpd-vhosts.conf file. However, I cannot get both to work(this is where I need your help!).
This is how I host my wordpress site:
<VirtualHost *:80>
ServerAdmin myemail#gmail.com
DocumentRoot "/opt/local/apache2/htdocs/mysite"
ServerName www.mysite.com
ErrorLog "/opt/local/apache2/logs/mysite.com-error_log"
CustomLog "/opt/local/apache2/logs/mysite.com-access_log" common
</VirtualHost>
This is how I host my reviewboard site(which then breaks my wordpress site):
<VirtualHost *:80>
ServerName www.mysite.com
DocumentRoot "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs"
# Error handlers
ErrorDocument 500 /errordocs/500.html
WSGIPassAuthorization On
WSGIScriptAlias "/reviewboard" "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/reviewboard.wsgi/reviewboard"
<Directory "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs">
AllowOverride All
Options -Indexes +FollowSymLinks
Allow from all
</Directory>
# Alias static media requests to filesystem
Alias /reviewboard/media "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/media"
Alias /reviewboard/static "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/static"
Alias /reviewboard/errordocs "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/errordocs"
Alias /reviewboard/favicon.ico "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/static/rb/images/favicon.png"
</VirtualHost>
So, now I want to be able to figure out how to combine these somehow so I can have reviewboard hosted at the path specified above without breaking my wordpress site. I tried using the Alias command as mentioned here: http://stackoverflow.com/questions/1553165/multiple-django-sites-with-apache-mod-wsgi
and here
https://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines
However I can't get it to work. Here's my WIP. If you can be very specific as what I need to do in order to change this so it works that would be great since I'm new to this kind of stuff. Thanks!
<VirtualHost *:80>
ServerName www.mysite.com
DocumentRoot "/opt/local/apache2/htdocs/mysite"
# Error handlers
#hmm not sure where to put this since my document root is different??????
#ErrorDocument 500 /errordocs/500.html
WSGIPassAuthorization On
Alias /reviewboard/ /opt/local/apache2/htdocs/mysite/reviewboard/htdocs/
<Directory "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs">
Options ExecCGI
SetHandler wsgi-script
AllowOverride All
Options -Indexes +FollowSymLinks
Allow from all
</Directory>
# Alias static media requests to filesystem
# Since I added the alias command above these are complaining about
#overlapping an earlier alias when I restart my apache server????
Alias /reviewboard/media "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/media"
Alias /reviewboard/static "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/static"
Alias /reviewboard/errordocs "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/errordocs"
Alias /reviewboard/favicon.ico "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/static/rb/images/favicon.png"
</VirtualHost>
If you're using the same domain for both, you can't use different VirtualHost sections for both. So first off, you have to merge both configurations into a single VirtualHost section. I'd start with the WordPress config, then add a few bits for ReviewBoard:
<VirtualHost *:80>
ServerAdmin myemail#gmail.com
DocumentRoot "/opt/local/apache2/htdocs/mysite"
ServerName www.mysite.com
ErrorLog "/opt/local/apache2/logs/mysite.com-error_log"
CustomLog "/opt/local/apache2/logs/mysite.com-access_log" common
WSGIPassAuthorization On
WSGIScriptAlias /reviewboard /opt/local/apache2/htdocs/mysite/reviewboard/rb.wsgi
<Directory /opt/local/apache2/htdocs/mysite/reviewboard>
Order deny,allow
Allow from all
</Directory>
Alias /reviewboard/media "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/media"
Alias /reviewboard/static "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/static"
Alias /reviewboard/errordocs "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/errordocs"
Alias /reviewboard/favicon.ico "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/static/rb/images/favicon.png"
</VirtualHost>
You need to create a WSGI script (but you should already have that). You might want to put it in a different path than what I put in the config here, so your permissions are more secure.
Using a combination of what djc wrote and some of my tinkering here's the working version so that:
www.mysite.com (wordpress site loads correctly)(mod_php)
www.mysite.com/reviewboard (reviewboard site loads correctly)(mod_wsgi)
Important make sure you clear your browser cache every time you restart your apache server as I kept falling into the trap of it showing incorrect data since I forgot to do that.
Important2 Make sure the "/reviewboard" follows the "reviewboard.wsgi" to become "reviewboard.wsgi/reviewboard" otherwise it will not work and gives a 404 error!
Here's the working version:
<VirtualHost *:80>
ServerAdmin myemail#gmail.com
ServerName www.mysite.com
DocumentRoot "/opt/local/apache2/htdocs/mysite"
ErrorLog "/opt/local/apache2/logs/mysite-error_log"
CustomLog "/opt/local/apache2/logs/mysite-access_log" common
WSGIPassAuthorization On
WSGIScriptAlias /reviewboard /opt/local/apache2/htdocs/mysite/reviewboard/htdocs/reviewboard.wsgi/reviewboard
<Directory /opt/local/apache2/htdocs/mysite/reviewboard>
Allow from all
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
# Alias static media requests to filesystem
Alias /reviewboard/media "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/media"
Alias /reviewboard/static "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/static"
Alias /reviewboard/errordocs "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/errordocs"
Alias /reviewboard/favicon.ico "/opt/local/apache2/htdocs/mysite/reviewboard/htdocs/static/rb/images/favicon.png"
</VirtualHost>

The 'official' way to deploy multiple Rail 3.1 Apps with Passenger

I'm currently trying to get a couple of small Rails apps running on a server, normally I've used Heroku but I decided to DIY it this time for practise, and everything that is suggested on the Internet doesn't work.
I've tried pretty much all of the resources I can find on both SO and the full Phusion guide, the closest I can come is "The page you were looking for doesn't exist." The app itself runs fine when I run it with script/rails server, initalizing a new rails app to a different sub_dir runs fine, but it'll only let me use index.html, nothing else. So it looks like some kind of routing issue, but when I tried the "scope do" it falls over on "scope".
What's the actual recommended and suggested way to have multiple rails sites on sub uris?
Apache2 Configuration File
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.cybershrike.co.uk/
DocumentRoot /web/rails
<Directory /web/rails>
Allow from all
</Directory>
RailsBaseURI /test
<Directory /web/rails/test>
Options -MultiViews
</Directory>
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.cybershrike.co.uk/
DocumentRoot /web/rails
<Directory /web/rails>
Allow from all
</Directory>
RailsBaseURI /kinu
RailsEnv development
SetEnv RAILS_RELATIVE_URL_ROOT "/kinu"
PassengerAppRoot /web/rails/kinu
<Directory /web/rails/kinu/public>
Options MultiViews Indexes FollowSymLinks
</Directory>
</VirtualHost>
Have you tried the proposed default passenger option?
RailsBaseURI /kinu
RailsEnv development
<Directory /web/rails/kinu/public>
Options -MultiViews
</Directory>
</VirtualHost>
Also you have to ensure, that file-permission allow the apache-server (ususally wwwuser) to access all files recursively including /web ?
And, just to cover all bases here: did you restart apache?

Resources