PHP Manage/Add Virtualhost - php

Is there a free PHP script that allows you to easily add a virtualhost?
I need to do this, because I'll be opening a free forum host, and want to allow users to use their own domain, aside from a free subdomain.

Restarting the server whenever you add a new domain to it could be problematic. If there was ever a problem writing the file, you lose all the sites, and if there is data cached (I'm thinking mainly about APC), you also lose the usefulness of the cache.
Having the default virtual host handle the new domains, and then having the PHP code acting according to $_SERVER['HTTP_HOST'] would be a more robust solution.

Easy way is to wildcard all domains to a set directory then there use mod rewrite to map the specific domains to specific directories or in the case of say a MU wordpress install you can use the domain to map to a user account.
you can edit the .htaccess file on the fly without restarting apache.

couldnt php create a vhost file which is included in the main vhost handle this?
using cron to determine weather there has been updates to the attached vhost you could reload apache

Related

Dynamically changing apache proxypass config (scripting httpd.conf?)

Background on the scenario:
Our CI system deploys multiple VM's, each with unique (though temporary) IPs. I have an apache proxy set up on the front to provide clean URL's to these VM's so that the various users don't have to remember IP's that can sometimes switch multiple times a day.
The problem is... I don't want to keep having to manually edit my httpd.conf file to change the IP's that the url's map to.
I know I could use PHP to edit the httpd.conf file, and probably restart apache. This seems like an anti-pattern to me. What is the better way to do this? Can Apache read the proxypass settings from a seperate file?

How can I load files from httpdocs on the https side of the website

On my webserver. I have two directories which are publicly accessible. One of those is httpdocs/ and the other is httpsdocs/. You can probably guess where they're for.
The problem occurs when I wanted to create a CMS for myself. I want it to load the page with HTTPS for security reasons, but the problem is that my files are inside the httpdocs directory. I can move only the files for the system to the httpsdocs, but my next thought was what if I want a login on the normal website for people to login and view hidden content for not registered users?
Now my question is: Is there a way to combine those to directories so all the files will be inside the httpdocs and I can access them with the security HTTPS provides?
Things that you may need to know:
The webserver runs php 5.2.9
I only have FTP access to the webserver and to these directories.
The server runs apache, so .htaccess is possible.
There are a ton of avenues, but most are blocked if you can't alter the httpd.conf or certain restrictions are in place.
Your best bet would probably to simply abandon http: and just stick to https: and create a redirecting .htaccess in the http: folder. That way everything runs on a single directory.
Unless there is a drawback that would not allow your users to use HTTPS over HTTP.
Getting your ISP to make the https folder a symlink to the http folder would be the easiest solution here.
It sounds like you are likely using a hosting service that has things seperated. If this is the case then the answer is no, you can't combine these two directories into one. You can ask your provider if they can make that arrangement, but it would require changes to the apache configuration that you can't do.
That being said, barring the ability to have that configuration modified, the only other answer you are going to find is to move everything under the httpsdocs directory. Unless you can get another account setup or possibly if they offer the ability to setup subdomains with HTTPS connections. That would be an alternative solution.

How to Restrict PHP's File Access to DOCUMENT_ROOT

Is it possible to restrict PHP's file access to its document root? Basically on my work's server we have our domains in a file structure like:
/home/something/domains/domain1/
/home/something/domains/domain2/
/home/something/domains/domain3/
Right now, any script on domain1 has read and write access to anything in /home/something/domains/ including all of our other domains. I would like to restrict file access for a script in domain1 to only that domain.
The server is hosted with mediatemple and it is their grid service, so I don't have root access or even vhost config access. We can change php.ini, and i know it offers open_basedir, but it doesn't sound like that solves my problem as I could only restrict file access to /domians/ and not the individual domains. Any help appreciated.
What I'm really trying to do:
This server was recently hacked, and the hackers were overwriting domains/.htaccess which affected all our sites. We have tons of sites and many of them have lots of lines of bad code. They uploaded WSO, a hacking backdoor/shell which gave them full access to everything. I don't know how they got access, I guess it was either from the timthumb exploit, one of the millions of lines of bad code, or they just got our FTP password somehow. We updated old timthumbs, changed the password and removed all the bad files we found, but since there is a decent chance whatever exploit they found is still on the server, or that we missed some backdoor, I would at least like to limit their access to the actual domain that contains the exploit or unfound backdoor.
My initial thought was to set open_basedir for each of the virtual hosts (even if you have to ask your host admin to do it for you), but I am doubtful that will even work because I am fairly certain external/shell commands that run in PHP/scripts will still work on directories outside of the designated tree.
After more consideration, the closest way to configure your setup and get what you want, that I could think of, would be to set up chroot-jailed user accounts for each vhost and have your webserver use those user accounts through a mechanism like the Apache 2 MPM ITK, which I can only assume your hosting provider will have trouble setting up.

Drupal: best practice to switch site domain after the project is over

Im running a Drupal Multisite environment on a lamp (debian, apache, php, mysql) server.
My current workflow for a new project is:
Activate a subdomain on my company site (let's say the project will be foo.com, i activate foo.example.com)
Becose of plesk (that's a bit drupal unfriendly) my step are:
Create the new subdomain in plesk, php running in fastcgi mode
Create the database
Delete httpdocs folder, recreate it and make the sites folder
Make a simbolic link to every file into /var/www/vhosts/drupal folder (drupal code base) excapt of sites and tmp folders, robots.txt and .htacces files (that usually will be customized per domain)
Make the folder foo.example.com inside sites, symbolic link to /var/www/vhosts/drupal/sites/all folder, and then back a symbolic link from sites/foo.com to /var/www/vhosts/drupal/sites/foo.example.com (in order to make drush see all the domains to some multisite tasks, like crontab)
Install, configure drupal in the subdomain, doing everythings needed to complete the project
At this point, i need to switch the project from the subdomain to the final domain, foo.com, that will reside o my server too.
I dont like much some kind of other hacks like domain redirects, i want the final project to be a 'clean' (for what clean mean in a multisite environment).
Actually i do those steps:
Creating the domain (via plesk) and database;
Copy the subdomain httpdocs to the full domain httpdocs (cp -a /var/www/vhosts/example.com/subdomains/foo/httpdocs /var/www/vhosts/foo.com/httpdocs)
Change owner and group of httpdocs to match the domain's owner and group
Login into drupal, change webiste info, and filesystem paths.
This let me do what i want, but i have some trouble with node's html contents; espacially for those that contain links, images, and files, that (even if IMCE is configurated to use relative path), will point to /sites/foo.example.com/files/[...] instead of /sites/foo.com/files/[...].. this happen for cck filefields too.
As i said, i need a clean solution, i know i could make a symbolic link from /sites/foo.example.com to /sites/foo.com but i really dont like this way.
So far, i tryed to search/replace all the occourrence into the database sql dump:
sed 's/foo.example.com/foo.com/g' dump.sql > foo.com.sql
But this seem to brake somethings; I get many errors in boostrap file (in the cache unserialize step, even if i cleared all the cache before and after the db export/import) and looks like some utf-8 encoding get broken.
I know the best deal will be to set the development area on a local server, but i need my customer to reach it from outside.. and our adsl situation (static IP and so on) is a bit wired.
Any idea?
p.s: sorry for my bad english, if my question is not clear enought let me know and i'll provide more details)
Your steps are basically what I do, but I am not using Plesk or doing a multisite installation. I just create a new vhost in a VPS instance, and cp -a the docroot, and reconfigure for a new database DSN.
Regarding the SQL problems, I typically edit the SQL dump in a UTF-8 clean editor to prevent that problem. On Windows, I use Notepad++.
I then restore the database, clean all caches, edit settings, and run cron. I will also typically rebuild the search index.
This sequence normally works.
The bootstrap problem likely has to do with the variables table. When you serialize a string (or something with an string), the length gets stored, too. I will do the search and replace, then open up MySQL Query Browser, and search for rows in variable table with the domain name, and fix the lengths by hand. The other option is to determine which option is being stored, and then recreate the setting.

Dynamic apache setting using PHP

starting new project where users can register and system will automatically install cms system for users registered domain and need to solve dynamic setting of the server (apache).
Registration information and info about the associations between domains and actual paths to the cms installation on the server will be stored in Mysql database.
Is there an easy way to configure apache to connect for all unknown domains to a specific php script, which will look into the database and provide the actual path to the relevant cms - apache will than use this info to correctly handle the request?
I think, that "easier" solution might be to use PHP to write the domains/paths/config to a file and force apache to use this file to handle requests - however as I expect, that the number of the domains might be higher and case that some domain will be deleted will not be rare - the file might become full of unwanted rules soon and hard to optimize, also apache restart would be needed in order to use changed file etc..therefore the question about dynamic solution - that might be much easier to manage (for me and for the admin system itself).
Yes - use a wildcard vhost in apache, and mod_rewrite to direct all URLs to your front controller (or use the 404 document handler).
C.

Categories