Croogo - CakePHP site on new VPS not loading js and css - apache

I've recently purchased a VPS on inmotionhosting.com, and having difficulty deploying my Croogo 2.3.2 when loading any js, css, img files under my custom theme /View/Themed/Mosaic/webroot and under my Croogo plugins /Vendor/croogo/croogo/Croogo/webroot/.
Of course, I've included the said resources using the proper CakePHP convention in my /View/Themed/Mosaic/Layouts/default.ctp:
echo $this->Html->css(array(
'bootstrap',
'bootstrap-theme',
'/croogo/css/reset',
'font-awesome.min',
'../slick/slick',
'../slick/slick-theme',
'/jquery-ui/jquery-ui.min',
'slicknav.min',
'styles'
));
echo $this->Layout->js();
echo $this->Html->script(array(
'/croogo/js/html5',
'/croogo/js/jquery/jquery-3.2.0.min',
'/jquery-ui/jquery-ui.min',
'/croogo/js/jquery/jquery.slug',
'/croogo/js/jquery/jquery.cookie',
'/croogo/js/jquery/jquery.hoverIntent.minified',
'/croogo/js/jquery/superfish',
'/croogo/js/jquery/supersubs',
'/croogo/js/jquery/jquery.tipsy',
'/croogo/js/jquery/jquery.elastic-1.6.1.js',
'/croogo/js/jquery/thickbox-compressed',
'/croogo/js/underscore-min',
'bootstrap.min',
'../slick/slick.min',
'jquery.slicknav.min',
'theme.js',
));
echo $this->fetch('script');
echo $this->fetch('css');
Croogo 2.3.2 has itself and cakephp run as a Vendor:
/Vendor/croogo
/Vendor/cakephp
Therefore, I have two .htaccess files.
One under /home/mosaic40/v2.mosaicmarble.com/:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
One under /home/mosaic40/v2.mosaicmarble.com/webroot/:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
mod_rewrite is installed and activated.
Last but not least, before anyone starts suggesting about my httpd config, here it is:
DocumentRoot /home/mosaic40/v2.mosaicmarble.com/
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
I can't seem to put my finger on it. in short, the plugins' and theme's resources (js/css/img) are not being loaded for some reason.
UPDATE 1:
It appears /webroot assets are not being loaded as well (example: http://v2.mosaicmarble.com/uploads/FG204-G.jpg).
The said link is http://v2.mosaicmarble.com/ (to see what's being rendered).
Croogo v2.3.2
CakePHP v2.9.3
Linux Server with NGINX installed
mod_rewrite active (click on any link, you'll see pretty urls)

When you are fetching assets for plugins via the dispatcher. You have to use a plugin namespace as the URL.
echo $this->Html->script('Croogo.html5');
See the manual on linking to plugin assets:
https://book.cakephp.org/3.0/en/plugins.html#linking-to-assets-in-plugins

The evident solution in the end was to disable NGINX... which is weird, because I own several other servers like Media Temple's VPS with NGINX installed and all my websites run flawlessly.
So, yea... disabling NGINX did the job.
Thank you #cgTag for your prompt response, it does help others in the future who may face a similar issue.

Related

How to setup apache server for React route?

I have my react app running great on my local dev server but it did not work when I dump my production ready files straight into Apache's htdocs directory:
Here is what I have:
/var/www/index.html
/var/www/bundle.js
and I have
DocumentRoot /var/www
in /etc/apache2/sites-available/000-default.conf
The fact is that
1). when I access http://...com/ that routed me to Login page
2). After I clicked a link
<Link to="main"><button>Log In</button></Link>
the content in the browser location field become:
http://...com/main
3). Now if I reload this url (http://...com/main), I got
The requested URL /main was not found on this server
My rounting in React:
<Router history={browserHistory }>
<Route path="/" component={TopContainer}>
<IndexRoute component={Login} />
<Route path='main' component={MainContainer} />
</Route>
</Router>
What else I am missing in the apache configuration?
thanks
Change the VirtualHost configuration (typically found in /etc/httpd/conf.d\vhosts.conf) by adding the following Rewrite* lines:
<VirtualHost *:8080>
ServerName example.com
DocumentRoot /var/www/httpd/example.com
<Directory "/var/www/httpd/example.com">
...
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>
</VirtualHost>
This tells Apache to serve any files that exist, but if they don't exist, just serve /index.html rather than a 404: not found.
Apache Reference: Configuring Apache Virtual Hosts
react-router History Reference: Configuring Your Server
Complete answer gratefully stolen from here
Edit: 'On' need to be uppercase in current apache version
The above solution works for Ubuntu as well but I have struggled a bit with it so here are the steps necessary to make it work.
Location of the file where you need to place the above mentioned configuration is under
/etc/apache2/sites-enabled
default is
/etc/apache2/sites-enabled/000-default.conf
Then you need to make sure that RewriteEngine is running (otherwise you will get an error when restarting Apache server).
sudo a2enmod rewrite
Finally, restart Apache server
sudo /etc/init.d/apache2 restart
Now, it should work.
When you are using default configuration (root of the website is under /var/www/html), then all you need to do is to place
<Directory "/var/www/html">
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>
to the above mentioned file under <VirtualHost ...>
If you have to use .htaccess and a sub directory then following works for me.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
What worked for me, echoing many of the answers and comments here:
sudo a2enmod rewrite
Open up /etc/apache2/apache2.conf
Paste in this with the path to your root:
<Directory "/var/www/PATH_TO_YOUR_ROOT">
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>
sudo service apache2 restart
Pasting into the site-specific conf file did not work as earlier answers suggested.
None of the solutions posted so far appear to address the issue where missing ressources incorrectly return 200 instead of 404, which can make debugging when certain files are missing rather annoying.
My solution is to instead watch what type of resource the request expects to recieve, since browsers will ask for HTML when navigating to a page (Firefox asks for text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8) but not when accessing resources after the initial load (JS files imported via <script> or as ES6 modules ask for */*, CSS files ask for text/css,*/*;q=0.1, accessing JSON via the fetch() API will specify application/json, text/plain, */* and so on). By relying on that assumption, one can configure Apache to serve the Single page app when trying to access a non-existent file (such as a route that only works within the Single-page app) without also sending it whenever said SPA asks for a CSS file that has been renamed or a missing JSON file.
EDIT: MDN has a list of common values for the Accept header.
<Directory "/var/www/httpd/example.com">
RewriteEngine on
# Browsers will specifically ask for HTML (among other things) on initial page load
# That is, if the *user* tries to access a *nonexisting* URL, the app is loaded instead
# but if a webpage attempts to load a missing resource it will return 404.
# (You can still go to /myreactapp/favicon.ico, but a missing /myreactapp/favicon.png resource won't return 200)
# if (HTTP_ACCESS.contains('text/html') && file_not_exists(REQUEST_FILENAME))
RewriteCond %{HTTP_ACCEPT} text/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [last]
# Any ressources loaded by index.html should behave correctly (i.e: Return 404 if missing)
RewriteRule ^ - [last]
AllowOverride None
Options FollowSymLinks Multiviews
Require all granted
</Directory>
Thank you! This worked for me.
I am pasting my config if you are serving multiple sites (virtualhost) and also SSL certificates (SSL was made with certbot), with redirect http to https
This setting works on Linode / Ubuntu
yoursite.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin webmaster#yoursite.com
ServerName yoursite.com
ServerAlias www.yoursite.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/html/yoursite.com/public_html
<Directory "/var/www/html/yoursite.com/public_html">
RewriteEngine on
# Browsers will specifically ask for HTML (among other things) on initial page load
# That is, if the *user* tries to access a *nonexisting* URL, the app is loaded instead
# but if a webpage attempts to load a missing resource it will return 404.
# (You can still go to /myreactapp/favicon.ico, but a missing /myreactapp/favicon.png resource won't return 200)
# if (HTTP_ACCESS.contains('text/html') && file_not_exists(REQUEST_FILENAME))
RewriteCond %{HTTP_ACCEPT} text/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [last]
# Any ressources loaded by index.html should behave correctly (i.e: Return 404 if missing)
RewriteRule ^ - [last]
AllowOverride None
Options FollowSymLinks Multiviews
Require all granted
</Directory>
# Log file locations
LogLevel warn
ErrorLog /var/www/html/yoursite.com/log/error.log
CustomLog /var/www/html/yoursite.com/log/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/yoursite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yoursite.com/privkey.pem
</VirtualHost>
</IfModule>
This is what we use at work for our production react app which is using BrowserRouter from react-router:
httpd.conf
<VirtualHost *:3000>
DocumentRoot "/var/www/html"
<Directory /var/www/html/>
Header set Cache-Control "no-cache"
# https://stackoverflow.com/a/34154531/2089675
FallbackResource /index.html
</Directory>
<Directory /var/www/html/static/>
# https://create-react-app.dev/docs/production-build/#static-file-caching
Header set Cache-Control "public, max-age=31536000"
# https://stackoverflow.com/a/54943214/5600537
RequestHeader edit "If-None-Match" '^"((.*)-gzip)"$' '"$1", "$2"'
</Directory>
</VirtualHost>
As you can see most of the comments in there are answers from SO, so I'm just giving back :)
configuration
Place the above file in /usr/local/apache2/conf/httpd.conf.
The config also assumes that you have put the contents of the build folder inside /var/www/html/. If you've placed them elsewhere, then adjust the path accordingly.
ports
The VirtualHost *:3000 part is just for exposing the server's port in the docker container (httpd:buster) used to run it. This is also the same port CRA defaults to in dev. An external proxy is used to manage where the application can be accessed from.
compression
Finally, if you are interested in serving gzipped files you may want to remove the RequestHeader edit line, and then do some more work to make sure .gz files can be served:
ex.
AddOutputFilterByType DEFLATE text/html application/javascript
React routing issue fixed on ubantu server
Solution:
Open the file using the console.
If you are using SSL
nano /etc/apache2/sites-available/000-default-le-ssl.conf
Add the following lines
===================================================================================
DocumentRoot /var/www/project
<Directory "/var/www/project">
RewriteEngine on
RewriteCond %{HTTP_ACCEPT} text/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [last]
RewriteRule ^ - [last]
AllowOverride None
Options FollowSymLinks Multiviews
Require all granted
Solution:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
If you've multiple virtual host then follow these steps
Goto to that VH and open the .htaccess file
add these lines and save it
restart the apache service again so that it can reflect into the settings
Go on this directory
/etc/apache2/sites-available
open File : 000-default.conf
Change its permission : 777
Paste code on bottom of file
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]
Restart server

mod_rewrite all to index.php in one environment

I know there are a million of questions about this, but I've tried the solutions in other questions and haven't got it to work in my case. I'm trying to redirect everything to index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This works in my local environment, but in my staging environment it fails and I get a 404 error. I've checked that mod_rewrite is enabled with phpinfo(). I've tried changing a few things like using ^(.*)$ instead of ^ and using /index.php instead of index.php
What else could be the problem?
Figured it out. It was a problem with the configuration of apache. Whoever set it up for the site I'm working on did it like this in the site's .conf file:
<Directory "/data/path/to/directory">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
# and a bunch of rewrite rules...
</Directory>
The AllowOverride None prohibits the use of .htaccess. In our local and development environments, the directory structure is different, so this code doesn't even get applied. That's why the .htaccess was working.
To solve the problem I'll either have to change the line mentioned above to AllowOverride All or add another configuration for the website I'm working on like the one above that does what I wanted to do with .htaccess.
Problems like this shouldn't come up for anyone if their environments are configured appropriately, but if anyone does run into a similar situation, check the configuration of the site in apache!

Codeigniter application index.php removed randomly in url on amazon EC2 clound server

I am working on a web application. I am facing a very strange issue while navigating the app on cloud amazon EC2 instance 1 that it randomly choose some links and when I click on those links it is redirecting me to page not found or sometime home page or sometime on login page. Although session still persist.
Previously I have added the .htaccess file to removed the index.php from the urls but after noticing this issue I have revert the changes but my re_write_mode still enabled in apache httpd.conf file and .htaccess file reside on root of the application with commented code.
Apache configuration mentioned below.
DocumentRoot "/var/www/html"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Not sure why you would be running in that problem from what you posted...here is the .htaccess file I use at the root of all of my CI projects to remove index.php from the URL
RewriteEngine On
# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/site_folder/, use /site_folder/
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
Nothing looks extremely out of the ordinary regarding your apache config. I would say that it sounds more like a problem with your "routes." Do you have any custom routes setup in your app?
Also, did you change your $config['index_page'] and/or $config['base_url'] in your config file?

Mod_ReWrite doesn't work on cakephp

I have recently deployed CakePHP however mod_Rewrite is not currently working:
I have the following in my apache2.conf
<Directory "/path/to/the/app">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
i have run
sudo a2enmod rewrite
which stated the module is already enabled and i have also checked the .htaccess file which has
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
has anyone got any other ideas as to why mod_rewrite isn't working?
Please note i have restarted apache with no sucess
There must be 3 .htaccess files in these locations
/.htaccess
/app/.htaccess
/app/webroot/.htaccess
Do you have them?
Turns out mod_rewrite was working, however I was using cakephp 1.3 which doesn't detect if the mod_rewrite works but applys a CSS style which wasn't applying to the div. it works now anyways

Making pretty permalinks work in WAMP

I am not able to switch to pretty permalinks in WAMP. Changing to any form other than default gives 404 error.
I have switched on the rewrite_module in Apache. I Googled the problem and found that following changes should be made to httpd.conf file. My httpd.conf file stands as
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
I also checked that the .htaccess file is getting created. It reads as
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /vit%20web/events/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /vit%20web/events/index.php [L]
</IfModule>
# END WordPress
All you need to do is turn on the mod_rewrite in the wamp settings tab.
Click Wamp -> Apache -> Apache Modules -> rewrite_module
If you select that then turn on %postname% it should work
Did you reboot Apache after editing httpd.conf?
A bulletproof check for mod_rewrite is to remove the <IfModule>...</IfModule> tags and try running WordPress - if you get a 500 Server Error, mod_rewrite isn't installed.
Also I would recommend changing <Directory /> to <Directory "C:/path/to/server/root"> (note the forward slashes too, even on Windows).
And the deny order should be switched if you're only on a development server;
Order allow,deny
Allow from all
The problem may be:
Whenever you have permalinks in subfolder of your main www folder (i.e.
RewriteBase /subdirectory/
you may need to have a .htaccess file in your main c:\wamp\www folder too.