Accessing files above the web server document root folder - php

Reading elsewhere, I have come across the concept of placing files I don't want accessible to the public above my web root folder. Doing this on my local WAMP stack works without issue, but once transferring over to my VPS (running Ubuntu 10.04.4 LTS) the PHP scripts can only see files under the root directory.
VirtualHost setup for my subdomain:
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin ##########.com
ServerName admin.########.com
ServerAlias www.admin.########.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php admin.php
DocumentRoot /srv/www/#######.com/public_html/admin/web_pages
<Directory /srv/www/#######.com/public_html/admin/web_pages>
Options +Indexes
AllowOverride All
</Directory>
# Log file locations
LogLevel warn
ErrorLog /srv/www/########.com/logs/error.log
CustomLog /srv/www/########.com/logs/access.log combined
</VirtualHost>
Log error given: File does not exist: /srv/www/#######.com/public_html/admin/web_pages/css, referer: http://www.admin.########.com/
(My css folder is above the web_pages folder, and is called via this path: ../css/####.css)
Is this an issue with my configuration settings, or am I attempting to do something I shouldn't be, and instead have everything I need under my web root? I would really like constants files and application logic not accessible, but css,html pages, and images are ok.

If you want to grant public access to some folders above web root, you may use Alias directive:
Alias /css/ "/srv/www/#######.com/public_html/admin/css"
<Directory /srv/www/#######.com/public_html/admin/css>
# some configuration directives here
</Directory>
After that, your css files will be accessible via new path (without ..): /css/####.css
Don't forget to switch on mod_alias Apache module

Related

VPS ubuntu apache server - web page

I bought a VPS server, where I installed apache, mysql, php and phpmyadmin. I created database (like in my local project).
So, now I want to migrate a local project to my server and please, tell me I am right:
Files .php has to be in folder var/www on my apache server? (here is index.html too) and from this folder connect with database?
I have to download NPM on ubuntu
I have to download GIT on ubuntu
Download repo from my github where I've got whole code (webpack dist files, node modules, src and whole config) to /var/www catalog
index.html has to be in top-level directory (just in var/www not for example var/www/src
Please help me and tell if I am thinking right.
Files .php has to be in folder var/www on my apache server? (here is index.html too) and from this folder connect with database?
It depends on what you set in /etc/apache2/sites-available/000-default.conf. I assume that you installed with default configuration from apt-get.
index.html has to be in top-level directory (just in var/www not for example var/www/src
If for example your source codes are in /var/www/src instead of /var/www, the just modify the 000-default.conf. Example:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName your_domain_name.com
DocumentRoot /var/www/src/
<Directory />
Options FollowSymLinks
AllowOverride None
Options -Indexes
</Directory>
<Directory /var/www/src>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
Options -Indexes
</Directory>
ErrorLog /var/log/your_log_file.log
LogLevel warn
CustomLog /var/log/your_access_log_file.log combined
</VirtualHost>
Restart Apache and try open in your browser to see if it works.
As for the database, since you have phpmyadmin, just export the sql data from your local machine and import it into the VPS phpmyadmin.

How to remove public from the URL of Laravel project?

I've followed this and this answers to remove /public form the URL of my Laravel project. Now /public is removed from the URL. But my IDE (phpstorm) is throwing an error:
Does anybody know how can I fix it?
Note: I use Laravel 5.4
Change your apache vhost / host document root to projects public folder. its realted to apache config.
<VirtualHost *:80>
ServerName findexample.com
DocumentRoot c:/xampp/htdocs/find_people/public
<Directory c:/xampp/htdocs/find_people/public>
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/find-people-error.log
CustomLog ${APACHE_LOG_DIR}/find-people-access.log combined
</VirtualHost>
See how to setup apache vhost on windows
You dont need to touch on any file to get what you want.
Try setting your document root to
path/to/your/laravel_app/public/
If your root is pointing to your public folder, when you access it from Browser, you will not need to use public in the url.
Remember that you must enable ModRewrite.

Creating Virtual Host on apache not loading styles from root directory

I am new to VPS hosting.... and giving a try to digital ocean Linux VPS. I need to point 2 domains to my PHP app.
This is how my Virtual Host looks like inside /etc/apache2/sites-available
<VirtualHost *:80>
ServerAdmin root#domain1.com
ServerName domain1.com
ServerAlias www.domain1.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
However... I am not able to load my styling and access any files or directories that is in var/www/html directory. How can I enable the permissions for my domains to access these resources.
Similarly my second Virtual host for another domain
DocumentRoot /var/www/html/app/login.php
Everything seems working while I am accessing the site using the IP...However... after setting up virtual host... the content is loading but can not access css and js... in the current root directory.
I am not sure what I need to do to set up my domains as expected.
I am using ../ notation to point my previous directory. There is no error with the site that is showing...just not loading CSS. Also I have the APIs in the root directory.... inside API folder.... that is not accessible while I am pointing them ../../API/v1/create_user.php

apache virtual host multiple directories

I have setup a virtual host in apache with multiple directories for www.abc.com and abc.com/api which I need to points towards different directory
<VirtualHost *:8080>
DocumentRoot "/var/www/html/api/"
ServerName www.abc.com
ServerAlias www.abc.com
DirectoryIndex index.php
# Other directives here
# Other directives here
Alias /api/ "/var/www/html/public/"
<Directory "/var/www/html/public">
</Directory>
</VirtualHost>
Its working when I go to www.abc.com/api/ but when I move beyond that www.abc.com/api/v1/ I got the error
Not Found
The requested URL /var/www/html/public/index.php was not found on this server.
How to fix the issue?
I suppose you are using an MVC framework. If that's the case you need to configure your routing path in order to access your desired file.

How to create multiple document roots on server?

I am developing a website where if a user enters any sub-domain the system changes to look within a specific folder within root. I can get to the files correctly, however because the document root is looking for /server-path/ as the root, none of my files, images etc work.
Here is my file structure if this helps:
For www.main-website.com,
The Server doc root = /server-path/
For subdomain.main-website.com
system looks at -> /server-path/folder/
The Server Doc Root still = /server-path/ - however I need this to now equal /server-path/folder as the DOC ROOT.
I have tried the following with no success:
Setting the $_SERVER['DOCUMENT_ROOT'] to equal /server-path/folder
Adding a php.ini file into the folder root with the following line:
doc_root = "/server-path/folder"
I'm not very competent with Apache or server changes so any help / suggestions would be welcomed.
You can set different document roots by using VirtualHost
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.main-website.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName subdomain.main-website.com
# Other directives here
</VirtualHost>
Tipically this content should be included into a .conf file inside /etc/httpd/conf.d/

Categories