apache serve files based on domain name - php

Lets say, i have 3 domain names
domain1.com
domain2.com
domain3.com
All # records currently go to my server, resolving to root/index.php.
Currently, i redirect each domain to different sub-folders with header redirects within index.php. So each url looks like
http://domain1.com/new_root1/index.php
http://domain2.com/new_root2/index.php
http://domain3.com/new_root3/index.php
Not very pretty.. but it works.
I am trying to configure my server to redirect with httpd.conf. What i would like is have domain1 and domain2 resolve:
http://domain[*].com/index.php
on local ip 192.168.0.101:80
and domain3 resolve:http://domain3.com/index.phpon local ip 192.168.0.100:1979
There is plenty of documentation, tutorials,etc, but nothing seems to fit this..
I have looked at .htacess. Preferred comments are to use httpd.conf. Searched Apache docs. All attempts fail.
Where do i look? Tx in advance

With basic name based vhost configuration all of the domains will use port 80 and work as expected. You can set httpd.conf to load all individual .conf files in a particular folder and then add individual .conf files to that folder, each containing 1 vhost definition.
Near the bottom of my apache2.conf I added:
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
# Include all my projects vhost conf files:
Include /vagrant/vhosts/*.conf
And in the folder I specified for loading my project vhost conf files I add a bunch of .conf files that look like this:
<VirtualHost *:80>
ServerName www.demo.vm
ServerAlias demo.vm
DocumentRoot /vagrant/htdocs/demo.vm/public
<Directory /vagrant/htdocs/demo.vm/public>
Require all granted
Options Indexes FollowSymLinks
</Directory>
SetEnv APPLICATION_ENV "development"
</VirtualHost>
If this is a local server you would have to edit your operating system hosts file to point your custom domain to the localhost.

Related

Apache2 - Set Document Root based on subdirectory

I have a default Virtual Host. It is configured to watch Document Root inside /var/www/html and works great. But I have a problem. I have multiple sites inside subdirectories and I don't know how to set Document Root for each of them. For example, /var/www/html/test, /var/www/html/test2, etc. When I include file from /var/www/html/test, it searches inside var/www/html. For example, I include file like include_once '/core.php', but I have an error Failed opening required '/var/www/html/core.php', because there is no current file here. Is it possible to set Document Root for each of directories inside main Virtual Host? Thank you!
My main Virtual Host configuration:
<VirtualHost *:80>
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
AccessFileName .htaccess
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Also, you can say, that I need to use relative paths, but it isn't possible in some ways, because, sometimes, I work with ready sites, created by another people.
Is it possible to set Document Root for each of directories inside
main Virtual Host?
Short answer is no, you cannot. 1 Document Root per Virtual Host. In order to understand how the things are working you need to understand what is the directives context. Lets take as an example VirtualHost directive. As we can see from the docs it has only 1 Context which is Server Config. And server config context tells next:
directive may be used in the server
configuration files (e.g., httpd.conf), but not within any
VirtualHost or Directory containers. It is not allowed in
.htaccess files at all.
But DoocumentRoot has two contexts: server config and virtual host. Rest you should figure out for yourself ;)
There are plenty ways how you can achieve desired result
Running several name-based web sites on a single IP address
Running different sites on different ports
Mixed port-based and ip-based virtual hosts
Hope that this answer will help you!

Apache control in multi site server configuration

I have an ubunto vm running a lamp stack, and a for now, one domain name redirecting to the vm's ip address.
Concept wise the main principle is I want users to register an account and get a "website" running over on a subdomain of my own domain. That part is easy with wildcard subdomains in apache vhosts etc and with certbot i managed to automatically have all subdomains protected under ssl.
Now, if a customer wants to buy his/her own domain name, with me or some other registrar they need to point an A record to my ip address and a CNAME from www to the domain name. And in my end I need to add a vhost file under sites-available folder configuring the virtual host file for that new domain name and restarting "gracefully" apache.
Here lies the problem. How can I manipulate apache vhosts files etc so I can accomplish this at the push of a button on my main website? I'm using PHP in my backend and doing it in php (shell exec etc) seems like a security risk..
Im running on GCP so any of GCP's services are available.
Thanks in advance.
If you are worried about a shell script in your server you can run an application using SSH from a different machine; It can be a small instance that an application can be something like a putty which is available only on windows. You should find something relevant for Linux as there are many alternatives available. Use SSH client application and Python language to automate it.
As the manual process involves running the same command multiple times you can create virtual host files and see if files are created in sites-available directory etc.
Once the new users register for your service you need to create virtual host files and run chown commands and see if the files are created in the sites from the directory and restart your apache. You can refer to the steps in Automating virtual host setup on your server.
Answering my own question:
Enabled mod_macros in apache;
sudo a2enmod macro
Added a macro for creating a http :80 virtual host and another for creating a https :443 virtual host with the necessary variables;
<Macro ProtectedVHost $domain>
<VirtualHost *:80>
ServerAdmin admin#email.com
ServerName $domain
ServerAlias www.$domain
DocumentRoot /var/www/customer_sites/$domain/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName $domain
ServerAlias www.$domain
ServerAdmin admin#email.com
DocumentRoot /var/www/customer_sites/$domain/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/$domain/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/$domain/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
</Macro>
Created a conf file out of my public_html folder on my main websites editable in php where i add records using my own macro as the user adds in his domain:
Use {Macro Name} {Domain Name}
Import that file into apache.conf
Finally i created a CRON that starts ever so often and reads that file and compares it to a saved version. If the files has changed, it will gracefully restar apache which will read and re-create the necessary virtual hosts.
Hope this helps anybody looking to do the same thing

How to setup virtual host using Wamp Server properly? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 months ago.
Improve this question
I need your help here in creating a virtual host in wampserver. In office I can create a virtual hosts but when I try to create in my laptop it doesn't work. I still can't figure out what's wrong. Here's what I did.
I copy the wordpress file in this folder. This will be the path of my project
E:\Subversion\chelle.wpblog.local
I edit the host file
C:\Windows\System32\drivers\etc\hosts
I added it to the end of file
127.0.0.1 chelle.wpblog.local
Next is I enable the virtual host in Apache
C:\wamp\bin\apache\Apache2.4.4\conf\httpd.conf
I uncomment this
Include conf/extra/httpd-vhosts.conf
Next is I setup the virtual host in WAMP
C:\wamp\bin\apache\Apache2.4.4\conf\extra\httpd-vhosts.conf
I add this at the bottom
<VirtualHost *:80>
ServerName chelle.wpblog.local
ServerAlias chelle.wpblog.local
DocumentRoot "E:/Subversion/chelle.wpblog.local/"
<Directory "E:/Subversion/chelle.wpblog.local/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Last is restart wampserver and open the chelle.wpblog.local in the browser. And it doesn't display. It display only google search results.
I was installing the zend framework on my local wamp using apache server.
First go and decide what will be your domain name for the local url.
Ex->www.test_zend_tutorial.com
then go and open the file located at "C:\WINDOWS\system32\drivers\etc"
hosts
write
127.0.0.1 (use one tab space) www.test_zend_tutorial.com
then go to the folder located at
D:\wamp\bin\apache\Apache2.2.17 (whatever is your version) \conf\
and open the file
httpd.conf
and search for text
Include conf/extra/httpd-vhosts.conf
and uncomment it by removing the # tag from the start of the line.Save the file and close it.
Now go to another folder located at
D:\wamp\bin\apache\Apache2.2.17\conf\extra
and open the file
httpd-vhosts.conf
and paste the code below at the last in this file
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot "D:\wamp\www"
ServerName localhost
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerName www.test_zend_tutorial.com
DocumentRoot "D:\wamp\www\(your project folder name)\public"
SetEnv APPLICATION_ENV "development"
<directory "D:\wamp\www\(your project folder name)\public">
DirectoryIndex index.php
AllowOverride all
Order Allow,Deny
Allow from all
</directory>
</VirtualHost>
and restart the wamp, now write the www.test_zend_tutorial.com in the browser and you will see the things working.
If when you use the new domain in the browser it goes to a google search or something like that then the change to your hosts file has not been recognised.
You have to either reboot after changing the hosts file or more simply run these 2 commands from a command windows ( as administrator )
net stop dnscache
net start dnscache
This will refresh windows DNS cache and pick up your hosts file changes.
It is also a good idea to ping your new domain to check it is being seen, use
ping chelle.wpblog.local
If it reports 127.0.0.1 as the ip address and 4 send and 4 receieved packet then the domain is now known to Windows, if it reports some other ip address the hosts file change has not succeeded.
Also it is a good idea when creating VHOSTS to also create one for localhost or the wamp home page will not work.
You also have old Apache 2.2 syntax (Order allow,deny allow from all) you could try using the Apache 2.4 syntax, see below
So try this
## must be first so the the wamp menu page loads
<VirtualHost *:80>
ServerAdmin webmaster#mail.net
DocumentRoot "D:/wamp/www"
ServerName localhost
<Directory "D:/wamp/www">
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#mail.net
ServerName chelle.wpblog.local
ServerAlias www.chelle.wpblog.local
DocumentRoot "E:/Subversion/chelle.wpblog.local"
<Directory "E:/Subversion/chelle.wpblog.local">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
In WampServer 3.0.4 you do not need to run through all those setting up.
Go to local host and under Tools area for "Add a Vertual Host" in the first time it will say turn on "Vertual Host sub menu" in wamp server settings.
To get there right click on wamp icon > go to wamp server settings > and click on Vertual Host sub menu. Then re-fresh the page and follow the setup process, you just need to provide the server name and path. It will do all the works for you.
Simple as that
Method 1:
in hosts file
127.0.0.1 mysite
::1 mysite
in C:\wamp64\bin\apache\apache2.4.23\conf\extra\httpd-vosts.conf file add your virtual host as follows
<VirtualHost *:80>
ServerName mysite
DocumentRoot "<path_to_your_local_site>"
<Directory "<path_to_your_local_site>">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
Make sure vhost configuration is enable in httpd.conf file is enabled as follows
C:\wamp64\bin\apache\apache2.4.23\conf\httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
After you have changed configuration as above, restart all service in wamp and load website as follows
http://mysite
your local site should open.
Method 2:
In latest versions of wamp, you can do this configuration via webpage.
Open localhost in your browser. You would see wamp welcome page.
Down below under Tools, there is option to add virtual host. click on the link which would take you to add virtual host page.
In Name of the host, enter your site name eg: mysite
Under Complete absolute path of the VirtualHost folder, enter the path of your site.
Save the config and restart all services. you should be able to open mysite with the link http://mysite
As pointed in another answer, in newer versions of wam
The answers above are overcomplicating the problem somewhat, and in fact the question is including extra information that's misleading the responders.
The clues lie in step 2 and the symptoms described in 5. The hosts file is set up but when the request is made to "blah.local", google responds. Apache is not involved.
The first step to diagnosis would be pinging blah.local and seeing that it maps correctly to 127.0.0.1. I have never (in many years as a developer) found it necessary to restart local DNS on either Windows or Linux/Mac so I would expect this to work out of the box. But if it doesn't, of course restart DNS and see that it does.
However, late versions of Chrome at least do not recognize the TLD '.local' and so when you put the URL back in the browser, it's passed on to Google as a search term. There may be a setting in your browser to tell it to handle the unsanctioned TLD, but your simplest solution would be to use a TLD that's sanctioned by W3C like '.com'.
In short, if your hosts file reads something like
127.0.0.1 local.chelle.wpblog.com
rather than
127.0.0.1 chelle.wpblog.local
everything should work.
Create the folder for your local website
Go to localhost (browser)
Click Add a Virtual Host button
Add the name of the virtual host
Add the complete absolute path
Restart DNS

How can I have a Django application on my main domain, and a PHP application on a subdomain?

I have a Django application hosted on my main domain (example.com), and I now need to host a PHP application on a subdomain (forum.example.com).
In the directory of the main domain, I have the following .htaccess entries:
SetHandler mod_python
PythonPath "/home/.../apps/example'] + sys.path"
PythonOption mod_python.importer.path "['/home/vlive/python']+ sys.path"
PythonHandler django.core.handlers.modpython
#PythonDebug On
SetEnv DJANGO_SETTINGS_MODULE example.settings
SetEnv PYTHON_EGG_CACHE /tmp/egg-cache
At present, when I load the subdomain (forum.example.com) I still see the main site (example.com).
How can I fix this?
You need to set up your subdomains via the VirtualHost directive and only add the mod_python / mod_wsgi handler in one of the VirtualHosts.
You said you loaded the subdomain and it still shows the main site. Would you mind showing us your Apache's site configuration?
The most common reason for wrong VirtualHost being selected is a missing NameVirtualHost directive that matches host/port qualifier specified in VirtualHost directive.
What NameVirtualHost directive do you have set? What is the argument to the two VirtualHost configurations?
The general layout would be:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName example.com
...
</VirtualHost>
<VirtualHost *:80>
ServerName forum.example.com
...
</VirtualHost>
These would usually be in the same file for a Linux Apache distribution though.
What have you used?
As Graham noted, most probably you have wrong VirtualHost configuration.
Double check that there is two different VH with right ServerName and that you don't use * (wildcard) in ServerName and ServerAlias that may overlap with subdomain or it goes AFTER subdomain.
Apache only search for first VH that matches, so if you have something like *.example.com above any other subdomain like forum.example.com would not work.
Also mod_wsgi is recommended by django documentation, consider switching to it.

Confused with Zend Server Configuration

I have installed Zend server and developed a sample application, and iam surprised when my application url could not open in the browser...
This is my url, related to my zend application:
http://localhost/app_name/public/controller/action/parameter
I got an error like: "The requested URL /app_name/public/controller/action/parameter was not found on this server".
When i read the quickstart guide from here, i learnt that i had to define a VirtualHost directive inside my httpd.conf file of Apache directory.
But to my surprise, i found the following lines already existing in my httpd.conf file:
DocumentRoot "C:\zend\Apache2/htdocs"
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
Could not understand what to do...
First of all , your document root was wrong.You are using forward and backward slash at the same time and accessing the localhost will take you to the htdocs not the virtual host.
Please follow these steps to create a virtual host or check your setting with these
Create a virtual hosts
<VirtualHost *:80>
ServerName cert.local
DocumentRoot "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/cert/public"
<Directory "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/cert/public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Register with your operating system (for windows)
Go to c:\WINDOWS\system32\drivers\etc and add the following line
127.0.0.1 cert.local
Restart the apache server and in order to access the virtual host go to http://cert.local/
If you want to access the htdocs and zend at the same time then you have to create another virtual host pointing to the htdocs.
Here is some basic about Virtual hosting
http://httpd.apache.org/docs/2.2/vhosts/
Hope this will help..:)
Got it... In the VirtualHost definition mentioned in the quickstart guide in the above link, the DocumentRoot and Directory were configured to the quickstart application, and if the document root was mentioned only till "htdocs" by default (not till application name/public), the zend format url doesn't work.
As i have many zend applications in my htdocs, i had to make the paths of DocumentRoot and Directory till "htdocs" itself, as it will vary in future for each application. So i added the following lines in my httpd.conf file, to make my app url work. Now i can run any zend application without modifying the httpd.conf file.
(and i also had to tell to the client to add these lines in his httpd.conf file for testing the application :)
<VirtualHost *:80>
<Directory C:\zend\Apache2\htdocs\>
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
Hope it helps someone.

Categories