Default route (controller/action) and links of css/js doesn't work cakephp - apache

After extracting the cake 2.5.2 (on Linux) to the directory /var/www/html/teste when accessing the url http://localhost/teste it correctly loads the contents of the controller pages and action display as from routes.php:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
But when trying to access the controller directly through url:
http://localhost/teste/pages/display I have the return of 404 Page not found.
Otherwise, the files css/js/img the webroot folder (not being included for the url generated) are not loaded.
Url generated that returns file not found:
http://localhost/teste/css/cake.generic.css
Url to access the file correctly:
http://localhost/teste/app/webroot/css/cake.generic.css
My solution to the files css/js/img was the following, I changed the form to call the file.
Standard form used:
echo $this->Html->css('cake.generic');
New form:
echo $this->Html->css('/app/webroot/css/cake.generic.css');
In short, I think the error might actually be in apache or something, but do not know exactly how can I validate that part.
My solution to the css / js / img so is it correct? Because I think that was to work as the example I downloaded the version of the cake properly.
If they need more information just tell me, thanks.

This is a symptom of an environment with mod_rewrite disabled.
If you use Ubuntu, just type:
sudo a2enmod rewrite
In your /etc/apache2/apache2.conf find the a block similar to this one:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
And replace the line
AllowOverride None
With
AllowOverride All
And finally, restart your apache with:
sudo service apache2 restart

Related

PHP Upgrade to 8 with Error Options -Indexes in .htaccess file

Have upgraded PHP 7.4 to to PHP 8.2.1 on mac osx, and for some reason, can not locate what is causing an issue with the .htaccess file having this code in it:
Options -Indexes -Multiviews
If I remove this code the site loads, but than shows all folders and files which is not desirable. Need site to load the index.php file that is being shown here, trying to hide this, but once this is in the .htaccess file, I'm getting the following error message:
Forbidden
You don't have permission to access this resource.
Is there something in PHP that needs to be set? I did change the php_module to point to PHP 8 in the httpd.conf file, so I don't understand what else might need to change to get my localhost loaded properly instead of showing files and folders?
BTW, this was working fine on PHP 7.4, but after updating, no longer works. Maybe I have to install other modules for 8.2.1 to work correctly?
If I switch back to using PHP 7.4 everything is loading fine on the site after restarting apache.
Need site to load the index.php file
You need to set the DirectoryIndex (mod_dir) - this defaults to index.html only (an Apache issue, not PHP). It is the DirectoryIndex that determines which file(s) Apache will try to serve when requesting a directory.
For example:
Options -Indexes -Multiviews
DirectoryIndex index.php
If a DirectoryIndex document (there can be more than 1) is not found... and mod_autoindex is disabled (ie. -Indexes) then you get a 403 Forbidden response. If mod_autoindex is enabled then you naturally see a directory listing. (If mod_autoindex is not installed at all then you get a 404 Not Found.)
mod_autoindex (ie. Indexes) is not enabled by default on Apache 2.4 (it is on Apache 2.2), however, it has likely been explicitly enabled elsewhere in the server config.

Debugging Apache AllowMethods error

We are running an API for our mobile app, and with the right HTTP headers, have been able to enable developing it locally using the live API without the need of a 'CORS plugin'.
Now, it does not work anymore, probably since moving the domain name from one user to another (using DirectAdmin), but I cannot figure out how to fix it. Moving the domain back to the original user does not fix it. We have been running Apache 2.4 for quite a while already, that's nothing new.
I tried adding the following to httpd.conf:
<Location /api>
AllowMethods GET POST OPTIONS DELETE PUT
Require all granted
</Location>
<Directory /home/username/domains/example.com/private_html/www/api>
AllowMethods GET POST OPTIONS DELETE PUT
Require all granted
</Directory>
I verified being in the right VirtualHost block by successfully changing the ErrorLog file location.
I also added Require all granted to all .htaccess documents from the private_html folder to the api folder, but the error log keeps saying: [allowmethods:error] [pid ...] [client ...] AH01623: client method denied by server configuration: 'OPTIONS' to /home/username/domains/example.com/private_html/www/api
Note that our mobile app actually still works (GET and POST), but PUT and DELETE don't, just like OPTIONS. It seems like the requests never even hit my domain folder, but get stuck in Apache config.
How can I debug this? How can I get Apache to tell me which policy is preventing that method?
I finally solved the issue with this httpd.conf block:
<Location "/">
AllowMethods GET POST OPTIONS DELETE PUT
Require all granted
</Location>
I was originally trying to limit that to just the API URL, but apparently that gets complicated because of RewriteRules. The first request on /api is passed through, but I needed a new block for each RewriteRule that happens. So I just used Location "/" to fully allow it... hope that doesn't introduce security issues.
#Sygmoral
This also fixed my problem!
If you ever get this message inside a new DirectAdmin environment getting the response on a OPTIONS method:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>405 Method Not Allowed</title>
</head><body>
<h1>Method Not Allowed</h1>
<p>The requested method OPTIONS is not allowed for this URL.</p>
</body></html>
Or if you've checked the apache error logs containing: AH01623: client method denied by server configuration: 'OPTIONS'
You should head out to the section in DirectAdmin " Custom HTTPD Configurations " (as admin) and add the lines to your domains custom httpd.conf:
<Location "/">
AllowMethods GET POST OPTIONS DELETE PUT
Require all granted
</Location>

Load images outside apache directory

Well, I have a website in apache directory: /var/www/html. Previously I had all my web images in /var/www/html/media, so I used to put /media/imgX.jpgfor loading them. but now I need to put them in /mnt/imgs.
For some reason, using this path does not work the same way as before.
I think it has something to do with permissions using files from outside apache directory but I am not sure about it. Any idea?
The easiest way to add directories to Apache httpd is to use the "alias" module:
Alias /images "/mnt/imgs"
<Directory "/mnt/imgs>
Options Indexes
AllowOverride All
Require all granted
</Directory>
Once you have set up this, test it with: "yourdomain.com/images".
Of course:
The directory "/mnt/imgs" need the appropriate rights, so Apache Httpd can access it. Therefore you need to find out the group it runs with.
These files will be available to the public, if you dont secure them.
Once this is functional, remove the "indexes" line from the snippet above.

403 Forbidden issues with Angular JS tutorial

I am trying to learn Angular JS using this online tutorial. I decided to download his test project to look at the file structure and how everything is working. The files are available from this GitHub repo:
I followed the instructions in the readme file but when I load the index page the Wine List fails to load due to a 403 Forbidden error. I have a hunch that this might be due to either file permission or mod_rewrite issues but I don't know how to solve it.
I have looked through the http.conf file at /etc/apache2/ and mod_rewrite appears to be enabled and I've also put a phpinfo(); file in my root directory and looked for mod_rewrite in the enabled modules and it appears to be there.
Any ideas?
In your Apache config file (httpd.conf or extras/httpd-vhosts.conf) add:
<Directory "C:/path/to/files">
Require all granted
</Directory>
Use your actual path, this will grant Apache access to the directory.
After reading through a few of the related questions I found a solution:
Adding the following to my vhost file did the trick:
Options +FollowSymLinks
I hope this helps someone else!

Broken links in local Wordpress site

I have a wordpress site set up on a live server, and I have replicated the site locally by following these steps:
FTPed live files to local
Set up virtual host (dev.domain.com) to point at local version of site
Imported the db locally
changed wp-config.php to the correct local db settings
changed 'home' and siteurl' in db.wp_options to point to http://dev.domain.com (from http://www.domain.com)
Home page loads fine, /wp-admin all loads fine.
Problem is in links to pages:
Permalinks are set to point to post name: http://dev.example.com/sample-post/, just as on live server. However, locally, all links to posts are broken, and Apache (2.2.17) is responding with the following error: "The requested URL /sample-post/ was not found on this server."
I'm assuming I've missed a configuration step somewhere, though I've followed this process umpteen times in the past with no problems. The issue with this particular site is that the theme has been hacked with lots and lots of absolute paths entered, meaning setting up a dev site has required loads of code changes.
I'm not really sure how to further trouble shoot this, not completely understanding how Wordpress / Apache handles permalinks
Copy the .htaccess if you haven't already
I think that might be the problem
OK - sorted this, it was to do with mod_rewrite on apache.
To fix (this is for my install of Ubuntu 11.04):
first enable mod_rewrite in apache
sudo a2enmod rewrite
Then edit the relevant file in /etc/apache2/sites-available (could be 'default', or one specific to site):
sudo vi /etc/apache2/sites-available/site-file
Change AllowOverride directive for your site document root from None to All:
:
<Directory /var/www/site.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
That seems to have done it.