Apache: No such file or directory - php

My website is on apache server.
Path: /var/www/example.com/public_html
I am trying to access file in public_html folder.
PHP
require('/application/config/development/dev_config.php');
ERROR
PHP Warning: require(/application/config/development/dev_config.php): failed to open stream: No such file or directory in /var/www/example.com/public_html/index.php on line 53
When I tried to do this:
require('application/config/development/dev_config.php');
It's running fine. But, I used forward slash in all over the place in my project. Is there any way to change this setting in .htaccess file? so, I can access it by using /.
Apache Config File
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Apache and PHP Understand / Differently
Apache views your web root as your root folder (/var/www/example.com/public_html/), whereas PHP is able to access your entire system (so '/' means '/'). That means that while
...
really means /var/www/example.com/public_html/home.php,
header('Location:/home.php');
would mean a home.php file in the root directory of your server's system.
Solution
The easiest (IMHO) way to avoid typing out the entire web directory structure when indicating a web file to PHP is to use the $_SERVER['DOCUMENT_ROOT'] variable. This queries Apache to find the path to the web directory root, and you can add the rest of the file path as though you were talking to Apache. For example:
header('Location:'.$_SERVER['DOCUMENT_ROOT'].'/home.php');
would be understood by PHP to mean /var/www/example.com/public_html/home.php.

/application does not exist, the actual folder you want is:
/var/www/example.com/public_html/application/config/development/dev_config.php
By beginning with / you are telling the system to look at the very root path of your file system (where/var is for example).
The reason application/config/development/dev_config.php works is as you are calling the folder relative to the current folder as you are already in public_html

Related

Disable PHP execution in one directory

In order to prevent PHP local file inclusion attacks I want to disable the execution of all PHP files in one directory completely. Using the line php_flag engine off within the .htcaccess file will cause a 500 error. According to another question this is due to the way PHP is installed.
Is there any other way to prevent PHP execution if the PHP installation cannot be altered?
Update: The files don't neccessarily have the .php ending.
Add this to your .htaccess file
<FilesMatch \.php$>
SetHandler None
</FilesMatch>
You're building your site via "allow all, then deny" logic. You should build it with "deny all, then allow" logic. For example, you're telling Apache to serve all files a particular directory and then you're overriding that config to tell Apache to not serve some files in that directory. I.e., you probably have something like this:
<VirtualHost *:80>
ServerName foo.com
DocumentRoot "/path/to/files"
</VirtualHost>
With a directory layout like this:
/path/to/files
index.php
config.php
/path/to/files/lib
db.php
etc.php
other_thing.php
With this setup, anybody can request http://foo.com/config.php or http://foo.com/lib/etc.php directly, which is what you're trying to prevent. Rather than adding individual exceptions for everything you want to deny, start the other way around. I.e., if you have files that you don't want to be served, then don't put them in the document root.
<VirtualHost *:80>
ServerName foo.com
DocumentRoot "/path/to/files/public"
</VirtualHost>
Note the DocumentRoot is now set to a subdirectory within your project. Put only your public assets in this directory. All other files go outside (i.e., above) public, and thus can not be served by Apache, while still allowing PHP to include them.
/path/to/files
config.php
/path/to/files/lib
db.php
etc.php
other_thing.php
/path/to/files/public
index.php
To protect your website from backdoor access files, you need to create a .htaccess file and upload it to your site’s desired directories.
Create a blank file named .htaccess and paste the following code inside it.
<Files *.php>
deny from all
</Files>
Add this below the <?php header, this will prevent a direct execution of php
defined('BASEPATH') OR exit('No direct script access allowed');

Change root folder for XAMP

I've got an XAMPP installation running great and I've configured my local machine to be accessed by other users via my IP address using this tutorial here.
However, when I navigate to this page I get the standard XAMP (horrible yellow and orange) homepage.
Where can I change my root directory for my website so I've got a folder called oursite within the htdocs folder of XAMPP which I'd like to load when a user navigates to my page instead of redirecting to the xampp folder.
Just as explained in the tutorial you mentionned, in the file C:\xampp\apache\conf\extra\httpd-vhosts.conf, you should have a VirtualHost definition like this one :
<VirtualHost *:80>
# Just Change the line above so that DocumentRoot point to your "oursite" folder
DocumentRoot "/Applications/MAMP/htdocs/oursite/"
ServerName Whatever.server.name.you.want
</VirtualHost>
Just Change the line above the previous comment so that DocumentRoot point to your "oursite" folder

How to deploy laravel Application so that url doesn't contain '/public/index'

I have a laravel application which contain an index file in its public folder which I can access using the following url:
myIpAddress/teamsync/public/index
But I want to use url like this:
myIpAddress/teamsync to access my index file.
Perhaps the best way to remove /public/... is to set up your virtual host configuration file to set up the /public directory as the document root. This kills two birds with one stone by allowing you to set yoursitename.com as Laravel's root, but also moving sensitive back-end files up away from the www root folder.
<VirtualHost *:80>
ServerName myserver
DocumentRoot /var/www/yourproject/public
ServerAlias www.example.com
</VirtualHost>
Examples on how to use virtual hosts:
With Apache: http://httpd.apache.org/docs/2.2/vhosts/examples.html
With Nginx: https://www.digitalocean.com/community/articles/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3

XAMPP localhost returns object not found after installing Laravel

After much faffing about I got laravel to work with XAMPP. However, I can't seem to access directories in the htdocs folder via localhost now. Attempt to access a file returns Object not found! along with The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
The changes I made to get laravel working seems like a blur now. The only thing I remember doing is editing the hosts file to enable the virtual host to work by adding 127.0.0.1 laravel.dev in the hosts file (using a mac btw). I also added a virtual host to the httpd-vhost.conf file.
I did undo the above changes but it didn't make a difference.
Any thoughts on whats gone wrong?
Thanks.
Dispelling Confusion
Just thought I'd clarify what my experience is. Before installing laravel 4 I could access all my projects with localhost/someProjectName but now it fails.
I'm trying to identify what change caused this behaviour. Btw, I have no problems accessing my laravel project (my mapping allows me access to it via laravel.dev)
Look into your /etc/httpd.conf file and see if you have Virtual hosts activated.
If so, check your etc/extra/httpd-vhosts.conf file. You might have set up a VirtualHost already.
EDIT: I have found that once you turn on Virtual Hosts, XAMPP needs a default VirtualHost for your basic htdocs files
Try adding a default VirtualHost (at the bottom of the file) like so:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/Applications/XAMPP/htdocs"
<Directory "/Applications/XAMPP/htdocs">
Options Indexes FollowSymLinks Includes execCGI
AllowOverride All
Order Allow,Deny
Allow From All
</Directory>
</VirtualHost>
It sounds to me like it is a problem with the way your directories are configured. Be sure to create a Virtual Host (if you're using Apache) that points to the public folder within your application directory.
For example, if your Laravel project is under /Applications/XAMPP/htdocs/Laravel then set up a Virtual Host like this:
<VirtualHost *:80>
DocumentRoot /Applications/XAMPP/htdocs/Laravel/public
# Other directives here
</VirtualHost>
Additionaly, if you're working with PHP >= 5.4, SSH into the application folder and run
./artisan serve
(Be sure that your PHP executable is in your PATH variable). Then go localhost:8000 and you should have your application running.
Running this command runs the PHP built-in webserver and saves you the trouble of configuring virtual hosts.
In your Apache's http.conf, find the DocumentRoot line and add the subdirectory /public on the end.
Once that is done and you've restarted Apache, you'll be able to access everything which is contained within your htdocs/public folder (including subdirectories of that folder), along with any routes you've defined in Laravel.
Laravel is designed to be set up this way as to protect the code and files by not having them in the folder which is served out to the web.
I have same issue and found that there are miss configuration in vhost. So that's are correct configuration.
in etc/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "D:/xampp/htdocs/laravel-master/public"
ServerName laravel.local
ErrorLog "logs/laravel-master-error.log"
<Directory D:/xampp/htdocs/laravel-master/public>
AllowOverride All
Order Allow,Deny
Allow From All
</Directory>
</VirtualHost>
In the hosts file
127.0.0.1 laravel.local
I don't know if you're running L4 or L3. However, launch CLI and
$ cd ./path/to/project
Then for L4:
$ php artisan serve
For L3:
$ php -S localhost:8000 -t public
Now you can go to localhost:8000 and see your application.
You're having problem because the object really doesn't exist in your htdocs directory. You don't have to append xampp after localhost or 127.0.0.1 because xampp will treat it as an object or a folder under htdocs.
if you want to access your blog, make sure you have a blog folder under htdocs and put in your URL localhost/blog

how to define a virtual root?

The root of my local server is /Users/myname/Sites and all web projects are in seperate directories in that e.g. /Users/myname/Sites/newproject
The problem I've got is I've got a site which has all html paths written beginning with a slash e.g <a href="/dir/file.txt"> which makes the browser look for the file file.txt in /Users/myname/Sites/ rather than in /Users/myname/Sites/newproject where the file is located.
Rather than edit every single file path to either remove the beginning slash or to put in a php variable at the start I was wondering if there was some easy way to do this site wide?
I've tried using <base href="http://localhost/newproject" />
I've tried setting the DocumentRoot in .htaccess
I've tried setting the $_SERVER['DOCUMENT_ROOT'] to $_SERVER['DOCUMENT_ROOT']."/newproject";
Unfortunately none of these seem to work.
You want to setup a virtual host (if you are using apache) to specify that the / url should map to /User/myname/Sites/newproject.
You would do this like so (edit httpd.conf or place a file in conf.d):
# Listen for virtual host requests on all IP addresses, port 80
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /User/myname/Sites/newproject
ServerName localhost
</VirtualHost>
Note that when you do this, you are essentially "setting document root to be your newproject folder", which will shadow everything else in the Sites folder.
If this is not what you are talking about, please let me know so I can modify my answer.
You can try setting up a virtual host. Assuming from your folder structure that you are on Mac OS X, try this:
http://mark-kirby.co.uk/2008/setting-up-virtual-hosts-on-os-x-leopard

Categories