Access separate docroot in apache - php

I have a site on a server running Apache2 that resides at docroot /var/www/html. I want to access some of the files on a separate site at docroot /var/www/vhosts/othersite. Is there a way to access these files from the first site?
Thanks,
Chris Birk

You can include them using the include and require calls, or use symlinks to create a soft link in project 1 from project 2. These obviously depend on what you're actually trying to accomplish.
Edit: Oh, also, you could potentially add the folders you want to PATH.

The right way to do this is with mod_rewrite, and there are several ways of mapping URLs to different paths in the documentation here.
The cheatin' way of doing it would be to create a symbolic link from the directory outside the document root to a directory inside the document root, making sure the user Apache runs has can read that directory, and follow symlinks is turned on.
Yet another way of doing it would be to create a subdomain as a VirtualHost, with a document root of that other directory.

Related

Some of the directories are accessible from public URL. How to avoid this in yii

In my Yii web application some of the directories are accessible from the public URL (main directory or application folder) like js, css, images etc. How to avoid this problem. This is a major security issue, but I don't know how to fix this. Please help me...
Thanks in advance...
If you're using Apache, you can restrict access to directories doing the following:
Create a .htaccess file in your directory so path/to/directory/to/deny/.htaccess
Open .htaccess and add Deny from all
You have to turn of apache directory listing. Use the link below.
How do I disable directory browsing?
Then change all files and folders permissions of a directory to 644 and 755.
Change all files and folders permissions of a directory to 644/755
Let me know if you need help
Here is some instructions given in documentation.
1) It's important that the directory be writable by the webserver user so that Yii can publish the resources there when needed.
2) When a project has multiple versions (production, testing, development, etc.) do not copy the assets/ folders from one area to another; allow Yii to deploy them automatically in each area.
3) Do not manually edit any file under assets/ - if you have a real need to make a change, find the publishing module, edit the source, delete the subfolder under assets/, and let Yii re-publish the updated files.
4) Do not reference names under the assets/ folder directly (say, to get at some other module's assets). If you need to use that
5) Do not add the contents of the assets/ folder to any source-code control system; these files have master source in other places.
6) It is safe to delete everything under assets/. Yii will re-publish the assets if they are not found under assets/.
Hope it will help you :)

"Symbolic" link to resolve requests to a folder outside public_html?

I have a lot of image and pdf files in a folder within the public_html directory, and I have a lot of scripts on public_html dynamically processing, creating and editing these files based on user requests.
Now the problem is that I've realized that having these files stored in folders in public_html isn't safe, maybe someone could just inject some script and delete them all! So what I want to do is move them outside public_html. However - since I have so very many scripts dealing with all these files, all the paths will be messed up. I am using scripts that use relative paths, (/home/public_html/designs/product1/...) as well as scripts that use absolute paths (www.mydomain.com/designs/product1/...).
I don't know much about symbolic links to be honest - I've read up some but I found it confusing since I'm not that good with Linux either.
My question is: is there any way that I can put some kind of symbolic link or connection, so that I can move all my product design files outside public_html, and yet don't have to change all my scripts to point to the new path? So maybe, some kind of code that resolves all public_html/designs/ requests from my scripts to the new path? Maybe symbolic links isn't what I need here, but something else?
create sysmbolic links is easy in linux
first go to public_html directory :
/home/public_html/
then move designs directory to upper directory [ or every where you want ]
mv designs ../
then create sysmbolic link to the real directory
ln -s ../designs designs
every thing will be work

how to include the file with relative path?

there is a file directory as this:magento\app\design\frontend\default\template\catalog\product\view.phtml
the another file directory as this:magento\blog\wp-blog-header.php
now,if i want to in view.phtml include the wp-blog-header.php.how do i do?thank you.
include('../../../../../../../wordpress/wp-blog-header.php');
You could use document root ("$_SERVER["DOCUMENT_ROOT"]"). But that only applies if your app is installed as the only app in the base directory on the web server. If you are running the app in a sub directory, that you need to adjust for that. However, this will make the app less portable as you cannot change the directory easily without having to modify codes.
Based on your example, it seems that you are running Magento, with Magento, you can use "Mage::getBaseDir" method to get the base directory of Magento and relative the include file from that.
Personally I would go from root up, not all of this ../../ jazz:
include($_SERVER['DOCUMENT_ROOT'] . "/path-to-include-file");
You sure the wordpress header is compatable with magento? Could face some horrible anomalies there.
That would work, you just have to count the slashes to make sure it's the same as before. However, you can also create a symbolic link in your magento\app\design\frontend\default\template\catalog\product\ directory that points to magento\blog\wp-blog-header.php.
ln -s magento\blog\wp-blog-header.php wp-blog-header.php would do the trick then you can just: include ("wp-blog-header.php");
You can also reference $_SERVER["DOCUMENT_ROOT"] in your include.

How to declare paths in PHP to allow easy app move to not rooted folder?

I have wamp setup on my windows box. Generally, when I bring a site down from the web, I create a folder inside my www folder for the site name. ex: c:\wamp\www\mysite. Once I have the folder, I copy down all the live files. The issue is that all the paths are then broken because my local folder isn't rooted.
What is the best way to setup paths so that if the site moves to a folder that isn't rooted, it will work easily?
I use a file (usually called something like config.php) to keep track of the root folder. My definitions (constants) look like this:
define('BASE_DIR','/wherever/whenever/');
define('LIB_DIR', BASE_DIR . 'lib/');
And then when you need to include a file
include LIB_DIR . 'aFile.php';
This would be something you do on a new site or if you have time to refactor your current site.
Create an include file, that has constants setup based upon whatever the root directory is... then in your code, use the constants you created to include files.
Also note, that when you are using directory "slashes", always use the build in constant DIRECTORY_SEPARATOR instead of hard coding it, this will allow you to go from WIndows to Linux seamlessly.
We use $_SERVER['DOCUMENT_ROOT'] to determine where we are in the filesystem and then simply append the folder name of our project to that. This works perfectly for us. You should always use a configuration.php where you define basic paths and URL's that may change when moving the project from one server/folder to another.
Option 1. Use <base href=""/> tag
Option 2. Use a config file, like #MattCan suggests
Option 3. Use a server environment variable, like #Bjorn suggests
Option 4. Create a virtual host on your apache, than you can create a domain who appoint exactly where are your app folder. Apache Doc here

how to use config file from a subdirectory while it is been kept at root directory in PHP

I am developing a web application. contents are:
root dir (/var/www/)
config.php
index.php
details.php
admin dir (/var/www/admin)
admin.php
I have included config.php file into index.php, details.php in root directory using require_once('config.php') as this file contains database passwords, styles, images directory paths..
how can i include that config files in my admin/admin.php file so that one config file can be used in anywhere(even in subdirectories) of my web application. Will it make any difference for the value of define('APP_BASE_PATH', dirname(__FILE__)); when same config file is used by all files in the web application.
if i am wrong somewhere then please get me right.
If your server properly configured, just
include $_SERVER['DOCUMENT_ROOT']."/config.php";
anywhere
You have also 2 other possible ways.
a Front controller setup, where ALL user requests going into one file. And ths one going to include all others from their subdirectories. Personally I don't like it cause this front file become a mess. Though it's widely used.
I decided not to mention it because noone would use a hardcoded full path anyway.
Update after clarification in comments: You are looking for a way to include a central configuration file from anywhere in your project's folder structure.
#Col. Shrapnel shows one way, DOCUMENT_ROOT. It's the only way to use an "absolute" path from a nested folder structure. It has the limitation I describe above, but it's fine otherwise.
If you want maximum portability (i.e. the possibility to run the app with e.g. www.example.com/myapp/version_1 as its root directory), you would have to use relative references from within your folder structure to "climb down" to the config file, e.g. ../../config.php that will work reliably too, although be a bit cumbersome e.g. if you move a script to a different folder and you have to update the relative path.
you can use the same config file every time... using "/" will take you back to the root directory... so in admin/admin.php use this:
require_once("/config.php");
you can use "../" to take you up one directory eg:
require_once("../config.php");
was this what you were looking for?

Categories