Running a CakePHP project on Localhost - cakephp

I have downloaded a CakePHP project from an online server and need to make it work locally, to do some changes.
I'm facing a problem similar to this one and have tried all possible solutions, with no luck.
Here's the situations:
Site home is in localhost/xpto (c:\xampp\htdocs\xpto) -> here, the site appears unformatted (no CSS) and too slow.
If I access localhost/xpto/something, I get the site with the CSS, but some links wont work.
I have mod_rewrite loaded (phpinfo()) together with the 3 .htaccess files, but still experience this problem.
Is there any known issue that I can explore to fix my site?
Please redirect me to any guides or tutorials that you may feel relevant to my issue

Use RewriteBase directive in .htaccess.
e.g my dev space is on my localhost, is located in /home/ati/public_html/cakerbs, under a subdir in my userdir. The rewriteBase looks like as follows in the root of the cake:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /~ati/cakerbs
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
in the app directory .htacces:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /~ati/cakerbs
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
in the webroot directory:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~ati/cakerbs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Check your XAMPP apache settings, it's very likely your current config is blocking things like the .htaccess files (I know EasyPHP does this by default with Cake). What I do to get around this error is to add a VirtualHost to my config like such:
<VirtualHost *>
DocumentRoot "C:/xampp/htdocs/xpto/"
ServerName xpto.dev
ServerAlias www.xpto.dev
ErrorLog "logs/xpto-error.log"
CustomLog "logs/xtpo-access.log" common
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
It may be better to update your whole config properly, but seeing as it's a dev environment for me, this does just fine. (Don't forget to add any hosts to your windows hosts file)
Then just open your favourite browser and go to http://xpto.dev/ and it should load up.
This may help your more: http://ailoo.net/2008/07/set-up-multiple-virtual-hosts-on-xampp-for-windows/

Related

My React JS app crashes when I realod the page using subdomain apache ubuntu server 20

could someone else help to me how I would configure apache/htaccess/reactapp in order to avoid the webapp crashes
My subdomain is eg. https://subdomain.example.com (it's working fine) the page loaded well if I refresh the page the page loaded well too. I think just right it is because the index.html is serving route home page.
But if I go to eg. https://subdomain.example.com/about (it's working fine as well) but here is the problem when I reload the page "Not Found - The requested URL was not found on this server". all pages after the domain crashes when I refresh the page
my htaccess
`
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
`
Server Ubuntu server 20.04
virtual host
`
<VirtualHost IP:443>
ServerName subdomain.example.com
DocumentRoot /var/www/my-app
<Directory /var/www/my-app>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
`
I just added this one in my htaccess root folder of ReactJS app
The credits for this guy: https://putridparrot.com/blog/react-router-dom-direct-url-or-refresh-results-in-404/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>

Error 403 forbidden when using React Router with Apache

When I deploy my website built with React and React-router the landing page of my website works fine but when I try to navigate into my website I run into 403 forbidden response. I use Apache as an HTTP server and the general config of my server is the following :
<IfModule mod_php.c>
php_value date.timezone Europe/Brussels
</IfModule>
<IfModule mod_userdir.c>
UserDir /var/www/users/staff/*
</IfModule>
For your information, the website is located under /var/www/projects
I added the following .htaccess to my project :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
Despite those configurations the 403 forbidden error when navigating in pages managed by react-router. Did I miss some configurations?
It seems that the settings you have applied are not enough, You should change some parameters in apache.conf too...
open the apache main config file, Its path varies depending on your distribution, that's located in one of these locations:
1- /etc/apache2/httpd.conf
2- /etc/apache2/apache2.conf
3- /etc/httpd/httpd.conf
4- /etc/httpd/conf/httpd.conf
look for "AllowOverride" parameter like the example below:
<Directory "/var/www/blahblah">
AllowOverride All
</Directory>
And give the "All" value to that.
Then create a .htaccess file in the root folder of the webserver & set the following configs:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
NOTE: Before performing these things you have to enable the rewrite module, By entering the following commands you can do this:
debian base: sudo a2enmod rewrite
centos: uncomment mod_rewrite.so in /etc/httpd/conf/httpd.conf
Finally, restart your web server... Now expected there is no anymore problem.
If you got any permission/server error take a look at the webserver logs which are located in one of these locations:
1- /var/log/httpd/
2- /var/log/apache2

vhosts conf for single page app

I have an angular app that I'm serving locally using apache. My vhosts.conf is:
<VirtualHost *:80>
ServerName my.app.com
DirectoryIndex index.html
DocumentRoot /export/www/app
<Directory "/export/www/app">
order allow,deny
allow from all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html/$1 [L]
</Directory>
</VirtualHost>
If I go to the root of the app everything loads fine. I do some navigation within the app which brings me to my.app.com/some/location and still no problem.
However if I reload the page at this point I get a 404 error. To get back to the point I want I need to either go back to the root and navigate via the app again or else search by url my.app.com/#/some/location
Is there something that I can do with a rewrite rule so that I don't need the hash to reload the route? I tried to edit my existing rule to:
RewriteRule ^(.*)$ index.html/#/$1 [L]
Any ideas greatly appreciated
C
The issue was that I was using a newer version of apache where the config rules have changed. My updated vhosts now reads
<VirtualHost *:80>
ServerName my.app.com
DirectoryIndex index.html
DocumentRoot /export/www/app
<Directory "/export/www/app">
order allow,deny
allow from all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
</Directory>
</VirtualHost>
It's weird that my.app.com/some/location works. With AcceptPAthInfo ON, httpd would serve index.html and pass PATH_INFO of /some/location but that shouldn't do the right thing on the client-side JS.
The rewrite you attempted seems like it shiould replace your existing rewrite entirely. But, you'd want to redirect explicitly to non-relative /index.html and add the [R] flag to make sure the redirect goes all the way to the client. Otherwise, it's just an internal substitution, and you need your browser to read the # because that's only handled on the client side.

CakePHP 2.x on CentOS 6

I am having issues with CakePHP 2.4.3 (tried on 2.5.2 too, same problem) installation on CentOS. Database can be accessed (read/write) from cakephp.
Tried for the past few hours to fix it, but here is where I am stuck :
1) This error keeps on appearing. "URL rewriting is not properly configured on your server"
- I have check, and the file is accessible from the site, "www.mysite.com/cake/app/webroot/css/cake.generic.css"
- I have updated httpd.conf to have "LoadModule rewrite_module modules/mod_rewrite.so"
- The files ".htaccess" are in 3 folders :
/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
/app/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
httpd.conf
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
2) My controllers cannot be called directly. It needs the "index.php" to be there.
Example :
Works on : "www.mysite.com/cake/index.php/grabs/myFunction"
Unable to work on : "www.mysite.com/cake/grabs/myFunction"
Please advise.
An important notice for people like me, who struggled with problem exactly like this one.
One of the POSSIBLE REASON for this behavior is when there are two or more CakePHP apps residing on one server. Anyway, try changing this line:
// Prefix each application on the same server with a different string, to avoid Memcache and APC conflicts.
$prefix = 'myapp_';
From your /app/Config/core.php to something distinct, let's say:
$prefix = 'coolRpgGame_';
It really saved my life and it worked immediately.

CSS not working with CakePHP App

I'm having a problem with CakePHP, the CSS is not found. When I view the source code in my browser I can see the cake generic CSS link in the head section. But when I click on it to see the actual source code, I get a 404 not found error.
Update:
I've followed the instructions here:
http://book.cakephp.org/2.0/en/installation/url-rewriting.html
to make sure the mod_rewrite is setup correctly but I'm still having trouble viewing the CSS file.
Update:
THis is what .htaccess looks like in my root folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
This is what .htaccess looks like in my app folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
and this is what it looks like in my webroot folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Regards,
Stephen
I managed to solve it. The problem was with the way I set up my virtual host, my httpd-vhosts.conf file now looks like this:
<VirtualHost *:80>
ServerAdmin webmaster#invoice.dev
DocumentRoot "/Users/stephenxconnolly/Documents/Websites/invoice-new"
ServerName invoice.dev
<Directory "/Users/stephenxconnolly/Documents/Websites/invoice-new/">
AllowOverride All
</Directory>
# ServerAlias www.dummy-host.example.com
ErrorLog "/private/var/log/apache2/invoice.dev-error_log"
CustomLog "/private/var/log/apache2/invoice.dev-access_log" common
</VirtualHost>
This solved the problem.
Are you letting CakePHP create the link to the CSS? Like this:
echo $this->Html->css('cake.generic');
If Apache is pointing at webroot, and your CSS is in webroot/css/cake.generic.css, then you should be able to browse it at
http://website/css/cake.generic.css
If you put the CSS file in webroot can you browse it ok?

Resources