Jump to content

A couple tools to simplify vhosts


jonsjava

Recommended Posts

I got bored recently, so I created a couple tools:

 

c.jonsjava.com (creates vhosts on CentOS/RHEL/Fedora servers)

u.jonsjava.com (creates vhosts on Debian/Ubuntu servers)

 

How it works

 

from the command line, run the following

 

curl http://example.com.c.jonsjava.com | bash

or

curl http://example.com.u.jonsjava.com | bash

 

When you do this, it will generate a shell script to create all the folders and files needed to run another vhost on your server.

 

Here's the output of the first command

$ curl http://example.com.c.jonsjava.com

#!/bin/bash
mkdir -p /var/www/vhost/example.com && chown apache:apache /var/www/vhost/example.com
DATA="<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot var/www/vhosts/example.com
        <Directory var/www/vhosts/example.com>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
        </Directory>

        CustomLog /var/log/httpd/example.com-access.log combined
        ErrorLog /var/log/httpd/example.com-error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
</VirtualHost>



#<VirtualHost _default_:443>
#        ServerName example.com
#        DocumentRoot var/www/vhosts/example.com
#        <Directory var/www/vhosts/example.com>
#                Options Indexes FollowSymLinks MultiViews
#                AllowOverride All
#        </Directory>
#
#        CustomLog /var/log/httpd/example.com-ssl-access.log combined
#        ErrorLog /var/log/httpd/example.com-ssl-error.log
#
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
#        LogLevel warn
#
#        SSLEngine on
#        SSLCertificateFile    /etc/pki/tls/certs/localhost.crt
#        SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
#
#        <FilesMatch \"\.(cgi|shtml|phtml|php)$\">
#                SSLOptions +StdEnvVars
#        </FilesMatch>
#
#        BrowserMatch \"MSIE [2-6]\" \
#                nokeepalive ssl-unclean-shutdown \
#                downgrade-1.0 force-response-1.0
#        BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
#</VirtualHost>"
echo "$DATA" > /etc/httpd/vhosts/example.com.conf && service httpd restart

 

And for ubuntu:

 

$ curl http://example.com.u.jonsjava.com

#!/bin/bash
mkdir -p /var/virtalwww/example.com && chown www-data:www-data /var/virtualwww/example.com
DATA="<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/virtualwww/example.com
ServerName example.com
ServerAlias www.example.com
<Directory />
	Options FollowSymLinks
	AllowOverride None
</Directory>
<Directory /var/virtualwww/example.com>
	Options Indexes FollowSymLinks MultiViews
	AllowOverride None
	Order allow,deny
	allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory \"/usr/lib/cgi-bin\">
	AllowOverride None
	Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
	Order allow,deny
	Allow from all
</Directory>

ErrorLog /var/log/apache2/example.com-error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/example.com-access.log combined

    Alias /doc/ \"/usr/share/doc/\"
    <Directory \"/usr/share/doc/\">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>"
echo "$DATA" > /etc/apache2/sites-available/example.com && ln -s /etc/apache/sites-available/example.com /etc/apache2/sites-enabled/example.com && /etc/init.d/apache2 restart

 

I built this script because I add vhosts all day every day on thousands of servers, and I got tired of sed'ing the config files all the time.

 

If you have ideas on how I can make this more useful, please let me know!

 

Oh, and for clarification, it is always DOMAIN.TLD.[c/u].jonsjava.com

Link to comment
https://forums.phpfreaks.com/topic/261244-a-couple-tools-to-simplify-vhosts/
Share on other sites

I failed to mention how to ensure that these work:

 

FOR CentOS/RHEL/Fedora:

ensure that the folder /etc/httpd/vhosts/ exists

ensure that the /etc/httpd/conf/httpd.conf has the following line in it somewhere

Include vhosts/*.conf

 

FOR Ubuntu/Debain:

make sure that www-data user is the user that runs apache2

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.