Solved

Perl Script to edit httpd.conf and changes few options

Posted on 2009-04-03
7
420 Views
Last Modified: 2012-05-06
Hi,

  I have experience with programming (PHP, javascript, Delphi)... But I'm new on Perl Programming. What I need is one script that opens one file modify some block of text and save it.

   More specifically, I need to edit the apache httpd.conf modify 3 or 4 lines and save it. Here is the before and after:


BEFORE:

~~~~ some block of text ~~~~

<VirtualHost 255.255.255.0>
DocumentRoot /home/./username/documentroot
ServerName www.domainname.com.br
RewriteEngine On
<Directory /home/./username/documentroot>
    AddType application/x-httpd-php .php .php3
    php_admin_flag safe_mode On
    php_admin_value include_path ".:/tmp/:/home/./username/documentroot"
    php_admin_value open_basedir ".:/tmp/:/home/./username/documentroot"
    php_admin_value safe_mode_include_dir /home/./username/documentroot
</Directory>
</VirtualHost>

~~~~ the rest of file ~~~~

AFTER:

~~~~ some block of text ~~~~~~~

<VirtualHost 255.255.255.0>
DocumentRoot /home/./username/documentroot
ServerName www.domainname.com.br
RewriteEngine On
CustomLog /home/./username/logs/access_log combined   <--- new line
<Directory /home/./username/documentroot>
    AddType application/x-httpd-php .php .php3
    php_admin_flag safe_mode On
    php_admin_value include_path ".:/tmp/:/home/./username/documentroot"
    php_admin_value open_basedir ".:/tmp/:/home/./username/documentroot
    php_admin_value safe_mode_include_dir /home/./username/documentroot
</Directory>
<Directory /home/./username/documentroot/stats>    <--- new line
    AllowOverride AuthConfig     <--- new line
</Directory>    <--- new line
</VirtualHost>


~~~~ the rest of file ~~~~


But the is more... Before insert the new lines I must be sure that they doesn't exists already.


Please help, thanks :)

0
Comment
Question by:xBellox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 200 total points
ID: 24064436
I could take a stab at a script to do that, but I'm sure someone will be along to post one soon enough. In the meantime, I would at least investigate a Perl module that is already written to manage Apache config. There is almost always something on CPAN. One downside is it requires a compiler to build, but builds fine on Linux / UNIX boxes with GCC installed.

cpan install Apache::Admin::Config

0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 225 total points
ID: 24064672
For complex processing, using a module as mrjoltcola suggests would be good.  If you just need to add a few lines, here is a script that'll do it.

I suggest you make a backup before running this.
You will also need write permission to the file.

#!/usr/bin/perl
 
#NOTE: Update file path/name as needed
open(my $fh, "+<", "/etc/apache2/httpd.conf") or die "Could not open file: $!\n";
my @lines=<$fh>;
seek($fh, 0, 0);
foreach (@lines) {
    print $fh $_;
    print "CustomLog /home/./username/logs/access_log combined\n" if /RewriteEngine On/;
    print "<Directory /home/./username/documentroot/stats>\n"
        . "  AllowOverride AuthConfig\n"
        . "</Directory>\n" if m|</Directory>|;
}
close($fh);

Open in new window

0
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 75 total points
ID: 24065946
since you have experience in PHP, you can do that in PHP, not necessary Perl. (unless its an absolute must)


$file=fopen("apache.config_path","r");
$newstring = <<<END
<Directory /home/./username/documentroot/stats>  
    AllowOverride AuthConfig 
</Directory>
END;
while ( ($line = fgets($file,4096)) !==FALSE) {
  if ( strpos($line,"RewriteEngine") !== FALSE) {
   print $line."CustomLog /home/./username/logs/access_log combined\n";
   continue;
  }else if (strpos($line,"</Directory>") !==FALSE) {
   print $line.$newstring."\n";
  }else{
   print $line;
  }
}
   fclose($file);

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Author Comment

by:xBellox
ID: 24077162
@mrjoltcola:

   Like I said, I'm new on perl scripting, I need more help than that. Thank you.


-------------------------

@Adam314:

   The script I need it's more complex than add lines at the end of the file. I'm googling "mrjoltcola"...

--------------------------

@ghostdog74

 I need to be in perl cause it will run inside another perl script. Thank you anyway.
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 225 total points
ID: 24077803
The documentation on the module to help is here:
    http://search.cpan.org/~rsoliv/Apache-Admin-Config-0.94/lib/Apache/Admin/Config.pm

This module is designed to help you read and make changes to apache config files.  If the changes you need to make are complex, using this module will help.  The changes you've requested so far are not complex, and can be made with a script like what I posted.

If you tell us what changes are needed, we can help direct you to the best solution.  So far, I'd say there is no need for the module - you can just add the lines to the file.
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24077922
>>I'm googling "mrjoltcola"

Don't do that, you don't want any of my modules for this! :)

If you aren't comfortable installing the module I mentioned, I would stick with Adam's solution, he will get you what you need.
0
 
LVL 9

Author Closing Comment

by:xBellox
ID: 31566448
It gave me a clue...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question