Skip to content

Instantly share code, notes, and snippets.

@shandanjay
Last active July 19, 2019 17:48
Show Gist options
  • Save shandanjay/a4224e14525ad4fe77a8 to your computer and use it in GitHub Desktop.
Save shandanjay/a4224e14525ad4fe77a8 to your computer and use it in GitHub Desktop.
Creating Virtual hosts in Apache under redhat linux systems (CentOS / Fedora)
#!/usr/bin/perl
#run as a super user
if (!@ARGV){
print 'usage example: #vhosts testing.localsitename.com /var/www/html/site_folder_name username' ."\n";
exit(0);
}
$url = $ARGV[0];
$url =~ s/\//\./g;
$file_name = '/etc/httpd/conf/vhosts/' . $url . '.conf';
system("touch ".$file_name);
open (FILE , ">$file_name") or die('Make sure you run this script as superuser');
$text = "
<VirtualHost *:80>
ServerName $ARGV[0]\n
DocumentRoot $ARGV[1]\n
ErrorLog logs/$ARGV[0]\n
<Directory $ARGV[1]>\n
Order Deny,Allow\n
AllowOverride all\n
allow from all\n
Options +Indexes\n
DirectoryIndex index.php index.html index.htm default.htm\n
</Directory>\n
</VirtualHost>";
print FILE $text;
close(FILE);
open(FILE, ">>/etc/hosts");
print FILE " \n 127.0.0.1 ". $ARGV[0] ." " . $ARGV[0];
close(FILE);
$username = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
system("chown -R $username:$username ".$ARGV[1]);
system("chcon -R -t httpd_user_script_exec_t ".$ARGV[1]." -u ". $ARGV[2] );
system("find $ARGV[1] -type d -print0 | xargs -0 chmod 755");
system("find $ARGV[1] -type f -print0 | xargs -0 chmod 644");
system("service httpd restart");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment