Archive

Archive for the ‘configuration’ Category

Apache virtual host script in perl

May 27th, 2010 Ivan Villareal 2 comments

This is a very short post about a little script I did to create remove virtual hosts easily.

I’ve found a couple of scripts (http://patrickgibson.com/utilities/virtualhost/, http://www.tamdenholm.com/portfolio/apache)  to do this but they didn’t worked right, I’m using Ubuntu Karmic, maybe thats why. However instead of fixing those scripts I went to my vim editor and started typing this, I’ve spent about 2 hours writing this while I was watching a crappy tv program, but in the end it worked, it surely contains some bugs, or it is unsafe to run it, I just wanted to share this.

#!/usr/bin/perl -w
use strict;
use File::Path qw(mkpath rmtree);
use Getopt::Long;
 
 
our $ipAddress        = '127.0.0.1';
our $apacheConfigFile = 'apache2.conf';
our $apacheConfigDir  = '/etc/apache2';
our $sitesAvailable   = 'sites-available';
 
our $docRootPrefix = '/home/ivan/www';
our $docRoot = 'public_html';
our $logsDir  = 'logs';
 
 
my $del = '';
my $add = '';
my $domain = '';
 
unless (GetOptions ('del' => \$del, 'add' => \$add, 'domain=s' => \$domain) or usage()) {
    usage();
}
 
#print $paramResults;
if ($add || $del) {
    if ($domain) {
        if ($add) {
            createVhost($domain);
        } elsif ($del) {
            deleteVhost($domain);
        }
    } else {
        usage();
    }
   #print 'add aqui';
} else {
    usage();
}
 
sub usage {
    print < <USAGE
This program will add or remove
apache virtual hosts on ubuntu karmic 9.10
 
usage: vhost-updater.pl [--add | --del] --domain newhost.tld 
USAGE
}
 
sub returnVhostPaths
{
    my $vhost = shift;
    my @dir = split(/\//, $docRootPrefix);
    my %res;
 
    push(@dir, $vhost);
 
    my $hostDir = join('/', @dir);
    $res{'docRoot'} = $hostDir . '/' . $docRoot;
    $res{'logsDir'} = $hostDir . '/' . $logsDir;
 
    #todo dir validation
    @dir = split(/\//, $apacheConfigDir);
    push(@dir, $sitesAvailable);
    push(@dir, $vhost);
    $res{'apacheConfig'} = join('/', @dir);
 
    return %res;   
}
 
sub createVhost {
    my $vhost = shift;
    #first create the docRoot
    my %vhostInfo = returnVhostPaths($vhost);
 
    informOut("Creating docroot dir: $vhostInfo{'docRoot'}");
    informOut("Creating log dir: $vhostInfo{'logsDir'}");
    mkpath($vhostInfo{'docRoot'}, $vhostInfo{'logsDir'});
 
 
    informOut("Site File: $vhostInfo{'apacheConfig'}");
 
    my $vhostContent = << "EOF";
<VirtualHost *:80>
    ServerName $vhost
    DocumentRoot $vhostInfo{'docRoot'}
    <directory $vhostInfo{'docRoot'}>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </directory>
        ErrorLog $vhostInfo{'logsDir'}/error_log
        CustomLog $vhostInfo{'logsDir'}/access_log "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-agent}i\\""
        LogLevel debug
 
 
EOF
    informOut("Creating Vhost...");
    open FILE, ">", $vhostInfo{'apacheConfig'} or die $!;
    print FILE $vhostContent;
    close FILE;
 
    informOut("Adding host $vhost");
    open FILE, ">>", '/etc/hosts' or die $!;
    print FILE $ipAddress ."\t". $vhost ."\n";
    close FILE;
 
    my $output = `/usr/sbin/a2ensite $vhost`;
    print $output;
 
    restartApache();
    #print $vhostConten t;
}
 
sub restartApache
{
    informOut("Restarting apache");
    my $output = `/etc/init.d/apache2 restart`;
    print $output; 
}
 
sub deleteVhost
{
    my $vhost = shift;
     my %vhostInfo = returnVhostPaths($vhost);
 
    informOut("Removing $vhost from hosts file");
    open IN, '< ', '/etc/hosts' or die $!;
    my @hostsFile = <IN>;
    close IN;
 
    my @contents = grep(!/^127.0.0.1\t$vhost/, @hostsFile);
 
    open FILE, ">", '/etc/hosts' or die $!;
    print FILE @contents;
    close FILE;
 
    my $output = `/usr/sbin/a2dissite $vhost`;
    print $output;
 
    informOut("Removing  $vhostInfo{'apacheConfig'} file");
    unlink($vhostInfo{'apacheConfig'});
 
    restartApache();
 
    print " manually remove $vhostInfo{'docRoot'}... \n";
 
}
 
sub informOut {
    my $message = shift;
    print "$message \n";
}

Here is a screenshot of the program running

vhost creator Screenshot

Categories: Linux, Perl, configuration Tags:

Upgrading php5.1 to php5.3 on CENTOS 4

April 22nd, 2010 Ivan Villareal 4 comments

I needed to update the php version of a centos 4 box, so this is what I did:

I’ve decided to go for the easy way, using the remi repo, so first I’ve Downloaded the Extra Packages for Enterprise Linux (EPEL)

[root@box php5.3]# wget http://download.fedora.redhat.com/pub/epel/4/i386/epel-release-4-9.noarch.rpm
--13:08:07--  http://download.fedora.redhat.com/pub/epel/4/i386/epel-release-4-9.noarch.rpm
           => `epel-release-4-9.noarch.rpm'
Resolving download.fedora.redhat.com... 209.132.183.67
Connecting to download.fedora.redhat.com|209.132.183.67|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13,686 (13K) [application/x-rpm]
 
100%[=================================================================================>] 13,686        --.--K/s            
 
13:08:08 (405.60 KB/s) - `epel-release-4-9.noarch.rpm' saved [13686/13686]

Then the remi repo:

[root@box php5.3]# wget http://rpms.famillecollet.com/enterprise/remi-release-4.rpm
--13:08:21--  http://rpms.famillecollet.com/enterprise/remi-release-4.rpm
           => `remi-release-4.rpm'
Resolving rpms.famillecollet.com... 88.191.60.189
Connecting to rpms.famillecollet.com|88.191.60.189|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5,120 (5.0K) [application/x-rpm]
 
100%[=================================================================================>] 5,120         32.45K/s            
 
13:08:22 (32.41 KB/s) - `remi-release-4.rpm' saved [5120/5120]

Then I’ve upgraded both repos:

[root@box php5.3]# rpm -Uvh remi-release-4*.rpm epel-release-4*.rpm
warning: remi-release-4.rpm: V3 DSA signature: NOKEY, key ID 00f97f56
warning: epel-release-4-9.noarch.rpm: V3 DSA signature: NOKEY, key ID 217521f6
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [ 50%]
   2:remi-release           ########################################### [100%]

and after that I downloaded the remi repo file for Yum:

[root@box php5.3]# cd /etc/yum.repos.d/
[root@box yum.repos.d]# wget http://rpms.famillecollet.com/remi-enterprise.repo
--13:09:28--  http://rpms.famillecollet.com/remi-enterprise.repo
           => `remi-enterprise.repo'
Resolving rpms.famillecollet.com... 88.191.60.189
Connecting to rpms.famillecollet.com|88.191.60.189|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 793 [text/plain]
 
100%[=================================================================================>] 793           --.--K/s            
 
13:09:28 (50.42 MB/s) - `remi-enterprise.repo' saved [793/793]

After that I just told yum to upgrade php:

[root@box yum.repos.d]# yum --enable remi update php
Repository remi is listed more than once in the configuration
Repository remi-test is listed more than once in the configuration
Setting up Update Process
Setting up repositories
epel                      100% |=========================| 3.4 kB    00:00    
remi                      100% |=========================| 2.6 kB    00:00    
Reading repository metadata in from local files
357f538e0bf37d10983cd3b22 100% |=========================| 732 kB    00:06    
epel      : ################################################## 2267/2267
e849a85ae6e8f1b8e5e71c08b 100% |=========================| 128 kB    00:03    
remi      : ################################################## 444/444
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for php to pack into transaction set.
php-5.3.2-1.el4.remi.x86_ 100% |=========================|  19 kB    00:00    
---> Package php.x86_64 0:5.3.2-1.el4.remi set to be updated
--> Running transaction check
--> Processing Dependency: php-common = 5.3.2-1.el4.remi for package: php
--> Processing Dependency: php-cli = 5.3.2-1.el4.remi for package: php
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for php-common to pack into transaction set.
php-common-5.3.2-1.el4.re 100% |=========================|  25 kB    00:01    
---> Package php-common.x86_64 0:5.3.2-1.el4.remi set to be updated
---> Downloading header for php-cli to pack into transaction set.
php-cli-5.3.2-1.el4.remi. 100% |=========================|  19 kB    00:01    
---> Package php-cli.x86_64 0:5.3.2-1.el4.remi set to be updated
--> Running transaction check
--> Processing Dependency: php-common = 5.1.6-3.el4s1.10 for package: php-mysql
--> Processing Dependency: php-common = 5.1.6-3.el4s1.10 for package: php-xml
--> Processing Dependency: php-common = 5.1.6-3.el4s1.10 for package: php-pdo
--> Processing Dependency: libedit.so.0()(64bit) for package: php-cli
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for php-xml to pack into transaction set.
php-xml-5.3.2-1.el4.remi. 100% |=========================|  20 kB    00:00    
---> Package php-xml.x86_64 0:5.3.2-1.el4.remi set to be updated
---> Downloading header for libedit to pack into transaction set.
libedit-20090923-3.0_1.el 100% |=========================| 5.8 kB    00:00    
---> Package libedit.x86_64 0:20090923-3.0_1.el4.rf set to be updated
---> Downloading header for php-pdo to pack into transaction set.
php-pdo-5.3.2-1.el4.remi. 100% |=========================|  19 kB    00:00    
---> Package php-pdo.x86_64 0:5.3.2-1.el4.remi set to be updated
---> Downloading header for php-mysql to pack into transaction set.
php-mysql-5.3.2-1.el4.rem 100% |=========================|  19 kB    00:00    
---> Package php-mysql.x86_64 0:5.3.2-1.el4.remi set to be updated
--> Running transaction check
--> Processing Dependency: libsqlite.so.0()(64bit) for package: php-pdo
--> Processing Dependency: libmysqlclient.so.16(libmysqlclient_16)(64bit) for package: php-mysql
--> Processing Dependency: libmysqlclient.so.16()(64bit) for package: php-mysql
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for sqlite2 to pack into transaction set.
sqlite2-2.8.17-2.el4.remi 100% |=========================| 5.2 kB    00:00    
---> Package sqlite2.x86_64 0:2.8.17-2.el4.remi set to be updated
---> Downloading header for mysql-libs to pack into transaction set.
mysql-libs-5.1.45-1.el4.r 100% |=========================|  24 kB    00:00    
---> Package mysql-libs.x86_64 0:5.1.45-1.el4.remi set to be updated
--> Running transaction check
--> Processing Dependency: libmysqlclient.so.15()(64bit) for package: perl-DBD-MySQL
--> Processing Dependency: libmysqlclient.so.15(libmysqlclient_15)(64bit) for package: mysql-server
--> Processing Dependency: libmysqlclient_r.so.15(libmysqlclient_15)(64bit) for package: mysql-server
--> Processing Dependency: libmysqlclient.so.15()(64bit) for package: mysql-server
--> Processing Dependency: libmysqlclient.so.15(libmysqlclient_15)(64bit) for package: perl-DBD-MySQL
--> Processing Dependency: mysql-libs = 5.0.82sp1-1.el4_8 for package: mysql
--> Processing Dependency: libmysqlclient.so.15(libmysqlclient_15)(64bit) for package: mysql
--> Processing Dependency: libmysqlclient.so.15()(64bit) for package: mysql
--> Processing Dependency: libmysqlclient_r.so.15()(64bit) for package: mysql-server
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for mysqlclient15 to pack into transaction set.
mysqlclient15-5.0.67-1.el 100% |=========================| 3.9 kB    00:00    
---> Package mysqlclient15.x86_64 0:5.0.67-1.el4.remi set to be updated
---> Package perl-DBD-mysql.x86_64 0:4.013-1.el4.rf set to be updated
---> Downloading header for mysql to pack into transaction set.
mysql-5.1.45-1.el4.remi.x 100% |=========================|  19 kB    00:00    
---> Package mysql.x86_64 0:5.1.45-1.el4.remi set to be updated
---> Downloading header for mysql-server to pack into transaction set.
mysql-server-5.1.45-1.el4 100% |=========================|  27 kB    00:00    
---> Package mysql-server.x86_64 0:5.1.45-1.el4.remi set to be updated
--> Running transaction check
--> Processing Dependency: libmysqlclient.so.14(libmysqlclient_14)(64bit) for package: perl-DBD-mysql
--> Processing Dependency: libmysqlclient.so.14()(64bit) for package: perl-DBD-mysql
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for mysqlclient14 to pack into transaction set.
mysqlclient14-4.1.22-1.el 100% |=========================| 5.5 kB    00:00    
---> Package mysqlclient14.x86_64 0:4.1.22-1.el4.remi set to be updated
--> Running transaction check
 
Dependencies Resolved
 
=============================================================================
Package                 Arch       Version          Repository        Size
=============================================================================
Installing:
perl-DBD-mysql          x86_64     4.013-1.el4.rf   rpmforge          228 k
     replacing  perl-DBD-MySQL.x86_64 4.008-1.el4s1
 
Updating:
php                     x86_64     5.3.2-1.el4.remi  remi              1.4 M
Installing for dependencies:
libedit                 x86_64     20090923-3.0_1.el4.rf  rpmforge          265 k
mysqlclient14           x86_64     4.1.22-1.el4.remi  remi              1.2 M
mysqlclient15           x86_64     5.0.67-1.el4.remi  remi              1.3 M
sqlite2                 x86_64     2.8.17-2.el4.remi  remi              161 k
Updating for dependencies:
mysql                   x86_64     5.1.45-1.el4.remi  remi              1.0 M
mysql-libs              x86_64     5.1.45-1.el4.remi  remi              1.7 M
mysql-server            x86_64     5.1.45-1.el4.remi  remi               11 M
php-cli                 x86_64     5.3.2-1.el4.remi  remi              2.6 M
php-common              x86_64     5.3.2-1.el4.remi  remi              963 k
php-mysql               x86_64     5.3.2-1.el4.remi  remi              155 k
php-pdo                 x86_64     5.3.2-1.el4.remi  remi              177 k
php-xml                 x86_64     5.3.2-1.el4.remi  remi              232 k
 
Transaction Summary
=============================================================================
Install      5 Package(s)        
Update       9 Package(s)        
Remove       0 Package(s)        
Total download size: 22 M
Is this ok [y/N]: y
Downloading Packages:
(1/14): php-xml-5.3.2-1.e 100% |=========================| 232 kB    00:04    
(2/14): libedit-20090923- 100% |=========================| 265 kB    00:04    
(3/14): php-common-5.3.2- 100% |=========================| 963 kB    00:15    
(4/14): mysqlclient15-5.0 100% |=========================| 1.3 MB    00:23    
(5/14): php-cli-5.3.2-1.e 100% |=========================| 2.6 MB    00:35    
(6/14): perl-DBD-mysql-4. 100% |=========================| 228 kB    00:04    
(7/14): php-pdo-5.3.2-1.e 100% |=========================| 177 kB    00:05    
(8/14): mysql-5.1.45-1.el 100% |=========================| 1.0 MB    00:15    
(9/14): sqlite2-2.8.17-2. 100% |=========================| 161 kB    00:05    
(10/14): mysqlclient14-4. 100% |=========================| 1.2 MB    00:15    
(11/14): mysql-server-5.1 100% |=========================|  11 MB    01:59    
(12/14): php-5.3.2-1.el4. 100% |=========================| 1.4 MB    00:23    
(13/14): mysql-libs-5.1.4 100% |=========================| 1.7 MB    00:23    
(14/14): php-mysql-5.3.2- 100% |=========================| 155 kB    00:04    
warning: rpmts_HdrFromFdno: V3 DSA signature: NOKEY, key ID 00f97f56
Public key for php-xml-5.3.2-1.el4.remi.x86_64.rpm is not installed
Retrieving GPG key from http://rpms.famillecollet.com/RPM-GPG-KEY-remi
Importing GPG key 0x00F97F56 "Remi Collet "
Is this ok [y/N]:
 
Key imported successfully
warning: only V3 signatures can be verified, skipping V4 signature
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
 
WARNING : This php-* RPM are not official Fedora build and
overrides the official ones. Don't file bugs on Fedora Project.
 
Use dedicated forums http://forums.famillecollet.com/
 
  Updating  : php-common                                           [ 1/24]warning: /etc/php.ini created as /etc/php.ini.rpmnew
  Updating  : php-common                   ####################### [ 1/24]
  Installing: mysqlclient15                ####################### [ 2/24]
warning: only V3 signatures can be verified, skipping V4 signature
 
WARNING : This MySQL RPM is not an official Fedora build and it
overrides the official one. Don't file bugs on Fedora Project.
Use dedicated forums http://forums.famillecollet.com/
 
  Updating  : mysql-libs                                           [ 3/24]warning: /etc/my.cnf created as /etc/my.cnf.rpmnew
  Updating  : mysql-libs                   ####################### [ 3/24]
  Updating  : mysql                        ####################### [ 4/24]
  Installing: mysqlclient14                ####################### [ 5/24]
  Installing: sqlite2                      ####################### [ 6/24]
  Updating  : php-pdo                      ####################### [ 7/24]
  Installing: libedit                      ####################### [ 8/24]
  Updating  : php-cli                      ####################### [ 9/24]
  Updating  : php                          ####################### [10/24]
  Updating  : php-mysql                    ####################### [11/24]
  Updating  : php-xml                      ####################### [12/24]
  Installing: perl-DBD-mysql               ####################### [13/24]
  Updating  : mysql-server                 ####################### [14/24]
  Cleanup   : php-xml                      ####################### [15/24]
  Cleanup   : php-common                   ####################### [16/24]
  Cleanup   : php-cli                      ####################### [17/24]
  Removing  : perl-DBD-MySQL               ####################### [18/24]
  Cleanup   : php-pdo                      ####################### [19/24]
  Cleanup   : mysql                        ####################### [20/24]
  Cleanup   : mysql-server                 ####################### [21/24]
  Cleanup   : php                          ####################### [22/24]
  Cleanup   : mysql-libs                   ####################### [23/24]
  Cleanup   : php-mysql                    ####################### [24/24]
 
Installed: perl-DBD-mysql.x86_64 0:4.013-1.el4.rf
Dependency Installed: libedit.x86_64 0:20090923-3.0_1.el4.rf mysqlclient14.x86_64 0:4.1.22-1.el4.remi mysqlclient15.x86_64 0:5.0.67-1.el4.remi sqlite2.x86_64 0:2.8.17-2.el4.remi
Updated: php.x86_64 0:5.3.2-1.el4.remi
Dependency Updated: mysql.x86_64 0:5.1.45-1.el4.remi mysql-libs.x86_64 0:5.1.45-1.el4.remi mysql-server.x86_64 0:5.1.45-1.el4.remi php-cli.x86_64 0:5.3.2-1.el4.remi php-common.x86_64 0:5.3.2-1.el4.remi php-mysql.x86_64 0:5.3.2-1.el4.remi php-pdo.x86_64 0:5.3.2-1.el4.remi php-xml.x86_64 0:5.3.2-1.el4.remi
Replaced: perl-DBD-MySQL.x86_64 0:4.008-1.el4s1
Complete!

When this was done, I just synced /etc/php.ini with the new one /etc/php.ini.rpmnew and /etc/my.cnf with /etc/my.cnf.rpmnew when this was done I just restarted mysql and apache and I php5.3 was now available.

Categories: Linux, configuration Tags: