Welcome Guest, Not a member yet? Register   Sign In
Automatic/Push Button site folder/vhost creation in Linux via CI/PHP
#1

[eluser]Devon Lambert[/eluser]
I realize that what I propose here may seem a little far fetched, but please hear me out.

I am hoping that someone out there knows a way to create a One Push button scenario whereby a site is created from within a CI/PHP created backend, with appropriate info being sent to your Database, meanwhile, on the server side of things (assuming Apache), your folder structure is automatically put into place, and then the final, and most difficult for me, the appropriate vhost addition is made to your Servers host file.

I've been able to tackle the vast majority of this, all except the vhost addition. The whole scenario MUST be possible as Cpanel has the ability to do this for you, therefore I believe that there must be some script that is able to replicate Cpanel's capabilities.

The reason for this is that I am creating a multi - site dashboard that runs on CI. I would love to not have to leave this dashboard, drop to the command line to create a new addition to my Host file every time I want to add a new site in the backend.

So... any takers? :-)

** UPDATE **

Think I may have found what I'm looking for:

http://httpd.apache.org/docs/2.0/vhosts/mass.html

May be useful to some one else as well.
#2

[eluser]n0xie[/eluser]
You can just write a bash script, then call that from within PHP. This is my addsite.sh I call when starting a new project which takes care of vhost and hosts file:

Code:
#!/bin/bash
CMDLN_ARGS="$@" # Command line arguments for this script
export CMDLN_ARGS

# Run this script as root if not already.
chk_root () {

if [ ! $( id -u ) -eq 0 ]; then
   echo "Please enter your password."
   exec sudo su -c "${0} ${CMDLN_ARGS}" # Call this prog as root
   exit ${?}  # sice we're 'execing' above, we wont reach this exit
              # unless something goes wrong.
fi

}
chk_root
FILE=$(cat /etc/hosts)
echo $FILE |grep -o "test$";

if [ `echo $FILE |grep -o "\$1$"` ];
then
  echo "/etc/hosts file is ok"
else
  echo "127.0.0.1         $1" >> /etc/hosts
fi
cp /etc/apache2/sites-available/template.conf /etc/apache2/sites-available/$1
cat /etc/apache2/sites-available/$1 | sed "s/site/$1/g" |cat > /etc/apache2/sites-available/$1
ln -s /etc/apache2/sites-available/$1 /etc/apache2/sites-enabled/$1
/etc/init.d/apache2 restart
echo "vhost creation done"
#3

[eluser]Devon Lambert[/eluser]
[quote author="n0xie" date="1274364339"]You can just write a bash script, then call that from within PHP. This is my addsite.sh I call when starting a new project which takes care of vhost and hosts file[/quote]

Hey Noxie,

Does this code actually make an addition to the existing vhost/host files OR does it create a new vhost/host file for each site?

Pardon my stupidity on this one, I only ask because I do not see where the actual site/domain is being defined?

i.e. How do you tell the script that your adding www.mygreatnewsite.com to the file and also how does it know that this new site is supposed to reference the same ip address as every other site already on file?

Thanks again for this, I'm sure once I figure it out it will be immensely useful.
#4

[eluser]Devon Lambert[/eluser]
A php version of the following would be nice!

http://travisonrails.com/2009/08/10/ruby...host-entry
#5

[eluser]n0xie[/eluser]
[quote author="Devon Lambert" date="1274390710"][quote author="n0xie" date="1274364339"]You can just write a bash script, then call that from within PHP. This is my addsite.sh I call when starting a new project which takes care of vhost and hosts file[/quote]

Hey Noxie,

Does this code actually make an addition to the existing vhost/host files OR does it create a new vhost/host file for each site?
[/quote]
It creates a new vhost for every file.

Quote:i.e. How do you tell the script that your adding www.mygreatnewsite.com to the file
It accepts a parameter, which is the name of the site. In my case ./addsite www.mygreatnewsite.com, creates www.mygreatnewsite.com. It copies my template vhost file then replaces all instances of the word 'site' with the actual site name.

Quote:how does it know that this new site is supposed to reference the same ip address as every other site already on file?
I don't really understand the question: you tell your vhosts what url belongs to it and apache takes care of the rest. The ip part is usually done using dns.
#6

[eluser]Devon Lambert[/eluser]
Ok I think I understand.

I will have multiple sites that need to live on the same server, same ip, etc...

So I would not want a separate vhost file for each site. What I want is to update the current vhost file for each new site that is added.

I hope this makes sense, as I'm still trying to wrap my brain around this whole virtual host scripting idea.

Thanks again Noxie.
#7

[eluser]n0xie[/eluser]
[quote author="Devon Lambert" date="1274476439"]
I will have multiple sites that need to live on the same server, same ip, etc...

So I would not want a separate vhost file for each site.[/quote]
Why not?
#8

[eluser]Devon Lambert[/eluser]
I guess I never realized that you could use multiple vhost files?

Again, my knowledge of the server environment is touchy at best. I like to keep as much knowledge on hand to make my way around, but scripting, hosts/vhosts, etc... are definitely not my strong points.
#9

[eluser]Kamarg[/eluser]
[quote author="Devon Lambert" date="1274391841"]A php version of the following would be nice!

http://travisonrails.com/2009/08/10/ruby...host-entry[/quote]

Untested but a quick attempt at translation without much error checking.
Code:
<?php
/*
########################################
##### VARIABLES YOU NEED TO CHANGE #####
########################################

host_dir  = '/etc/hosts'            # path to your hosts file
sites_dir = '/Library/Webserver'    # path to the directory where you keep your sites (NO TRAILING SLASH!!!)
conf_dir  = '/etc/apache2/sites'    # path to directory where named conf files live
name      = ''                        # the folder that contains the site
hostname  = ''                        # an optional local domain

########################################
*/

$host_dir     = null;
$sites_dir    = null;
$conf_dir     = null;
$name         = null;
$hostname     = null;

$conf_contents <<< EOF
<VirtualHost *:80>
  ServerName $hostname

  DocumentRoot "$sites_dir/$name"
    DirectoryIndex index.php
    <Directory "$sites_dir/$name">
        Options FollowSymLinks MultiViews Includes
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
EOF;

// first things first: make sure named conf file doesn't exist already
if(file_exists("$conf_dir/$name.conf")) {
  return FALSE;
}

// check to make sure host file exists
if(($host_dir)) {
    if(file_put_contents($host_dir, "127.0.0.1\$hostname") === FALSE) {
        return FALSE;
    }
    
    if(file_put_contents($conf_dir . '/' . $name . '.conf', $conf_contents) === FALSE) {
        return FALSE;
    }
}

Edit: Noticed I still had Ruby comments instead of PHP comments.
#10

[eluser]Devon Lambert[/eluser]
[quote author="Kamarg" date="1274497364"][quote author="Devon Lambert" date="1274391841"]A php version of the following would be nice!

http://travisonrails.com/2009/08/10/ruby...host-entry[/quote]

Untested but a quick attempt at translation without much error checking.
[/quote]

Gotta love the CI community!

You guys are the shiznit. Which basically means that you rock. :-)

Thanks to both Noxie and Kamarg.

I have enough to go on here guys. I'll try to get this implemented over the weekend, wish me luck.




Theme © iAndrew 2016 - Forum software by © MyBB