Permission denied when trying to access file on Apache - php

I try to run very basic php app using apache and docker compose and have a problem with apache settings. When I try to access any static file in subdirectory I get lack of permissions. PHP calls seem to work fine.
So:
when I try to request localhost/LICENSE.txt I get the license.
when I try to access localhost/test.php or localhost/app/test.php it works fine
when I try to access localhost/app/test.html I get Forbiddenin
the browser andclient denied by server configuration:
/var/www/localhost/htdocs/src/app/test.html` in error log
Of course files I try to request are in the right locations.
Here's apache version I am trying to run:
/var/www/localhost/htdocs # httpd -v
Server version: Apache/2.4.33 (Unix)
Server built: Mar 27 2018 11:32:10
Here's my host config:
<VirtualHost *:80>
ServerAdmin test#example.com
DocumentRoot /var/www/localhost/htdocs/src
ServerName schmidt.local
<Directory "/var/www/localhost/htdocs/src">
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
RewriteEngine On
</Directory>
<LocationMatch "^/(.*\.php(/.*)?)$">
ProxyPass fcgi://php-fpm:7000/var/www/localhost/htdocs/src/$1
</LocationMatch>
</VirtualHost>
So as you can see I already have request instead of allow as suggested here.

Related

Symfony 5: 404 page for /lucky/number application

I just started learning Symfony and try create 1th application by article https://symfony.com/doc/current/page_creation.html
WebServer: Apache 2.4.41
Server: Ubuntu 20
PHP: 7.4.3
I created domain on server symfony.
Installed in /var/www/html/symfony/ by command symfony new --full ./
Add required files (controller and write route to config/routes.yaml). Installed composer require symfony/apache-pack.
Execute $ php bin/console debug:router:
Name
Method
Scheme
Host
Path
...
app_lucky_number
ANY
ANY
ANY
/lucky/number
Opened in browser http://server.ip/symfony/lucky/number and get 404 Apache error.
Opened in browser http://server.ip/symfony/public/ and OK.
Opened in browser http://server.ip/symfony/public/index.php/lucky/number and OK.
Apache config in /etc/apache2/.../symfony.conf:
<VirtualHost *:80>
ServerName symfony
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/symfony/public
<Directory /var/www/html/symfony/public>
Options FollowSymlinks
Require all granted
#AllowOverride All
Order Allow,Deny
Allow from All
#FallbackResource /index.php
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
mod_rewrite is enable.
Anybody have any ideas how to fix it?
You need to add .htaccess in your project.
If you will use only apache configuration, you will be enabled FallbackResource index.php in your Directory
Following this configuration : https://symfony.com/doc/current/setup/web_server_configuration.html
See section : Use the following optimized configuration to disable .htaccess support and increase web server performance:

zf3 virtual host redirects to the wrong page

I configured zend framework 3 through composer on remote debian server. I added virtual host to etc/apache2/sites-available/mysite.conf like below but it shows just "It works!" page instead of zf3 public. Do anyone knows hos to solve it?
<VirtualHost *:80>
ServerName zfapp.localhost
DocumentRoot /var/www/mysite/skeleton-application/public
<Directory /var/www/mysite/skeleton-application/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
</Directory>
Check the following:
Have you enabled the site on the remote Debian server. To enable the site you should use a2ensite mysite
The ServerName directive should correspond the the HTTP hostname in the HTTP request. When you are dereferencing a URL on the remote server, the hostname part in the browser should correspond with the ServerName. It is unlikely you are using zfapp.localhost since most OS's have reserved host entries of 127.0.0.1 for any localhost derivative.

Slim 2.6.2 render() Only Works for Index.html

I'm currently learning about the Slim framework over at TeamTreehouse.com, and ran into an issue that I haven't been able to resolve.
At this point in the project, we have installed Slim via Composer and setup .htaccess and index.php files in our document root (which on my computer is /home/daniel/src/public_html/treehouse/build_websites_php/).
In a templates folder we have index.html and contact.html. Here is the layout of the folders.
DocumentRoot (/home/daniel/src/public_html/treehouse/build_websites_php/)
index.php
.htaccess
composer.json
composer.lock
vendor/
templates/
index.html
contact.html
In index.php, I instantiate a new Slim object:
$app = new \Slim\Slim();
And then call the get() method and then call render() to render the index.html and contact.html pages when the url is localhost/treehouse/build_websites/ and localhost/treehouse/build_websites/contact, respectively.
$app->get('/', function () use($app) {
$app->render('index.html');
});
$app->get('/contact', function () use($app) {
$app->render('contact.html');
});
Then run the app:
$app->run();
My index.html page shows up fine, but I get a 404 error (not through Slim, just the server's default) when I try and visit the /contact url. Here are some specs from my system:
Ubuntu 16.04.1 LTS
Apache 2.4.18
Slim 2.6.2
PHP 7.0.8
Anything in my /home/daniel/src/public_html/ directory can be accessed by Apache, as I've run PHP scripts from in there for the past year.
I've tried the suggestions from here (and restarted the server after each update to conf.d or other files) and have had no luck.
Any help would be greatly appreciated, I've only been using PHP/Ubuntu/Apache for about a year, so I'm probably missing something obvious!
Here is the index.php file:
<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/', function () use($app) {
/* When using render(), the url localhost/treehouse/build_websites_php/ to
gets you the home page. */
$app->render('index.html');
});
/* This SHOULD bring up the contact page at url
localhost/treehouse/build_websites_php/contact, but it doesn't! */
$app->get('/contact', function () use($app) {
$app->render('contact.html');
});
$app->run();
?>
Here is the .htacess file:
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /home/daniel/src/public_html/treehouse/build_websites_php/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
And here are various conf.d files for Apache:
/etc/apache2/apache2.conf
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /home/daniel/src/public_html>
Order allow,deny
Allow from all
Require all granted
</Directory>
I tried adding AllowOverride All as suggested here to the last directive and then I wasn't able to access PHP files from the server at all and got a 500 error instead of a 404 error.
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
/etc/apache2/sites-available/mysite.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /home/daniel/src/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
OK wow I finally figured it out thanks to Mika pointing me in the right direction. It turns out, my /etc/apache2/sites-available/mysite.conf file needed the following directive:
<Directory /home/daniel/src/public_html >
AllowOverride All
</Directory>
I had tried adding that directive to /etc/apache2/apache2.conf in addition to the other directives like so:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /home/daniel/src/public_html>
Order allow,deny
Allow from all
Require all granted
AllowOverride All #THIS DIDN'T WORK
</Directory>
But the AllowOverride All from above through a 500 error from the server. Apparently, it had to be by itself in the /etc/apache2/sites-available/mysite.conf file, who knew!
I also ran sudo a2enmod rewrite && sudo /etc/init.d/apache2 restart to make sure the mod_rewrite was loaded after discovering this error message (thanks to Mika for pointing out to check the log files!):
[Sun Nov 13 10:37:51.054347 2016] [core:alert] [pid 10979] [client ::1:51900] /home/daniel/src/public_html /treehouse/build_websites_php/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
I did that before adding the AllowOverride All directive, so I'm not sure if it played a part in solving the problem, but I figured I would document it for any interested.
These sites had valuable information on how to finally solve the issue:
1 - solved mod_rewrite issue
2 - solved where to place the AllowOverride All directive
3 - Slim troubleshooting for .htaccess
It looks like your Apache settings do not allow the .htaccess file to override any settings. Add something like the following to apache2.conf.
<Directory /home/daniel/src/public_html>
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>

php-fpm + bindfs not working

I'm trying to setup my local web server using vagrant. My vagrant shared folder is in my home folder (~/home/vagrant/www) and I want to use bindfs to mount this folder inside /var/www.
These are the specs of my virtual machine:
Apache/2.4.23 (Ubuntu)
PHP 7.0.12
Ubuntu 14.04
I am using php-fpm to execute php scripts but after using bindfs, my site will always return File not found.
Also here is my virtualhost configuration:
<VirtualHost *:80>
ServerName project1.dev
## Vhost docroot
DocumentRoot "/var/www/project1/public"
## Directories, there should at least be a declaration for /var/www/project1/public
<Directory "/var/www/project1/public">
Options Indexes FollowSymlinks MultiViews
AllowOverride All
Require all granted
<FilesMatch "\.php$">
Require all granted
SetHandler proxy:fcgi://127.0.0.1:9000
</FilesMatch>
</Directory>
## Logging
ErrorLog "/var/log/apache2/av_anhk5lpgjldb_error.log"
ServerSignature Off
CustomLog "/var/log/apache2/av_anhk5lpgjldb_access.log" combined
## Server aliases
ServerAlias www.project1.dev
## SetEnv/SetEnvIf for environment variables
SetEnv APP_ENV dev
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
## Custom fragment
</VirtualHost>
Anyone can help me?
I manage to successfully run php-fpm + bindfs in my virtual machine. I just made sure that user who is running php-fpm and apache are the one I set in my bindfs command. My apache is run by www-user so I change my command to sudo bindfs -o perms=0755,mirror-only=www-user,force-group=www-data,force-user=www-user /home/vagrant/www /var/www and made sure that apache is also run by www-user.

Symfony 2.3 production mode urls not resolving

I have just gone through the Symblog tutorials on http://tutorial.symblog.co.uk/docs/customising-the-view-more-with-twig.html
In prod mode, if i access the url like so:
http://local.mysite.co.uk
I get the home page with links to all the blog posts.
However if i click one of the blog posts then I get a 404, in fact any of the links (to the about or contact) all return a 404, eg:
http://local.mysite.co.uk/12/a-day-with-symfony2
If I then access the prod mode via the app_dev.php all the links work again, ie:
http://local.mysite.co.uk/app.php/12/a-day-with-symfony2
I have run to ensure the mod rewrites should be working and restarted apache but no joy.
sudo a2enmod actions
Does anyone know why this might be happening?
Thanks,
John
PS: This is the current vhost file for the dev site i am learning with (Ubuntu lts, apache 2.4, php 5.4ish)
<virtualhost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin webmaster#domain.com
ServerName mysite.co.uk
ServerAlias www.mysite.co.uk
ServerAlias local.mysite.co.uk
# Index file and Document Root (where the public files are located)
DirectoryIndex app.php
DocumentRoot /var/webroot/www/vhosts/mysite.co.uk/htdocs/Symfony/web/
# Custom log file locations
LogLevel warn
ErrorLog /var/webroot/www/vhosts/mysite.co.uk/log/error.log
CustomLog /var/webroot/www/vhosts/mysite.co.uk/log/access.log combined
</virtualhost>
When I add an AllowOverride All to the vhost file and restart apache I get the follwoing error:
* Starting web server apache2
*
* The apache2 configtest failed.
Output of config test was:
AH00526: Syntax error on line 15 of /etc/apache2/sites-enabled/mysite.co.uk.conf:
AllowOverride not allowed here
Action 'configtest' failed.
The Apache error log may have more information.
Altering the vhost worked (thanks to https://stackoverflow.com/users/308825/maerlyn).
Placing a directory tag with the Allowoverride All fixed the issue. Seems that the rewrites are specific to each vhost.
Here is my config for the symfony tutorial which is now functioning in prod mode:
<directory /var/webroot/www/vhosts/mysite.co.uk/htdocs/Symfony/web/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</directory>
<virtualhost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin webmaster#domain.com
ServerName mysite.co.uk
ServerAlias www.mysite.co.uk
ServerAlias local.mysite.co.uk
# Index file and Document Root (where the public files are located)
DirectoryIndex app.php
DocumentRoot /var/webroot/www/vhosts/mysite.co.uk/htdocs/Symfony/web/
# Custom log file locations
LogLevel warn
ErrorLog /var/webroot/www/vhosts/mysite.co.uk/log/error.log
CustomLog /var/webroot/www/vhosts/mysite.co.uk/log/access.log combined
</virtualhost>

Categories