How to create multiple document roots on server? - php

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/

Related

Can we create two website with the same code and database

Requirement is to create two website
www.example1.com
www.example2.com
with the same codebase and database without copying it to another directory or importing the sql file.
Is it possible through just pointing it based on domain name and using .htaccess file.
Any help or hints is appreciated.
Yes you can. Start by placing all of your project files on the same directory, for the sake of this example let's call it /var/www/example
Then go to /etc/apache2/sites-available and create two .conf files:
example1.conf: Virtual Host Configuration for www.example1.com
example2.conf: Virtual Host Configuration for www.example2.com
The contents of the two configuration files:
example1.conf
<VirtualHost *:80>
DocumentRoot "/var/www/example"
ServerName www.example1.com
</VirtualHost>
exampl2.conf
<VirtualHost *:80>
DocumentRoot "/var/www/example"
ServerName www.example2.com
</VirtualHost>
As you can see both of them refer the same directory as Document Root but listen to different URLs.
After you're done creating those files don't forget to run the following commands on your terminal:
a2ensite example1.conf
a2ensite example2.conf
and finally,
service apache2 reload

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.

Accessing files above the web server document root folder

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

With multiple websites hosted at same ip in Apache, how do I set which one is the default?

I have multiple VirtualHosts in Apache, all listening on port 80 with their ServerNames set to different web addresses. When I go directly to the ip address, Apache sends me to one of those websites as default. I want to change which virtualhost handles requests to the ip address. How would I do this?
<!-- This is currently the default (probably because it's first alphabetically) -->
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName mywebsite.com
DocumentRoot /var/www/testingother
...
</VirtualHost>
<!-- I want this to be default -->
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName myotherwebsite.com
DocumentRoot /var/www/testing
...
</VirtualHost>
From my own apache configuration file:
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"
Note that this DocumentRoot is outside a VirtualHost block. I would assume you have something similar but if not, you can set it up like that. That would be easier then messing around with the order in which they appear.
Default virtual host is the first one to be loaded.
When in single file httpd.conf or vhosts.conf, just change the order of vhosts.
When in vhost file based config (e.g. Debian uses that), it is a convention to prefix vhost files with a number. Default is usually 000-default.
From Apache documentation:
If the request contained an unknown or no Host: header it is always
served from the primary name-based vhost (the vhost for that
address/port appearing first in the configuration file).

Hosting multiple local sites with XAMPP

I'm new to using XAMPP so this may be simple to some people.
I have a few php projects that I would like to be able to debug locally and view in the browser (not concurrently, but without having to change config files/copy project folders each time I want to work on a different project).
On IIS, you could set up multiple sites to serve from your machine, and I'm looking for something similar in XAMPP. When using IIS, I added multiple records to the Windows hosts file so I could access the locally hosted sites by typing friendly web-style addresses (like http://myproject1.dev)
Thanks.
Greg, you're almost there--you need (like Moses said) to setup virtual hosts.
So if your Windows hosts file has
127.0.0.1 localhost
127.0.0.1 mysite-dev.com
127.0.0.1 anothersite-dev.com
Your virtual hosts file (httpd-vhosts.conf) might look like:
<VirtualHost *:80>
DocumentRoot C:/xampp/htdocs/
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
ServerName mysite-dev.com
DocumentRoot "C:/sites/mysite-dev"
<Directory "C:/sites/mysite-dev">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName anothersite-dev.com
DocumentRoot "C:/sites/anothersite-dev"
<Directory "C:/sites/anothersite-dev">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Don't forget to restart the web server after you make any changes.
I would like to make an additional in terms of up to date information.
XAMMP uses port 80 by default and we are able to publish 1 website. I also use IIS for .Net projects. In this respect, I set the port to XAMMP except the 80 port. So I avoid a conflict.
When we want to publish more than one website, we should do the following operations to httpd.conf (this is the current name).
1. Setting the ports
Find the #Listen expression in the httpd.conf file.
Change Listen 80 to Listen 8000 (or whatever else you want)
Listen 8000
If you need 3 different websites, type the others, including 1 definition on each line, as follows.
Listen 8001
Listen 8002
Listen 8003
2. Define the file paths of sites accessed through ports
Again, find in the httpd.conf file.
Identify the folders of each website as follows.
As you would see, I've created 3 directories called 8000, 8001, 8002 and 8003 under the htdocs directory within the XAMMP directory.
<VirtualHost *:8000>
 DocumentRoot "C:\XAMPP\htdocs\8000"
 ServerName localhost:8000
<\ VirtualHost>
<VirtualHost *:8001>
 DocumentRoot "C:\XAMPP\htdocs\8001"
 ServerName localhost:8001
<\ VirtualHost>
<VirtualHost *:8002>
 DocumentRoot "C:\XAMPP\htdocs\8002"
 ServerName localhost:8002
<\ VirtualHost>
<VirtualHost *:8003>
 DocumentRoot "C:\XAMPP\htdocs\8003"
 ServerName localhost:8003
<\ VirtualHost>
Restart your Apahche server on XAMMP.
You can now view your 3rd site, such as http://localhost:8003 or http://192.168.1.1:8003/.
Hope to be useful.
This question was asked almost ten years ago, and the answers above are a bit dated. Note that XAMPP has a "How-To" for virtual hosts avilable off the dashboard, when you install it.
From the "Welcome to XAMPP for Windows" page (localhost/dashboard, the default when you first load localhost) click on the "HOW-TO" Guides in the top menu bar. From there, look for the link "Configure Virtual Hosts" which will lead you to the localhost page "http://localhost/dashboard/docs/configure-vhosts.html"
In a nutshell, the process involves editing the "httpd-vhosts.conf" file (typically in C:\XAMPP\apache\conf\extra) and replacing the contents of that file with something like this:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/"
ServerName localhost
</VirtualHost>
# vhosts - note sample entry from XAMPP how-to throws an error, so try this:
<VirtualHost *:80>
DocumentRoot "C:/Users/jdoe/Documents/dev.mysite.com/htdocs"
ServerName mysite.local
<Directory "C:/Users/jdoe/Documents/dev.mysite.com/htdocs">
Require all granted
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Additional vhosts (including SSL hosts) can be had by cloning the entry, and modifying DocumentRoot and ServerName directives and port numbers (e.g. 443 for TLS (SSL)). You can find tutorials on the web for creating and signing your own certificate, if you want to go that route.
The final step is to get your Windows machine to point your browser to the Apache host for your virtual domain (e.g. above, http://mysite.local). Using a text editor (Notebook will do) as administrator append the following entry onto your hosts file, which lives here:
C:\Windows\System32\drivers\etc\hosts
Append this entry to the hosts file:
127.0.0.1 mysite.local
IMPORTANT - you must restart your Windows machine or the new host will not respond. Some documentations will tell you just to restart the browser and Apache server, but I've found that's not sufficient.
IME, the hosts system and Apache directives can be particular, so be patient. You may need to rebuild configs, restart Apache, and restart your machine more than once.

Categories